fork/fish-shell

git clone https://git.julia.blue/fork/fish-shell

Johannes AltmanningerMerge pull request #116984a12d5771

master
7.4 KiB133 linesraw
1# Completions for yadm v2
2
3# Print an optspec for argparse to handle yadm's options
4# that are independent of any subcommand. Borrowed from git completion file.
5function __fish_yadm_global_optspecs
6    string join \n v-version h/help C= c=+ 'e-exec-path=?' H-html-path M-man-path I-info-path p/paginate \
7        P/no-pager o-no-replace-objects b-bare G-git-dir= W-work-tree= N-namespace= S-super-prefix= \
8        l-literal-pathspecs g-glob-pathspecs O-noglob-pathspecs i-icase-pathspecs \
9        Y/yadm-dir= R-yadm-repo= n-yadm-config= E-yadm-encrypt= a-yadm-archive= B-yadm-bootstrap=
10end
11
12# Config values accepted by `yadm config`
13# See `yadm gitconfig` for setting git config values
14function __fish_yadm_config_keys
15    yadm introspect configs
16end
17
18set -l __fish_yadm_subcommands (yadm introspect commands)
19
20# Borrowed from git completion file
21function __fish_yadm_needs_command
22    # Figure out if the current invocation already has a command.
23    set -l cmd (commandline -xpc)
24    set -e cmd[1]
25    argparse -s (__fish_yadm_global_optspecs) -- $cmd 2>/dev/null
26    or return 0
27    # These flags function as commands, effectively.
28    set -q _flag_version; and return 1
29    set -q _flag_html_path; and return 1
30    set -q _flag_man_path; and return 1
31    set -q _flag_info_path; and return 1
32    if set -q argv[1]
33        # Also print the command, so this can be used to figure out what it is.
34        echo $argv[1]
35        return 1
36    end
37    return 0
38end
39
40# Borrowed from git completion file
41function __fish_yadm_using_command
42    set -l cmd (__fish_yadm_needs_command)
43    test -z "$cmd"
44    and return 1
45    contains -- $cmd $argv
46    and return 0
47    return 1
48end
49
50# Manual wrapping (instead of `complete -w`) is necessary here because we
51# don't want to inherit all completions from git
52function __fish_complete_yadm_like_git
53    # Remove the first word from the commandline because that is "yadm"
54    set -l cmdline (commandline -xpc | string escape; commandline -ct)[2..-1]
55
56    # `yadm gitconfig` is same as `git config`
57    if __fish_seen_subcommand_from gitconfig
58        set cmdline (string replace 'gitconfig' 'config' "$cmdline")
59    end
60
61    set -l yadm_work_tree (yadm gitconfig --get core.worktree)
62    set -l yadm_repo (yadm introspect repo)
63
64    argparse -u 'R-yadm-repo=&' -- $cmdline 2>/dev/null
65    if set -q _flag_yadm_repo
66        set yadm_repo $_flag_yadm_repo
67        # argparse -u *always* sets $argv to remaining arguments after consuming all options,
68        # and it *always* sets $argv_opts to any specified non-& options and any unknown options
69        # (the -- is needed in case $argv originally contained a -- followed by arguments starting with a -)
70        set cmdline $argv_opts -- $argv
71    end
72
73    set -l git_wrapper_cmd "git --work-tree $yadm_work_tree --git-dir $yadm_repo $cmdline"
74
75    # `complete -a` expects each completion to be separated by space, not newline
76    complete -C "$git_wrapper_cmd"
77end
78
79# General git wrapping
80complete -f -c yadm -n "not __fish_seen_subcommand_from $__fish_yadm_subcommands" -a '(__fish_complete_yadm_like_git)'
81
82# Global options
83complete -F -r -c yadm -l yadm-dir -s Y -a '(__fish_complete_directories)' -d 'Override the yadm directory'
84complete -F -r -c yadm -l yadm-repo -d 'Override location of yadm repository'
85complete -F -r -c yadm -l yadm-config -d 'Override location of yadm configuration file'
86complete -F -r -c yadm -l yadm-encrypt -d 'Override location of yadm encryption configuration'
87complete -F -r -c yadm -l yadm-archive -d 'Override location of yadm encrypted files archive'
88complete -F -r -c yadm -l yadm-bootstrap -d 'Override location of yadm bootstrap program'
89
90# Subcommands
91complete -f -c yadm -n __fish_yadm_needs_command -a version -d 'Print yadm version'
92complete -f -c yadm -n __fish_yadm_needs_command -a init -d 'Initialize new repository for tracking dotfiles'
93complete -f -c yadm -n __fish_yadm_needs_command -a config -d 'Manage configuration for yadm'
94complete -f -c yadm -n __fish_yadm_needs_command -a list -d 'Print list of files managed by yadm'
95complete -f -c yadm -n __fish_yadm_needs_command -a bootstrap -d 'Execute bootstrap script'
96complete -f -c yadm -n __fish_yadm_needs_command -a encrypt -d 'Encrypt files matched by encrypt spec file'
97complete -f -c yadm -n __fish_yadm_needs_command -a decrypt -d 'Decrypt files matched by encrypt spec file'
98complete -f -c yadm -n __fish_yadm_needs_command -a alt -d 'Setup and process alternate files'
99complete -f -c yadm -n __fish_yadm_needs_command -a perms -d 'Update permissions'
100complete -f -c yadm -n __fish_yadm_needs_command -a enter -d 'Run sub-shell with all git variables set'
101complete -f -c yadm -n __fish_yadm_needs_command -a git-crypt -d 'Pass options to git-crypt if installed'
102complete -f -c yadm -n __fish_yadm_needs_command -a gitconfig -d 'Pass options to the git config command'
103complete -f -c yadm -n __fish_yadm_needs_command -a upgrade -d 'Migrate from v1 to v2'
104complete -f -c yadm -n __fish_yadm_needs_command -a introspect -d 'Report internal yadm data'
105complete -f -c yadm -n __fish_yadm_needs_command -a clone -d 'Clone remote repository for tracking dotfiles'
106
107# Options for subcommands
108complete -f -c yadm -n "__fish_yadm_using_command init" -s f -d 'Overwrite existing repository'
109complete -f -c yadm -n "__fish_yadm_using_command init" -s w -r -a '(__fish_complete_directories)' -d 'Override work-tree'
110complete -f -c yadm -n "__fish_yadm_using_command clone" -s f -d 'Overwrite existing repository'
111complete -f -c yadm -n "__fish_yadm_using_command clone" -s w -r -a '(__fish_complete_directories)' -d 'Override work-tree'
112complete -f -c yadm -n "__fish_yadm_using_command clone" -s b -d 'Use another branch'
113complete -f -c yadm -n "__fish_yadm_using_command clone" -l bootstrap -d 'Force run the bootstrap script'
114complete -f -c yadm -n "__fish_yadm_using_command clone" -l no-bootstrap -d 'Do not execute bootstrap script'
115complete -f -c yadm -n "__fish_yadm_using_command list" -s a -d 'List all files'
116complete -f -c yadm -n "__fish_yadm_using_command decrypt" -s l -d 'List files without extracting'
117complete -f -c yadm -n "__fish_yadm_using_command introspect" -x -a 'commands configs repo switches'
118
119# These inherit some options from git
120complete -f -c yadm -n "__fish_yadm_using_command gitconfig" -a '(__fish_complete_yadm_like_git)'
121
122# Some options for `yadm config` takes configuration keys as arguments, and these should have
123# yadm's config keys instead of git's. Inherit all other options.
124complete -f -c yadm -n "__fish_yadm_using_command config; and not __fish_seen_argument -l get -l get-all -l replace-all -l unset -l unset-all" -a '(__fish_complete_yadm_like_git)'
125
126complete -f -c yadm -n "__fish_yadm_using_command config" -l get -r -a '(__fish_yadm_config_keys)' -d 'Get config with name'
127complete -f -c yadm -n "__fish_yadm_using_command config" -l get-all -a '(__fish_yadm_config_keys)' -d 'Get all values matching key'
128complete -f -c yadm -n "__fish_yadm_using_command config" -l replace-all -r -a '(__fish_yadm_config_keys)' -d 'Replace all matching variables'
129complete -f -c yadm -n "__fish_yadm_using_command config" -l unset -a '(__fish_yadm_config_keys)' -d 'Remove a variable'
130complete -f -c yadm -n "__fish_yadm_using_command config" -l unset-all -a '(__fish_yadm_config_keys)' -d 'Remove matching variables'
131
132# If no argument is specified for `config`, it's as if --get was used
133complete -f -c yadm -n "__fish_yadm_using_command config; and __fish_is_nth_token 3" -a '(__fish_yadm_config_keys)'