diff --git a/.config/zsh/functions/collapsed_pwd b/.config/zsh/functions/collapsed_pwd index c47a6e8..a04d3c9 100644 --- a/.config/zsh/functions/collapsed_pwd +++ b/.config/zsh/functions/collapsed_pwd @@ -1,6 +1,6 @@ Z_COLLAPSED_PWD_RESERVE_COMPONENTS=${Z_COLLAPSED_PWD_RESERVE_COMPONENTS:-1} Z_COLLAPSED_PWD_MAX_LENGTH=${Z_COLLAPSED_PWD_MAX_LENGTH:-32} -Z_COLLAPSED_PWD_EXPAND_LAST=${Z_COLLAPSED_PWD_EXPAND_LAST:-1} +Z_COLLAPSED_PWD_EXPAND_LAST=${Z_COLLAPSED_PWD_EXPAND_LAST:-no} () { [[ $# == 0 ]] && local pwd="$PWD" || local pwd=$1 @@ -24,24 +24,29 @@ Z_COLLAPSED_PWD_EXPAND_LAST=${Z_COLLAPSED_PWD_EXPAND_LAST:-1} for i in {$begin_index..$end_index}; do (( total_count <= Z_COLLAPSED_PWD_MAX_LENGTH )) && break - local name=${names[$i]} - local first_c=${name:0:1} + local full_name=${names[$i]} + local first_c=${full_name:0:1} [[ $first_c == '.' ]] && offset=2 || offset=1 - local new_name=${name:0:$offset} - (( ${#name} == ${#new_name} )) && continue + local new_name=${full_name:0:$offset} + (( ${#full_name} == ${#new_name} )) && continue names[$i]=$Z_PROMPT_COLLAPSED_PWD$new_name$Z_PROMPT_PWD_L - total_count=$(( total_count - ${#name:$offset} )) - local last_collapsed=($i $name $offset) + total_count=$(( total_count - ${#full_name:$offset} )) + local last_collapsed=($i $full_name $offset) done # 尽可能展开最后一个压缩的目录名 - if ((Z_COLLAPSED_PWD_EXPAND_LAST)) && [[ -n $last_collapsed ]]; then - local expand_count=$(( Z_COLLAPSED_PWD_MAX_LENGTH - total_count )) - if (( expand_count > 0 )); then - local expanded_count=$(( expand_count + last_collapsed[3] )) - local new_name=${${last_collapsed[2]}:0:$expanded_count} - names[$last_collapsed[1]]=$Z_PROMPT_COLLAPSED_PWD$new_name$Z_PROMPT_PWD_L + if [[ $Z_COLLAPSED_PWD_EXPAND_LAST = yes ]] && [[ -n $last_collapsed ]]; then + local free_count=$(( Z_COLLAPSED_PWD_MAX_LENGTH - total_count )) + if (( free_count > 0 )); then + local visible_count=$(( free_count + last_collapsed[3] )) + local full_name=$last_collapsed[2] + local new_name=${full_name:0:$visible_count} + if [[ ${#new_name} == ${#full_name} ]]; then + names[$last_collapsed[1]]=$new_name + else + names[$last_collapsed[1]]=$Z_PROMPT_COLLAPSED_PWD$new_name$Z_PROMPT_PWD_L + fi fi fi diff --git a/.zshrc b/.zshrc index 9db8d81..1cd08ac 100644 --- a/.zshrc +++ b/.zshrc @@ -146,6 +146,8 @@ fi Z_COLLAPSED_PWD_RESERVE_COMPONENTS=1 # 最大路径长度(非严格) Z_COLLAPSED_PWD_MAX_LENGTH=32 +# 当空间足够时,尽可能展开最后一个压缩的目录名 +#Z_COLLAPSED_PWD_EXPAND_LAST=yes autoload -Uz collapsed_pwd precmd() {