mirror of
https://github.com/lloeki/dotfiles.git
synced 2025-12-06 07:24:39 +01:00
moving prompt utils out of bash prompt
This commit is contained in:
parent
860ebed1c3
commit
b398a6c739
3 changed files with 68 additions and 63 deletions
26
bash/ext
26
bash/ext
|
|
@ -1,4 +1,7 @@
|
||||||
# Make Bash more like Zsh
|
# Make Bash more like Zsh
|
||||||
|
|
||||||
|
|
||||||
|
# Hooks
|
||||||
# derived from http://glyf.livejournal.com/63106.html
|
# derived from http://glyf.livejournal.com/63106.html
|
||||||
|
|
||||||
# default: NOOPs
|
# default: NOOPs
|
||||||
|
|
@ -25,3 +28,26 @@ __preexec_invoke_exec () {
|
||||||
PROMPT_COMMAND="precmd"
|
PROMPT_COMMAND="precmd"
|
||||||
trap '__preexec_invoke_exec' DEBUG
|
trap '__preexec_invoke_exec' DEBUG
|
||||||
cd() { __cd_invoke_chpwd $@; }
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
||||||
64
bash/prompt
64
bash/prompt
|
|
@ -1,66 +1,4 @@
|
||||||
|
source "$DOTFILES_SHELL_DIR/prompt_utils"
|
||||||
# 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
|
|
||||||
}
|
|
||||||
|
|
||||||
# git prompt info
|
# git prompt info
|
||||||
source $DOTFILES_SHELL_DIR/git_prompt_info
|
source $DOTFILES_SHELL_DIR/git_prompt_info
|
||||||
|
|
|
||||||
41
shell/prompt_utils
Normal file
41
shell/prompt_utils
Normal file
|
|
@ -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"
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue