mirror of
https://github.com/kovidgoyal/kitty
synced 2026-07-23 00:38:10 +02:00
Use the new completionspec for rc cmds
This commit is contained in:
@@ -6,10 +6,10 @@ from contextlib import suppress
|
||||
from dataclasses import dataclass
|
||||
from typing import (
|
||||
TYPE_CHECKING, Any, Callable, Dict, FrozenSet, Iterable, Iterator,
|
||||
List, NoReturn, Optional, Set, Tuple, Type, Union, cast, Mapping
|
||||
List, NoReturn, Optional, Set, Tuple, Type, Union, cast
|
||||
)
|
||||
|
||||
from kitty.cli import get_defaults_from_seq, parse_args, parse_option_spec
|
||||
from kitty.cli import get_defaults_from_seq, parse_args, parse_option_spec, CompletionSpec
|
||||
from kitty.cli_stub import RCOptions as R
|
||||
from kitty.constants import appname, list_kitty_resources, running_in_kitty
|
||||
from kitty.types import AsyncResponse
|
||||
@@ -88,7 +88,8 @@ CmdGenerator = Iterator[CmdReturnType]
|
||||
PayloadType = Optional[Union[CmdReturnType, CmdGenerator]]
|
||||
PayloadGetType = PayloadGetter
|
||||
ArgsType = List[str]
|
||||
ImageCompletion: Dict[str, Tuple[str, Tuple[str, ...]]] = {'files': ('Images', ('*.png', '*.jpg', '*.jpeg', '*.webp', '*.gif', '*.bmp', '*.tiff'))}
|
||||
ImageCompletion = CompletionSpec.from_string('type:file group:"Images"')
|
||||
ImageCompletion.extensions = 'png', 'jpg', 'jpeg', 'webp', 'gif', 'bmp', 'tiff'
|
||||
|
||||
|
||||
MATCH_WINDOW_OPTION = '''\
|
||||
@@ -184,7 +185,7 @@ class ArgsHandling:
|
||||
json_field: str = ''
|
||||
count: Optional[int] = None
|
||||
spec: str = ''
|
||||
completion: Optional[Mapping[str, Tuple[str, Union[Callable[[], Iterable[str]], Tuple[str, ...]]]]] = None
|
||||
completion: CompletionSpec = CompletionSpec()
|
||||
value_if_unspecified: Tuple[str, ...] = ()
|
||||
minimum_count: int = -1
|
||||
first_rest: Optional[Tuple[str, str]] = None
|
||||
@@ -198,24 +199,11 @@ class ArgsHandling:
|
||||
return self.count
|
||||
|
||||
def as_go_completion_code(self, go_name: str) -> Iterator[str]:
|
||||
from ..cli import serialize_as_go_string
|
||||
c = self.args_count
|
||||
if c is not None:
|
||||
yield f'{go_name}.Stop_processing_at_arg = {c}'
|
||||
if self.completion:
|
||||
for k, v in self.completion.items():
|
||||
if k == 'files':
|
||||
title, pats = v
|
||||
assert isinstance(pats, tuple)
|
||||
gpats = (f'"{serialize_as_go_string(x)}"' for x in pats)
|
||||
yield f'{go_name}.Completion_for_arg = fnmatch_completer("{serialize_as_go_string(title)}", {", ".join(gpats)})'
|
||||
elif k == 'names':
|
||||
title, gen = v
|
||||
assert callable(gen)
|
||||
gpats = (f'"{serialize_as_go_string(x)}"' for x in gen())
|
||||
yield f'{go_name}.Completion_for_arg = names_completer("{serialize_as_go_string(title)}", {", ".join(gpats)})'
|
||||
else:
|
||||
raise KeyError(f'Unknown args completion type: {k}')
|
||||
yield from self.completion.as_go_code(go_name)
|
||||
|
||||
def as_go_code(self, cmd_name: str, field_types: Dict[str, str], handled_fields: Set[str]) -> Iterator[str]:
|
||||
c = self.args_count
|
||||
@@ -313,6 +301,7 @@ class StreamInFlight:
|
||||
|
||||
class RemoteCommand:
|
||||
Args = ArgsHandling
|
||||
CompletionSpec = CompletionSpec
|
||||
|
||||
name: str = ''
|
||||
short_desc: str = ''
|
||||
|
||||
@@ -30,7 +30,10 @@ class GotoLayout(RemoteCommand):
|
||||
' You can use special match value :code:`all` to set the layout in all tabs.'
|
||||
)
|
||||
options_spec = MATCH_TAB_OPTION
|
||||
args = RemoteCommand.Args(spec='LAYOUT_NAME', count=1, completion={'names': ('Layouts', layout_names)}, json_field='layout', args_choices=layout_names)
|
||||
args = RemoteCommand.Args(
|
||||
spec='LAYOUT_NAME', count=1, json_field='layout',
|
||||
completion=RemoteCommand.CompletionSpec.from_string('type:keyword group:"Layout" kwds:' + ','.join(layout_names())),
|
||||
args_choices=layout_names)
|
||||
|
||||
def message_to_kitty(self, global_opts: RCOptions, opts: 'CLIOptions', args: ArgsType) -> PayloadType:
|
||||
if len(args) != 1:
|
||||
|
||||
@@ -89,8 +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 ...', json_field='colors', special_parse='parse_colors_and_files(args)', completion={
|
||||
'files': ('CONF files', ('*.conf',))})
|
||||
args = RemoteCommand.Args(spec='COLOR_OR_FILE ...', json_field='colors', special_parse='parse_colors_and_files(args)',
|
||||
completion=RemoteCommand.CompletionSpec.from_string('type:file group:"CONF files", ext:conf'))
|
||||
|
||||
def message_to_kitty(self, global_opts: RCOptions, opts: 'CLIOptions', args: ArgsType) -> PayloadType:
|
||||
final_colors: Dict[str, Optional[int]] = {}
|
||||
|
||||
@@ -42,7 +42,9 @@ Change the default enabled layout value so that the new value takes effect for a
|
||||
as well.
|
||||
'''
|
||||
args = RemoteCommand.Args(
|
||||
spec='LAYOUT ...', minimum_count=1, json_field='layouts', completion={'names': ('Layouts', layout_names)}, args_choices=layout_names)
|
||||
spec='LAYOUT ...', minimum_count=1, json_field='layouts',
|
||||
completion=RemoteCommand.CompletionSpec.from_string('type:keyword group:"Layout" kwds:' + ','.join(layout_names())),
|
||||
args_choices=layout_names)
|
||||
|
||||
def message_to_kitty(self, global_opts: RCOptions, opts: 'CLIOptions', args: ArgsType) -> PayloadType:
|
||||
if len(args) < 1:
|
||||
|
||||
Reference in New Issue
Block a user