4 ms·
Another useful thing to have in one's PS1 is an optional newline if the last output didn't print one itself. Turns out this is non-trivial to get working perfec
by Liskni_si 5y ago
Another useful thing to have in one's PS1 is an optional newline if the last output didn't print one itself. Turns out this is non-trivial to get working perfectly (https://stackoverflow.com/questions/19943482/configure-shell-to-always-print-prompt-on-new-line-like-zsh https://stackoverflow.com/questions/19943482/configure-shell..., https://work.lisk.in/2020/11/20/even-faster-bash-startup.html#update-1-why-not-fix-typing-before-the-prompt-instead https://work.lisk.in/2020/11/20/even-faster-bash-startup.htm...). The version that I've been using for a while without problems is this one:
function __col1_ps1 {
[[ $MC_SID ]] && return
local termios cur_y
# ask the terminal for any pending (line buffered) input
termios=$(stty --save) && stty -icanon && stty "$termios"
# if there's pending input, assume it's been echoed and we're not in first column
# otherwise ask the terminal for current column and read it from input
if read -t 0 || { IFS='[;' read -s -r -d'R' -p$'\033[6n' _ _ cur_y && [[ $cur_y != 1 ]]; }; then
echo $'\001\033[41m↵\033[m\002\n\001\r\002'
fi
}
PS1='$(__col1_ps1)<the rest of my prompt>'
My full prompt is here: https://github.com/liskin/dotfiles/blob/9785740f7f97d7da1f847053fb29fc5a3174d075/.bashrc.d/20_prompt.sh#L10-L27 https://github.com/liskin/dotfiles/blob/9785740f7f97d7da1f84...