Fix zsh collapsed_pwd

This commit is contained in:
rydesun 2023-05-18 12:46:43 +08:00
parent ccbe950ba9
commit 8ec2d96e25
2 changed files with 56 additions and 44 deletions

View file

@ -3,57 +3,67 @@ Z_COLLAPSED_PWD_MAX_LENGTH=${Z_COLLAPSED_PWD_MAX_LENGTH:-32}
Z_COLLAPSED_PWD_EXPAND_LAST=${Z_COLLAPSED_PWD_EXPAND_LAST:-yes} Z_COLLAPSED_PWD_EXPAND_LAST=${Z_COLLAPSED_PWD_EXPAND_LAST:-yes}
Z_COLLAPSED_PWD_EXPAND_ELLIPSIS=${Z_COLLAPSED_PWD_EXPAND_ELLIPSIS:-⋯⋯} Z_COLLAPSED_PWD_EXPAND_ELLIPSIS=${Z_COLLAPSED_PWD_EXPAND_ELLIPSIS:-⋯⋯}
() { local color_l=$Z_PROMPT_COLLAPSED_PWD
[[ $# == 0 ]] && local pwd="$PWD" || local pwd=$1 local color_r=$Z_PROMPT_PWD_L
[[ -z "$pwd" || "$pwd" == "/" ]] && echo $pwd && return
pwd="${pwd%/}" if [[ $# == 0 ]] then
local home="${HOME%/}" local pwd="$PWD"
[[ "$pwd" == "$home" ]] && echo "~" && return else
local pwd=$1
color_l=$2
color_r=$3
fi
local offset=${#home} [[ -z "$pwd" || "$pwd" == "/" ]] && echo $pwd && return
[[ "$pwd" == "$home/"* ]] && pwd="~${pwd:$offset}"
local names=("${(s:/:)pwd}") pwd="${pwd%/}"
local component_count=${#names} local home="${HOME%/}"
(( component_count <= Z_COLLAPSED_PWD_RESERVE_COMPONENTS + 2 )) && [[ "$pwd" == "$home" ]] && echo "~" && return
echo "$pwd" && return
local total_count=${#pwd} local offset=${#home}
local begin_index=$(( Z_COLLAPSED_PWD_RESERVE_COMPONENTS + 2 )) [[ "$pwd" == "$home/"* ]] && pwd="~${pwd:$offset}"
local end_index=$(( component_count - 1 ))
for i in {$begin_index..$end_index}; do
(( total_count <= Z_COLLAPSED_PWD_MAX_LENGTH )) && break
local full_name=${names[$i]} local names=("${(s:/:)pwd}")
local first_c=${full_name:0:1} local component_count=${#names}
[[ $first_c == '.' ]] && offset=2 || offset=1 (( component_count <= Z_COLLAPSED_PWD_RESERVE_COMPONENTS + 2 )) &&
local new_name=${full_name:0:$offset} echo "$pwd" && return
(( ${#full_name} == ${#new_name} )) && continue
names[$i]=$Z_PROMPT_COLLAPSED_PWD$new_name$Z_PROMPT_PWD_L
total_count=$(( total_count - ${#full_name:$offset} )) local total_count=${#pwd}
local last_collapsed=($i $full_name $offset) local begin_index=$(( Z_COLLAPSED_PWD_RESERVE_COMPONENTS + 2 ))
done local end_index=$(( component_count - 1 ))
for i in {$begin_index..$end_index}; do
(( total_count <= Z_COLLAPSED_PWD_MAX_LENGTH )) && break
# 尽可能展开最后一个压缩的目录名 local full_name=${names[$i]}
if [[ $Z_COLLAPSED_PWD_EXPAND_LAST = yes ]] && [[ -n $last_collapsed ]]; then local first_c=${full_name:0:1}
ellipsis_count=${#Z_COLLAPSED_PWD_EXPAND_ELLIPSIS} [[ $first_c == '.' ]] && offset=2 || offset=1
local free_count=$(( Z_COLLAPSED_PWD_MAX_LENGTH - total_count - local new_name=${full_name:0:$offset}
ellipsis_count )) (( ${#full_name} == ${#new_name} )) && continue
if (( free_count > 0 )); then names[$i]=$color_l$new_name$color_r
local visible_count_half=$(( (free_count + last_collapsed[3]) / 2 ))
local full_name=$last_collapsed[2] total_count=$(( total_count - ${#full_name:$offset} ))
local new_name_left=${full_name:0:$visible_count_half} local last_collapsed=($i $full_name $offset)
local new_name_right=${full_name:$(( ${#full_name} - visible_count_half ))} done
local new_name=$new_name_left$Z_COLLAPSED_PWD_EXPAND_ELLIPSIS$new_name_right
names[$last_collapsed[1]]=$Z_PROMPT_COLLAPSED_PWD$new_name$Z_PROMPT_PWD_L # 尽可能展开最后一个压缩的目录名
fi if [[ $Z_COLLAPSED_PWD_EXPAND_LAST = yes ]] && [[ -n $last_collapsed ]]; then
ellipsis_count=${#Z_COLLAPSED_PWD_EXPAND_ELLIPSIS}
local free_count=$(( Z_COLLAPSED_PWD_MAX_LENGTH - total_count -
ellipsis_count ))
if (( free_count > 0 )); then
local visible_count=$(( free_count + last_collapsed[3] ))
local visible_count_right=$(( visible_count / 2 ))
local visible_count_left=$(( visible_count_right + (visible_count % 2) ))
local full_name=$last_collapsed[2]
local new_name_left=${full_name:0:$visible_count_left}
local new_name_right=${full_name:$(( ${#full_name} - visible_count_right ))}
local new_name=$new_name_left$Z_COLLAPSED_PWD_EXPAND_ELLIPSIS$new_name_right
names[$last_collapsed[1]]=$color_l$new_name$color_r
fi fi
fi
local IFS="/" local IFS="/"
echo "${names[*]}" echo "${names[*]}"
}
# vim:set filetype=zsh: # vim:set filetype=zsh:

6
.zshrc
View file

@ -128,8 +128,8 @@ Z_PROMPT_ROOT=%B%F{red}»%b%f
# {{{ PROMPT # {{{ PROMPT
# 加载git状态部件 # 加载git状态部件
if [[ -f $Z_SRC_GIT_PROMPT ]]; then if [[ -f "$Z_SRC_GIT_PROMPT" ]]; then
source $Z_SRC_GIT_PROMPT source "$Z_SRC_GIT_PROMPT"
GIT_PS1_SHOWCOLORHINTS=1 GIT_PS1_SHOWCOLORHINTS=1
GIT_PS1_COMPRESSSPARSESTATE=1 GIT_PS1_COMPRESSSPARSESTATE=1
GIT_PS1_SHOWCONFLICTSTATE=yes GIT_PS1_SHOWCONFLICTSTATE=yes
@ -149,6 +149,8 @@ Z_COLLAPSED_PWD_MAX_LENGTH=32
# 当空间足够时,尽可能展开最后一个压缩的目录名 # 当空间足够时,尽可能展开最后一个压缩的目录名
Z_COLLAPSED_PWD_EXPAND_LAST=yes Z_COLLAPSED_PWD_EXPAND_LAST=yes
autoload -Uz collapsed_pwd autoload -Uz collapsed_pwd
# 如果不先执行,$()替换就会每次都读取文件?
collapsed_pwd &>/dev/null
precmd() { precmd() {
local last_status=$? local last_status=$?