commit 78dfcd5ae2268ed2c6ba66292bcb1d6ac5f3da4d parent eded72fb5965f78486ff80ef930c749a390de544 Author: hhvn <dev@hhvn.uk> Date: Sun, 23 May 2021 15:01:42 +0100 .c/k/prompt .c/k/kshrc: fix prompt counting Diffstat:
M | .config/ksh/kshrc | | | 2 | +- |
M | .config/ksh/prompt | | | 21 | +++++++++++++-------- |
2 files changed, 14 insertions(+), 9 deletions(-)
diff --git a/.config/ksh/kshrc b/.config/ksh/kshrc @@ -15,7 +15,7 @@ seteditor nvim settitle(){ # set terminal's title - printf "\033]0;%s\007" "$@" + printf "\[\033]0;%s\007\]" "$@" } PS1="\$(__ksh_prompt \$?)\$(settitle ksh)" diff --git a/.config/ksh/prompt b/.config/ksh/prompt @@ -1,20 +1,25 @@ #!/bin/ksh +# use \[..\] to prevent counting +__prompt_col(){ + printf '\[%s\]' "$(tput setaf $1)" +} + __ksh_prompt(){ errorcode=$1 # tput setaf 10 >/dev/null - [ $errorcode -eq 0 ] && tput setaf 15 || tput setaf 10 + [ $errorcode -eq 0 ] && __prompt_col 15 || __prompt_col 10 printf '%s ' "$errorcode" - tput setaf 5 + __prompt_col 5 printf "%s " "$(whoami)" - tput setaf 3 + __prompt_col 3 printf "%s" $(git branch 2>/dev/null | grep '^*' | awk '{print $2 "|"}') [ "$_PROMPT_GIT" = "y" ] && { git diff 1>/dev/null 2>/dev/null && { - git diff | grep '.' >/dev/null && tput setaf 8 || tput setaf 15 + git diff | grep '.' >/dev/null && __prompt_col 8 || __prompt_col 15 printf "%s+" $(git diff 2>/dev/null | grep -E '^(\+\+\+|---)' | sed -E 's~^[^a]*a/|^[^b]*b/~~' \ | grep -v /dev/null | uniq | wc -l | tr -d '\n' | grep '.' || printf 0) - git log --pretty=format:"commit %h%d" | head -n 1 | grep -v '.*\/.*' >/dev/null && tput setaf 14 && { + git log --pretty=format:"commit %h%d" | head -n 1 | grep -v '.*\/.*' >/dev/null && __prompt_col 14 && { git log --pretty=format:"commit %h%d" | awk -v "c=0" ' /^commit/ { if ($0 ~ /\//) { @@ -27,13 +32,13 @@ __ksh_prompt(){ printf ' ' } } - tput setaf 7 + __prompt_col 7 printf "%s " $(pwd | sed "s-$HOME-~-") echo "$0" | grep -v ksh >/dev/null && { printf "($(basename $0))" } - printf "$ " - tput sgr0 + printf "$\[ \]" # uncount an additional char + printf '\[%s\]' "$(tput sgr0)" } export _PROMPT_GIT=y