Skip to content

Commit

Permalink
Add bash completion script
Browse files Browse the repository at this point in the history
Signed-off-by: Phil Dibowitz <phil@ipom.com>
  • Loading branch information
jaymzh committed Feb 12, 2024
1 parent 9813b20 commit eda3c43
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -415,3 +415,9 @@ will allow me to make sure it shows up in the Repology badge above.
Since it's Ruby, it should work across all platforms, however, it's developed
and primarily tested on Linux as well as regularly used on Mac. I've not tested
it on Windows, but I'll happily accept patches for Windows compatibility.

**How do I get tab-completion?**

If the package for your OS/distro didn't set it up manually, you should find
that `sugarjar_completion.bash` is included in the package, and you can simply
source that in your dotfiles, assuming you are using bash.
42 changes: 42 additions & 0 deletions extras/sugarjar_completion.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# bash completion for sugarjar

SJCONFIG="$HOME/.config/sugarjar/config.yaml"

_sugarjar_completions()
{
if [ "${#COMP_WORDS[@]}" -eq 2 ]; then
return
fi

local -a suggestions

# grap the feature_prefix if we have one so that we
# can let the user ignore that part. If we have `yq`
# we'll use it as that's going to be always 100%
# reliable, but if we don't, do our best with shell
# utils
local prefix=''
if [ -e "$SJCONFIG" ]; then
if type yq &>/dev/null; then
prefix=$(yq .feature_prefix $SJCONFIG)
else
# the xargs removes extra spaces
prefix=$(grep feature_prefix $SJCONFIG | cut -f2 -d: | xargs)
fi
fi

case "${COMP_WORDS[1]}" in
co|checkout|bclean)
local branches=$(git branch | sed -e 's/* //g' | xargs)
if [ -n "$prefix" ]; then
local branches=$(echo $branches | sed -e "s!$prefix!!g")
fi
suggestions=($(compgen -W "$branches" -- "${COMP_WORDS[2]}"))
COMPREPLY=("${suggestions[@]}")
;;
*)
return
esac
}

complete -F _sugarjar_completions sj
2 changes: 1 addition & 1 deletion sugarjar.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Gem::Specification.new do |spec|
spec.files =
Dir.glob('lib/sugarjar/*.rb') +
Dir.glob('bin/*') +
docs
Dir.glob('extras/*')

spec.add_dependency 'deep_merge'
spec.add_dependency 'mixlib-log'
Expand Down

0 comments on commit eda3c43

Please sign in to comment.