README: improve wording an provide a more robust example of use
[config/bash_prompt.git] / set_prompt
1 #!/bin/bash
2 #
3 # set_prompt - define useful constants and load bash prompts which use them
4
5 # shellcheck disable=SC2034
6 function set_prompt()
7 {
8   local STYLES_DIR="${BASH_SOURCE%/*}/styles"
9
10   if [ "x$1" = "x" ];
11   then
12     echo "Available prompts:";
13     for file in "$STYLES_DIR"/*;
14     do
15       grep -q '^[ \t]*PS1' "$file" && echo "  $(basename "$file")";
16     done
17
18     return 0
19   fi
20
21   local STYLE_FILE="$STYLES_DIR/$1"
22
23   if [ ! -f "$STYLE_FILE" ];
24   then
25     echo "Cannot find prompt style '$(basename "$STYLE_FILE")'." 1>&2
26     return 1
27   fi
28
29   # ANSI Escape Color
30   # http://www.termsys.demon.co.uk/vtansi.htm
31   # http://ascii-table.com/ansi-escape-sequences.php
32   # http://www2.gar.no/glinkj/help/cmds/ansa.htm
33   # http://vt100.net/docs/tp83/appendixh.html
34
35   local ATTR_RESET=0
36   local BOLD=1
37   local DIM=2
38
39   local UNDERSCORE=4
40   local BLINK=5
41
42   local REVERSE=7
43   local HIDDEN=8
44
45   local FG_BLACK=30
46   local FG_RED=31
47   local FG_GREEN=32
48   local FG_YELLOW=33
49   local FG_BLUE=34
50   local FG_MAGENTA=35
51   local FG_CYAN=36
52   local FG_WHITE=37
53
54   local FG_NULL=00
55
56   local BG_BLACK=40
57   local BG_RED=41
58   local BG_GREEN=42
59   local BG_YELLOW=43
60   local BG_BLUE=44
61   local BG_MAGENTA=45
62   local BG_CYAN=46
63   local BG_WHITE=47
64
65   local BG_NULL=00
66
67   # ANSI Escape Commands
68   #
69   # NOTE:
70   #
71   # Bash recommends enclosing non-printing characters in a delimited sequence.
72   #
73   # From the bash(1) man page:
74   #
75   # \[     begin a sequence of non-printing characters, which could be used to embed a terminal control sequence into the prompt
76   # \]     end a sequence of non-printing characters
77   #
78   local ESC='\033'
79   local RESET="\\[${ESC}[${ATTR_RESET}m\\]"
80
81   # Shortcuts for Colored Text (Foreground Only)
82
83   # NORMAL TEXT
84   local BLACK="\\[${ESC}[${ATTR_RESET};${FG_BLACK}m\\]"
85   local RED="\\[${ESC}[${ATTR_RESET};${FG_RED}m\\]"
86   local GREEN="\\[${ESC}[${ATTR_RESET};${FG_GREEN}m\\]"
87   local YELLOW="\\[${ESC}[${ATTR_RESET};${FG_YELLOW}m\\]"
88   local BLUE="\\[${ESC}[${ATTR_RESET};${FG_BLUE}m\\]"
89   local MAGENTA="\\[${ESC}[${ATTR_RESET};${FG_MAGENTA}m\\]"
90   local CYAN="\\[${ESC}[${ATTR_RESET};${FG_CYAN}m\\]"
91   local WHITE="\\[${ESC}[${ATTR_RESET};${FG_WHITE}m\\]"
92
93   # BOLD TEXT
94   local BOLD_BLACK="\\[${ESC}[${BOLD};${FG_BLACK}m\\]"
95   local BOLD_RED="\\[${ESC}[${BOLD};${FG_RED}m\\]"
96   local BOLD_GREEN="\\[${ESC}[${BOLD};${FG_GREEN}m\\]"
97   local BOLD_YELLOW="\\[${ESC}[${BOLD};${FG_YELLOW}m\\]"
98   local BOLD_BLUE="\\[${ESC}[${BOLD};${FG_BLUE}m\\]"
99   local BOLD_MAGENTA="\\[${ESC}[${BOLD};${FG_MAGENTA}m\\]"
100   local BOLD_CYAN="\\[${ESC}[${BOLD};${FG_CYAN}m\\]"
101   local BOLD_WHITE="\\[${ESC}[${BOLD};${FG_WHITE}m\\]"
102
103   # Some shortcuts to switch charset mode
104   # https://en.wikipedia.org/wiki/Box-drawing_characters#Unix.2C_CP.2FM.2C_BBS
105
106   # From the console_codes(4) man page:
107   #
108   # ESC ( B               Select default (ISO 8859-1 mapping)
109   # ESC ( 0               Select VT100 graphics mapping
110   local MODE_DEFAULT='\[\033(B\]'
111   local MODE_GRAPHIC='\[\033(0\]'
112
113   # The linux tty terminal does not display the DEC graphic set when it is in
114   # UTF-8 mode (see also unicode_start and kbd_mode) so disable UTF-8 when
115   # entering the GRAPHIC mode and re-enable it in DEFAULT mode.
116   #
117   # From the console_codes(4) man page:
118   #
119   # ESC % @               Select default (ISO 646 / ISO 8859-1)
120   # ESC % G               Select UTF-8
121   #
122   case $TERM in
123     linux*)
124       # TODO: check if the terminal is actually in UTF-8 mode, maybe using
125       # kbd_mode.
126       MODE_DEFAULT='\[\033(B\033%G\]'
127       MODE_GRAPHIC='\[\033%@\033(0\]'
128
129       # If the variables from above were used in a printf format string (e.g.
130       # inside the __git_ps1 argument), '%G' and '%@' would be wrongly
131       # interpreted as conversion specifiers.
132       #
133       # Define alternative variables to use with printf, to ensure that
134       # a literal '%' is produced eventually in the output of printf.
135       MODE_DEFAULT_QUOTED='\[\033(B\033%%G\]'
136       MODE_GRAPHIC_QUOTED='\[\033%%@\033(0\]'
137       ;;
138     *)
139       local MODE_DEFAULT_QUOTED="$MODE_DEFAULT"
140       local MODE_GRAPHIC_QUOTED="$MODE_GRAPHIC"
141       ;;
142   esac
143
144   # VT100 Line Drawing Set
145   # http://vt100.net/docs/vt102-ug/table5-15.html
146   #
147   # Also called DEC Special Graphics set
148   # https://en.wikipedia.org/wiki/DEC_Special_Graphics
149
150   local UPPER_LEFT_CORNER="l"
151   local UPPER_RIGHT_CORNER="k"
152   local LOWER_LEFT_CORNER="m"
153   local LOWER_RIGHT_CORNER="j"
154
155   local CROSS="n"
156
157   local HORIZ_LINE="q"
158   local VERT_LINE="x"
159
160   local UPPER_T="v"
161   local LOWER_T="w"
162   local RIGHT_T="t"
163   local LEFT_T="u"
164
165   local CENTERED_DOT="~"
166
167   local SOLID_BLOCK="a"
168   local TRANSPARENT_BLOCK="_"
169
170   # An attempt to reset the charset attributes.
171   # http://www.in-ulm.de/~mascheck/various/alternate_charset/
172   #
173   # ESC (B                      Select G0 Character Set: United States (USASCII)
174   # ESC )0                      Select G1 Character Set: Special Character and Line Drawing Set
175   # O        ( Ctrl-O )         Switch to Standard Character Set
176   # ESC [?5l                    DEC Private Mode Reset: Normal Video
177   # ESC 7                       Save Cursor
178   # ESC [r                      weird (actually 'ESC [0;0r' ? Set Scrolling Region [top;bottom] )
179   # ESC 8                       Restore Cursor
180   local RESET_CHARSET='\[\033(B\033)0\017\033[?5l\0337\033[r\0338\]'
181
182   # Add the current user@host : pwd to the xterm title bar
183   case $TERM in
184     xterm*|rxvt*|Eterm|aterm|kterm|gnome*|interix)
185       PROMPT_COMMAND='export ERR=$?; true; echo -ne "\033]0;${USER}@${HOSTNAME%%.*}:${PWD/$HOME/\~}\007"'
186       ;;
187     screen*)
188       PROMPT_COMMAND='export ERR=$?; true; echo -ne "\033k${USER}@${HOSTNAME%%.*}:${PWD/$HOME/\~}\033\\"'
189       ;;
190     *)
191       PROMPT_COMMAND='export ERR=$?; true'
192       ;;
193   esac
194
195   # shellcheck source=/dev/null
196   source "$STYLE_FILE"
197
198   export PS1 PROMPT_COMMAND
199 }