1
0
Fork 0

Zsh: Shrink PWD in RPROMPT dynamically

This commit is contained in:
tastytea 2022-04-21 23:04:25 +02:00
parent 025c3ae0ff
commit a2a261ddbe
Signed by: tastytea
SSH Key Fingerprint: SHA256:FBkvrOlhq5use1XEttyUGT4bUTDVA1ar9SgIc9P03cM
1 changed files with 20 additions and 1 deletions

View File

@ -89,9 +89,28 @@ else
add-zsh-hook precmd vcs_info
fi
# Reduce directory elements to first letter until directory path is reasonably
# short. Never shrink first element.
function _shrunkpwd()
{
local dir=${(D)PWD}
local -i maxwidth=$(( ${COLUMNS} / 3 ))
local -a shortdir=(${(s:/:)dir})
for index in {2..${#shortdir}}; do
if [[ ${#${(j:/:)shortdir}} -gt ${maxwidth} ]]; then
shortdir[index]=${shortdir[index][1]}
fi
done
local out=${(j:/:)shortdir}
[[ ${out[1]} != '~' ]] && out[1]="/${out[1]}"
print -n ${out}
}
setopt PROMPT_SUBST
PROMPT="%B%F{${zsh_theme_colours[prompt]}}${showuser}%b"'${vcs_info_msg_0_}'"%B%F{${zsh_theme_colours[prompt]}}%#%f%b "
RPROMPT="%F{${zsh_theme_colours[minor]}}%(?..[%B%F{${zsh_theme_colours[highlight]}}%?%b%F{${zsh_theme_colours[minor]}}] )%4~%f"
RPROMPT="%F{${zsh_theme_colours[minor]}}%(?..[%B%F{${zsh_theme_colours[highlight]}}%?%b%F{${zsh_theme_colours[minor]}}] )"'$(_shrunkpwd)'"%f"
}