more work on porting rc command parsing to Go

This commit is contained in:
Kovid Goyal
2022-08-30 15:49:26 +05:30
parent 79c8862d4c
commit 6f4968305a
19 changed files with 291 additions and 27 deletions

View File

@@ -172,6 +172,7 @@ class ArgsHandling:
minimum_count: int = -1
first_rest: Optional[Tuple[str, str]] = None
special_parse: str = ''
args_choices: Optional[Callable[[], Iterable[str]]] = None
@property
def args_count(self) -> Optional[int]:
@@ -193,6 +194,11 @@ class ArgsHandling:
yield '}'
if self.minimum_count > -1:
yield f'if len(args) < {self.minimum_count} {{ return fmt.Errorf("%s", Must specify at least {self.minimum_count} arguments to {cmd_name}) }}'
if self.args_choices:
achoices = tuple(self.args_choices())
yield 'achoices := map[string]bool{' + ' '.join(f'"{x}":true,' for x in achoices) + '}'
yield 'for _, a := range args {'
yield 'if !achoices[a] { return fmt.Errorf("Not a valid choice: %s. Allowed values are: %s", a, "' + ', '.join(achoices) + '") }'
if self.json_field:
jf = self.json_field
dest = f'payload.{jf.capitalize()}'
@@ -207,6 +213,10 @@ class ArgsHandling:
if self.special_parse:
if self.special_parse.startswith('!'):
yield f'io_data.multiple_payload_generator, err = {self.special_parse[1:]}'
elif self.special_parse.startswith('+'):
fields, sp = self.special_parse[1:].split(':', 1)
handled_fields.update(set(fields.split(',')))
yield f'err = {sp}'
else:
yield f'{dest}, err = {self.special_parse}'
yield 'if err != nil { return err }'

View File

@@ -101,7 +101,7 @@ Path to a file whose contents you wish to send. Note that in this case the file
are sent as is, not interpreted for escapes.
'''
no_response = True
argspec = '[TEXT TO SEND]'
args = RemoteCommand.Args(spec='[TEXT TO SEND]', json_field='data', special_parse='+session_id:parse_send_text(io_data, args)')
def message_to_kitty(self, global_opts: RCOptions, opts: 'CLIOptions', args: ArgsType) -> PayloadType:
limit = 1024

View File

@@ -63,9 +63,8 @@ default=false
Don't wait for a response from kitty. This means that even if setting the background image
failed, the command will exit with a success code.
''' + '\n\n' + MATCH_WINDOW_OPTION
argspec = 'PATH_TO_PNG_IMAGE'
args_count = 1
args_completion = {'files': ('PNG Images', ('*.png',))}
args = RemoteCommand.Args(spec='PATH_TO_PNG_IMAGE', count=1, special_parse='!read_window_logo(args[0])', completion={
'files': ('PNG Images', ('*.png',))})
images_in_flight: Dict[str, IO[bytes]] = {}
is_asynchronous = True

View File

@@ -37,8 +37,7 @@ By default, background opacity are only changed for the currently active window.
cause background opacity to be changed in all windows.
''' + '\n\n' + MATCH_WINDOW_OPTION + '\n\n' + MATCH_TAB_OPTION.replace('--match -m', '--match-tab -t')
argspec = 'OPACITY'
args_count = 1
args = RemoteCommand.Args(spec='OPACITY', count=1, json_field='opacity')
def message_to_kitty(self, global_opts: RCOptions, opts: 'CLIOptions', args: ArgsType) -> PayloadType:
opacity = max(0.1, min(float(args[0]), 1.0))

View File

@@ -89,7 +89,8 @@ type=bool-set
Restore all colors to the values they had at kitty startup. Note that if you specify
this option, any color arguments are ignored and :option:`kitty @ set-colors --configured` and :option:`kitty @ set-colors --all` are implied.
''' + '\n\n' + MATCH_WINDOW_OPTION + '\n\n' + MATCH_TAB_OPTION.replace('--match -m', '--match-tab -t')
args = RemoteCommand.Args(spec='COLOR_OR_FILE ...', completion={'files': ('CONF files', ('*.conf',))})
args = RemoteCommand.Args(spec='COLOR_OR_FILE ...', json_field='colors', special_parse='parse_colors_and_files(args)', completion={
'files': ('CONF files', ('*.conf',))})
def message_to_kitty(self, global_opts: RCOptions, opts: 'CLIOptions', args: ArgsType) -> PayloadType:
final_colors: Dict[str, Optional[int]] = {}

View File

@@ -41,8 +41,8 @@ type=bool-set
Change the default enabled layout value so that the new value takes effect for all newly created tabs
as well.
'''
argspec = 'LAYOUTS'
args_completion = {'names': ('Layouts', layout_names)}
args = RemoteCommand.Args(
spec='LAYOUT ...', minimum_count=1, json_field='layouts', completion={'names': ('Layouts', layout_names)}, args_choices=layout_names)
def message_to_kitty(self, global_opts: RCOptions, opts: 'CLIOptions', args: ArgsType) -> PayloadType:
if len(args) < 1:

View File

@@ -28,8 +28,7 @@ class SetFontSize(RemoteCommand):
' with a :code:`+` or :code:`-` increments the font size by the specified'
' amount.'
)
argspec = 'FONT_SIZE'
args_count = 1
args = RemoteCommand.Args(spec='FONT_SIZE', count=1, special_parse='+increment_op:parse_set_font_size(args[0], io_data)', json_field='size')
options_spec = '''\
--all -a
type=bool-set

View File

@@ -95,7 +95,7 @@ type=bool-set
Also change the configured paddings and margins (i.e. the settings kitty will use for new
windows).
''' + '\n\n' + MATCH_WINDOW_OPTION + '\n\n' + MATCH_TAB_OPTION.replace('--match -m', '--match-tab -t')
argspec = 'MARGIN_OR_PADDING ...'
args = RemoteCommand.Args(spec='MARGIN_OR_PADDING ...', minimum_count=1, json_field='settings', special_parse='parse_set_spacing(args)')
def message_to_kitty(self, global_opts: RCOptions, opts: 'CLIOptions', args: ArgsType) -> PayloadType:
if not args:

View File

@@ -57,7 +57,7 @@ the keyword NONE to revert to using the default colors.
type=bool-set
Close the tab this command is run in, rather than the active tab.
'''
argspec = 'COLORS'
args = RemoteCommand.Args(spec='COLORS', json_field='colors', minimum_count=1, special_parse='parse_tab_colors(args)')
def message_to_kitty(self, global_opts: RCOptions, opts: 'CLIOptions', args: ArgsType) -> PayloadType:
try:

View File

@@ -28,7 +28,7 @@ class SetTabTitle(RemoteCommand):
' title of the currently active window in the tab is used.'
)
options_spec = MATCH_TAB_OPTION
argspec = 'TITLE ...'
args = RemoteCommand.Args(spec='TITLE ...', json_field='title')
def message_to_kitty(self, global_opts: RCOptions, opts: 'CLIOptions', args: ArgsType) -> PayloadType:
return {'title': ' '.join(args), 'match': opts.match}