1
0

zsh: treewide: minor fixes/style

This commit is contained in:
Konstantin Demin 2022-07-19 02:41:45 +03:00
parent 0ec196bef2
commit 724df8e2d8
Signed by: krd
GPG Key ID: 1F33CB0BA4731BC6
21 changed files with 102 additions and 51 deletions

View File

@ -1,11 +1,14 @@
#!/bin/zsh #!/bin/zsh
gpg-warmup() { gpg-warmup() {
local t r
(( ${+commands[gpg]} )) || return 1 (( ${+commands[gpg]} )) || return 1
local r t
t=$(mktemp) t=$(mktemp)
command gpg -abs "$t" command gpg -abs "$t"
r=$? r=$?
command rm -f "$t" "$t.asc" command rm -f "$t" "$t.asc"
return "$r" return "$r"
} }

View File

@ -2,9 +2,11 @@
z-alt-grep() { z-alt-grep() {
local -a a local -a a
local n
a=( ${(@s:|:)1} ) a=( ${(@s:|:)1} )
[ ${#a} = 0 ] && a=( "$1" ) [ ${#a} = 0 ] && a=( "$1" )
local n=${#a} n=${#a}
[ -z "$1" ] && n=0 [ -z "$1" ] && n=0
case "$n" in case "$n" in
0) ;; 0) ;;

View File

@ -2,6 +2,7 @@
idle() { idle() {
local f local f
f=$(type "$1") f=$(type "$1")
case "$f" in case "$f" in
"$1 is /"*) z-idle-ext "$@" ;; "$1 is /"*) z-idle-ext "$@" ;;
@ -11,6 +12,7 @@ idle() {
z-idle-ext() { z-idle-ext() {
local -a s local -a s
s+=( $(z-alt-find 'nice -n +40') ) s+=( $(z-alt-find 'nice -n +40') )
s+=( $(z-alt-find 'chrt -i 0' ) ) s+=( $(z-alt-find 'chrt -i 0' ) )
s+=( $(z-alt-find 'ionice -c 3') ) s+=( $(z-alt-find 'ionice -c 3') )

View File

@ -1,13 +1,17 @@
#!/bin/zsh #!/bin/zsh
kconf-set() { kconf-set() {
local n=$1 v=$2 ; shift 2 local n v
n=$1 v=$2 ; shift 2
grep -ElZ "^((CONFIG_)?$n=|# (CONFIG_)?$n is not set)" "$@" \ grep -ElZ "^((CONFIG_)?$n=|# (CONFIG_)?$n is not set)" "$@" \
| xargs -0 -r sed -i -E -e "s/^(((CONFIG_)?$n)=.+|# ((CONFIG_)?$n) is not set)\$/\\2\\4=$v/" | xargs -0 -r sed -i -E -e "s/^(((CONFIG_)?$n)=.+|# ((CONFIG_)?$n) is not set)\$/\\2\\4=$v/"
} }
kconf-unset() { kconf-unset() {
local n=$1 ; shift local n
n=$1 ; shift
grep -ElZ "^(CONFIG_)?$n=" "$@" \ grep -ElZ "^(CONFIG_)?$n=" "$@" \
| xargs -0 -r sed -i -E -e "s/^((CONFIG_)?$n)=.+\$/# \\1 is not set/" | xargs -0 -r sed -i -E -e "s/^((CONFIG_)?$n)=.+\$/# \\1 is not set/"
} }

View File

@ -10,9 +10,11 @@ esac
z-alt-ls() { z-alt-ls() {
local -a a local -a a
local n
a=( ${(@s:|:)1} ) a=( ${(@s:|:)1} )
[ ${#a} = 0 ] && a=( "$1" ) [ ${#a} = 0 ] && a=( "$1" )
local n=${#a} n=${#a}
[ -z "$1" ] && n=0 [ -z "$1" ] && n=0
case "$n" in case "$n" in
0) ;; 0) ;;

View File

@ -2,4 +2,4 @@
for i ( buildah ) ; do for i ( buildah ) ; do
__z_comp_bash $i __z_comp_bash $i
done done ; unset i

View File

@ -5,5 +5,5 @@ __z_comp__podman() { command podman completion zsh ; }
for i ( kubectl podman ) ; do for i ( kubectl podman ) ; do
__z_comp_external $i "__z_comp__$i" __z_comp_external $i "__z_comp__$i"
done done ; unset i
unset -fm '__z_comp__*' unset -fm '__z_comp__*'

View File

@ -2,4 +2,4 @@
for i ( fd fdfind hyperfine ) ; do for i ( fd fdfind hyperfine ) ; do
__z_comp_system $i __z_comp_system $i
done done ; unset i

View File

@ -2,9 +2,11 @@
## sort-n-fill PATH ## sort-n-fill PATH
function { function {
local -a p=( $path ) local -a p
local -aU t npath games local -aU t npath games
p=( $path )
## strip "games" first :) ## strip "games" first :)
t=( ${(@)p:#*games*} ) t=( ${(@)p:#*games*} )
games+=( ${(@)p:|t} ) games+=( ${(@)p:|t} )

View File

@ -6,8 +6,8 @@
## $1 - alternatives list ## $1 - alternatives list
## $2 - arguments to test command (USE WITH CAUTION!) ## $2 - arguments to test command (USE WITH CAUTION!)
z-alt-find() { z-alt-find() {
local i c r t
local -a v a local -a v a
local i c r t
v=( ${(@s:|:)1} ) v=( ${(@s:|:)1} )
[ ${#v} = 0 ] && v=( "$1" ) [ ${#v} = 0 ] && v=( "$1" )
@ -41,8 +41,9 @@ z-alt-find() {
## $4 - function prologue ## $4 - function prologue
## $5 - function epilogue ## $5 - function epilogue
z-alt-set-static() { z-alt-set-static() {
local n t a r
local -a s local -a s
local n t a r
n="$1" ; t='' n="$1" ; t=''
if [[ "$n" =~ '\|' ]] ; then if [[ "$n" =~ '\|' ]] ; then
t=${n:${MBEGIN}} ; n=${n:0:${MBEGIN}-1} t=${n:${MBEGIN}} ; n=${n:0:${MBEGIN}-1}
@ -59,6 +60,7 @@ z-alt-set-static() {
s+=( 'return 127' ) s+=( 'return 127' )
fi fi
eval "$n () { ${s[@]} ; } ; typeset -g $n" eval "$n () { ${s[@]} ; } ; typeset -g $n"
return $r return $r
} }
@ -69,8 +71,9 @@ z-alt-set-static() {
## $4 - function prologue ## $4 - function prologue
## $5 - function epilogue ## $5 - function epilogue
z-alt-set-dynamic() { z-alt-set-dynamic() {
local n t
local -a s local -a s
local n t
n="$1" ; t='' n="$1" ; t=''
if [[ "$n" =~ '\|' ]] ; then if [[ "$n" =~ '\|' ]] ; then
t=${n:${MBEGIN}} ; n=${n:0:${MBEGIN}-1} t=${n:${MBEGIN}} ; n=${n:0:${MBEGIN}-1}

View File

@ -2,13 +2,15 @@
z-time() { z-time() {
local a b elapsed result local a b elapsed result
a=${EPOCHREALTIME} a=${EPOCHREALTIME}
"$@" "$@"
result=$? result=$?
b=$(( ${EPOCHREALTIME} - a )) b=$(( EPOCHREALTIME - a ))
elapsed=$(z-ts-to-human "$b" 6) elapsed=$(z-ts-to-human "$b" 6)
echo 1>&2 echo 1>&2
echo "time took: ${elapsed}" 1>&2 echo "time took: ${elapsed}" 1>&2
return ${result} return ${result}
} }
@ -18,8 +20,10 @@ typeset -gA ZSHU_PS
ZSHU_PS[cmd_threshold]=3 ZSHU_PS[cmd_threshold]=3
__z_cmdtime_precmd() { __z_cmdtime_precmd() {
local t=${EPOCHREALTIME} local t x elapsed
# local t=${(%):-%D{%s.%9.}}
t=${EPOCHREALTIME}
# t=${(%):-%D{%s.%9.}}
ZSHU_PS[elapsed]='' ZSHU_PS[elapsed]=''
(( ${+ZSHU_PS[cmd_ts]} )) || return (( ${+ZSHU_PS[cmd_ts]} )) || return
@ -27,13 +31,13 @@ __z_cmdtime_precmd() {
t=$(( t - ${ZSHU_PS[cmd_ts]} )) t=$(( t - ${ZSHU_PS[cmd_ts]} ))
unset "ZSHU_PS[cmd_ts]" unset "ZSHU_PS[cmd_ts]"
local x=$(( ${ZSHU_PS[cmd_threshold]} + 0 )) x=$(( ${ZSHU_PS[cmd_threshold]} + 0 ))
[ "$x" = '0' ] && return [ "$x" = '0' ] && return
x=$(( t - x )) x=$(( t - x ))
[ "${x:0:1}" = '-' ] && return [ "${x:0:1}" = '-' ] && return
local elapsed=$(z-ts-to-human "$t") elapsed=$(z-ts-to-human "$t")
ZSHU_PS[elapsed]=" %f[%B%F{yellow}+${elapsed}%b%f] " ZSHU_PS[elapsed]=" %f[%B%F{yellow}+${elapsed}%b%f] "
} }

View File

@ -15,9 +15,10 @@ __z_compdump_invalidate() {
} }
__z_compdump_verify() { __z_compdump_verify() {
local i s
unset "ZSHU[compdump_refresh]" unset "ZSHU[compdump_refresh]"
ZSHU[compdump_meta]='ZSH_VERSION ZSH_PATCHLEVEL FPATH PATH' ZSHU[compdump_meta]='ZSH_VERSION ZSH_PATCHLEVEL FPATH PATH'
local i s
for i ( ${(s: :)ZSHU[compdump_meta]} ) ; do for i ( ${(s: :)ZSHU[compdump_meta]} ) ; do
s=$(__z_compdump_print "$i") s=$(__z_compdump_print "$i")
command grep -Fx -e "$s" "${ZSHU[f_compdump]}" &>/dev/null && continue command grep -Fx -e "$s" "${ZSHU[f_compdump]}" &>/dev/null && continue
@ -28,6 +29,7 @@ __z_compdump_verify() {
__z_compdump_finalize() { __z_compdump_finalize() {
local i local i
if (( ${+ZSHU[compdump_refresh]} )) ; then if (( ${+ZSHU[compdump_refresh]} )) ; then
{ {
echo echo
@ -41,11 +43,13 @@ __z_compdump_finalize() {
} }
__z_comp_bash() { __z_comp_bash() {
local f p x
(( ${+commands[$1]} )) || return 1 (( ${+commands[$1]} )) || return 1
(( ${+_comps[$1]} )) && return 2 (( ${+_comps[$1]} )) && return 2
(( ${+ZSHU[compdump_bash]} )) || return 3 (( ${+ZSHU[compdump_bash]} )) || return 3
(( ${+2} )) && return 0 (( ${+2} )) && return 0
local f p x
f=0 f=0
for p ( /usr/share/bash-completion/completions ) ; do for p ( /usr/share/bash-completion/completions ) ; do
x="_$1" ; [ -s "$p/$x" ] && f=1 && break x="_$1" ; [ -s "$p/$x" ] && f=1 && break
@ -53,35 +57,47 @@ __z_comp_bash() {
done done
[ "$f" = 0 ] && return 4 [ "$f" = 0 ] && return 4
complete -C "$x" "$1" complete -C "$x" "$1"
return 0 return 0
} }
__z_comp_external() { __z_comp_external() {
local f
(( ${+commands[$1]} )) || return 1 (( ${+commands[$1]} )) || return 1
(( ${+_comps[$1]} )) && return 2 (( ${+_comps[$1]} )) && return 2
local f="${ZSHU[d_cache]}/completion/_$1"
f="${ZSHU[d_cache]}/completion/_$1"
if ! [ -s "$f" ] ; then if ! [ -s "$f" ] ; then
"$2" > "$f" || return 3 "$2" > "$f" || return 3
fi fi
autoload -Uz "_$1" autoload -Uz "_$1"
return 0 return 0
} }
__z_comp_system() { __z_comp_system() {
local d
(( ${+commands[$1]} )) || return 1 (( ${+commands[$1]} )) || return 1
(( ${+_comps[$1]} )) && return 2 (( ${+_comps[$1]} )) && return 2
for d ( $fpath ) ; do for d ( $fpath ) ; do
[ -s "$d/_$1" ] || continue [ -s "$d/_$1" ] || continue
autoload -Uz "_$1" autoload -Uz "_$1"
return 0 return 0
done ; unset d done
return 3 return 3
} }
## reload or new session are required to regenerate compcache ## reload or new session are required to regenerate compcache
z-comp-invalidate() { z-comp-invalidate() {
local f
[ -n "$1" ] || return 1 [ -n "$1" ] || return 1
local f="${ZSHU[d_cache]}/completion/_$1"
f="${ZSHU[d_cache]}/completion/_$1"
[ -f "$f" ] || return 2 [ -f "$f" ] || return 2
command rm -f "$f" || return 3 command rm -f "$f" || return 3
} }

View File

@ -21,10 +21,12 @@ z-git-test() {
} }
__z_git_pwd() { __z_git_pwd() {
local p s last pfx
z-git-test || return z-git-test || return
local p=${(%):-%~}
p=${(%):-%~}
[[ "$p" =~ '/.+' ]] || return [[ "$p" =~ '/.+' ]] || return
local s pfx last
s=$(__z_git rev-parse --show-prefix) s=$(__z_git rev-parse --show-prefix)
s="${s%%/}" s="${s%%/}"
if [ -n "$s" ] ; then if [ -n "$s" ] ; then

View File

@ -2,6 +2,7 @@
z-history() { z-history() {
local list local list
zparseopts -E l=list zparseopts -E l=list
if [[ -n "$list" ]]; then if [[ -n "$list" ]]; then
builtin fc "$@" builtin fc "$@"
@ -11,6 +12,8 @@ z-history() {
} }
z-grephist() { z-grephist() {
local what=$1 ; shift local x
z-history -m "*${what}*" "$@"
x=$1 ; shift
z-history -m "*${x}*" "$@"
} }

View File

@ -20,16 +20,20 @@ ZSHU_PM[host]="%B%(!.%F{red}.%F{blue})${ZSHU[host]}%f%b"
if autoload -Uz add-zsh-hook ; then if autoload -Uz add-zsh-hook ; then
__z_pwd() { __z_pwd() {
local p=${(%):-%~} local p pfx last
p=${(%):-%~}
[[ "$p" =~ '/.+' ]] || return [[ "$p" =~ '/.+' ]] || return
local pfx="${p:h}" pfx="${p:h}"
pfx="${pfx%%/}" pfx="${pfx%%/}"
local last="${p:t}" last="${p:t}"
ZSHU_PS[pwd]="%F{cyan}${pfx}/%B${last}%f%b" ZSHU_PS[pwd]="%F{cyan}${pfx}/%B${last}%f%b"
} }
# ZSHU[pwd_hook]='' # ZSHU[pwd_hook]=''
__z_pwd_hook() { __z_pwd_hook() {
local i
unset "ZSHU_PS[pwd]" unset "ZSHU_PS[pwd]"
for i ( ${(s: :)ZSHU[pwd_hook]} __z_pwd ) ; do for i ( ${(s: :)ZSHU[pwd_hook]} __z_pwd ) ; do
"$i" "$i"

View File

@ -27,7 +27,8 @@ function {
typeset -gA ZSHU_RUN typeset -gA ZSHU_RUN
z-run-test() { z-run-test() {
local key i local key v i
key=$1 ; shift key=$1 ; shift
v=0 v=0
for i ( ${ZSHU_PARENTS_NAME} ) ; do for i ( ${ZSHU_PARENTS_NAME} ) ; do

View File

@ -4,8 +4,8 @@ typeset -Uga ZSHU_TERM_MISSING
z-ti-test() { z-ti-test() {
local r i local r i
r=0
r=0
for i ; do for i ; do
[ -z "$i" ] && continue [ -z "$i" ] && continue
if ! (( ${+terminfo[$i]} )) ; then if ! (( ${+terminfo[$i]} )) ; then

View File

@ -1,12 +1,14 @@
#!/bin/zsh #!/bin/zsh
z-ts-to-human() { z-ts-to-human() {
local t=$1 local t s ns d h m f x
local s=$(( int(t) ))
local ns=$(( int((t - s) * (10**9)) )) t=$1
s=$(( int(t) ))
ns=$(( int((t - s) * (10**9)) ))
t=$s t=$s
local d=0 h=0 m=0 d=0 h=0 m=0
if [ $t -ge 86400 ] ; then if [ $t -ge 86400 ] ; then
d=$(( t / 86400 )) d=$(( t / 86400 ))
t=$(( t % 86400 )) t=$(( t % 86400 ))
@ -20,10 +22,10 @@ z-ts-to-human() {
t=$(( t % 60 )) t=$(( t % 60 ))
fi fi
local f='%s.%6.' f='%s.%6.'
f=$(strftime "$f" $t $ns) f=$(strftime "$f" $t $ns)
local x=3 x=3
## keep math in sync with format above ## keep math in sync with format above
case "$2" in case "$2" in
0) x=7 ;; 0) x=7 ;;
@ -35,5 +37,6 @@ z-ts-to-human() {
[ $s -ge 60 ] && f="${m}m:$f" [ $s -ge 60 ] && f="${m}m:$f"
[ $s -ge 3600 ] && f="${h}h:$f" [ $s -ge 3600 ] && f="${h}h:$f"
[ $s -ge 86400 ] && f="${d}d:$f" [ $s -ge 86400 ] && f="${d}d:$f"
echo "$f" echo "$f"
} }

View File

@ -30,14 +30,11 @@ fi
autoload -Uz +X compinit && \ autoload -Uz +X compinit && \
compinit -i -C -d "${ZSHU[f_compdump]}" compinit -i -C -d "${ZSHU[f_compdump]}"
function { for i ( ${ZSHU[d_conf]}/completion/*.zsh(N.r) ) ; do
local i
for i ( ${ZSHU[d_conf]}/completion/*.zsh(N.r) ) ; do
source "$i" source "$i"
done done ; unset i
for i ( ${ZSHU[d_conf]}/local/completion/*.zsh(N.r) ) ; do for i ( ${ZSHU[d_conf]}/local/completion/*.zsh(N.r) ) ; do
source "$i" source "$i"
done done ; unset i
}
__z_compdump_finalize __z_compdump_finalize

View File

@ -16,6 +16,7 @@ fi
z-kseq-ti() { z-kseq-ti() {
[ -z "$1" ] && return [ -z "$1" ] && return
[ -z "$2" ] && return [ -z "$2" ] && return
z-ti-test "$2" && ZSHU_TI_KEYS[${terminfo[$2]}]=$1 z-ti-test "$2" && ZSHU_TI_KEYS[${terminfo[$2]}]=$1
} }
@ -23,6 +24,7 @@ z-kseq-ti() {
z-kseq-fb() { z-kseq-fb() {
[ -z "$1" ] && return [ -z "$1" ] && return
[ -z "$2" ] && return [ -z "$2" ] && return
ZSHU_FB_KEYS[$2]=$1 ZSHU_FB_KEYS[$2]=$1
} }
@ -65,8 +67,9 @@ z-kseq-fb Ctrl-LeftArrow '^[[1;5D'
z-kseq-fb Esc-w '\ew' z-kseq-fb Esc-w '\ew'
z-bind () { z-bind () {
local i sequence widget
local -a maps local -a maps
local -Ua keys
local widget i k
while [ "$1" != "--" ] ; do while [ "$1" != "--" ] ; do
maps+=( "$1" ) maps+=( "$1" )
@ -76,7 +79,6 @@ z-bind () {
widget="$2" widget="$2"
local -Ua keys
keys+=( ${(k)ZSHU_TI_KEYS[(r)$1]} ) keys+=( ${(k)ZSHU_TI_KEYS[(r)$1]} )
keys+=( ${(k)ZSHU_FB_KEYS[(r)$1]} ) keys+=( ${(k)ZSHU_FB_KEYS[(r)$1]} )

View File

@ -2,10 +2,11 @@
function { function {
local -a a local -a a
local x local i x
for i ( ${ZSHU_PARENTS_PID} ) ; do for i ( ${ZSHU_PARENTS_PID} ) ; do
[ -r /proc/$i/environ ] || continue [ -r "/proc/$i/environ" ] || continue
x=$(tr '\0' '\n' < /proc/$i/environ | sed -En '/^TERM=(.+)$/{s//\1/;p;}') x=$(sed -zEn '/^TERM=(.+)$/{s//\1/;p;}' "/proc/$i/environ")
[ -n "$x" ] || continue [ -n "$x" ] || continue
a+=( "$x" ) a+=( "$x" )
done done