Add custom completions to bash

This commit is contained in:
fanyx 2022-06-09 13:53:09 +02:00
parent 8d27594354
commit 05f42e9d6e
4 changed files with 173 additions and 0 deletions

27
.config/completions/sv Normal file
View File

@ -0,0 +1,27 @@
# bash completion for runit sv(1)
_sv()
{
local cur prev words cword commands
_init_completion || return
commands='up down status once pause cont hup alarm interrupt 1 2 term kill exit start stop restart shutdown force-stop force-reload force-restart force-shutdown'
case $prev in
-w)
return
;;
-* | sv)
COMPREPLY=( $(compgen -W "${commands}" -- ${cur}) )
return
;;
*)
COMPREPLY=( /var/service/* )
COMPREPLY+=( /home/hendrik/sv/* )
COMPREPLY=( ${COMPREPLY[@]##*/} )
COMPREPLY=( $(compgen -W '${COMPREPLY[@]}' -- ${cur}) )
return
;;
esac
}
complete -F _sv sv

133
.config/completions/xbps Normal file
View File

@ -0,0 +1,133 @@
_xbps_parse_help() {
local IFS line word
$1 --help 2>&1 | while IFS=$'\n' read -r line; do
[[ $line == *([ $'\t'])-* ]] || continue
IFS=$' \t,='
for word in $line; do
[[ $word == -* ]] || continue
printf -- '%s\n' $word
done
done | sort | uniq
}
_xbps_all_packages() {
xbps-query -Rs "$1*" | sed 's/^... \([^ ]*\)-.* .*/\1/'
}
_xbps_installed_packages() {
xbps-query -l | sed 's/^.. \([^ ]*\)-.* .*/\1/'
}
_xbps_all_reply() {
COMPREPLY=( $( compgen -W '$(_xbps_all_packages "$1")' -- "$1") )
}
_xbps_installed_reply() {
COMPREPLY=( $( compgen -W '$(_xbps_installed_packages)' -- "$1") )
}
_xbps_complete() {
local cur prev words cword
_init_completion || return
if [[ "$cur" == -* ]]; then
COMPREPLY=( $( compgen -W '$( _xbps_parse_help "$1" )' -- "$cur") )
return
fi
local common='C|-config|r|-rootdir'
local morecommon="$common|c|-cachedir"
local modes='auto manual hold unhold'
local props='architecture
archive-compression-type
automatic-install
build-options
conf_files
conflicts
filename-sha256
filename-size
homepage
install-date
install-msg
install-script
installed_size
license
maintainer
metafile-sha256
packaged-with
pkgver
preserve
provides
remove-msg
remove-script
replaces
repository
shlib-provides
shlib-requires
short_desc
source-revisions
state'
case $1 in
xbps-dgraph)
if [[ $prev != -@(c|o|r) ]]; then
_xbps_installed_reply $cur
return
fi
;;
xbps-install | xi)
if [[ $prev != -@($morecommon) ]]; then
_xbps_all_reply $cur
return
fi
;;
xbps-pkgdb)
if [[ $prev == -@(m|-mode) ]]; then
COMPREPLY=( $( compgen -W "$modes" -- "$cur") )
return
fi
if [[ $prev != -@($common) ]]; then
_xbps_installed_reply $cur
return
fi
;;
xbps-query)
if [[ $prev == -@(p|-property) ]]; then
COMPREPLY=( $( compgen -W "$props" -- "$cur") )
return
fi
if [[ $prev != -@($morecommon|o|-ownedby) ]]; then
local w
for w in "${words[@]}"; do
if [[ "$w" == -@(R|-repository) ]]; then
_xbps_all_reply $cur
return
fi
done
_xbps_installed_reply $cur
return
fi
;;
xbps-reconfigure)
if [[ $prev != -@($common) ]]; then
_xbps_installed_reply $cur
return
fi
;;
xbps-remove | xr)
if [[ $prev != -@($morecommon) ]]; then
_xbps_installed_reply $cur
return
fi
;;
esac
_filedir
}
complete -F _xbps_complete xbps-checkvers xbps-create xbps-dgraph xbps-install \
xbps-pkgdb xbps-query xbps-reconfigure xbps-remove xbps-rindex xi xr

View File

@ -3,11 +3,17 @@
# import general use aliases # import general use aliases
[[ -r ~/.alias ]] && . ~/.alias [[ -r ~/.alias ]] && . ~/.alias
# config files
alias cb="vim ~/.config/bspwm/bspwmrc"
alias cs="vim ~/.config/sxhkd/sxhkdrc"
# xbps-install # xbps-install
# use xtools instead # use xtools instead
# alias xin="doas xbps-install -S" # alias xin="doas xbps-install -S"
alias xup="xi -Su" alias xup="xi -Su"
complete -F _xbps_complete xi
# xbps-query # xbps-query
alias xqr="xbps-query" alias xqr="xbps-query"
alias xs="xqr -Rs" alias xs="xqr -Rs"

View File

@ -8,6 +8,7 @@
[[ $- != *i* ]] && return [[ $- != *i* ]] && return
PATH=~/.local/bin:$PATH PATH=~/.local/bin:$PATH
PATH=~/.luarocks/bin:$PATH
################################# #################################
# - bash_aliases # - bash_aliases
@ -16,6 +17,12 @@ PATH=~/.local/bin:$PATH
# always bash-completion first # always bash-completion first
[[ -r /usr/share/bash-completion/bash_completion ]] && . /usr/share/bash-completion/bash_completion [[ -r /usr/share/bash-completion/bash_completion ]] && . /usr/share/bash-completion/bash_completion
if [[ -d ~/.config/completions ]]; then
for i in ~/.config/completions/*; do
. $i
done
fi
# integrate git prompt functions # integrate git prompt functions
# will break PS1 if not used # will break PS1 if not used
[[ -f /usr/share/git/git-prompt.sh ]] && . /usr/share/git/git-prompt.sh [[ -f /usr/share/git/git-prompt.sh ]] && . /usr/share/git/git-prompt.sh