From 724df8e2d842e0d45274660977c41b49273e2700 Mon Sep 17 00:00:00 2001 From: Konstantin Demin Date: Tue, 19 Jul 2022 02:41:45 +0300 Subject: [PATCH] zsh: treewide: minor fixes/style --- .config/zsh/alias/gpg.zsh | 5 ++++- .config/zsh/alias/grep.zsh | 4 +++- .config/zsh/alias/idle.zsh | 2 ++ .config/zsh/alias/kconfig.zsh | 8 ++++++-- .config/zsh/alias/ls.zsh | 4 +++- .config/zsh/completion/bash.zsh | 2 +- .config/zsh/completion/external.zsh | 2 +- .config/zsh/completion/system.zsh | 2 +- .config/zsh/env.zsh | 4 +++- .config/zsh/lib/alternatives.zsh | 9 ++++++--- .config/zsh/lib/cmdtime.zsh | 14 +++++++++----- .config/zsh/lib/completion.zsh | 26 +++++++++++++++++++++----- .config/zsh/lib/git.zsh | 6 ++++-- .config/zsh/lib/history.zsh | 7 +++++-- .config/zsh/lib/prompt.zsh | 10 +++++++--- .config/zsh/lib/pswalk.zsh | 3 ++- .config/zsh/lib/term.zsh | 2 +- .config/zsh/lib/time.zsh | 15 +++++++++------ .config/zsh/rc/completion.zsh | 15 ++++++--------- .config/zsh/rc/keyboard.zsh | 6 ++++-- .config/zsh/rc/terminal.zsh | 7 ++++--- 21 files changed, 102 insertions(+), 51 deletions(-) diff --git a/.config/zsh/alias/gpg.zsh b/.config/zsh/alias/gpg.zsh index 6096616..a9410cb 100644 --- a/.config/zsh/alias/gpg.zsh +++ b/.config/zsh/alias/gpg.zsh @@ -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" } diff --git a/.config/zsh/alias/grep.zsh b/.config/zsh/alias/grep.zsh index e975d75..c194910 100644 --- a/.config/zsh/alias/grep.zsh +++ b/.config/zsh/alias/grep.zsh @@ -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) ;; diff --git a/.config/zsh/alias/idle.zsh b/.config/zsh/alias/idle.zsh index 2edc848..6456eaa 100644 --- a/.config/zsh/alias/idle.zsh +++ b/.config/zsh/alias/idle.zsh @@ -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') ) diff --git a/.config/zsh/alias/kconfig.zsh b/.config/zsh/alias/kconfig.zsh index 8251344..30af63d 100644 --- a/.config/zsh/alias/kconfig.zsh +++ b/.config/zsh/alias/kconfig.zsh @@ -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/" } diff --git a/.config/zsh/alias/ls.zsh b/.config/zsh/alias/ls.zsh index f946f8c..7da20b5 100644 --- a/.config/zsh/alias/ls.zsh +++ b/.config/zsh/alias/ls.zsh @@ -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) ;; diff --git a/.config/zsh/completion/bash.zsh b/.config/zsh/completion/bash.zsh index 1741e8e..bae692a 100644 --- a/.config/zsh/completion/bash.zsh +++ b/.config/zsh/completion/bash.zsh @@ -2,4 +2,4 @@ for i ( buildah ) ; do __z_comp_bash $i -done +done ; unset i diff --git a/.config/zsh/completion/external.zsh b/.config/zsh/completion/external.zsh index 026ea93..8a39774 100644 --- a/.config/zsh/completion/external.zsh +++ b/.config/zsh/completion/external.zsh @@ -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__*' diff --git a/.config/zsh/completion/system.zsh b/.config/zsh/completion/system.zsh index de680d2..3c8778d 100644 --- a/.config/zsh/completion/system.zsh +++ b/.config/zsh/completion/system.zsh @@ -2,4 +2,4 @@ for i ( fd fdfind hyperfine ) ; do __z_comp_system $i -done +done ; unset i diff --git a/.config/zsh/env.zsh b/.config/zsh/env.zsh index eeb1cb0..eb0f21e 100644 --- a/.config/zsh/env.zsh +++ b/.config/zsh/env.zsh @@ -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} ) diff --git a/.config/zsh/lib/alternatives.zsh b/.config/zsh/lib/alternatives.zsh index b8ccc50..243e77d 100644 --- a/.config/zsh/lib/alternatives.zsh +++ b/.config/zsh/lib/alternatives.zsh @@ -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} diff --git a/.config/zsh/lib/cmdtime.zsh b/.config/zsh/lib/cmdtime.zsh index 04a0c9b..0b9f335 100644 --- a/.config/zsh/lib/cmdtime.zsh +++ b/.config/zsh/lib/cmdtime.zsh @@ -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] " } diff --git a/.config/zsh/lib/completion.zsh b/.config/zsh/lib/completion.zsh index 8a3425a..8e69275 100644 --- a/.config/zsh/lib/completion.zsh +++ b/.config/zsh/lib/completion.zsh @@ -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 } diff --git a/.config/zsh/lib/git.zsh b/.config/zsh/lib/git.zsh index 1021fdf..eed6c81 100644 --- a/.config/zsh/lib/git.zsh +++ b/.config/zsh/lib/git.zsh @@ -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 diff --git a/.config/zsh/lib/history.zsh b/.config/zsh/lib/history.zsh index 5436917..fcfb437 100644 --- a/.config/zsh/lib/history.zsh +++ b/.config/zsh/lib/history.zsh @@ -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}*" "$@" } diff --git a/.config/zsh/lib/prompt.zsh b/.config/zsh/lib/prompt.zsh index 210152e..6273230 100644 --- a/.config/zsh/lib/prompt.zsh +++ b/.config/zsh/lib/prompt.zsh @@ -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" diff --git a/.config/zsh/lib/pswalk.zsh b/.config/zsh/lib/pswalk.zsh index a9c66a9..65a3774 100644 --- a/.config/zsh/lib/pswalk.zsh +++ b/.config/zsh/lib/pswalk.zsh @@ -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 diff --git a/.config/zsh/lib/term.zsh b/.config/zsh/lib/term.zsh index 781215e..047d2fb 100644 --- a/.config/zsh/lib/term.zsh +++ b/.config/zsh/lib/term.zsh @@ -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 diff --git a/.config/zsh/lib/time.zsh b/.config/zsh/lib/time.zsh index 5de4885..b0636cb 100644 --- a/.config/zsh/lib/time.zsh +++ b/.config/zsh/lib/time.zsh @@ -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" } diff --git a/.config/zsh/rc/completion.zsh b/.config/zsh/rc/completion.zsh index fe8fd58..89fe299 100644 --- a/.config/zsh/rc/completion.zsh +++ b/.config/zsh/rc/completion.zsh @@ -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 diff --git a/.config/zsh/rc/keyboard.zsh b/.config/zsh/rc/keyboard.zsh index f9b0ecf..4e6d1ce 100644 --- a/.config/zsh/rc/keyboard.zsh +++ b/.config/zsh/rc/keyboard.zsh @@ -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]} ) diff --git a/.config/zsh/rc/terminal.zsh b/.config/zsh/rc/terminal.zsh index acb5529..0f5bc1d 100644 --- a/.config/zsh/rc/terminal.zsh +++ b/.config/zsh/rc/terminal.zsh @@ -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