r"""Presents the user with prompt (typically in the form of question) which the user must answer yes or no. Arguments: prompt (str): The prompt to show default: The default option; `True` means "yes" Returns: `True` if the answer was "yes", `False` if "no" Examp
(prompt, default=None)
| 49 | return p |
| 50 | |
| 51 | def yesno(prompt, default=None): |
| 52 | r"""Presents the user with prompt (typically in the form of question) |
| 53 | which the user must answer yes or no. |
| 54 | |
| 55 | Arguments: |
| 56 | prompt (str): The prompt to show |
| 57 | default: The default option; `True` means "yes" |
| 58 | |
| 59 | Returns: |
| 60 | `True` if the answer was "yes", `False` if "no" |
| 61 | |
| 62 | Examples: |
| 63 | |
| 64 | >>> yesno("A number:", 20) |
| 65 | Traceback (most recent call last): |
| 66 | ... |
| 67 | ValueError: yesno(): default must be a boolean or None |
| 68 | >>> saved_stdin = sys.stdin |
| 69 | >>> try: |
| 70 | ... sys.stdin = io.TextIOWrapper(io.BytesIO(b"x\nyes\nno\n\n")) |
| 71 | ... yesno("is it good 1") |
| 72 | ... yesno("is it good 2", True) |
| 73 | ... yesno("is it good 3", False) |
| 74 | ... finally: |
| 75 | ... sys.stdin = saved_stdin |
| 76 | [?] is it good 1 [yes/no] Please answer yes or no |
| 77 | [?] is it good 1 [yes/no] True |
| 78 | [?] is it good 2 [Yes/no] False |
| 79 | [?] is it good 3 [yes/No] False |
| 80 | |
| 81 | Tests: |
| 82 | |
| 83 | >>> p = testpwnproc("print(yesno('is it ok??'))") |
| 84 | >>> b"is it ok" in p.recvuntil(b"??") |
| 85 | True |
| 86 | >>> p.sendline(b"x\nny") |
| 87 | >>> b"True" in p.recvall() |
| 88 | True |
| 89 | """ |
| 90 | |
| 91 | if default is not None and not isinstance(default, bool): |
| 92 | raise ValueError('yesno(): default must be a boolean or None') |
| 93 | |
| 94 | if term.term_mode: |
| 95 | term.output(' [?] %s [' % prompt) |
| 96 | yesfocus, yes = term.text.bold('Yes'), 'yes' |
| 97 | nofocus, no = term.text.bold('No'), 'no' |
| 98 | hy = term.output(yesfocus if default is True else yes) |
| 99 | hs = term.output('/') |
| 100 | hn = term.output(nofocus if default is False else no) |
| 101 | he = term.output(']\n') |
| 102 | cur = default |
| 103 | while True: |
| 104 | k = term.key.get() |
| 105 | if k in ('y', 'Y', '<left>') and cur is not True: |
| 106 | cur = True |
| 107 | hy.update(yesfocus) |
| 108 | hn.update(no) |