bash/PROMPT_DIRTRIM: complete rewrite
Complete rewrite of the _lp_get_dirtrim into _lp_set_dirtrim with a much faster implementation that avoids char by char processing of $PWD. Also use (( )) builtin.
This commit is contained in:
parent
f38be781bc
commit
81c93c1d52
36
liquidprompt
36
liquidprompt
@ -32,7 +32,7 @@
|
|||||||
# David Loureiro <david.loureiro@sysfera.com> # Small portability fix
|
# David Loureiro <david.loureiro@sysfera.com> # Small portability fix
|
||||||
# Étienne Deparis <etienne@depar.is> # Fossil support
|
# Étienne Deparis <etienne@depar.is> # Fossil support
|
||||||
# Florian Le Frioux <florian@lefrioux.fr> # Use ± mark when root in VCS dir.
|
# Florian Le Frioux <florian@lefrioux.fr> # Use ± mark when root in VCS dir.
|
||||||
# François Schmidts <francois.schmidts@gmail.com> # Small code fix, _lp_get_dirtrim
|
# François Schmidts <francois.schmidts@gmail.com> # Initial PROMPT_DIRTRIM support
|
||||||
# Frédéric Lepied <flepied@gmail.com> # Python virtual env
|
# Frédéric Lepied <flepied@gmail.com> # Python virtual env
|
||||||
# Jonas Bengtsson <jonas.b@gmail.com> # Git remotes fix
|
# Jonas Bengtsson <jonas.b@gmail.com> # Git remotes fix
|
||||||
# Joris Dedieu <joris@pontiac3.nfrance.com> # Portability framework, FreeBSD support, bugfixes.
|
# Joris Dedieu <joris@pontiac3.nfrance.com> # Portability framework, FreeBSD support, bugfixes.
|
||||||
@ -605,26 +605,27 @@ _lp_shorten_path()
|
|||||||
# of the displayed path (if "\w" is present in the PS1 var).
|
# of the displayed path (if "\w" is present in the PS1 var).
|
||||||
# liquidprompt can calculate this number under two condition, path shortening
|
# liquidprompt can calculate this number under two condition, path shortening
|
||||||
# must be activated and PROMPT_DIRTRIM must be already set.
|
# must be activated and PROMPT_DIRTRIM must be already set.
|
||||||
_lp_get_dirtrim() {
|
_lp_set_dirtrim() {
|
||||||
[[ "$LP_ENABLE_SHORTEN_PATH" != 1 ]] && echo $PROMPT_DIRTRIM && return
|
|
||||||
|
|
||||||
local p="${PWD/$HOME/~}"
|
local p="${PWD/$HOME/~}"
|
||||||
local len=${#p}
|
local -i max_len=${COLUMNS:-80}*$LP_PATH_LENGTH/100
|
||||||
local max_len=$((${COLUMNS:-80}*$LP_PATH_LENGTH/100))
|
local -i dt=0
|
||||||
local PROMPT_DIRTRIM=0
|
|
||||||
|
|
||||||
if [[ "$((len))" -gt "$((max_len))" ]]; then
|
if (( ${#p} > max_len )); then
|
||||||
local i
|
local q="/${p##*/}"
|
||||||
|
local show="$q"
|
||||||
for ((i=$len;i>=0;i--))
|
# +3 because of the ellipsis: "..."
|
||||||
|
while (( ${#show}+3 < max_len ))
|
||||||
do
|
do
|
||||||
[[ $(($len-$i)) -gt $max_len ]] && break
|
(( dt++ ))
|
||||||
[[ "${p:i:1}" == "/" ]] && PROMPT_DIRTRIM=$((PROMPT_DIRTRIM+1))
|
p="${p%$q}"
|
||||||
|
q="/${p##*/}"
|
||||||
|
show="$q$show"
|
||||||
done
|
done
|
||||||
[[ "$((PROMPT_DIRTRIM))" -eq 0 ]] && PROMPT_DIRTRIM=1
|
(( dt == 0 )) && dt=1
|
||||||
fi
|
fi
|
||||||
unset p
|
PROMPT_DIRTRIM=$dt
|
||||||
echo "$PROMPT_DIRTRIM"
|
# For debugging
|
||||||
|
# echo $PROMPT_DIRTRIM
|
||||||
}
|
}
|
||||||
|
|
||||||
# Display a ":"
|
# Display a ":"
|
||||||
@ -1635,7 +1636,8 @@ _lp_set_prompt()
|
|||||||
# LP_HOST is a global set at load time
|
# LP_HOST is a global set at load time
|
||||||
LP_PERM="$(_lp_permissions_color)"
|
LP_PERM="$(_lp_permissions_color)"
|
||||||
LP_PWD="$(_lp_shorten_path)"
|
LP_PWD="$(_lp_shorten_path)"
|
||||||
[[ -n "$PROMPT_DIRTRIM" ]] && PROMPT_DIRTRIM="$(_lp_get_dirtrim)"
|
# Check that both LP_ENABLE_SHORTEN and PROMPT_DIRTRIM are set
|
||||||
|
[[ "_$LP_ENABLE_SHORTEN_PATH/$PROMPT_DIRTRIM" = _1/?* ]] && _lp_set_dirtrim
|
||||||
|
|
||||||
if _lp_are_vcs_enabled; then
|
if _lp_are_vcs_enabled; then
|
||||||
LP_VCS="$(_lp_git_branch_color)"
|
LP_VCS="$(_lp_git_branch_color)"
|
||||||
|
Loading…
Reference in New Issue
Block a user