diff options
author | Leonid Volnitsky <Leonid@Volnitsky.com> | 2009-07-01 19:00:12 +0300 |
---|---|---|
committer | Leonid Volnitsky <Leonid@Volnitsky.com> | 2009-07-01 19:00:12 +0300 |
commit | 32c68713f2e94bb70518cc00ecd712efa83393c9 (patch) | |
tree | 1463eeaf3eda387151d4b8ef728ae95ea5b24ce7 /git-prompt.sh | |
parent | Merge commit 'niklas/master' into dir (diff) | |
download | git-prompt-32c68713f2e94bb70518cc00ecd712efa83393c9.tar.gz git-prompt-32c68713f2e94bb70518cc00ecd712efa83393c9.tar.bz2 git-prompt-32c68713f2e94bb70518cc00ecd712efa83393c9.zip |
-- cwd_truncate() -- no external cmd implementation
Diffstat (limited to 'git-prompt.sh')
-rwxr-xr-x | git-prompt.sh | 63 |
1 files changed, 54 insertions, 9 deletions
diff --git a/git-prompt.sh b/git-prompt.sh index 9ba0b35..eb6a11b 100755 --- a/git-prompt.sh +++ b/git-prompt.sh @@ -1,6 +1,5 @@ # don't set prompt if this is not interactive shell -# it is better if this test is done before git-prompt.sh is source-ed for performance reasons. [[ $- != *i* ]] && return ################################################################### CONFIG @@ -147,15 +146,60 @@ export who_where truncate_working_directory() { - pwd=`echo $PWD | sed "s:^${HOME}:~:"` - [[ $truncate_pwd != "on" ]] && return - chars_per_dir=5 - while [[ $((chars_per_dir--)) -gt $((min_chars_per_pwd)) && `echo ${pwd} | wc -m` -gt $((max_pwd_length)) ]]; do - pwd=`echo ${pwd} | sed "s:[^\/~]*\(/.\{${chars_per_dir}\}\):\1:g"` - done - unset chars_per_dir + pwd=`echo $PWD | sed "s:^${HOME}:~:"` + [[ $truncate_pwd != "on" ]] && return + chars_per_dir=5 + while [[ $((chars_per_dir--)) -gt $((min_chars_per_pwd)) && `echo ${pwd} | wc -m` -gt $((max_pwd_length)) ]]; do + pwd=`echo ${pwd} | sed "s:[^\/~]*\(/.\{${chars_per_dir}\}\):\1:g"` + done + unset chars_per_dir + } + +cwd_truncate() { + # https://www.blog.montgomerie.net/pwd-in-the-title-bar-or-a-regex-adventure-in-bash + + [[ $truncate_pwd != "on" ]] && return + + local pwd_length=10 + + # Get the current working directory. We'll format it in $dir. + local dir="$PWD" + + # Substitute a leading path that's in $HOME for "~" + if [[ "$HOME" == ${dir:0:${#HOME}} ]] ; then + dir="~${dir:${#HOME}}" + fi + + # Append a trailing slash if it's not there already. + #if [[ ${dir:${#dir}-1} != "/" ]] ; then + # dir="$dir/" + #fi + + # Truncate if we're too long. + # We preserve the leading '/' or '~/', and substitute + # ellipses for some directories in the middle. + if [[ "$dir" =~ (~){0,1}/.*(.{${pwd_length}}) ]] ; then + local tilde=${BASH_REMATCH[1]} + local directory=${BASH_REMATCH[2]} + + # At this point, $directory is the truncated end-section of the + # path. We will now make it only contain full directory names + # (e.g. "ibrary/Mail" -> "/Mail"). + if [[ "$directory" =~ [^/]*(.*) ]] ; then + directory=${BASH_REMATCH[1]} + fi + + # Can't work out if it's possible to use the Unicode ellipsis, + # '…' (Unicode 2026). Directly embedding it in the string does not + # seem to work, and \u escape sequences ('\u2026') are not expanded. + #printf -v dir "$tilde/\u2026$s", $directory" + dir="$tilde/...$directory" + fi + + pwd="$dir" } + set_shell_title() { xterm_title() { echo -n "]2;${@}" ; } # FIXME: replace hardcodes with terminfo codes @@ -532,7 +576,8 @@ prompt_command_function() { set_shell_title "$PWD/" parse_vcs_status - truncate_working_directory + #truncate_working_directory + cwd_truncate PS1="$colors_reset$rc$head_local$label$color_who_where$dir_color$pwd$tail_local$dir_color> $colors_reset" unset head_local tail_local pwd |