moving prompt utils out of bash prompt

This commit is contained in:
Loic Nageleisen 2013-12-19 17:00:48 +01:00
parent 860ebed1c3
commit b398a6c739
3 changed files with 68 additions and 63 deletions

View file

@ -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
}

View file

@ -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