Skip to content

Commit

Permalink
Merge pull request #55 from yumemi-nohshiro/feat/experimental-shell-c…
Browse files Browse the repository at this point in the history
…ompletion

feat: bash/zsh completion (experimental)
  • Loading branch information
siketyan authored Jan 17, 2023
2 parents 1ce78a2 + bc85876 commit 2c23931
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 2 deletions.
89 changes: 89 additions & 0 deletions resources/shell/bash/ghr-completion.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#!/usr/bin/env bash

__ghr_complete__static() {
local options

options=("${@:2}")

compgen -W "${options[*]}" -- "$1"
}

__ghr_complete__repos() {
local repositories suggestions

repositories="$(ghr list)"
suggestions="$(compgen -W "${repositories}" -- "$1")"

if [[ $1 != -* && ${COMP_CWORD} -ne 2 ]]; then
return
fi

echo "$suggestions"
}

__ghr_complete() {
local cword

# Replaces ':' in $COMP_WORDBREAKS to prevent bash appends the suggestion after ':' repeatedly
COMP_WORDBREAKS=${COMP_WORDBREAKS//:/}

cword="${COMP_WORDS[COMP_CWORD]}"

if [ "${COMP_CWORD}" = 1 ]; then
COMPREPLY=($(__ghr_complete__static "${cword}" --help cd clone delete help init open path profile shell version))
return 0
fi

case "${COMP_WORDS[1]}" in
cd)
COMPREPLY=($(__ghr_complete__repos "${cword}"))
;;
clone)
COMPREPLY=($(__ghr_complete__static "${cword}" --help))
;;
delete)
COMPREPLY=($(__ghr_complete__static "${cword}" --help))
;;
init)
COMPREPLY=($(__ghr_complete__static "${cword}" --help))
;;
open)
COMPREPLY=($(__ghr_complete__static "${cword}" --help))
;;
path)
COMPREPLY=($(__ghr_complete__repos "${cword}"))
;;
profile)
COMPREPLY=($(__ghr_complete__static "${cword}" --help))
;;
shell)
COMPREPLY=($(__ghr_complete__static "${cword}" --help))
;;
version)
COMPREPLY=($(__ghr_complete__static "${cword}" --help))
;;
help)
COMPREPLY=()
;;
*)
;;
esac
}

# complete is a bash builtin, but recent versions of ZSH come with a function
# called bashcompinit that will create a complete in ZSH. If the user is in
# ZSH, load and run bashcompinit before calling the complete function.
if [[ -n ${ZSH_VERSION-} ]]; then
# First calling compinit (only if not called yet!)
# and then bashcompinit as mentioned by zsh man page.
if ! command -v compinit >/dev/null; then
autoload -U +X compinit && if [[ ${ZSH_DISABLE_COMPFIX-} = true ]]; then
compinit -u
else
compinit
fi
fi
autoload -U +X bashcompinit && bashcompinit
fi

complete -F __ghr_complete -o bashdefault -o default ghr
2 changes: 2 additions & 0 deletions resources/shell/ghr.bash → resources/shell/bash/ghr.bash
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,5 @@ ghr() {

$__GHR $@
}

source ./ghr-completion.bash
2 changes: 2 additions & 0 deletions resources/shell/ghr.fish → resources/shell/fish/ghr.fish
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@ function ghr

command ghr $argv[1..]
end

complete -c ghr -n "__fish_seen_subcommand_from cd path" -a "(ghr list)"
4 changes: 2 additions & 2 deletions src/cmd/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ pub struct Cmd {
impl Cmd {
pub fn run(self) -> Result<()> {
let script = match self.kind {
Kind::Bash => include_str!("../../resources/shell/ghr.bash"),
Kind::Fish => include_str!("../../resources/shell/ghr.fish"),
Kind::Bash => include_str!("../../resources/shell/bash/ghr.bash"),
Kind::Fish => include_str!("../../resources/shell/fish/ghr.fish"),
};

print!("{}", script);
Expand Down

0 comments on commit 2c23931

Please sign in to comment.