autocomplete : add my own funcs.
This commit is contained in:
parent
cb7d6668fb
commit
41ba3676cd
|
@ -68,3 +68,92 @@ chmod a+x admin.sh autocomplete.sh
|
||||||
Source: https://askubuntu.com/a/483149/24155
|
Source: https://askubuntu.com/a/483149/24155
|
||||||
|
|
||||||
[source](https://unix.stackexchange.com/a/291867)
|
[source](https://unix.stackexchange.com/a/291867)
|
||||||
|
|
||||||
|
# My own autocomplete functions.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# bash autocomplete.
|
||||||
|
# usage: _foo () { _autocomplete "{foo,bar}" } ; complete -F _foo foo
|
||||||
|
# there are also options like -o nospace. see man for more info.
|
||||||
|
_autocomplete()
|
||||||
|
{
|
||||||
|
local commands="$@"
|
||||||
|
local cur prev
|
||||||
|
|
||||||
|
COMPREPLY=()
|
||||||
|
|
||||||
|
cur="${COMP_WORDS[COMP_CWORD]}"
|
||||||
|
prev="${COMP_WORDS[COMP_CWORD-1]}"
|
||||||
|
command="${COMP_WORDS[0]}"
|
||||||
|
|
||||||
|
COMPREPLY=( $(compgen -W "${commands}" -- ${cur}) )
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
# autocomplete only first argument.
|
||||||
|
_autocomplete_first()
|
||||||
|
{
|
||||||
|
local commands="$@"
|
||||||
|
local cur prev
|
||||||
|
|
||||||
|
COMPREPLY=()
|
||||||
|
|
||||||
|
cur="${COMP_WORDS[COMP_CWORD]}"
|
||||||
|
prev="${COMP_WORDS[COMP_CWORD-1]}"
|
||||||
|
command="${COMP_WORDS[0]}"
|
||||||
|
|
||||||
|
if [[ "${prev}" = "${command}" ]]; then
|
||||||
|
COMPREPLY=( $(compgen -W "${commands}" -- ${cur}) )
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# autocomplete only first argument. the rest is ls output.
|
||||||
|
_autocomplete_first_ls()
|
||||||
|
{
|
||||||
|
local commands="$@"
|
||||||
|
local cur prev
|
||||||
|
|
||||||
|
COMPREPLY=()
|
||||||
|
|
||||||
|
cur="${COMP_WORDS[COMP_CWORD]}"
|
||||||
|
prev="${COMP_WORDS[COMP_CWORD-1]}"
|
||||||
|
command="${COMP_WORDS[0]}"
|
||||||
|
|
||||||
|
if [[ "${prev}" = "${command}" ]]; then
|
||||||
|
COMPREPLY=( $(compgen -W "${commands}" -- ${cur}) )
|
||||||
|
return 0
|
||||||
|
else
|
||||||
|
COMPREPLY=( $(compgen -W "$(ls -a)" -- ${cur}) )
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# autocomplete nested program.
|
||||||
|
_autocomplete_nested()
|
||||||
|
{
|
||||||
|
local cur prev words cword split i
|
||||||
|
_init_completion -s || return
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
sample usage:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# autocomplete.
|
||||||
|
_tb_containers()
|
||||||
|
{
|
||||||
|
_autocomplete "$(toolbox list -c | sed -e '1d' | awk '{ print $2 }')"
|
||||||
|
}
|
||||||
|
|
||||||
|
complete -F _tb_containers tb tbk tb_rpmfusion
|
||||||
|
```
|
||||||
|
|
Loading…
Reference in a new issue