From b398a6c739f69b4c755d9c95148bbbc01faee0be Mon Sep 17 00:00:00 2001 From: Loic Nageleisen Date: Thu, 19 Dec 2013 17:00:48 +0100 Subject: [PATCH] moving prompt utils out of bash prompt --- bash/ext | 26 +++++++++++++++++++ bash/prompt | 64 +--------------------------------------------- shell/prompt_utils | 41 +++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 63 deletions(-) create mode 100644 shell/prompt_utils diff --git a/bash/ext b/bash/ext index 200e376..37e84b5 100644 --- a/bash/ext +++ b/bash/ext @@ -1,4 +1,7 @@ # Make Bash more like Zsh + + +# Hooks # derived from http://glyf.livejournal.com/63106.html # default: NOOPs @@ -25,3 +28,26 @@ __preexec_invoke_exec () { PROMPT_COMMAND="precmd" trap '__preexec_invoke_exec' DEBUG cd() { __cd_invoke_chpwd $@; } + + +# clears a line that was not terminated by a LF +# fixing the dangling prompt issue +# marks it with a reverse %, like zsh +clear_incomplete_line() { + # ask for cursor position + echo -en "\033[6n" + # read answer + IFS=';' read -r -d R -a pos + # extract tput-compatible answer + local row=$((${pos[0]:2} - 1)) + local col=$((${pos[1]} - 1)) + # move back over terminal answer echo, which will hopefully be overwritten + tput cup $row $col + + # not on first column? clean up! (overwrites answer) + # we print a terminal width worth of columns, but since + # it goes too far, we backtrack with CR + [[ $col != 0 ]] && printf "\e[7m%%\e[m%*s\r" $((COLUMNS-1)) + # else e.g prompt will overwrite answer echo +} + diff --git a/bash/prompt b/bash/prompt index dc8d7cb..1b99bac 100644 --- a/bash/prompt +++ b/bash/prompt @@ -1,66 +1,4 @@ - -# truncates a string on the left -# $1: string to truncate -# $2: maximum length -# $3: truncation string replacement (optional) -# $4: separator symbol (optional, prevents truncation of rightmost item) -__truncate_left() { - local str="$1" - local maxlen="$2" - local trunc_symbol="$3" - local sep_symbol="$4" - - # get minimum length to not truncate a long name - if [ -n "$sep_symbol" ] - then - local component=${1##*$sep_symbol} - maxlen=$(( ( maxlen < ${#component} ) ? ${#component} : maxlen )) - fi - - # truncation point - local offset=$(( ${#str} - maxlen )) - - if [ ${offset} -gt "0" ] - then - #truncation is needed - str=${str:$offset:$maxlen} #truncate - str=${trunc_symbol}/${str#*/} #add symbol - fi - echo "$str" -} - -# truncates a path -__truncate_path() { - #gain some place with '~' - local path=${1/#$HOME/\~} - __truncate_left "$path" 25 '…' '/' -} - -# truncates CWD -__tpwd() { - __truncate_path "$PWD" -} - -# clears a line that was not terminated by a LF -# fixing the dangling prompt issue -# marks it with a reverse %, like zsh -clear_incomplete_line() { - # ask for cursor position - echo -en "\033[6n" - # read answer - IFS=';' read -r -d R -a pos - # extract tput-compatible answer - local row=$((${pos[0]:2} - 1)) - local col=$((${pos[1]} - 1)) - # move back over terminal answer echo, which will hopefully be overwritten - tput cup $row $col - - # not on first column? clean up! (overwrites answer) - # we print a terminal width worth of columns, but since - # it goes too far, we backtrack with CR - [[ $col != 0 ]] && printf "\e[7m%%\e[m%*s\r" $((COLUMNS-1)) - # else e.g prompt will overwrite answer echo -} +source "$DOTFILES_SHELL_DIR/prompt_utils" # git prompt info source $DOTFILES_SHELL_DIR/git_prompt_info diff --git a/shell/prompt_utils b/shell/prompt_utils new file mode 100644 index 0000000..4619c6c --- /dev/null +++ b/shell/prompt_utils @@ -0,0 +1,41 @@ +# truncates a string on the left +# $1: string to truncate +# $2: maximum length +# $3: truncation string replacement (optional) +# $4: separator symbol (optional, prevents truncation of rightmost item) +__truncate_left() { + local str="$1" + local maxlen="$2" + local trunc_symbol="$3" + local sep_symbol="$4" + + # get minimum length to not truncate a long name + if [ -n "$sep_symbol" ] + then + local component=${1##*$sep_symbol} + maxlen=$(( ( maxlen < ${#component} ) ? ${#component} : maxlen )) + fi + + # truncation point + local offset=$(( ${#str} - maxlen )) + + if [ ${offset} -gt "0" ] + then + #truncation is needed + str=${str:$offset:$maxlen} #truncate + str=${trunc_symbol}/${str#*/} #add symbol + fi + echo "$str" +} + +# truncates a path +__truncate_path() { + #gain some place with '~' + local path=${1/#$HOME/\~} + __truncate_left "$path" 25 '…' '/' +} + +# truncates CWD +__tpwd() { + __truncate_path "$PWD" +}