bash: clear incomplete line

This commit is contained in:
Loic Nageleisen 2013-12-19 20:28:46 +01:00
parent cc536c39cc
commit d4b90d1df8
2 changed files with 21 additions and 18 deletions

View file

@ -30,24 +30,27 @@ trap '__preexec_invoke_exec' DEBUG
cd() { __cd_invoke_chpwd $@; } cd() { __cd_invoke_chpwd $@; }
# clears a line that was not terminated by a LF # clears a line that was not terminated by a LF fixing the dangling prompt
# fixing the dangling prompt issue # issue by marking it with a reversed %, like zsh
# marks it with a reverse %, like zsh
clear_incomplete_line() { clear_incomplete_line() {
# ask for cursor position local row
echo -en "\033[6n" local col
# 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) # ask for cursor position then read answer
# we print a terminal width worth of columns, but since # TODO: fix read: mashing keyboard makes syntax errors
# it goes too far, we backtrack with CR stty -echo
[[ $col != 0 ]] && printf "\e[7m%%\e[m%*s\r" $((COLUMNS-1)) echo -en "\033[6n"
# else e.g prompt will overwrite answer echo IFS=';' read -r -d R -a pos
stty echo
# extract tput-compatible answer
row=$(( ${pos[0]:2} - 1 ))
col=$(( ${pos[1]} - 1 ))
# not on first column? do clean up: fill with spaces and rag left
if [[ $col != 0 ]]; then
printf "\e[7m%%\e[m"
printf "%*s\r" $(( COLUMNS - $col ))
fi
} }

View file

@ -12,9 +12,9 @@ source $DOTFILES_SHELL_DIR/bundler
source $DOTFILES_SHELL_DIR/go source $DOTFILES_SHELL_DIR/go
precmd() { precmd() {
clear_incomplete_line
set_prompt set_prompt
set_term_title set_term_title
#clear_incomplete_line
} }
chpwd() { chpwd() {