Merge pull request #89 from jaesivsm/feature/dirtrim

Feature/dirtrim
This commit is contained in:
nojhan 2013-01-12 08:36:00 -08:00
commit 3aedf713f4
2 changed files with 43 additions and 17 deletions

View File

@ -134,6 +134,13 @@ Note also that all the `LP_ENABLE_…` variables override the templates,
i.e. if you use `$LP_BATT` in your template and you set `LP_ENABLE_BATT=0` i.e. if you use `$LP_BATT` in your template and you set `LP_ENABLE_BATT=0`
in your config file, you will not have the battery informations. in your config file, you will not have the battery informations.
If you are using bash and want to use the `PROMPT_DIRTRIM` built-in
functionality to shorten but still have liquidprompt calculating the number of
directories to keep in the path, precise a value for `PROMPT_DIRTRIM` before
sourcing liquidprompt and liquidprompt will override this value with one
fitting the width of your terminal.
## CUSTOMIZING THE PROMPT ## CUSTOMIZING THE PROMPT
### ADD A PS1 PREFIX ### ADD A PS1 PREFIX

View File

@ -29,7 +29,7 @@
# Joris Dedieu <joris@pontiac3.nfrance.com> # Portability framework, FreeBSD support, bugfixes. # Joris Dedieu <joris@pontiac3.nfrance.com> # Portability framework, FreeBSD support, bugfixes.
# Ludovic Rousseau <ludovic.rousseau@gmail.com> # Lot of bugfixes. # Ludovic Rousseau <ludovic.rousseau@gmail.com> # Lot of bugfixes.
# Yann 'Ze' Richard <ze@nbox.org> # Do not fail on missing commands. # Yann 'Ze' Richard <ze@nbox.org> # Do not fail on missing commands.
# François Schmidts <fschmidts@olfeo.com> # Simpler SSH_IP acquiring method. # François Schmidts <francois.schmidts@gmail.com> # small code fix, _lp_get_dirtrim
# Thomas Debesse <thomas.debesse@gmail.com> # Fix columns use. # Thomas Debesse <thomas.debesse@gmail.com> # Fix columns use.
# 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.
# Luc Didry <luc@fiat-tux.fr> # Zsh port # Luc Didry <luc@fiat-tux.fr> # Zsh port
@ -411,8 +411,12 @@ _lp_proxy()
# + keep some left part of the path if asked # + keep some left part of the path if asked
_lp_shorten_path() _lp_shorten_path()
{ {
if [[ "$LP_ENABLE_SHORTEN_PATH" != 1 ]] ; then if [[ "$LP_ENABLE_SHORTEN_PATH" != 1 || -n "$PROMPT_DIRTRIM" ]] ; then
echo "$1" if [[ "$_LP_WORKING_SHELL" == "bash" ]]; then
echo "\\w"
else
echo "$(print -P "%~")"
fi
return return
fi fi
# the character that will replace the part of the path that is masked # the character that will replace the part of the path that is masked
@ -423,17 +427,10 @@ _lp_shorten_path()
keep=$LP_PATH_KEEP keep=$LP_PATH_KEEP
fi fi
local len_percent=$2 local p="${PWD/$HOME/~}"
local p
if [[ "$_LP_WORKING_SHELL" == "bash" ]]; then
p=$(echo "$1" | sed -e "s|$HOME|~|")
elif [[ "$_LP_WORKING_SHELL" == "zsh" ]]; then
p=$1
fi
local len="${#p}" local len="${#p}"
local max_len=$((${COLUMNS:-80}*$len_percent/100)) local max_len=$((${COLUMNS:-80}*$LP_PATH_LENGTH/100))
local mask_len="${#mask}" local mask_len="${#mask}"
local slashes=0 local slashes=0
@ -506,6 +503,31 @@ _lp_shorten_path()
fi fi
} }
# In bash shell, PROMPT_DIRTRIM is the number of directory to keep at the end
# of the displayed path (if "\w" is present in the PS1 var).
# liquidprompt can calculate this number under two condition, path shortening
# must be activated and PROMPT_DIRTRIM must be already set.
_lp_get_dirtrim() {
[[ "$LP_ENABLE_SHORTEN_PATH" != 1 ]] && echo 0 && return
local p="${PWD/$HOME/~}"
local len=${#p}
local max_len=$((${COLUMNS:-80}*$LP_PATH_LENGTH/100))
local PROMPT_DIRTRIM=0
if [[ "$((len))" -gt "$((max_len))" ]]; then
local i
for ((i=$len;i>=0;i--))
do
[[ $(($len-$i)) -gt $max_len ]] && break
[[ "${p:i:1}" == "/" ]] && PROMPT_DIRTRIM=$((PROMPT_DIRTRIM+1))
done
[[ "$((PROMPT_DIRTRIM))" -eq 0 ]] && PROMPT_DIRTRIM=1
fi
echo "$PROMPT_DIRTRIM"
}
# Display a ":" # Display a ":"
# colored in green if user have write permission on the current directory # colored in green if user have write permission on the current directory
# colored in red if it have not. # colored in red if it have not.
@ -1094,11 +1116,8 @@ _lp_set_prompt()
LP_USER=$(_lp_user) LP_USER=$(_lp_user)
# 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)
if [[ "$_LP_WORKING_SHELL" == "bash" ]]; then LP_PWD=$(_lp_shorten_path)
LP_PWD=$(_lp_shorten_path "$PWD" $LP_PATH_LENGTH) [[ -n "$PROMPT_DIRTRIM" ]] && PROMPT_DIRTRIM=$(_lp_get_dirtrim)
elif [[ "$_LP_WORKING_SHELL" == "zsh" ]]; then
LP_PWD=$(_lp_shorten_path "$(print -P "%~")" $LP_PATH_LENGTH)
fi
LP_PROXY="${LP_COLOR_PROXY}$(_lp_proxy)${NO_COL}" LP_PROXY="${LP_COLOR_PROXY}$(_lp_proxy)${NO_COL}"
# right of main prompt: space at left # right of main prompt: space at left