]> snippets.scripts.mit.edu Git - Scripts/git/.git/blob - bash/newline-fix.bash
newline-fix.bash: Force the bash prompt to start after a newline
[Scripts/git/.git] / bash / newline-fix.bash
1 # newline-fix.bash: Force the bash prompt to start after a newline
2 # http://snippets.scripts.mit.edu/gitweb.cgi/.git/blob/HEAD:/bash/newline-fix.bash
3 #
4 # Some commands don’t print a newline at the end of their output, or
5 # take long enough to return that you’ve already typed part of the
6 # next command.  Either causes bash to start its prompt in the middle
7 # of a line, which confuses it.  This script sets $PROMPT_COMMAND to
8 # echo a magic sequence of terminal commands that will display a red
9 # “<no LF>\n” before the prompt if it would otherwise start in the
10 # middle of a line.
11 #
12 # Copyright © 2010 Anders Kaseorg <andersk@mit.edu>
13 #
14 # This program is free software: you can redistribute it and/or modify
15 # it under the terms of the GNU General Public License as published by
16 # the Free Software Foundation, either version 2 of the License, or
17 # (at your option) any later version.
18 #
19 # This program is distributed in the hope that it will be useful, but
20 # WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22 # General Public License for more details.
23 #
24 # Usage: Source this file from your ~/.bashrc.
25
26 show_no_lf () {
27     tput -S <<NOLF && echo -n '<no LF>' && \
28     tput -S <<SPSP && echo -n '  ' && \
29     tput -S <<EOF
30 sc
31 tbc
32 cr
33 cuf 8
34 hts
35 rc
36 setaf 1
37 rev
38 NOLF
39 sgr0
40 ht
41 sc
42 tbc
43 cr
44 $(for ((i=8; i<=320; i+=8)); do echo cuf 8; echo hts; done)
45 rc
46 SPSP
47 cr
48 el
49 EOF
50 }
51
52 show_no_lf="$(show_no_lf)" && \
53 PROMPT_COMMAND="echo -n \"\$show_no_lf\" >&2;$PROMPT_COMMAND"