Keep some left part of the shortened path

When shortening the path, keep the PATH_KEEP left most directories (from root,
except for ~), then insert " … ", then the rest of the shortened path.
This commit is contained in:
nojhan 2012-07-28 22:26:10 +02:00
parent 4df87ea6ac
commit f39fa12bc5

View File

@ -61,6 +61,9 @@ LOAD_THRESHOLD=60
# Recommended value is 35 # Recommended value is 35
PATH_LENGTH=35 PATH_LENGTH=35
# How many directories to keep at the beginning of a shortened path
# Recommended value is 2
PATH_KEEP=2
############### ###############
# OS specific # # OS specific #
@ -372,7 +375,7 @@ __hg_branch_color()
fi fi
} }
# Simple BASH function that shortens # BASH function that shortens
# a very long path for display by removing # a very long path for display by removing
# the left most parts and replacing them # the left most parts and replacing them
# with a leading ... # with a leading ...
@ -383,12 +386,20 @@ __hg_branch_color()
# length including the '/'s and ... # length including the '/'s and ...
# http://hbfs.wordpress.com/2009/09/01/short-pwd-in-bash-prompts/ # http://hbfs.wordpress.com/2009/09/01/short-pwd-in-bash-prompts/
# #
# + keep some left part of the path if asked
__shorten_path() __shorten_path()
{ {
# the character that will replace the part of the path that is masked
local mask=" … "
# index of the directory to keep from the root (starts at 0)
local keep=$((PATH_KEEP-1))
local len_percent=$2
local p=$(echo "$1" | sed -e "s|$HOME|~|") local p=$(echo "$1" | sed -e "s|$HOME|~|")
local len="${#p}" local len="${#p}"
local len_percent=$2
local max_len=$(($COLUMNS*$len_percent/100)) local max_len=$(($COLUMNS*$len_percent/100))
local mask_len="${#mask}"
if [ "$len" -gt "$max_len" ] if [ "$len" -gt "$max_len" ]
then then
@ -410,8 +421,8 @@ __shorten_path()
# left-most that doesn't break the # left-most that doesn't break the
# length limit # length limit
# #
local i=0 local i=$keep
while [ "$((len-pos[i]))" -gt "$((max_len-3))" ] while [ "$((len-pos[i]))" -gt "$((max_len-mask_len))" ]
do do
i=$((i+1)) i=$((i+1))
done done
@ -432,15 +443,15 @@ __shorten_path()
# constraints are broken because # constraints are broken because
# the maximum allowed size is smaller # the maximum allowed size is smaller
# than the last part of the path, plus # than the last part of the path, plus
# '...' # ''
# #
echo "${p:((len-max_len+3))}" echo "${p:0:((${pos[${keep}]}+1))}${mask}${p:((len-max_len+mask_len))}"
else else
# constraints are satisfied, at least # constraints are satisfied, at least
# some parts of the path, plus ..., are # some parts of the path, plus , are
# shorter than the maximum allowed size # shorter than the maximum allowed size
# #
echo "${p:pos[i]}" echo "${p:0:((${pos[${keep}]}+1))}${mask}${p:pos[i]}"
fi fi
else else
echo "$p" echo "$p"