Fix path trimming and line break

abomination
Felix 2018-10-09 20:02:03 +02:00
parent 5cf4e21b03
commit bc8ffae08c
1 changed files with 22 additions and 57 deletions

View File

@ -120,46 +120,13 @@ prompt_pure_preprompt_render() {
# Initialize the preprompt array.
local -a preprompt_parts
# Trim prompt dirs
# adapted from https://github.com/agkozak/agkozak-zsh-prompt/blob/ee704a226e74246d1d03c1b7658963f87cc3b1c1/agkozak-zsh-prompt.plugin.zsh#L154
# TODO: This is so ugly... maybe profile it
# TODO: This sets prompt length wrong!
# TODO: Also: /usr/.../dir instead or /../subdir/dir
typeset -g DIRMAXTRIM=${PURE_DIRMAXTRIM:-2}
_trim_dir() {
local dir dir_count
case $HOME in
/) dir=${PWD} ;;
*) dir=${PWD#$HOME} ;;
esac
# The number of directory elements is the number of slashes in ${PWD#$HOME}
dir_count=$((${#dir} - ${#${dir//\//}}))
if (( dir_count <= $DIRMAXTRIM )); then
case $PWD in
${HOME}) printf '%s' '~' ;;
${HOME}*) printf '~%s' "${dir}" ;;
*) print -n "$PWD" ;;
esac
else
local lopped_path i
lopped_path=${dir}
i=0
while (( i != $DIRMAXTRIM )); do
lopped_path=${lopped_path%\/*}
(( i++ ))
done
case $PWD in
${HOME}*) printf '~/...%s' "${dir#${lopped_path}}" ;;
*) printf '...%s' "${PWD#${lopped_path}}" ;;
esac
fi
}
# Set the path.
#preprompt_parts+=('%F{black}%K{white} %~ %f%k')
# %...{} is a prompt escape sequence.
# _trim_dir() will be executed at render time
preprompt_parts+=('%F{black}%K{white} %{$(_trim_dir)%} %f%k')
# Trim is done via %(cond|if true|if false), %-DIGIT~ selects path components
# See https://rockhopper.dk/linux/zsh-promptwindow-title-trim-middle-of-path/
# and https://unix.stackexchange.com/questions/273529/shorten-path-in-zsh-prompt
# and http://zsh.sourceforge.net/Doc/Release/Prompt-Expansion.html
# and https://github.com/wesbos/Cobalt2-iterm/issues/15
preprompt_parts+=('%F{black}%K{white}% %(5~|%-1~/…/%3~|%4~) %f%k')
# Add git branch and dirty status info.
typeset -gA prompt_pure_vcs_info
@ -183,29 +150,27 @@ prompt_pure_preprompt_render() {
# removes the preprompt and only the original PROMPT remains.
cleaned_ps1=${PROMPT##*${prompt_newline}}
fi
local prompt_space='%-666v '
if [[ $PROMPT = *$prompt_space* ]]; then
cleaned_ps1=${PROMPT##*${prompt_space}}
fi
unset MATCH MBEGIN MEND
typeset -g expanded_prompt_pre=''
expanded_prompt_pre=${(j..)preprompt_parts}
#expanded_prompt_pre+=('%F{black}%K{white} %~ %f%k')
local expanded_prompt_pre
expanded_prompt_pre="${(j. .)preprompt_parts}"
expanded_prompt_pre="${(S%%)expanded_prompt_pre}"
# Length is 20 chars more than actual path string
# needs to be inside single [] brackets and as quoted string
# TODO: actually fix this and set prompt_newline before cleaned_ps1
# else zsh gets the wrong width for the prompt and messes up
if [ "${#expanded_prompt_pre}" -lt 45 ]; then
# Newline/no newline are swapped
prompt_newline='%666v ' # insert space before virtualenv etc.
else
prompt_newline=$'\n%{\r%}'
fi
local prompt_space_or_newline
if [ "${#expanded_prompt_pre}" -gt "30" ]; then
prompt_space_or_newline=$prompt_newline
else
prompt_space_or_newline=$prompt_space
fi
# Construct the new prompt with a clean preprompt.
local -ah ps1
ps1=(
${(j. .)preprompt_parts} # Join parts, space separated.
$prompt_newline # Separate preprompt and prompt.
$prompt_space_or_newline # Separate preprompt and prompt.
$cleaned_ps1
)
@ -232,7 +197,7 @@ prompt_pure_precmd() {
unset prompt_pure_cmd_timestamp
# shows the full path in the title
prompt_pure_set_title 'expand-prompt' '%~'
prompt_pure_set_title 'expand-prompt' '%(4~|%-1~/…/%2~|%3~)'
# preform async git dirty check and fetch
prompt_pure_async_tasks
@ -615,7 +580,7 @@ prompt_pure_setup() {
if [[ -z $prompt_newline ]]; then
# This variable needs to be set, usually set by promptinit.
typeset -g prompt_newline='%666v '
typeset -g prompt_newline=$'\n%{\r%}'
fi
zmodload zsh/datetime
@ -680,4 +645,4 @@ prompt_pure_setup() {
}
prompt_pure_setup "$@"
# vim: noet sw=8
# vim: noet sw=4