3 # set_prompt - define useful constants and load bash prompts which use them
5 # shellcheck disable=SC2034
8 local STYLES_DIR="${BASH_SOURCE%/*}/styles"
12 echo "Available prompts:";
13 for file in "$STYLES_DIR"/*;
15 grep -q '^[ \t]*PS1' "$file" && echo " $(basename "$file")";
21 local STYLE_FILE="$STYLES_DIR/$1"
23 if [ ! -f "$STYLE_FILE" ];
25 echo "Cannot find prompt style '$(basename "$STYLE_FILE")'." 1>&2
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
67 # ANSI Escape Commands
71 # Bash recommends enclosing non-printing characters in a delimited sequence.
73 # From the bash(1) man page:
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
79 local RESET="\\[${ESC}[${ATTR_RESET}m\\]"
81 # Shortcuts for Colored Text (Foreground Only)
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\\]"
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\\]"
103 # Some shortcuts to switch charset mode
104 # https://en.wikipedia.org/wiki/Box-drawing_characters#Unix.2C_CP.2FM.2C_BBS
106 # From the console_codes(4) man page:
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\]'
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.
117 # From the console_codes(4) man page:
119 # ESC % @ Select default (ISO 646 / ISO 8859-1)
120 # ESC % G Select UTF-8
124 # TODO: check if the terminal is actually in UTF-8 mode, maybe using
126 MODE_DEFAULT='\[\033(B\033%G\]'
127 MODE_GRAPHIC='\[\033%@\033(0\]'
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.
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\]'
139 local MODE_DEFAULT_QUOTED="$MODE_DEFAULT"
140 local MODE_GRAPHIC_QUOTED="$MODE_GRAPHIC"
144 # VT100 Line Drawing Set
145 # http://vt100.net/docs/vt102-ug/table5-15.html
147 # Also called DEC Special Graphics set
148 # https://en.wikipedia.org/wiki/DEC_Special_Graphics
150 local UPPER_LEFT_CORNER="l"
151 local UPPER_RIGHT_CORNER="k"
152 local LOWER_LEFT_CORNER="m"
153 local LOWER_RIGHT_CORNER="j"
165 local CENTERED_DOT="~"
167 local SOLID_BLOCK="a"
168 local TRANSPARENT_BLOCK="_"
170 # An attempt to reset the charset attributes.
171 # http://www.in-ulm.de/~mascheck/various/alternate_charset/
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
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\]'
182 # Add the current user@host : pwd to the xterm title bar
184 xterm*|rxvt*|Eterm|aterm|kterm|gnome*|interix)
185 PROMPT_COMMAND='export ERR=$?; true; echo -ne "\033]0;${USER}@${HOSTNAME%%.*}:${PWD/$HOME/\~}\007"'
188 PROMPT_COMMAND='export ERR=$?; true; echo -ne "\033k${USER}@${HOSTNAME%%.*}:${PWD/$HOME/\~}\033\\"'
191 PROMPT_COMMAND='export ERR=$?; true'
195 # shellcheck source=/dev/null
198 export PS1 PROMPT_COMMAND