From a2a261ddbecbf63151b7b87441f6574022b0b5a2 Mon Sep 17 00:00:00 2001 From: tastytea Date: Thu, 21 Apr 2022 23:04:25 +0200 Subject: [PATCH] Zsh: Shrink PWD in RPROMPT dynamically --- .config/zsh/themes/prompt.zsh | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/.config/zsh/themes/prompt.zsh b/.config/zsh/themes/prompt.zsh index 8ecda82..2419cc1 100644 --- a/.config/zsh/themes/prompt.zsh +++ b/.config/zsh/themes/prompt.zsh @@ -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" }