Skip to content

Instantly share code, notes, and snippets.

@ihodes
Created May 13, 2014 15:34
Show Gist options
  • Save ihodes/a664cdc68494ecfe2e13 to your computer and use it in GitHub Desktop.
Save ihodes/a664cdc68494ecfe2e13 to your computer and use it in GitHub Desktop.
# From https://gist.github.com/bahamas10/6567725 to fix stupid bash issues
# http://stackoverflow.com/questions/2575037/how-to-get-the-cursor-position-in-bash
# print the current column of the cursor
curcol() {
local pos oldstty row=0 col=0
exec < /dev/tty
oldstty=$(stty -g)
stty raw -echo min 0
tput u7 > /dev/tty
IFS=';' read -r -d R -a pos
stty "$oldstty"
# change from one-based to zero based so they work with: tput cup $row $col
col=${pos[1]}
col=$((${col:-1} - 1))
echo "$col"
}
# conditonally output `%\n` based on the current column of the cursor
fancynewline() {
if (( $(curcol) != 0 )); then
tput bold
tput bold
tput rev
tput rev
echo '\n'
tput sgr0
fi
}
PROMPT_COMMAND=fancynewline
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment