Improvments for zsh shorten_path
This commit is contained in:
parent
f51d6d21b5
commit
4e94fa71ff
73
liquidprompt
73
liquidprompt
@ -340,20 +340,20 @@ __shorten_path()
|
|||||||
|
|
||||||
local len_percent=$2
|
local len_percent=$2
|
||||||
|
|
||||||
local p=$(echo "$1" | sed -e "s|$HOME|~|")
|
|
||||||
local len="${#p}"
|
local len="${#p}"
|
||||||
|
|
||||||
local max_len=$((${COLUMNS:-80}*$len_percent/100))
|
local max_len=$((${COLUMNS:-80}*$len_percent/100))
|
||||||
local mask_len="${#mask}"
|
local mask_len="${#mask}"
|
||||||
local slashes=0
|
local slashes=0
|
||||||
|
|
||||||
if [[ "$len" -gt "$max_len" ]]
|
if [[ "$WORKING_SHELL" == "bash" ]]; then
|
||||||
then
|
if [[ "$len" -gt "$max_len" ]]
|
||||||
# finds all the '/' in
|
then
|
||||||
# the path and stores their
|
local p=$(echo "$1" | sed -e "s|$HOME|~|")
|
||||||
# positions
|
# finds all the '/' in
|
||||||
#
|
# the path and stores their
|
||||||
if [[ "$WORKING_SHELL" == "bash" ]]; then
|
# positions
|
||||||
|
#
|
||||||
local pos=()
|
local pos=()
|
||||||
for ((i=0;i<len;i++))
|
for ((i=0;i<len;i++))
|
||||||
do
|
do
|
||||||
@ -404,60 +404,11 @@ __shorten_path()
|
|||||||
#
|
#
|
||||||
echo "${p:0:((${pos[${keep}]}+1))}${mask}${p:pos[i]}"
|
echo "${p:0:((${pos[${keep}]}+1))}${mask}${p:pos[i]}"
|
||||||
fi
|
fi
|
||||||
elif [[ "$WORKING_SHELL" == "zsh" ]]; then
|
else
|
||||||
pos=()
|
echo "$p"
|
||||||
for ((i=0;i<len;i++))
|
|
||||||
do
|
|
||||||
if [[ "$(echo $p[$i,$i])" == "/" ]]
|
|
||||||
then
|
|
||||||
pos+=($i)
|
|
||||||
slashes=$(( $slashes + 1 ))
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
pos+=($len)
|
|
||||||
|
|
||||||
# we have the '/'s, let's find the
|
|
||||||
# left-most that doesn't break the
|
|
||||||
# length limit
|
|
||||||
#
|
|
||||||
local i=$keep
|
|
||||||
if [[ $keep > $slashes ]] ; then
|
|
||||||
i=$slashes
|
|
||||||
fi
|
|
||||||
i=$(( $i + 1 ))
|
|
||||||
while [[ $(( $len - $pos[$i] )) -gt $(( $max_len - $mask_len )) ]]
|
|
||||||
do
|
|
||||||
i=$(( $i + 1 ))
|
|
||||||
done
|
|
||||||
|
|
||||||
# let us check if it's OK to
|
|
||||||
# print the whole thing
|
|
||||||
#
|
|
||||||
if [[ $pos[$i] -eq "0" ]]
|
|
||||||
then
|
|
||||||
# the path is shorter than
|
|
||||||
# the maximum allowed length,
|
|
||||||
# so no need for ...
|
|
||||||
#
|
|
||||||
echo "$p"
|
|
||||||
elif [[ $pos[$i] == "$len" ]]
|
|
||||||
then
|
|
||||||
# constraints are broken because
|
|
||||||
# the maximum allowed size is smaller
|
|
||||||
# than the last part of the path, plus
|
|
||||||
# ' … '
|
|
||||||
#
|
|
||||||
echo "$( echo $p[1,$(( $pos[$keep] ))] )${mask}$( echo $p[$(( 0 - $max_len + $mask_len )),-1] )"
|
|
||||||
else
|
|
||||||
# constraints are satisfied, at least
|
|
||||||
# some parts of the path, plus ' … ', are
|
|
||||||
# shorter than the maximum allowed size
|
|
||||||
#
|
|
||||||
echo "$( echo $p[1,$(( $pos[$keep] ))])${mask}$( echo $p[$pos[$i],-1] )"
|
|
||||||
fi
|
|
||||||
fi
|
fi
|
||||||
else
|
elif [[ "$WORKING_SHELL" == "zsh" ]]; then
|
||||||
echo "$p"
|
echo "%-${keep}~%${max_len}<${mask}<%~%<<"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user