Cancelling the choice kitten should return the empty response

This commit is contained in:
Kovid Goyal
2022-01-22 12:13:32 +05:30
parent 9419125387
commit 32e31a3c6b

View File

@@ -98,9 +98,8 @@ For example: y:Yes and n;red:No
--default -d --default -d
A default choice or text. If unspecified, it is "y" for yesno and empty for the A default choice or text. If unspecified, it is "y" for :code:`yesno`, the first choice
others. If the input type is choices and the specified value is not one of the for :code:`choices` and empty for others. The default choice is selected when the user
available choices, it is the first choice. The default choice is selected when the user
presses the Enter key. presses the Enter key.
''' '''
@@ -159,9 +158,10 @@ class Choose(Handler):
allowed.append(letter) allowed.append(letter)
self.choices[letter] = Choice(text, idx, color) self.choices[letter] = Choice(text, idx, color)
self.allowed = frozenset(allowed) self.allowed = frozenset(allowed)
self.response = cli_opts.default self.response = ''
if cli_opts.type in ('yesno', 'choices') and self.response not in self.allowed: self.response_on_accept = cli_opts.default or ''
self.response = 'y' if cli_opts.type == 'yesno' else tuple(self.choices.keys())[0] if cli_opts.type in ('yesno', 'choices') and self.response_on_accept not in self.allowed:
self.response_on_accept = 'y' if cli_opts.type == 'yesno' else tuple(self.choices.keys())[0]
def initialize(self) -> None: def initialize(self) -> None:
self.cmd.set_cursor_visible(False) self.cmd.set_cursor_visible(False)
@@ -215,7 +215,7 @@ class Choose(Handler):
for letter, choice in self.choices.items(): for letter, choice in self.choices.items():
text = choice.text[:choice.idx] text = choice.text[:choice.idx]
color = choice.color or ('yellow' if letter == self.response else 'green') color = choice.color or ('yellow' if letter == self.response_on_accept else 'green')
text += styled(choice.text[choice.idx], fg=color) text += styled(choice.text[choice.idx], fg=color)
text += choice.text[choice.idx + 1:] text += choice.text[choice.idx + 1:]
text += ' ' text += ' '
@@ -260,7 +260,7 @@ class Choose(Handler):
nx = x + wcswidth(yes) + len(sep) nx = x + wcswidth(yes) + len(sep)
self.clickable_ranges['y'].append(Range(x, x + wcswidth(yes) - 1, y + line_count)) self.clickable_ranges['y'].append(Range(x, x + wcswidth(yes) - 1, y + line_count))
self.clickable_ranges['n'].append(Range(nx, nx + wcswidth(no) - 1, y + line_count)) self.clickable_ranges['n'].append(Range(nx, nx + wcswidth(no) - 1, y + line_count))
if self.response == 'y': if self.response_on_accept == 'y':
yes = highlight(yes, line_count == 1) yes = highlight(yes, line_count == 1)
else: else:
no = highlight(no, line_count == 1) no = highlight(no, line_count == 1)
@@ -292,6 +292,7 @@ class Choose(Handler):
if key_event.matches('esc'): if key_event.matches('esc'):
self.on_interrupt() self.on_interrupt()
elif key_event.matches('enter'): elif key_event.matches('enter'):
self.response = self.response_on_accept
self.quit_loop(0) self.quit_loop(0)
def on_click(self, ev: MouseEvent) -> None: def on_click(self, ev: MouseEvent) -> None:
@@ -307,7 +308,6 @@ class Choose(Handler):
self.draw_screen() self.draw_screen()
def on_interrupt(self) -> None: def on_interrupt(self) -> None:
self.response = ''
self.quit_loop(1) self.quit_loop(1)
on_eot = on_interrupt on_eot = on_interrupt