Cleanup previous PR

yesno default should be yes.
Pressing q to quit is undiscoverable
Clicking empty cells should not trigger buttons as that is
undiscoverable and possibly surprising
This commit is contained in:
Kovid Goyal
2022-01-22 10:13:20 +05:30
parent 9944b895a6
commit 0d33380e6a
2 changed files with 21 additions and 16 deletions

View File

@@ -98,9 +98,10 @@ For example: y:Yes and n;red:No
--default -d
A default choice or text. If unspecified, it is no for yesno and empty for the
A default choice or text. If unspecified, it is "y" for yesno and empty for the
others. If the input type is choices and the specified value is not one of the
available choices, it is empty.
available choices, it is empty. The default choice is selected when the user
presses the Enter key.
'''
@@ -138,10 +139,11 @@ class Choose(Handler):
def __init__(self, cli_opts: AskCLIOptions) -> None:
self.cli_opts = cli_opts
self.allowed = frozenset('yn')
self.choices: Dict[str, Choice] = {}
self.clickable_ranges: Dict[str, Range] = {}
if cli_opts.type != 'yesno':
if cli_opts.type == 'yesno':
self.allowed = frozenset('yn')
else:
allowed = []
for choice in cli_opts.choices:
color = 'green'
@@ -153,12 +155,9 @@ class Choose(Handler):
allowed.append(letter)
self.choices[letter] = Choice(text, idx, color)
self.allowed = frozenset(allowed)
if not cli_opts.default:
self.response = 'n' if cli_opts.type == 'yesno' else ''
elif cli_opts.type == 'choices' and cli_opts.default not in self.allowed:
self.response = ''
else:
self.response = cli_opts.default
self.response = cli_opts.default
if cli_opts.type in ('yesno', 'choices') and self.response not in self.allowed:
self.response = 'y' if cli_opts.type == 'yesno' else ''
def initialize(self) -> None:
self.cmd.set_cursor_visible(False)
@@ -234,7 +233,7 @@ class Choose(Handler):
extra = (self.screen_size.cols - w) // 2
x = extra
nx = x + wcswidth(yes) + len(sep)
self.clickable_ranges = {'y': Range(x, x + wcswidth(yes) - 1, y), 'n': Range(nx, nx + 2, y)}
self.clickable_ranges = {'y': Range(x, x + wcswidth(yes) - 1, y), 'n': Range(nx, nx + 1, y)}
self.print(' ' * extra + text, end='')
def on_text(self, text: str, in_bracketed_paste: bool = False) -> None:
@@ -242,7 +241,7 @@ class Choose(Handler):
if text in self.allowed:
self.response = text
self.quit_loop(0)
elif self.cli_opts.type == 'yesno' and text == 'q':
elif self.cli_opts.type == 'yesno':
self.on_interrupt()
def on_key(self, key_event: KeyEventType) -> None:

View File

@@ -654,7 +654,7 @@ class Boss:
if window.has_running_program:
msg += ' ' + _('It is running a program.')
self._run_kitten(
'ask', ['--type=yesno', '--default=y', '--message', msg],
'ask', ['--type=yesno', '--message', msg],
window=window,
custom_callback=partial(self.handle_close_window_confirmation, window.id)
)
@@ -672,11 +672,17 @@ class Boss:
if tab:
self.confirm_tab_close(tab)
def confirm(self, msg: str, callback: Callable[..., None], *args: Any,
window: Optional[Window] = None, confirm_on_cancel: bool = False) -> None:
def confirm(
self, msg: str, # can contain newlines and ANSI formatting
callback: Callable[..., None], # called with True or False and *args
*args: Any, # passed to the callback function
window: Optional[Window] = None, # the window associated with the confirmation
confirm_on_cancel: bool = False, # on closing window
confirm_on_accept: bool = True, # on pressing enter
) -> None:
def callback_(res: Dict[str, Any], x: int, boss: Boss) -> None:
callback(res.get('response') == 'y', *args)
self._run_kitten('ask', ['--type=yesno', '--default=y', '--message', msg],
self._run_kitten('ask', ['--type=yesno', '--message', msg, '--default', 'y' if confirm_on_accept else 'n'],
window=window, custom_callback=callback_, default_data={'response': 'y' if confirm_on_cancel else 'n'})
def confirm_tab_close(self, tab: Tab) -> None: