my way of shorting path was way slower than the one originally implemented

redoing the dirtrim feature though
This commit is contained in:
François Schmidts 2013-01-09 13:50:06 +01:00
parent ea5d844819
commit fd9e15c737

View File

@ -398,7 +398,7 @@ _lp_proxy()
# + keep some left part of the path if asked
_lp_shorten_path()
{
if [[ "$LP_ENABLE_SHORTEN_PATH" != 1 ]] ; then
if [[ "$LP_ENABLE_SHORTEN_PATH" != 1 || -n "$PROMPT_DIRTRIM" ]] ; then
if [[ "$_LP_WORKING_SHELL" == "bash" ]]; then
echo "\\w"
else
@ -490,6 +490,35 @@ _lp_shorten_path()
fi
}
_lp_get_dirtrim() {
if [[ "$LP_ENABLE_SHORTEN_PATH" != 1 ]]; then
[[ -n "$PROMPT_DIRTRIM" ]] && echo "$PROMPT_DIRTRIM"
return
fi
local tot
[[ "$PWD" == *$HOME* ]] && tot="$((${#PWD}-${#HOME}+1))" || tot="${#PWD}"
local max_len=$((${COLUMNS:-80}*$LP_PATH_LENGTH/100))
local PROMPT_DIRTRIM=0
if [[ "$((tot))" -gt "$((max_len))" ]]; then
local i
local len=0
local p="$(echo $PWD | rev)"
local IFS="/"
for i in $p
do
len="$((len+${#i}))"
[[ "$((len))" -gt "$((max_len))" ]] && break
PROMPT_DIRTRIM=$((PROMPT_DIRTRIM+1))
done
[[ "$((PROMPT_DIRTRIM))" -eq 0 ]] && PROMPT_DIRTRIM=1
fi
echo "$PROMPT_DIRTRIM"
}
# Display a ":"
# colored in green if user have write permission on the current directory
# colored in red if it have not.
@ -991,6 +1020,7 @@ _lp_set_prompt()
# LP_HOST is a global set at load time
LP_PERM=$(_lp_permissions_color)
LP_PWD=$(_lp_shorten_path)
PROMPT_DIRTRIM=$(_lp_get_dirtrim)
LP_PROXY="${LP_COLOR_PROXY}$(_lp_proxy)${NO_COL}"
# right of main prompt: space at left