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
gpg-warmup() {
local t r
(( ${+commands[gpg]} )) || return 1
local r t
t=$(mktemp)
command gpg -abs "$t"
r=$?
command rm -f "$t" "$t.asc"
return "$r"
}

View File

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

View File

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

View File

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

View File

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

View File

@ -2,4 +2,4 @@
for i ( buildah ) ; do
__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
__z_comp_external $i "__z_comp__$i"
done
done ; unset i
unset -fm '__z_comp__*'

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -2,6 +2,7 @@
z-history() {
local list
zparseopts -E l=list
if [[ -n "$list" ]]; then
builtin fc "$@"
@ -11,6 +12,8 @@ z-history() {
}
z-grephist() {
local what=$1 ; shift
z-history -m "*${what}*" "$@"
local x
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
__z_pwd() {
local p=${(%):-%~}
local p pfx last
p=${(%):-%~}
[[ "$p" =~ '/.+' ]] || return
local pfx="${p:h}"
pfx="${p:h}"
pfx="${pfx%%/}"
local last="${p:t}"
last="${p:t}"
ZSHU_PS[pwd]="%F{cyan}${pfx}/%B${last}%f%b"
}
# ZSHU[pwd_hook]=''
__z_pwd_hook() {
local i
unset "ZSHU_PS[pwd]"
for i ( ${(s: :)ZSHU[pwd_hook]} __z_pwd ) ; do
"$i"

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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