diff --git a/README.md b/README.md index c7e8b7b..b956b9e 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,8 @@ any; the same as the login user; * the current host, if you are connected via an SSH or telnet connection, with different colors for each case; +* a green colon if the user has write permissions on the current directory, a red +one if he has not; * the current directory in bold, shortened if it takes too much space, while preserving the first two directories; * the name of the current branch if you are in a version control repository diff --git a/liquidprompt.bash b/liquidprompt.bash index cc4752a..ec2da49 100755 --- a/liquidprompt.bash +++ b/liquidprompt.bash @@ -258,6 +258,101 @@ __host_color() echo -ne "${ret}${NO_COL}" } +# BASH function that shortens +# a very long path for display by removing +# the left most parts and replacing them +# with a leading ... +# +# the first argument is the path +# +# the second argument is the maximum allowed +# length including the '/'s and ... +# http://hbfs.wordpress.com/2009/09/01/short-pwd-in-bash-prompts/ +# +# + keep some left part of the path if asked +__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 len="${#p}" + local max_len=$(($COLUMNS*$len_percent/100)) + local mask_len="${#mask}" + + if [ "$len" -gt "$max_len" ] + then + # finds all the '/' in + # the path and stores their + # positions + # + local pos=() + for ((i=0;i