| #!/usr/bin/env bash |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| _odysseus_scripts_dir() { |
| |
| |
| local self="${BASH_SOURCE[0]}" |
| while [ -L "$self" ]; do self=$(readlink "$self"); done |
| cd "$(dirname "$self")/.." && pwd |
| } |
|
|
| declare -A _ODYSSEUS_SUBS_CACHE=() |
|
|
| _odysseus_refresh_cache() { |
| local dir="$(_odysseus_scripts_dir)" |
| _ODYSSEUS_SUBS_CACHE=() |
| |
| |
| local py="$dir/../venv/bin/python" |
| [ -x "$py" ] || py="$(command -v python3)" |
| local f |
| for f in "$dir"/odysseus-*; do |
| [ -x "$f" ] || continue |
| case "$f" in *.bak|*.pyc|*.pre-*) continue ;; esac |
| local name="$(basename "$f")" |
| local sub="${name#odysseus-}" |
| local help_out |
| help_out=$("$py" "$f" --help 2>/dev/null) || continue |
| local commands |
| commands=$(echo "$help_out" | grep -oE '\{[a-z0-9_,-]+\}' | head -1 \ |
| | tr -d '{}' | tr ',' ' ') |
| _ODYSSEUS_SUBS_CACHE[$sub]="$commands" |
| done |
| } |
|
|
| _odysseus_complete() { |
| [ ${#_ODYSSEUS_SUBS_CACHE[@]} -eq 0 ] && _odysseus_refresh_cache |
|
|
| local cur="${COMP_WORDS[COMP_CWORD]}" |
| local cmd="${COMP_WORDS[0]}" |
|
|
| |
| if [ "$cmd" = "odysseus" ]; then |
| if [ "$COMP_CWORD" -eq 1 ]; then |
| local subs="${!_ODYSSEUS_SUBS_CACHE[@]} help" |
| COMPREPLY=($(compgen -W "$subs" -- "$cur")) |
| return 0 |
| fi |
| |
| local sub="${COMP_WORDS[1]}" |
| |
| if [ "$sub" = "help" ] && [ "$COMP_CWORD" -eq 2 ]; then |
| COMPREPLY=($(compgen -W "${!_ODYSSEUS_SUBS_CACHE[*]}" -- "$cur")) |
| return 0 |
| fi |
| if [ "$COMP_CWORD" -eq 2 ]; then |
| COMPREPLY=($(compgen -W "${_ODYSSEUS_SUBS_CACHE[$sub]}" -- "$cur")) |
| return 0 |
| fi |
| return 0 |
| fi |
|
|
| |
| local sub="${cmd#odysseus-}" |
| if [ "$COMP_CWORD" -eq 1 ]; then |
| COMPREPLY=($(compgen -W "${_ODYSSEUS_SUBS_CACHE[$sub]}" -- "$cur")) |
| return 0 |
| fi |
| } |
|
|
| |
| complete -F _odysseus_complete odysseus |
| for f in "$(_odysseus_scripts_dir)"/odysseus-*; do |
| [ -x "$f" ] || continue |
| case "$f" in *.bak|*.pyc|*.pre-*) continue ;; esac |
| complete -F _odysseus_complete "$(basename "$f")" |
| done |
|
|