Allow using file completion for any type of command line arg

This commit is contained in:
Kovid Goyal 2022-08-09 15:56:39 +05:30
parent 6860f7ffd6
commit 5ad55dd165
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 7 additions and 20 deletions

View File

@ -451,8 +451,6 @@ def as_type_stub(seq: OptionSpecSeq, disabled: OptionSpecSeq, class_name: str, e
t = 'typing.Optional[str]' t = 'typing.Optional[str]'
elif otype == 'list': elif otype == 'list':
t = 'typing.Sequence[str]' t = 'typing.Sequence[str]'
elif otype == 'path':
t = 'str'
elif otype in ('choice', 'choices'): elif otype in ('choice', 'choices'):
if opt['choices']: if opt['choices']:
t = 'typing.Literal[{}]'.format(','.join(f'{x!r}' for x in opt['choices'])) t = 'typing.Literal[{}]'.format(','.join(f'{x!r}' for x in opt['choices']))
@ -535,8 +533,6 @@ class Options:
raise SystemExit('{} is not a valid value for the {} option. Valid values are: {}'.format( raise SystemExit('{} is not a valid value for the {} option. Valid values are: {}'.format(
val, emph(alias), ', '.join(choices))) val, emph(alias), ', '.join(choices)))
self.values_map[name] = val self.values_map[name] = val
elif typ == 'path':
self.values_map[name] = val
elif typ in nmap: elif typ in nmap:
f = nmap[typ] f = nmap[typ]
try: try:
@ -616,6 +612,7 @@ are running a program that does not set titles.
--config -c --config -c
type=list type=list
completion=type:file ext:conf group:"Config files"
{config_help} {config_help}
@ -627,7 +624,6 @@ Syntax: :italic:`name=value`. For example: :option:`{appname} -o` font_size=20
--directory --working-directory -d --directory --working-directory -d
default=. default=.
type=path
completion=type:directory completion=type:directory
Change to the specified directory when launching. Change to the specified directory when launching.
@ -639,8 +635,7 @@ Detach from the controlling terminal, if any.
--session --session
type=path completion=type:file ext:session relative:conf group:"Session files"
completion=ext:session relative:conf group:"Session files"
Path to a file containing the startup :italic:`session` (tabs, windows, layout, Path to a file containing the startup :italic:`session` (tabs, windows, layout,
programs). Use - to read from STDIN. See the :file:`README` file for details and programs). Use - to read from STDIN. See the :file:`README` file for details and
an example. Environment variables are expanded, relative paths are resolved relative an example. Environment variables are expanded, relative paths are resolved relative
@ -748,8 +743,7 @@ present in the main font.
--watcher --watcher
type=path completion=type:file ext:py relative:conf group:"Watcher files"
completion=ext:py relative:conf group:"Watcher files"
This option is deprecated in favor of the :opt:`watcher` option in This option is deprecated in favor of the :opt:`watcher` option in
:file:`{conf_name}.conf` and should not be used. :file:`{conf_name}.conf` and should not be used.

View File

@ -386,14 +386,6 @@ def complete_kitty_cli_arg(ans: Completions, opt: Optional[OptionDict], prefix:
from kitty.config import option_names_for_completion from kitty.config import option_names_for_completion
k = 'Config directives' k = 'Config directives'
ans.add_match_group(k, {k+'=': '' for k in option_names_for_completion() if k.startswith(prefix)}, trailing_space=False) ans.add_match_group(k, {k+'=': '' for k in option_names_for_completion() if k.startswith(prefix)}, trailing_space=False)
elif dest == 'config':
def is_conf_file(x: str) -> bool:
if os.path.isdir(x):
return True
return x.lower().endswith('.conf')
complete_files_and_dirs(ans, prefix, files_group_name='Config files', predicate=is_conf_file)
elif dest == 'listen_on': elif dest == 'listen_on':
if ':' not in prefix: if ':' not in prefix:
k = 'Address type' k = 'Address type'
@ -644,7 +636,7 @@ def complete_file_path(ans: Completions, spec: Dict[str, str], prefix: str, only
def complete_path(ans: Completions, opt: OptionDict, prefix: str) -> None: def complete_path(ans: Completions, opt: OptionDict, prefix: str) -> None:
spec = opt['completion'] spec = opt['completion']
t = spec.get('type', 'file') t = spec['type']
if t == 'file': if t == 'file':
complete_file_path(ans, spec, prefix) complete_file_path(ans, spec, prefix)
elif t == 'directory': elif t == 'directory':
@ -654,7 +646,7 @@ def complete_path(ans: Completions, opt: OptionDict, prefix: str) -> None:
def complete_basic_option_args(ans: Completions, opt: OptionDict, prefix: str) -> None: def complete_basic_option_args(ans: Completions, opt: OptionDict, prefix: str) -> None:
if opt['choices']: if opt['choices']:
ans.add_match_group(f'Choices for {opt["dest"]}', tuple(k for k in opt['choices'] if k.startswith(prefix))) ans.add_match_group(f'Choices for {opt["dest"]}', tuple(k for k in opt['choices'] if k.startswith(prefix)))
elif opt['type'] == 'path': elif opt['completion'].get('type') in ('file', 'directory'):
complete_path(ans, opt, prefix) complete_path(ans, opt, prefix)

View File

@ -201,7 +201,7 @@ use the title of the current OS window, if any.
--logo --logo
type=path type=path
completion=ext:png group:"PNG images" relative:conf completion=type:file ext:png group:"PNG images" relative:conf
Path to a PNG image to use as the logo for the newly created window. See Path to a PNG image to use as the logo for the newly created window. See
:opt:`window_logo_path`. Relative paths are resolved from the kitty configuration directory. :opt:`window_logo_path`. Relative paths are resolved from the kitty configuration directory.
@ -228,6 +228,7 @@ from, or specify them individually, for example: :code:`--color background=white
--watcher -w --watcher -w
type=list type=list
completion=type:file ext:py relative:conf group:"Python scripts"
Path to a Python file. Appropriately named functions in this file will be called Path to a Python file. Appropriately named functions in this file will be called
for various events, such as when the window is resized, focused or closed. See for various events, such as when the window is resized, focused or closed. See
the section on watchers in the launch command documentation: :ref:`watchers`. the section on watchers in the launch command documentation: :ref:`watchers`.