From: Iain Patterson Date: Tue, 2 Feb 2016 10:44:29 +0000 (+0000) Subject: Respect PROMPT_DIRTRIM with some enhancements. X-Git-Url: http://git.iain.cx/?a=commitdiff_plain;h=d2bb143d0545987c58f65a8b18c502f792f3f748;p=profile.git Respect PROMPT_DIRTRIM with some enhancements. Shorten the path in the prompt when PROMPT_DIRTRIM is set but do so by truncating components lower in the path rather than replacing them with elipses. --- diff --git a/.profile.d/ps1.bashrc b/.profile.d/ps1.bashrc index 278f107..0283611 100644 --- a/.profile.d/ps1.bashrc +++ b/.profile.d/ps1.bashrc @@ -90,7 +90,7 @@ function __ps1() { # Default __ps1_user to 1. [ -z "$__ps1_user" ] && __ps1_user=1 - PS1='\[\033[$(__ps1_background $?)m\]$(__ps1_user $? \u@)\[\033[$(__ps1_col $? 2>/dev/null)m\]$(__ps1_user $? \h)\[\033[$(__ps1_colour_escape $? $GIT_COLOUR $PROMPT_BACKGROUND_COLOUR)m\]$(__ps1_git $? 2>/dev/null)\[\033[0m\]\[\033[$(__ps1_colour_escape $? $P4_COLOUR $PROMPT_BACKGROUND_COLOUR)m\]$(__ps1_p4 $? 2>/dev/null)\[\033[0m\]\[\033[$(__ps1_colour_escape $? $SVN_COLOUR $PROMPT_BACKGROUND_COLOUR)m\]$(__ps1_svn $? 2>/dev/null)\[\033[$(__ps1_background $?)m\]$(__ps1_ret $? 2>/dev/null)$(__ps1_bg $?)$(__ps1_colon $?)\w\[\033[$(__ps1_root $? 2>/dev/null)m\]\$\[\033[0m\] ' + PS1='\[\033[$(__ps1_background $?)m\]$(__ps1_user $? \u@)\[\033[$(__ps1_col $? 2>/dev/null)m\]$(__ps1_user $? \h)\[\033[$(__ps1_colour_escape $? $GIT_COLOUR $PROMPT_BACKGROUND_COLOUR)m\]$(__ps1_git $? 2>/dev/null)\[\033[0m\]\[\033[$(__ps1_colour_escape $? $P4_COLOUR $PROMPT_BACKGROUND_COLOUR)m\]$(__ps1_p4 $? 2>/dev/null)\[\033[0m\]\[\033[$(__ps1_colour_escape $? $SVN_COLOUR $PROMPT_BACKGROUND_COLOUR)m\]$(__ps1_svn $? 2>/dev/null)\[\033[$(__ps1_background $?)m\]$(__ps1_ret $? 2>/dev/null)$(__ps1_bg $?)$(__ps1_colon $?)$(__ps1_short $?)\[\033[$(__ps1_root $? 2>/dev/null)m\]\$\[\033[0m\] ' return 0 } @@ -199,6 +199,34 @@ function __ps1_colon() { return $1 } +function __ps1_short() { + local dirtrim=${PROMPT_DIRTRIM//[^0-9]/} + dirtrim=${dirtrim##0} + if [ -z "$dirtrim" ]; then + echo "$PWD" + return $1 + fi + local dirname=${PWD##*/} + local basename=${PWD%/$dirname} + local reversed= + local component + for component in ${basename//\// }; do + reversed="$component $reversed" + done + local n=1 + local short= + for component in $reversed; do + if [ $n -ge $dirtrim ]; then + short="/${component:0:1}$short" + else + short="/$component$short" + fi + n=$((n+1)) + done + echo "$short/$dirname" + return $1 +} + function prompt() { local blurb="Usage: prompt hide|show "