2023-12-07 04:02:47 +03:00
|
|
|
# Bash autocomplete.
|
|
|
|
# There are also options like -o nospace. see man for more info.
|
|
|
|
# Usage: _foo() { _autocomplete "{foo,bar}" } ; complete -F _foo foo
|
2023-12-07 01:44:42 +03:00
|
|
|
function _autocomplete() {
|
2023-12-05 21:50:45 +03:00
|
|
|
local IFS=$'\n'
|
|
|
|
local commands="${*}"
|
2024-01-03 22:18:18 +03:00
|
|
|
|
2023-12-05 21:50:45 +03:00
|
|
|
COMPREPLY=()
|
2023-10-23 03:31:00 +03:00
|
|
|
|
2023-12-05 21:50:45 +03:00
|
|
|
local cur="${COMP_WORDS[COMP_CWORD]}"
|
|
|
|
local prev="${COMP_WORDS[COMP_CWORD-1]}"
|
|
|
|
local command="${COMP_WORDS[0]}"
|
2023-10-23 03:31:00 +03:00
|
|
|
|
2023-12-05 21:50:45 +03:00
|
|
|
COMPREPLY=( $(compgen -W "${commands}" -- ${cur}) )
|
|
|
|
return 0
|
2023-10-23 03:31:00 +03:00
|
|
|
}
|
|
|
|
|
2023-12-07 04:02:47 +03:00
|
|
|
# Autocomplete only first argument.
|
2023-12-07 01:44:42 +03:00
|
|
|
function _autocomplete_first() {
|
2023-12-05 21:50:45 +03:00
|
|
|
local IFS=$'\n'
|
|
|
|
local commands="${*}"
|
2023-11-27 16:28:27 +03:00
|
|
|
|
2023-12-05 21:50:45 +03:00
|
|
|
COMPREPLY=()
|
2023-10-23 03:31:00 +03:00
|
|
|
|
2023-12-05 21:50:45 +03:00
|
|
|
local cur="${COMP_WORDS[COMP_CWORD]}"
|
|
|
|
local prev="${COMP_WORDS[COMP_CWORD-1]}"
|
|
|
|
local command="${COMP_WORDS[0]}"
|
2023-10-23 03:31:00 +03:00
|
|
|
|
2023-12-05 21:50:45 +03:00
|
|
|
if [[ "${prev}" = "${command}" ]]; then
|
|
|
|
COMPREPLY=( $(compgen -W "${commands}" -- ${cur}) )
|
|
|
|
return 0
|
|
|
|
fi
|
2023-10-23 03:31:00 +03:00
|
|
|
}
|
|
|
|
|
2024-01-03 22:18:18 +03:00
|
|
|
# Autocomplete only first argument and the rest with files.
|
|
|
|
function _autocomplete_first_ls() {
|
|
|
|
local IFS=$'\n'
|
|
|
|
local commands="${*}"
|
|
|
|
|
|
|
|
COMPREPLY=()
|
|
|
|
|
|
|
|
local cur="${COMP_WORDS[COMP_CWORD]}"
|
|
|
|
local prev="${COMP_WORDS[COMP_CWORD-1]}"
|
|
|
|
local command="${COMP_WORDS[0]}"
|
|
|
|
|
|
|
|
if [[ "${prev}" = "${command}" ]]; then
|
|
|
|
COMPREPLY=( $(compgen -W "${commands}" -- ${cur}) )
|
|
|
|
return 0
|
|
|
|
else
|
|
|
|
COMPREPLY=( $(compgen -W "$(ls)" -- ${cur}) )
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2023-11-22 13:20:01 +03:00
|
|
|
# Autocomplete by grepping file names.
|
2023-12-07 01:44:42 +03:00
|
|
|
function _autocomplete_grep() {
|
2023-12-05 21:50:45 +03:00
|
|
|
local IFS=$'\n'
|
|
|
|
COMPREPLY=()
|
2023-10-30 14:22:24 +03:00
|
|
|
|
2023-12-05 21:50:45 +03:00
|
|
|
local pattern="${1}"
|
|
|
|
local cur="${COMP_WORDS[COMP_CWORD]}"
|
|
|
|
local prev="${COMP_WORDS[COMP_CWORD-1]}"
|
|
|
|
local command="${COMP_WORDS[0]}"
|
2023-10-30 14:22:24 +03:00
|
|
|
|
2023-12-05 21:50:45 +03:00
|
|
|
COMPREPLY=( $(compgen -W "$(ls | grep -E ${pattern})" -- ${cur}) )
|
|
|
|
return 0
|
2023-10-30 14:22:24 +03:00
|
|
|
}
|
|
|
|
|
2023-12-07 04:02:47 +03:00
|
|
|
# Autocomplete nested program.
|
2023-12-07 01:44:42 +03:00
|
|
|
function _autocomplete_nested() {
|
2023-12-05 21:50:45 +03:00
|
|
|
# local IFS=$'\n'
|
|
|
|
local cur prev words cword split i
|
|
|
|
_init_completion -s || return
|
2023-10-23 03:31:00 +03:00
|
|
|
|
2023-12-05 21:50:45 +03:00
|
|
|
for ((i = 1; i <= cword; i++)); do
|
|
|
|
if [[ ${words[i]} != -* ]]; then
|
|
|
|
local PATH=$PATH:/sbin:/usr/sbin:/usr/local/sbin
|
|
|
|
local root_command=${words[i]}
|
|
|
|
_command_offset ${i}
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
done
|
2023-10-23 00:24:36 +03:00
|
|
|
}
|