Add default choice option for ask kitten

This commit is contained in:
pagedown 2022-01-22 02:46:37 +08:00
parent 4dd29c726a
commit 9b4d1219b8
No known key found for this signature in database
GPG Key ID: E921CF18AC8FF6EB

View File

@ -95,6 +95,12 @@ letter is the accelerator key and text is the corresponding text. There can be
an optional color specification after the letter to indicate what color it should an optional color specification after the letter to indicate what color it should
be. be.
For example: y:Yes and n;red:No 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
others. If the input type is choices and the specified value is not one of the
available choices, it is empty.
''' '''
@ -132,7 +138,6 @@ class Choose(Handler):
def __init__(self, cli_opts: AskCLIOptions) -> None: def __init__(self, cli_opts: AskCLIOptions) -> None:
self.cli_opts = cli_opts self.cli_opts = cli_opts
self.response = 'n' if cli_opts.type == 'yesno' else ''
self.allowed = frozenset('yn') self.allowed = frozenset('yn')
self.choices: Dict[str, Choice] = {} self.choices: Dict[str, Choice] = {}
self.clickable_ranges: Dict[str, Range] = {} self.clickable_ranges: Dict[str, Range] = {}
@ -148,6 +153,12 @@ 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)
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
def initialize(self) -> None: def initialize(self) -> None:
self.cmd.set_cursor_visible(False) self.cmd.set_cursor_visible(False)