Browse Source

improve fish completion by adding more completion options

Tin Lai 5 years ago
parent
commit
f5e8599de4
1 changed files with 63 additions and 39 deletions
  1. 63 39
      completion/yadm.fish_completion

+ 63 - 39
completion/yadm.fish_completion

@@ -1,53 +1,77 @@
 #!/usr/bin/fish
 
-function __fish_yadm_first_arg
-  set cmd (commandline -opc)
-  if [ (count $cmd) -eq 1 -a $cmd[1] = 'yadm' ]
-    return 0
-  end
-  return 1
+function __fish_yadm_universial_optspecs
+    string join \n 'a-yadm-dir=' 'b-yadm-repo=' 'c-yadm-config=' \
+                    'd-yadm-encrypt=' 'e-yadm-archive=' 'f-yadm-bootstrap='
 end
 
-function __fish_yadm_using_command
-    set cmd (commandline -opc)
-    if [ (count $cmd) -gt 1 -a "x$argv[1]" = "x$cmd[-1]" ]
-        return 0
+function __fish_yadm_needs_command
+    # Figure out if the current invocation already has a command.
+    set -l cmd (commandline -opc)
+    set -e cmd[1]
+    argparse -s (__fish_yadm_universial_optspecs) -- $cmd 2>/dev/null
+    or return 0
+    if set -q argv[1]
+        echo $argv[1]
+        return 1
     end
-    return 1
+    return 0
 end
 
-# no dirs in complete list
-complete -c yadm -x
+function __fish_yadm_using_command
+    set -l cmd (__fish_yadm_needs_command)
+    test -z "$cmd"
+    and return 1
+    contains -- $cmd $argv
+    and return 0
+end
 
 # yadm's specific autocomplete
-complete -f -c yadm -n '__fish_yadm_first_arg' -a 'clone' -d 'Clone an existing repository'
-complete -f -c yadm -n '__fish_yadm_first_arg' -a 'alt' -d 'Create links for alternates'
-complete -f -c yadm -n '__fish_yadm_first_arg' -a 'bootstrap' -d 'Execute $HOME/.config/yadm/bootstrap'
-complete -f -c yadm -n '__fish_yadm_first_arg' -a 'encrypt' -d 'Encrypt files'
-complete -f -c yadm -n '__fish_yadm_first_arg' -a 'perms' -d 'Fix perms for private files'
-complete -f -c yadm -n '__fish_yadm_first_arg' -a 'enter' -d 'Run sub-shell with GIT variables set'
-complete -f -c yadm -n '__fish_yadm_first_arg' -a 'git-crypt' -d 'Run git-crypt commands for the yadm repo'
-
-complete -f -c yadm -n '__fish_yadm_first_arg' -a 'init' -d 'Initialize an empty repository'
-complete -f -c yadm -n '__fish_yadm_using_command init' -s f
-
-complete -f -c yadm -n '__fish_yadm_first_arg' -a 'list' -d 'List tracked files'
-complete -f -c yadm -n '__fish_yadm_using_command list' -s a
-
-complete -f -c yadm -n '__fish_yadm_first_arg' -a 'decrypt' -d 'Decrypt files'
-complete -f -c yadm -n '__fish_yadm_using_command decrypt' -s l
-
-complete -f -c yadm -n '__fish_yadm_first_arg' -a 'introspect'
-complete -f -c yadm -n '__fish_yadm_using_command introspect' -a "commands"
-complete -f -c yadm -n '__fish_yadm_using_command introspect' -a "configs"
-complete -f -c yadm -n '__fish_yadm_using_command introspect' -a "repo"
-complete -f -c yadm -n '__fish_yadm_using_command introspect' -a "switches"
-
-complete -f -c yadm -n '__fish_yadm_first_arg' -a 'config' -d 'Configure a setting'
+complete -x -c yadm -n '__fish_yadm_needs_command'       -a 'clone'      -d 'Clone an existing repository'
+complete -F -c yadm -n '__fish_yadm_using_command clone' -s w            -d 'work-tree to use (default: $HOME)'
+complete -f -c yadm -n '__fish_yadm_using_command clone' -s b            -d 'branch to clone'
+complete -x -c yadm -n '__fish_yadm_using_command clone' -s f            -d 'force to overwrite'
+complete -x -c yadm -n '__fish_yadm_using_command clone' -l bootstrap    -d 'force bootstrap to run'
+complete -x -c yadm -n '__fish_yadm_using_command clone' -l no-bootstrap -d 'prevent bootstrap from beingrun'
+
+complete -x -c yadm -n '__fish_yadm_needs_command' -a 'alt'       -d 'Create links for alternates'
+complete -x -c yadm -n '__fish_yadm_needs_command' -a 'bootstrap' -d 'Execute $HOME/.config/yadm/bootstrap'
+complete -x -c yadm -n '__fish_yadm_needs_command' -a 'perms'     -d 'Fix perms for private files'
+complete -x -c yadm -n '__fish_yadm_needs_command' -a 'enter'     -d 'Run sub-shell with GIT variables set'
+complete    -c yadm -n '__fish_yadm_needs_command' -a 'git-crypt' -d 'Run git-crypt commands for the yadm repo'
+complete -x -c yadm -n '__fish_yadm_needs_command' -a 'help'      -d 'Print a summary of yadm commands'
+complete -x -c yadm -n '__fish_yadm_needs_command' -a 'upgrade'   -d 'Upgrade to version 2 of yadm directory structure'
+complete -x -c yadm -n '__fish_yadm_needs_command' -a 'version'   -d 'Print the version of yadm'
+
+complete -x -c yadm -n '__fish_yadm_needs_command' -a 'init' -d 'Initialize an empty repository'
+complete -x -c yadm -n '__fish_yadm_using_command init' -s f -d 'force to overwrite'
+complete -F -c yadm -n '__fish_yadm_using_command init' -s w -d 'set work-tree (default: $HOME)'
+
+complete -x -c yadm -n '__fish_yadm_needs_command' -a 'list' -d 'List tracked files at current directory'
+complete -x -c yadm -n '__fish_yadm_using_command list' -s a -d 'list all managed files instead'
+
+complete -x -c yadm -n '__fish_yadm_needs_command' -a 'encrypt' -d 'Encrypt files'
+complete -x -c yadm -n '__fish_yadm_needs_command' -a 'decrypt' -d 'Decrypt files'
+complete -x -c yadm -n '__fish_yadm_using_command decrypt' -s l -d 'list the files stored without extracting'
+
+complete -x -c yadm -n '__fish_yadm_needs_command' -a 'introspect' -d 'Report internal yadm data'
+complete -x -c yadm -n '__fish_yadm_using_command introspect' -a (printf -- '%s\n' 'commands configs repo switches') -d 'category'
+
+complete -x -c yadm -n '__fish_yadm_needs_command' -a 'gitconfig' -d 'Pass options to the git config command'
+complete -x -c yadm -n '__fish_yadm_needs_command' -a 'config'    -d 'Configure a setting'
 for name in (yadm introspect configs)
-    complete -f -c yadm -n "__fish_yadm_using_command config" -a "$name" -d "yadm config"
+    complete -x -c yadm -n '__fish_yadm_using_command config' -a '$name' -d 'yadm config'
 end
 
+# yadm universial options
+complete --force-files -c yadm -s Y -l yadm-dir       -d 'Override location of yadm directory'
+complete --force-files -c yadm      -l yadm-repo      -d 'Override location of yadm repository'
+complete --force-files -c yadm      -l yadm-config    -d 'Override location of yadm configuration file'
+complete --force-files -c yadm      -l yadm-encrypt   -d 'Override location of yadm encryption configuration'
+complete --force-files -c yadm      -l yadm-archive   -d 'Override location of yadm encrypted files archive'
+complete --force-files -c yadm      -l yadm-bootstrap -d 'Override location of yadm bootstrap program'
+
 # wraps git's autocomplete
 set -l GIT_DIR (yadm introspect repo)
-complete -c yadm -w "git --git-dir $GIT_DIR"
+# setup the correct git-dir by appending it to git's argunment
+complete -c yadm -w "git --git-dir=$GIT_DIR"