From 89e4dce48f5a8dee3a636701d307e5033e8fe518 Mon Sep 17 00:00:00 2001 From: Hugues Hiegel Date: Thu, 13 Mar 2008 00:23:32 +0100 Subject: Always truncates the path and git-branch to let a minimal free space for command prompt in the same line. Almost 40 characters (50% of a 80-wide terminal) will always be free for the command prompt : - the path will then be shrinked inside the remaining space available (letting at least 10 characters for the git-branch), until it reaches 10 characters min. - the git-branch (or the named-git-rev, or the git-commit-id) will then be shrinked until 10 characters min., always displaying, if available, the parent-level (~...), and, always, the first letters of the git-branch/named-rev/commit-id. --- 01_Functions.zsh | 2 +- 11_Prompts.zsh | 46 ++++++++++++++++++++++++++++------------------ 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/01_Functions.zsh b/01_Functions.zsh index 18cf1c6..1b53f40 100644 --- a/01_Functions.zsh +++ b/01_Functions.zsh @@ -72,7 +72,7 @@ get_git_branch () # If neither on a named commit-ish, show abbreviated commit-id [ "$my_git_branch" == "" ] &&\ - my_git_branch="($(git-show --pretty=format:%h 2>&- | head -n1)...)" + my_git_branch="($(git-show --pretty=format:%H 2>&- | head -n1))" fi echo $my_git_branch diff --git a/11_Prompts.zsh b/11_Prompts.zsh index 671f86d..0baea0e 100644 --- a/11_Prompts.zsh +++ b/11_Prompts.zsh @@ -154,31 +154,41 @@ old_precmd() # GITBRANCH=$(get_git_branch) GITBRANCHSIZE=${#GITBRANCH} - [ $GITBRANCHSIZE -gt 0 ] && GITBRANCHSIZE=$(($GITBRANCHSIZE + 1)) - GITBRANCH=${GITBRANCH:+$C_$COLOR_DOUBLEDOT$_C:$C_"$(get_git_status)"$_C$GITBRANCH} + [ $GITBRANCHSIZE -gt 0 ] && GITBRANCHSIZE=$(($GITBRANCHSIZE)) MY_PATH="%(!.%d.%~)" PATHSIZE=$(print -Pn $MY_PATH) PATHSIZE=${#PATHSIZE} - spaceleft=`print -Pn "%n@%m $(expand_text "$GITBRANCH") $ ls -laCdtrux -$(expand_text "$DATE")"` + spaceleft=`print -Pn "%n@%m $ ls -laCdtrux $(expand_text "$DATE")"` spaceleft=$(($COLUMNS - ${#spaceleft})) - minimalpathsize=`print -Pn "../%1~"` - minimalpathsize=${#minimalpathsize} - #minimalgitsize=$((1 + 8)) # ':' + git-abbrev-commit-ish - #if [ $spaceleft -lt $(( $PATHSIZE + $GITBRANCHSIZE )) ] - #then - # reduce the git-branch until it is shrinked to 7 characters max. - # if [ $GITBRANCHSIZE -gt 0 ] - # then - # GITBRANCHSIZE=$(( $spaceleft - $PATHSIZE )) - # [ $GITBRANCHSIZE -lt $minimalgitsize ] && GITBRANCHSIZE=$minimalgitsize - # GITBRANCH=`print -Pn "%$((GITBRANCHSIZE - 1))>...>$GITBRANCH>>"` - # fi - - #fi + #minimalpathsize=`print -Pn "../%1~"` + #minimalpathsize=${#minimalpathsize} + minimalpathsize=10 + minimalgitsize=10 # git-abbrev-commit-ish... + if [ $GITBRANCHSIZE -gt 0 ] + then + if [ $spaceleft -lt $(( $PATHSIZE + $GITBRANCHSIZE )) ] + then + local unbreakablegittail + # reduce the git-branch until it is shrinked to $minimalgitsize characters max. + + if [ $GITBRANCH[-1] = ")" ] + then + unbreakablegittail=${${(M)GITBRANCH%\~*}} + [ "$unbreakablegittail" = "" -a $GITBRANCHSIZE -gt $minimalgitsize ] && unbreakablegittail=")" + fi + if [ $GITBRANCHSIZE -gt $minimalgitsize ] + then + GITBRANCHSIZE=$(( $spaceleft - $PATHSIZE )) + [ $GITBRANCHSIZE -lt $minimalgitsize ] && GITBRANCHSIZE=$minimalgitsize + fi + GITBRANCH=`print -Pn "%"$(($GITBRANCHSIZE - ${#unbreakablegittail}))">..>"${GITBRANCH%\~*}${unbreakablegittail:+"%"${#unbreakablegittail}"<<"$GITBRANCH}` + fi + fi # then we reduce the path until it reaches the last path element, - #spaceleft=$(($spaceleft - $GITBRANCHSIZE)) + spaceleft=$(($spaceleft - $GITBRANCHSIZE)) [ $spaceleft -lt $minimalpathsize ] && spaceleft=$minimalpathsize + GITBRANCH=${GITBRANCH:+$C_$COLOR_DOUBLEDOT$_C:$C_"$(get_git_status)"$_C$GITBRANCH} CURDIR="$C_$COLOR_PATH$_C%`echo $spaceleft`<..<"$MY_PATH"%<<$C_$color[reset]$_C" ## Le prompt le plus magnifique du monde, et c'est le mien ! -- cgit v1.2.3