migration
This commit is contained in:
@@ -0,0 +1,127 @@
|
||||
# himi bash completion
|
||||
# Source this or drop in ~/.local/share/bash-completion/completions/
|
||||
|
||||
_himi() {
|
||||
local cur prev words cword
|
||||
|
||||
# Compatibility for older bash
|
||||
if type _init_completion &>/dev/null; then
|
||||
_init_completion || return
|
||||
else
|
||||
cur="${COMP_WORDS[COMP_CWORD]}"
|
||||
prev="${COMP_WORDS[COMP_CWORD-1]}"
|
||||
cword=$COMP_CWORD
|
||||
words=("${COMP_WORDS[@]}")
|
||||
fi
|
||||
|
||||
local commands="add enable disable remove rebuild list entropy test config uninstall"
|
||||
local flags="-o -n -h --help"
|
||||
|
||||
local config_dir="${XDG_CONFIG_HOME:-$HOME/.config}/himi"
|
||||
local lists_dir="${config_dir}/lists"
|
||||
local pool_dir="${config_dir}/pool"
|
||||
|
||||
# Check if we're in a multi-arg command (enable/disable)
|
||||
local cmd=""
|
||||
local i
|
||||
for ((i=1; i < cword; i++)); do
|
||||
case "${words[i]}" in
|
||||
enable|disable|add|entropy|test|config)
|
||||
cmd="${words[i]}"
|
||||
break
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
case "$cmd" in
|
||||
enable)
|
||||
# Show disabled lists + 'all' keyword
|
||||
if [[ -d "$lists_dir" ]]; then
|
||||
local available="all "
|
||||
local f
|
||||
for f in "$lists_dir"/*.txt; do
|
||||
[[ -f "$f" ]] || continue
|
||||
local name=$(basename "$f" .txt)
|
||||
[[ ! -L "$pool_dir/$name.txt" ]] && available+="$name "
|
||||
done
|
||||
COMPREPLY=($(compgen -W "$available" -- "$cur"))
|
||||
fi
|
||||
return
|
||||
;;
|
||||
disable)
|
||||
# Show enabled lists + 'all' keyword
|
||||
if [[ -d "$pool_dir" ]]; then
|
||||
local enabled="all "
|
||||
local f
|
||||
for f in "$pool_dir"/*.txt; do
|
||||
[[ -L "$f" ]] || continue
|
||||
enabled+="$(basename "$f" .txt) "
|
||||
done
|
||||
COMPREPLY=($(compgen -W "$enabled" -- "$cur"))
|
||||
fi
|
||||
return
|
||||
;;
|
||||
remove)
|
||||
if [[ -d "$lists_dir" ]]; then
|
||||
local lists=""
|
||||
for f in "$lists_dir"/*.txt; do
|
||||
[[ -f "$f" ]] || continue
|
||||
lists+="$(basename "$f" .txt) "
|
||||
done
|
||||
COMPREPLY=($(compgen -W "$lists" -- "$cur"))
|
||||
fi
|
||||
return
|
||||
;;
|
||||
add)
|
||||
COMPREPLY=($(compgen -f -X '!*.txt' -- "$cur"))
|
||||
return
|
||||
;;
|
||||
entropy)
|
||||
local templates="default easy secure pin diceware"
|
||||
if [[ -f "${config_dir}/config.sh" ]]; then
|
||||
local parsed
|
||||
parsed=$(grep -E '^\s*"[a-z]+\|' "${config_dir}/config.sh" 2>/dev/null | sed 's/.*"\([a-z]*\)|.*/\1/' | tr '\n' ' ')
|
||||
[[ -n "$parsed" ]] && templates="$parsed"
|
||||
fi
|
||||
COMPREPLY=($(compgen -W "$templates" -- "$cur"))
|
||||
return
|
||||
;;
|
||||
test|config)
|
||||
# No completion for test (user types pattern) or config (no args)
|
||||
return
|
||||
;;
|
||||
esac
|
||||
|
||||
# Handle flags
|
||||
case "$prev" in
|
||||
-n)
|
||||
COMPREPLY=($(compgen -W "1 3 5 10" -- "$cur"))
|
||||
return
|
||||
;;
|
||||
-o)
|
||||
local templates="default easy secure pin diceware"
|
||||
if [[ -f "${config_dir}/config.sh" ]]; then
|
||||
local parsed
|
||||
parsed=$(grep -E '^\s*"[a-z]+\|' "${config_dir}/config.sh" 2>/dev/null | sed 's/.*"\([a-z]*\)|.*/\1/' | tr '\n' ' ')
|
||||
[[ -n "$parsed" ]] && templates="$parsed"
|
||||
fi
|
||||
COMPREPLY=($(compgen -W "$templates" -- "$cur"))
|
||||
return
|
||||
;;
|
||||
esac
|
||||
|
||||
# First argument: commands, flags, or templates
|
||||
if [[ ${cword} -eq 1 ]]; then
|
||||
local templates="default easy secure pin diceware"
|
||||
if [[ -f "${config_dir}/config.sh" ]]; then
|
||||
local parsed
|
||||
parsed=$(grep -E '^\s*"[a-z]+\|' "${config_dir}/config.sh" 2>/dev/null | sed 's/.*"\([a-z]*\)|.*/\1/' | tr '\n' ' ')
|
||||
[[ -n "$parsed" ]] && templates="$parsed"
|
||||
fi
|
||||
|
||||
COMPREPLY=($(compgen -W "$commands $flags $templates" -- "$cur"))
|
||||
return
|
||||
fi
|
||||
}
|
||||
|
||||
complete -F _himi himi
|
||||
Reference in New Issue
Block a user