Skip to content

Commit

Permalink
Add completion for bash and fish shells
Browse files Browse the repository at this point in the history
  • Loading branch information
ovk committed Aug 13, 2022
1 parent 7f396b1 commit a1fb0ce
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -460,3 +460,8 @@ For Arch Linux, dotref can be installed from the AUR [dotref](https://aur.archli
Dotref works on Windows and has been tested in WSL2, MSYS2 and Cygwin (it may work in other environments as well).
It can be either installed manually or with `pip`.

## Shell Completion
Arch package includes shell auto-completion for Bash and Fish shells, which will be installed automatically.
For other types of installations, the completion [files](https://github.com/ovk/dotref/tree/master/completion)
can be downloaded and installed manually, if desired.

38 changes: 38 additions & 0 deletions completions/completion.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
_dotref()
{
local cur
cur="${COMP_WORDS[COMP_CWORD]}"

if [ $COMP_CWORD -eq 1 ]; then
COMPREPLY=($(compgen -W '-h --help init sync unlink status profiles version' -- $cur))
else
case ${COMP_WORDS[1]} in
init|sync|unlink|status|profiles)
_dotref_opt_complete
;;
esac

fi
}

_dotref_opt_complete()
{
local cur
cur="${COMP_WORDS[COMP_CWORD]}"

if [ $COMP_CWORD -ge 2 ]; then
case ${COMP_WORDS[COMP_CWORD-1]} in
-d|--dotdir)
COMPREPLY=($(compgen -d -- $cur))
;;
-s|--statefile|-p|--profile)
COMPREPLY=($(compgen -f -- $cur))
;;
*)
COMPREPLY=($(compgen -W '-v --verbose -s --statefile -d --dotdir -p --profile' -- $cur))
;;
esac
fi
}

complete -o bashdefault -o default -F _dotref dotref
23 changes: 23 additions & 0 deletions completions/completion.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
set -l commands -h --help init sync unlink status profiles version

set -l h -s h -l help -d 'Print help message and exit'
set -l v -s v -l verbose -d 'Produce more verbose output'
set -l d -s d -l dotdir -d 'Directory containing profiles' -rF
set -l s -s s -l statefile -d 'Dotref state file' -rF
set -l p -s p -l profile -d 'Name of the profile to use' -rF

complete -c dotref -f

complete -c dotref -n "not __fish_seen_subcommand_from $commands" -a "$commands"

for line in 'init: p d s v' \
'sync: d s v' \
'unlink: d s v' \
'status: d s v' \
'profiles: p d v'
set -l command (echo "$line" | cut -d: -f1)

for opt in (echo "$line" | cut -d: -f2 | string split -n ' ')
complete -c dotref -n "__fish_seen_subcommand_from $command" $$opt
end
end
2 changes: 2 additions & 0 deletions pkg/arch/PKGBUILD
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ sha256sums=('SKIP')
package() {
cd "${pkgname}"
python setup.py install --root="${pkgdir}/"
install -Dm644 ${srcdir}/${pkgname}/completions/completion.bash "${pkgdir}/usr/share/bash-completion/completions/${pkgname}"
install -Dm644 ${srcdir}/${pkgname}/completions/completion.fish "${pkgdir}/usr/share/fish/completions/${pkgname}.fish"
}

0 comments on commit a1fb0ce

Please sign in to comment.