Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supports fish shell #174

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

soraxas
Copy link

@soraxas soraxas commented Jul 23, 2024

Closes #134

soraxas and others added 2 commits July 24, 2024 00:49
Signed-off-by: Tin Lai <tin@tinyiu.com>
@soraxas
Copy link
Author

soraxas commented Jul 23, 2024

Result:

$ shtab --shell=fish shtab.main.get_main_parser
# AUTOMATICALLY GENERATED by `shtab`

complete -c shtab -n __fish_use_subcommand -s h -l help -d 'show this help message and exit'
complete -c shtab -n __fish_use_subcommand -l version -d 'show program\'s version number and exit'
complete -c shtab -n __fish_use_subcommand -s s -l shell -a '(printf "%s\t%s\n" bash shell zsh shell tcsh shell fish shell)' -x
complete -c shtab -n __fish_use_subcommand -l prefix -d 'prepended to generated functions to avoid clashes'
complete -c shtab -n __fish_use_subcommand -l preamble -d 'prepended to generated script'
complete -c shtab -n __fish_use_subcommand -l prog -d 'custom program name (overrides `parser.prog`)'
complete -c shtab -n __fish_use_subcommand -s u -l error-unimportable -d 'raise errors if `parser` is not found in $PYTHONPATH'
complete -c shtab -n __fish_use_subcommand -l verbose -d 'Log debug information'
complete -c shtab -n __fish_use_subcommand -l print-own-completion -d 'print shtab\'s own completion' -a '(printf "%s\t%s\n" bash print_own_completion zsh print_own_completion tcsh print_own_completion fish print_own_completion)' -x
$ shtab --shell=fish tmuxp.cli.create_parser
# AUTOMATICALLY GENERATED by `shtab`

complete -c tmuxp -n '__fish_seen_subcommand_from load' -s h -l help -d 'show this help message and exit'
complete -c tmuxp -n '__fish_seen_subcommand_from load' -s L -d 'passthru to tmux(1) -L'
complete -c tmuxp -n '__fish_seen_subcommand_from load' -s S -d 'passthru to tmux(1) -S'
complete -c tmuxp -n '__fish_seen_subcommand_from load' -s f -d 'passthru to tmux(1) -f'
complete -c tmuxp -n '__fish_seen_subcommand_from load' -s s -d 'start new session with new session name'
complete -c tmuxp -n '__fish_seen_subcommand_from load' -s y -l yes -d 'always answer yes'
complete -c tmuxp -n '__fish_seen_subcommand_from load' -s d -d 'load the session without attaching it'
complete -c tmuxp -n '__fish_seen_subcommand_from load' -s a -l append -d 'load workspace, appending windows to the current session'
complete -c tmuxp -n '__fish_seen_subcommand_from load' -s 2 -d 'force tmux to assume the terminal supports 256 colours.'
complete -c tmuxp -n '__fish_seen_subcommand_from load' -s 8 -d 'like -2, but indicates that the terminal supports 88 colours.'
complete -c tmuxp -n '__fish_seen_subcommand_from load' -l log-file -d 'file to log errors/output to'
complete -c tmuxp -n '__fish_seen_subcommand_from shell' -s h -l help -d 'show this help message and exit'
complete -c tmuxp -n '__fish_seen_subcommand_from shell' -s S -d 'pass-through for tmux -S'
complete -c tmuxp -n '__fish_seen_subcommand_from shell' -s L -d 'pass-through for tmux -L'
complete -c tmuxp -n '__fish_seen_subcommand_from shell' -s c -d 'instead of opening shell, execute python code in libtmux and exit'
complete -c tmuxp -n '__fish_seen_subcommand_from shell' -l best -d 'use best shell available in site packages'
complete -c tmuxp -n '__fish_seen_subcommand_from shell' -l pdb -d 'use plain pdb'
complete -c tmuxp -n '__fish_seen_subcommand_from shell' -l code -d 'use stdlib\'s code.interact()'
complete -c tmuxp -n '__fish_seen_subcommand_from shell' -l ptipython -d 'use ptpython + ipython'
complete -c tmuxp -n '__fish_seen_subcommand_from shell' -l ptpython -d 'use ptpython'
complete -c tmuxp -n '__fish_seen_subcommand_from shell' -l ipython -d 'use ipython'
complete -c tmuxp -n '__fish_seen_subcommand_from shell' -l bpython -d 'use bpython'
complete -c tmuxp -n '__fish_seen_subcommand_from shell' -l use-pythonrc -d 'load PYTHONSTARTUP env var and ~/.pythonrc.py script in --code'
complete -c tmuxp -n '__fish_seen_subcommand_from shell' -l no-startup -d 'load PYTHONSTARTUP env var and ~/.pythonrc.py script in --code'
complete -c tmuxp -n '__fish_seen_subcommand_from shell' -l use-vi-mode -d 'use vi-mode in ptpython/ptipython'
complete -c tmuxp -n '__fish_seen_subcommand_from shell' -l no-vi-mode -d 'use vi-mode in ptpython/ptipython'
complete -c tmuxp -n '__fish_seen_subcommand_from import teamocil' -s h -l help -d 'show this help message and exit'
complete -c tmuxp -n '__fish_seen_subcommand_from import tmuxinator' -s h -l help -d 'show this help message and exit'
complete -c tmuxp -n '__fish_seen_subcommand_from import' -s h -l help -d 'show this help message and exit'
complete -c tmuxp -n '__fish_seen_subcommand_from convert' -s h -l help -d 'show this help message and exit'
complete -c tmuxp -n '__fish_seen_subcommand_from convert' -s y -l yes -d 'always answer yes'
complete -c tmuxp -n '__fish_seen_subcommand_from debug-info' -s h -l help -d 'show this help message and exit'
complete -c tmuxp -n '__fish_seen_subcommand_from ls' -s h -l help -d 'show this help message and exit'
complete -c tmuxp -n '__fish_seen_subcommand_from edit' -s h -l help -d 'show this help message and exit'
complete -c tmuxp -n '__fish_seen_subcommand_from freeze' -s h -l help -d 'show this help message and exit'
complete -c tmuxp -n '__fish_seen_subcommand_from freeze' -s S -d 'pass-through for tmux -S'
complete -c tmuxp -n '__fish_seen_subcommand_from freeze' -s L -d 'pass-through for tmux -L'
complete -c tmuxp -n '__fish_seen_subcommand_from freeze' -s f -l workspace-format -d 'format to save in' -a '(printf "%s\t%s\n" yaml workspace_format json workspace_format)' -x
complete -c tmuxp -n '__fish_seen_subcommand_from freeze' -s o -l save-to -d 'file to save to'
complete -c tmuxp -n '__fish_seen_subcommand_from freeze' -s y -l yes -d 'always answer yes'
complete -c tmuxp -n '__fish_seen_subcommand_from freeze' -s q -l quiet -d 'don\'t prompt for confirmation'
complete -c tmuxp -n '__fish_seen_subcommand_from freeze' -l force -d 'overwrite the workspace file'
complete -c tmuxp -n __fish_use_subcommand -s h -l help -d 'show this help message and exit'
complete -c tmuxp -n __fish_use_subcommand -s V -l version -d 'show program\'s version number and exit'
complete -c tmuxp -n __fish_use_subcommand -l log-level -d 'log level (debug, info, warning, error, critical) (default "info")' -a '(printf "%s\t%s\n" debug log_level info log_level warning log_level error log_level critical log_level)' -x

complete -c tmuxp -n '__fish_seen_subcommand_from import' -a '(printf "%s\t%s\n" teamocil "convert and import a teamocil config" tmuxinator "convert and import a tmuxinator config")' -x
complete -c tmuxp -n __fish_use_subcommand -a '(printf "%s\t%s\n" load "load tmuxp workspaces" shell "launch python shell for tmux server, session, window and pane" import "import workspaces from teamocil and tmuxinator." convert "convert workspace files between yaml and json." debug-info "print out all diagnostic info" ls "list workspaces in tmuxp directory" edit "run $EDITOR on workspace file" freeze "freeze a live tmux session to a tmuxp workspace file")' -x

@casperdcl casperdcl added enhancement New feature or request external-request You asked, we did shell-fish labels Jul 23, 2024
@casperdcl
Copy link
Collaborator

Thanks for this! There are a few failing tests though...

 _______________________ test_main_self_completion[fish] ________________________
tests/test_shtab.py:79: in test_main_self_completion
    assert expected[shell] in captured.out
E   KeyError: 'fish'
___________________________ test_prog_scripts[fish] ____________________________
tests/test_shtab.py:115: in test_prog_scripts
    raise NotImplementedError(shell)
E   NotImplementedError: fish
________________________ test_positional_choices[fish] _________________________
tests/test_shtab.py:154: in test_positional_choices
    completion = shtab.complete(parser, shell=shell)
shtab/__init__.py:970: in complete
    return completer(
shtab/__init__.py:932: in complete_fish
    option_strings, choices = get_fish_commands(parser, choice_functions=choice_functions)
shtab/__init__.py:920: in get_fish_commands
    recurse(root_parser)
shtab/__init__.py:882: in recurse
    choices_to_action = {v.dest: v.help for v in positional._choices_actions}
E   AttributeError: '_StoreAction' object has no attribute '_choices_actions'
____________________ test_add_argument_to_positional[fish] _____________________
tests/test_shtab.py:287: in test_add_argument_to_positional
    completion_manual = shtab.complete(parser, shell=shell)
shtab/__init__.py:970: in complete
    return completer(
shtab/__init__.py:932: in complete_fish
    option_strings, choices = get_fish_commands(parser, choice_functions=choice_functions)
shtab/__init__.py:920: in get_fish_commands
    recurse(root_parser)
shtab/__init__.py:899: in recurse
    recurse(
shtab/__init__.py:882: in recurse
    choices_to_action = {v.dest: v.help for v in positional._choices_actions}
E   AttributeError: 'PrintCompletionAction' object has no attribute '_choices_actions'

@skshetry
Copy link
Member

skshetry commented Jul 23, 2024

fish shell completion is painful when you have subcommand and sub-subcommands (eg: dvc exp show --option). Have you tried these cases?

I’ll try tomorrow, it would be quite useful for me if it works.

soraxas and others added 3 commits July 24, 2024 21:56
Copy link

codecov bot commented Jul 24, 2024

Codecov Report

Attention: Patch coverage is 92.85714% with 6 lines in your changes missing coverage. Please review.

Project coverage is 89.63%. Comparing base (1f87154) to head (09ded71).

Files Patch % Lines
shtab/__init__.py 92.85% 6 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #174      +/-   ##
==========================================
+ Coverage   88.88%   89.63%   +0.75%     
==========================================
  Files           3        3              
  Lines         360      444      +84     
==========================================
+ Hits          320      398      +78     
- Misses         40       46       +6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@@ -38,8 +38,8 @@
SUPPORTED_SHELLS: List[str] = []
_SUPPORTED_COMPLETERS = {}
CHOICE_FUNCTIONS: Dict[str, Dict[str, str]] = {
"file": {"bash": "_shtab_compgen_files", "zsh": "_files", "tcsh": "f"},
"directory": {"bash": "_shtab_compgen_dirs", "zsh": "_files -/", "tcsh": "d"}}
"file": {"bash": "_shtab_compgen_files", "zsh": "_files", "tcsh": "f", "fish": None},
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what's the usage for these

Copy link
Collaborator

@casperdcl casperdcl Jul 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's for shtab.FILE and shtab.DIRECTORY as per the pathcomplete example in https://docs.iterative.ai/shtab/use/#library-usage i.e.

pathcomplete <TAB> # should suggest filenames
pathcomplete --dir <TAB> # should suggest directories

Copy link
Author

@soraxas soraxas Jul 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see; but there's no distinction between filenames and directories. In addition, complete in fish by default matches files, unless you explicitly tells it not to, with an additional flag

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from https://fishshell.com/docs/current/completions.html#useful-functions-for-writing-completions it looks like these are required:

  • -f
  • -F
  • __fish_complete_directories
  • __fish_complete_path

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request external-request You asked, we did shell-fish
Projects
None yet
Development

Successfully merging this pull request may close these issues.

fish shell support
3 participants