1
0

zsh: update

This commit is contained in:
Konstantin Demin 2024-01-16 02:55:39 +03:00
parent 19571a4046
commit 9bd1bb79ea
Signed by: krd
GPG Key ID: 1F33CB0BA4731BC6
9 changed files with 173 additions and 80 deletions

View File

@ -20,7 +20,7 @@ for i ( d_zdot d_cache d_conf d_bin d_var ) ; do
done ; unset i d done ; unset i d
## early escape ## early escape
unsetopt GLOBAL_RCS unsetopt global_rcs
## safety measure: ## safety measure:
## redirect all following activity within ZDOTDIR to cache ## redirect all following activity within ZDOTDIR to cache
@ -56,4 +56,4 @@ ZSHU[t_end]=${(%):-%D{%s.%6.}}
ZSHU[t_load]=$(( ZSHU[t_end] - ZSHU[t_begin] )) ZSHU[t_load]=$(( ZSHU[t_end] - ZSHU[t_begin] ))
ZSHU[t_load]=${ZSHU[t_load]:0:6} ZSHU[t_load]=${ZSHU[t_load]:0:6}
unset "ZSHU[t_begin]" "ZSHU[t_end]" unset 'ZSHU[t_begin]' 'ZSHU[t_end]'

View File

@ -1,54 +1,53 @@
#!/bin/zsh #!/bin/zsh
z-time() { z-time() {
local a b elapsed result local a r
a=${EPOCHREALTIME} a=${EPOCHREALTIME}
"$@" "$@" ; r=$?
result=$? a=$(( EPOCHREALTIME - a ))
b=$(( EPOCHREALTIME - a )) a=$(z-ts-to-human "$a" 6)
elapsed=$(z-ts-to-human "$b" 6) echo >&2
echo 1>&2 echo "time took: $a" >&2
echo "time took: ${elapsed}" 1>&2
return ${result} return $r
} }
if autoload -Uz add-zsh-hook ; then if autoload -Uz add-zsh-hook ; then
typeset -gA ZSHU_PS typeset -gA ZSHU_PS
ZSHU_PS[cmd_threshold]=3 ZSHU_PS[cmd_threshold]=1
__z_cmdtime_precmd() { __z_cmdtime_measure() {
local t x elapsed local t x
t=${EPOCHREALTIME} x=${EPOCHREALTIME}
# t=${(%):-%D{%s.%9.}}
ZSHU_PS[elapsed]='' unset 'ZSHU[cmd_dt]' 'ZSHU_PS[elapsed]'
(( ${+ZSHU_PS[cmd_ts]} )) || return (( ${+ZSHU[cmd_ts]} )) || return
t=$(( t - ${ZSHU_PS[cmd_ts]} )) t=$(( x - ${ZSHU[cmd_ts]} ))
unset "ZSHU_PS[cmd_ts]" ZSHU[cmd_ts]=$x
x=$(( ${ZSHU_PS[cmd_threshold]} + 0 )) x=${ZSHU_PS[cmd_threshold]}
[ "$x" = '0' ] && return x=$(( x + 0 )) || x=0
[ "$x" = 0 ] && return
x=$(( t - x )) x=$(( t - x ))
[ "${x:0:1}" = '-' ] && return [ "${x:0:1}" = '-' ] && return
elapsed=$(z-ts-to-human "$t") t=$(z-ts-to-human "$t")
ZSHU_PS[elapsed]=" %f[%B%F{yellow}+${elapsed}%b%f] " ZSHU[cmd_dt]=$t
ZSHU_PS[elapsed]=" %f[%B%F{yellow}+$t%b%f]"
} }
__z_cmdtime_preexec() { __z_cmdtime_set() {
ZSHU_PS[cmd_ts]=${EPOCHREALTIME} ZSHU[cmd_ts]=${EPOCHREALTIME}
# ZSHU_PS[cmd_ts]=${(%):-%D{%s.%9.}}
} }
add-zsh-hook precmd __z_cmdtime_precmd add-zsh-hook precmd __z_cmdtime_measure
add-zsh-hook preexec __z_cmdtime_preexec add-zsh-hook preexec __z_cmdtime_set
else else
echo "cmd time measurement is disabled due to missing hook support" 1>&2 echo "cmd time measurement is disabled due to missing hook support" >&2
fi fi

View File

@ -17,7 +17,7 @@ __z_compdump_invalidate() {
__z_compdump_verify() { __z_compdump_verify() {
local i s 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'
for i ( ${(s: :)ZSHU[compdump_meta]} ) ; do for i ( ${(s: :)ZSHU[compdump_meta]} ) ; do
s=$(__z_compdump_print "$i") s=$(__z_compdump_print "$i")
@ -37,9 +37,9 @@ __z_compdump_finalize() {
__z_compdump_print "$i" __z_compdump_print "$i"
done done
} | tee -a "${ZSHU[f_compdump]}" &>/dev/null } | tee -a "${ZSHU[f_compdump]}" &>/dev/null
unset "ZSHU[compdump_refresh]" unset 'ZSHU[compdump_refresh]'
fi fi
unset "ZSHU[compdump_meta]" unset 'ZSHU[compdump_meta]'
} }
__z_comp_bash() { __z_comp_bash() {

View File

@ -1,8 +1,12 @@
#!/bin/zsh #!/bin/zsh
## fancy and manageable PS1 for git ## fancy and manageable PS1 for git
typeset -gA ZSHU_PS typeset -gA ZSHU_GIT ZSHU_PM ZSHU_PS
ZSHU_PS[git]=0 ZSHU_PS[git]=0
ZSHU_PM[git_branch]='🞷'
ZSHU_PM[git_detach]='☈'
ZSHU_PM[git_tag]='🗹'
ZSHU_PM[git_commit]='⌽'
__z_git_avail() { (( $+commands[git] )) ; } __z_git_avail() { (( $+commands[git] )) ; }
@ -10,6 +14,8 @@ __z_git() { GIT_OPTIONAL_LOCKS=0 command git "$@"; }
__z_git_is_repo() { __z_git rev-parse --git-dir &>/dev/null ; } __z_git_is_repo() { __z_git rev-parse --git-dir &>/dev/null ; }
__z_git_desc_tag() { __z_git describe --tags "$@" ; }
z-git-test() { z-git-test() {
[ "${ZSHU_PS[git]}" = '1' ] || return 1 [ "${ZSHU_PS[git]}" = '1' ] || return 1
@ -21,24 +27,93 @@ z-git-test() {
} }
__z_git_pwd() { __z_git_pwd() {
local p s last pfx local x
unset 'ZSHU_PS[git_ref]' 'ZSHU_PS[git_tag]'
unset 'ZSHU_GIT[path_root]' 'ZSHU_GIT[path_mid]' 'ZSHU_GIT[path_last]'
unset 'ZSHU_GIT[commit]' 'ZSHU_GIT[detached]' 'ZSHU_GIT[ref]' 'ZSHU_GIT[tag]'
z-git-test || return z-git-test || return
p=${(%):-%~} x=$(__z_git rev-parse --short HEAD 2>/dev/null)
[[ "$p" =~ '/.+' ]] || return [ -n "$x" ] || return
s=$(__z_git rev-parse --show-prefix) ZSHU_GIT[commit]=$x
s="${s%%/}"
if [ -n "$s" ] ; then ## git ref
p=${p%%/$s} while : ; do
last="${s:t}" ZSHU_GIT[detached]=1
pfx="${s%${last}}" x=$(__z_git symbolic-ref --short HEAD 2>/dev/null)
if [ -n "$x" ] ; then
ZSHU_GIT[detached]=0
ZSHU_GIT[ref]=$x
ZSHU_PS[git_ref]="%F{green}%B${ZSHU_PM[git_branch]}%b ${ZSHU_GIT[ref]}%f"
break
fi
x=$(__z_git for-each-ref --format='%(refname)' --count=1 --points-at=${ZSHU_GIT[commit]} refs/heads/ refs/remotes/)
if [ -n "$x" ] ; then
ZSHU_GIT[detached]=0
ZSHU_GIT[ref]=${x#refs/*/}
ZSHU_PS[git_ref]="%F{yellow}%B${ZSHU_PM[git_branch]}%b ${ZSHU_GIT[ref]}%f"
break
fi
ZSHU_GIT[ref]=${ZSHU_GIT[commit]}
ZSHU_PS[git_ref]="%F{red}%B${ZSHU_PM[git_detach]}%b ${ZSHU_GIT[ref]}%f"
break
done
## git tag
while [ ${ZSHU_GIT[detached]} = 1 ] ; do
x=$(__z_git_desc_tag --exact-match HEAD 2>/dev/null)
if [ -n "$x" ] ; then
ZSHU_GIT[tag]=$x
ZSHU_PS[git_tag]="%F{green}%B${ZSHU_PM[git_tag]}%b ${ZSHU_GIT[tag]}%f"
break
fi
x=$(__z_git_desc_tag HEAD 2>/dev/null)
if [ -n "$x" ] ; then
ZSHU_GIT[tag]=${x%-*}
ZSHU_PS[git_tag]="%F{yellow}%B${ZSHU_PM[git_commit]}%b ${ZSHU_GIT[tag]}%f"
break
fi
break
done
## try to fancy split current path
while : ; do
x=${(%):-%~}
[[ "$x" =~ '/.+' ]] || break
local pfx last mid
pfx=$(__z_git rev-parse --show-prefix)
pfx="${pfx%/}" pfx="${pfx%/}"
pfx="/${pfx}${pfx:+/}" if [ -n "${pfx}" ] ; then
else x=${x%/${pfx}}
last="/" last="${pfx:t}"
fi mid="${pfx%${last}}"
ZSHU_PS[pwd]="%F{magenta}$p%F{cyan}${pfx}%B${last}%f%b" mid="${mid%/}"
mid="/${mid}${mid:+/}"
ZSHU_GIT[path_mid]=${mid}
ZSHU_GIT[path_last]=${last}
else
ZSHU_GIT[path_last]='/'
fi
break
done
ZSHU_GIT[path_root]=$x
x="%F{magenta}${ZSHU_GIT[path_root]:gs/%/%%}"
x="$x%F{cyan}${ZSHU_GIT[path_mid]:gs/%/%%}"
x="$x%B${ZSHU_GIT[path_last]:gs/%/%%}%f%b"
ZSHU_PS[pwd]=$x
x="${ZSHU_PS[git_tag]}"
ZSHU_PS[pwd_extra]=" ${ZSHU_PS[git_ref]}${x:+ }$x"
} }
z-git-enable() { ZSHU_PS[git]=1 ; } z-git-enable() { ZSHU_PS[git]=1 ; }

View File

@ -5,6 +5,8 @@ typeset -gA ZSHU_PM ZSHU_PS
ZSHU_PM[rst]='%b%k%u%s%f' ZSHU_PM[rst]='%b%k%u%s%f'
ZSHU_PM[crlf]=$'\n' ZSHU_PM[crlf]=$'\n'
ZSHU_PS[shlvl]='%(2L.%B%F{white}|%F{cyan}%L%b%f.)'
ZSHU_PM[status]='▪' ZSHU_PM[status]='▪'
ZSHU_PS[lastcmd]="%B%(?.%F{green}.%F{red})${ZSHU_PM[status]}%f%b" ZSHU_PS[lastcmd]="%B%(?.%F{green}.%F{red})${ZSHU_PM[status]}%f%b"
@ -34,8 +36,9 @@ __z_pwd() {
__z_pwd_hook() { __z_pwd_hook() {
local i 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
unset 'ZSHU_PS[pwd_extra]'
"$i" "$i"
(( ${+ZSHU_PS[pwd]} )) && return (( ${+ZSHU_PS[pwd]} )) && return
done done
@ -44,5 +47,5 @@ __z_pwd_hook() {
add-zsh-hook precmd __z_pwd_hook add-zsh-hook precmd __z_pwd_hook
else else
echo "shiny pwd's are disabled due to missing hook support" 1>&2 echo "shiny pwd's are disabled due to missing hook support" >&2
fi fi

View File

@ -4,10 +4,10 @@ typeset -Uga ZSHU_PARENTS_PID
typeset -ga ZSHU_PARENTS_NAME typeset -ga ZSHU_PARENTS_NAME
function { function {
local i cmd local i c
i=$$ ; while : ; do i=$$ ; while : ; do
i=$(ps -o ppid= -p $i 2>/dev/null || : ) i=$(ps -o ppid= -p $i 2>/dev/null) || :
i=${i//[^0-9]} i=${i//[^0-9]}
[[ "$i" =~ '^[1-9][0-9]*$' ]] || break [[ "$i" =~ '^[1-9][0-9]*$' ]] || break
## don't deal with PID1 ## don't deal with PID1
@ -16,8 +16,9 @@ function {
done done
for i ( ${ZSHU_PARENTS_PID} ) ; do for i ( ${ZSHU_PARENTS_PID} ) ; do
cmd=$(ps -o comm= -p $i 2>/dev/null || : ) c=$(ps -o comm= -p $i 2>/dev/null) || :
[ -n "${cmd}" ] && ZSHU_PARENTS_NAME+=( "${cmd##*/}" ) [ -n "$c" ] || continue
ZSHU_PARENTS_NAME+=( "${c##*/}" )
done done
typeset -r ZSHU_PARENTS_PID typeset -r ZSHU_PARENTS_PID
@ -27,20 +28,19 @@ function {
typeset -gA ZSHU_RUN typeset -gA ZSHU_RUN
z-run-test() { z-run-test() {
local key v i local k i
key=$1 ; shift k=$1 ; shift
v=0
for i ( ${ZSHU_PARENTS_NAME} ) ; do for i ( ${ZSHU_PARENTS_NAME} ) ; do
if (( ${+argv[(r)$i]} )) ; then (( ${+argv[(r)$i]} )) || continue
ZSHU_RUN[${key}]=1
return ZSHU_RUN[$k]=1
fi return
done done
ZSHU_RUN[${key}]=0 ZSHU_RUN[$k]=0
} }
z-run-test gui konsole xterm x-terminal-emulator z-run-test gui konsole xterm x-terminal-emulator
z-run-test nested screen tmux mc z-run-test nested screen tmux mc
z-run-test nested1 mc z-run-test nested1L mc
z-run-test elevated sudo su z-run-test elevated sudo su

View File

@ -1,11 +1,12 @@
#!/bin/zsh #!/bin/zsh
z-ts-to-human() { z-ts-to-human() {
local t s ns d h m f x local t s n d h m f x
t=$1 t=$1
t=$(( float(t) ))
s=$(( int(t) )) s=$(( int(t) ))
ns=$(( int((t - s) * (10**9)) )) n=$(( int((t - s) * (10**9)) ))
t=$s t=$s
d=0 h=0 m=0 d=0 h=0 m=0
@ -22,15 +23,13 @@ z-ts-to-human() {
t=$(( t % 60 )) t=$(( t % 60 ))
fi fi
f='%s.%6.' ## strftime does desired rounding for $n/(10**9) internally
f=$(strftime "$f" $t $ns) f=$(strftime '%s.%6.' $t $n)
x=3
## keep math in sync with format above ## keep math in sync with format above
x=3
case "$2" in case "$2" in
0) x=7 ;; 0) x=7 ;;
[1-5]) x=$(( 6 - $2 )) ;; [1-6]) x=$(( 6 - $2 )) ;;
6) x=0 ;;
esac esac
[ $x -gt 0 ] && f="${f:0:-$x}s" [ $x -gt 0 ] && f="${f:0:-$x}s"

View File

@ -1,6 +1,5 @@
#!/bin/zsh #!/bin/zsh
typeset -gA ZSHU
ZSHU[title_tab]='%15<..<%~%<<' ZSHU[title_tab]='%15<..<%~%<<'
ZSHU[title_window]='%n@%m:%~' ZSHU[title_window]='%n@%m:%~'

View File

@ -1,6 +1,6 @@
#!/bin/zsh #!/bin/zsh
# z-starship-init typeset -gA ZSHU_PS1
## three-line prompt ## three-line prompt
function { function {
@ -13,7 +13,6 @@ function {
line+='${ZSHU_PM[id]:+"%B%F{white}${ZSHU_PM[id]}${ZSHU_PM[rst]}%B%F{black}|%b%f"}' line+='${ZSHU_PM[id]:+"%B%F{white}${ZSHU_PM[id]}${ZSHU_PM[rst]}%B%F{black}|%b%f"}'
line+="${ZSHU_PM[user]}%F{white}@${ZSHU_PM[host]}" line+="${ZSHU_PM[user]}%F{white}@${ZSHU_PM[host]}"
line+='${ZSHU_PS[elapsed]}' line+='${ZSHU_PS[elapsed]}'
line+='${ZSHU_PS[status_extra]}'
line+="${ZSHU_PM[rst]}" line+="${ZSHU_PM[rst]}"
line+="${ZSHU_PM[crlf]}" line+="${ZSHU_PM[crlf]}"
@ -27,10 +26,11 @@ function {
line+="%B%F{black}└[%f%b" line+="%B%F{black}└[%f%b"
line+="${ZSHU_PS[lastcmd]}" line+="${ZSHU_PS[lastcmd]}"
line+='${ZSHU_PS[shlvl]}'
line+="%B%F{black}|%b%f" line+="%B%F{black}|%b%f"
line+="${ZSHU_PS[cmd]}" line+="${ZSHU_PS[cmd]}"
ZSHU_PS[ps1_3L]="${(j::)line}" ZSHU_PS1[3L]="${(j::)line}"
} }
## two-line prompt ## two-line prompt
@ -51,11 +51,11 @@ function {
line+="%B%F{black}└[%f%b" line+="%B%F{black}└[%f%b"
line+="${ZSHU_PS[lastcmd]}" line+="${ZSHU_PS[lastcmd]}"
line+='${ZSHU_PS[shlvl]}'
line+="%B%F{black}|%b%f" line+="%B%F{black}|%b%f"
line+='${ZSHU_PS[status_extra]}'
line+="${ZSHU_PS[cmd]}" line+="${ZSHU_PS[cmd]}"
ZSHU_PS[ps1_2L]="${(j::)line}" ZSHU_PS1[2L]="${(j::)line}"
} }
## one-line prompt ## one-line prompt
@ -64,6 +64,7 @@ function {
line+="${ZSHU_PM[rst]}" line+="${ZSHU_PM[rst]}"
line+="${ZSHU_PS[lastcmd]}" line+="${ZSHU_PS[lastcmd]}"
line+='${ZSHU_PS[shlvl]}'
line+="%B%F{black}|%b" line+="%B%F{black}|%b"
line+="${ZSHU_PM[user]}" line+="${ZSHU_PM[user]}"
line+="%B%F{black}|%b" line+="%B%F{black}|%b"
@ -74,9 +75,26 @@ function {
line+="%B%F{black}|%b" line+="%B%F{black}|%b"
line+="${ZSHU_PS[cmd]}" line+="${ZSHU_PS[cmd]}"
ZSHU_PS[ps1_1L]="${(j::)line}" ZSHU_PS1[1L]="${(j::)line}"
} }
PS1=${ZSHU_PS[ps1_3L]} z-ps() {
[ "${ZSHU_RUN[nested]}" = 1 ] && PS1=${ZSHU_PS[ps1_2L]} [ -n "$1" ] || {
[ "${ZSHU_RUN[nested1]}" = 1 ] && PS1=${ZSHU_PS[ps1_1L]} echo "${ZSHU_PS[ps1]}"
return
}
local k
for k ( "$1" "${1}L" ) ; do
(( ${+ZSHU_PS1[$k]} )) || continue
ZSHU_PS[ps1]=$k
PS1=${ZSHU_PS1[$k]}
return
done
return 1
}
z-ps 3
[ "${ZSHU_RUN[nested]}" = 1 ] && z-ps 2
[ "${ZSHU_RUN[nested1L]}" = 1 ] && z-ps 1