bash : add many autocompletes.

This commit is contained in:
Dmitry Voronin 2023-10-23 03:31:00 +03:00
parent 52c3c0b5e7
commit ee96817c77
10 changed files with 130 additions and 13 deletions

View file

@ -150,7 +150,7 @@ export -f archive archive_fast archive_check unarchive
# autocompletes.
_archive_list()
{
autocomplete "$(ls *_*.tar.* 2> /dev/null)"
_autocomplete "$(ls *_*.tar.* 2> /dev/null)"
}
complete -F _archive_list archive_check unarchive
complete -o plusdirs -F _archive_list archive_check unarchive

View file

@ -1,8 +1,72 @@
# bash autocomplete.
# usage: _foo () { autocomplete "{foo,bar}" } ; complete -F _foo foo
# usage: _foo () { _autocomplete "{foo,bar}" } ; complete -F _foo foo
# there are also options like -o nospace. see man for more info.
# from man: The shell function function is executed in the current shell environment. When the function is executed, the first argument ($1) is the name of the command whose arguments are being completed, the second argument ($2) is the word being completed, and the third argument ($3) is the word preceding the word being completed on the current command line. When it finishes, the possible completions are retrieved from the value of the COMPREPLY array variable.
autocomplete()
_autocomplete()
{
COMPREPLY=( $(compgen -W "${@}") )
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
}

View file

@ -29,7 +29,7 @@ checksum()
# autocomplete.
_checksum()
{
autocomplete "{new,check}"
_autocomplete_first "{new,check}"
}
complete -F _checksum checksum

View file

@ -13,11 +13,25 @@ alias dcu="docker compose up -d"
alias dcd="docker compose down"
alias dcp="docker compose pull"
alias dcl="docker compose logs -f"
alias dcul="docker compose up -d && docker compose logs -f"
alias dcdu="dcd; dcu"
alias dcr="docker compose restart"
alias dcs="docker compose stop"
alias dcpu="dcp; dcu"
dcdu()
{
dcd "${@}"
dcu "${@}"
}
dcpu()
{
dcp "${@}"
dcu "${@}"
}
dcul()
{
dcu "${@}" && dcl "${@}"
}
# find out container's IP address.
docker_ip()
@ -30,3 +44,17 @@ docker_update()
{
docker images --format "{{.Repository}}:{{.Tag}}" | xargs -L1 docker pull
}
# autocomplete.
_dc_services()
{
_autocomplete "$(docker compose config --services 2> /dev/null)"
}
_dc_containers()
{
_autocomplete "$(docker ps --format "\""{{.Names}}"\"")"
}
complete -F _dc_services dcu dcd dcp dcl dcul dcdu dcr dcs dcpu
complete -F _dc_containers docker_volumes docker_ip

View file

@ -9,7 +9,6 @@ alias gd="git diff"
alias gc="git commit -m"
alias gch="git checkout"
alias gb="git branch"
alias gco="git checkout"
alias gf="git fetch --all -v"
alias gt="git tag"
alias gi="git ls-files -ci --exclude-standard -z | xargs -0 git rm --cached"
@ -66,3 +65,7 @@ gu()
# some extra aliases for gu.
alias guv="gu 'Dmitry Voronin' 'account@voronind.com'"
alias guf="gu 'Dmitry Voronin' 'dd.voronin@fsight.ru'"
# autocomplete.
# _completion_loader git
# __git_complete gps _git_push

View file

@ -20,3 +20,11 @@ own()
# remove access from group and others.
chmod -077 -R "$file"
}
# autocomplete.
_own()
{
_autocomplete_first_ls "{0,1000}"
}
complete -F _own own

View file

@ -51,7 +51,7 @@ tka()
# autocompletes.
_ta()
{
autocomplete "$(tmux list-sessions | sed -e 's/:.*//')"
_autocomplete_first "$(tmux list-sessions | sed -e 's/:.*//')"
}
complete -F _ta ta tk

View file

@ -65,4 +65,12 @@ tb_rpmfusion()
}
# list all containers.
alias tbl="podman ps -a"
alias tbl="toolbox list -c"
# autocomplete.
_tb_containers()
{
_autocomplete "$(toolbox list -c | sed -e '1d' | awk '{ print $2 }')"
}
complete -F _tb_containers tb tbk tb_rpmfusion

View file

@ -11,3 +11,6 @@ try()
fi
done
}
# autocomplete. TODO: make it respect command's completion? RN it only offers programs for each arg.
complete -F _autocomplete_nested try

View file

@ -1,3 +1,6 @@
# short aliases.
alias w='watch -x bash -c'
alias ww='watch -n 0 -x bash -c'
# autocomplete.
complete -F _autocomplete_nested w ww