Update zsh collapsed_pwd

This commit is contained in:
rydesun 2023-05-17 20:42:55 +08:00
parent 72daa373ca
commit c3eb33fff7
2 changed files with 20 additions and 5 deletions

View file

@ -1,3 +1,6 @@
Z_COLLAPSED_PWD_RESERVE_COMPONENTS=${Z_COLLAPSED_PWD_RESERVE_COMPONENTS:-1}
Z_COLLAPSED_PWD_MAX_LENGTH=${Z_COLLAPSED_PWD_MAX_LENGTH:-32}
() {
[[ $# == 0 ]] && local pwd="$PWD" || local pwd=$1
[[ -z "$pwd" || "$pwd" == "/" ]] && echo $pwd && return
@ -10,16 +13,23 @@
[[ "$pwd" == "$home/"* ]] && pwd="~${pwd:$offset}"
local names=("${(s:/:)pwd}")
local sum=${#names}
((sum <= 3)) && echo "$pwd" && return
local component_count=${#names}
(( component_count <= Z_COLLAPSED_PWD_RESERVE_COMPONENTS + 2 )) &&
echo "$pwd" && return
local chars_length=${#pwd}
local begin_index=$(( Z_COLLAPSED_PWD_RESERVE_COMPONENTS + 2 ))
local end_index=$(( component_count - 1 ))
for i in {$begin_index..$end_index}; do
(( chars_length <= Z_COLLAPSED_PWD_MAX_LENGTH )) && break
for i in {3..$((sum-1))}; do
local name=${names[$i]}
[[ ${#name} == 1 ]] && continue
local first_c=${name:0:1}
[[ $first_c == '.' ]] && name=${name:0:2} || name=${name:0:1}
[[ $first_c == '.' ]] && offset=2 || offset=1
chars_length=$(( chars_length - ${#name:$offset} ))
name=${name:0:$offset}
names[$i]=$Z_PROMPT_COLLAPSED_PWD$name$Z_PROMPT_PWD_L
done
@ -27,4 +37,5 @@
echo "${names[*]}"
}
# vim:set filetype=zsh:

6
.zshrc
View file

@ -114,7 +114,7 @@ Z_PROMPT_ERR=%F{red}%K{black}▌%f%k
Z_PROMPT_OK=%F{blue}%K{black}▌%f%k
Z_PROMPT_PWD_L=%F{blue}%K{black}
Z_PROMPT_PWD_R=" %f%k"
Z_PROMPT_COLLAPSED_PWD=%F{cyan}%K{black}
Z_PROMPT_COLLAPSED_PWD=%F{red}%K{black}
if ((Z_ENV_DESKTOP)); then
Z_PROMPT_SSH=%F{blue}%f
Z_PROMPT_NVIM=%F{blue}%f
@ -142,6 +142,10 @@ if [[ -f $Z_SRC_GIT_PROMPT ]]; then
fi
# 加载折叠路径部件
# 开头保持不压缩的目录名的个数
Z_COLLAPSED_PWD_RESERVE_COMPONENTS=1
# 最大路径长度(非严格)
Z_COLLAPSED_PWD_MAX_LENGTH=32
autoload -Uz collapsed_pwd
precmd() {