Merge pull request #297 from stephankn/TruncateLen

extend gp_truncate_pwd to accept parameter with max pwd length
master
Martin Gondermann 9 years ago committed by GitHub
commit 2e19631cff
  1. 2
      README.md
  2. 3
      gitprompt.sh

@ -230,7 +230,7 @@ function prompt_callback {
- There are two helper functions that can be used within `prompt_callback`: - There are two helper functions that can be used within `prompt_callback`:
- `gp_set_window_title <String>` - sets the window title to the given string (should work for XTerm type terminals like in OS X or Ubuntu) - `gp_set_window_title <String>` - sets the window title to the given string (should work for XTerm type terminals like in OS X or Ubuntu)
- `gp_truncate_pwd` - a function that returns the current PWD truncated to fit the current terminal width - `gp_truncate_pwd` - a function that returns the current PWD truncated to fit the current terminal width. Specify the length to truncate to as a parameter. Otherwise it defaults to 1/3 of the terminal width.
- If you want to show the git prompt only if you are in a git repository you - If you want to show the git prompt only if you are in a git repository you
can set ``GIT_PROMPT_ONLY_IN_REPO=1`` before sourcing the gitprompt script can set ``GIT_PROMPT_ONLY_IN_REPO=1`` before sourcing the gitprompt script

@ -586,10 +586,11 @@ function is_function {
} }
# Helper function that truncates $PWD depending on window width # Helper function that truncates $PWD depending on window width
# Optionally specify maximum length as parameter (defaults to 1/3 of terminal)
function gp_truncate_pwd { function gp_truncate_pwd {
local tilde="~" local tilde="~"
local newPWD="${PWD/#${HOME}/${tilde}}" local newPWD="${PWD/#${HOME}/${tilde}}"
local pwdmaxlen=$((${COLUMNS:-80}/3)) local pwdmaxlen=${1:-$((${COLUMNS:-80}/3))}
[ ${#newPWD} -gt $pwdmaxlen ] && newPWD="...${newPWD:3-$pwdmaxlen}" [ ${#newPWD} -gt $pwdmaxlen ] && newPWD="...${newPWD:3-$pwdmaxlen}"
echo -n "$newPWD" echo -n "$newPWD"
} }

Loading…
Cancel
Save