From fd9e15c7373b0c3a99617d775f70df685c73b0ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Schmidts?= Date: Wed, 9 Jan 2013 13:50:06 +0100 Subject: [PATCH] my way of shorting path was way slower than the one originally implemented redoing the dirtrim feature though --- liquidprompt | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/liquidprompt b/liquidprompt index 7c8531e..ce1d480 100755 --- a/liquidprompt +++ b/liquidprompt @@ -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