Skip to content

Commit

Permalink
feat(blih): Add bash completion to blih
Browse files Browse the repository at this point in the history
* feat(blih): Add bash completion to blih (#8)

* fix(blih): Fix if command contain -

* fix(blih): Do not complete flags in subcommands
  • Loading branch information
Hinara authored and Xephi committed May 27, 2019
1 parent 548c35e commit a17d919
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 0 deletions.
107 changes: 107 additions & 0 deletions bash_completion.d/blih
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#!/usr/bin/env bash

_blih_sshkey() {
local commands=(
upload
list
delete
)
local options
__blih_arg "_blih_sshkey"
}

_blih_repository() {
local commands=(
create
delete
info
getacl
list
setacl
)
local options
__blih_arg "_blih_repository"
return 0
}

__blih_binary_flags() {
local parts=( $1 )
local IFS='|'
local extglob=$( echo "${parts[*]}" )
echo "@($extglob)"
}

__blih_get_next_command() {
local binary_flags=$1
local counter=$command_pos
local in_arg=0
(( counter++ ))
while [ "$counter" -lt "$COMP_CWORD" ]; do
case "${COMP_WORDS[$counter]}" in
$(__blih_binary_flags "$binary_flags"))
;;
-*)
in_arg=1
flag=${COMP_WORDS[$counter]}
;;
*)
if [ "$in_arg" -eq "0" ]; then
command_pos=$counter
command=${COMP_WORDS[$counter]}
return 0
fi
in_arg=0
;;
esac
(( counter++ ))
done
if [ "$in_arg" -ne "0" ]; then
return 2
fi
return 1
}

__blih_arg() {
__blih_get_next_command "$2"
case $? in
0)
local completions_func=$1_${command//-/_}
declare -F $completions_func >/dev/null && $completions_func
;;
1)
case "$cur" in
-*)
COMPREPLY=( $( compgen -W '${options[*]}' -- $cur ) );;
*)
COMPREPLY=( $( compgen -W '${commands[*]}' -- $cur ) );;
esac
;;
2)
local completions_func=$1_${flag//-/_}
declare -F $completions_func >/dev/null && $completions_func
esac
}

_blih() {
COMPREPLY=()
local cur=${COMP_WORDS[COMP_CWORD]}
local command
local command_pos=0
local flag

local commands=(
repository
sshkey
whoami
)
local options=(
-u --user
-v --verbose
-b --baseurl
-t --token
)
__blih_arg "_blih" "-v --verbose"
return 0
}

complete -F _blih blih blih.py
2 changes: 2 additions & 0 deletions install_packages_dump.sh
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,5 @@ cd epitech-emacs
git checkout 431df95085337217bbc834e28cc0f09e11b2caf3
./INSTALL.sh system
cd .. && rm -rf epitech-emacs

install -m 644 bash_completion.d/blih /usr/share/bash-completion/completions

0 comments on commit a17d919

Please sign in to comment.