Browse code

ps1.sh: add variant for bash < 4.2

Indeed, ps1.sh was written and tested with bash > 4.2.
bash < 4.2 does not offer "declare -g".

Xavier G authored on 20/04/2017 20:22:14
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,100 @@
1
+# Declare and fill variables providing ANSI colors enclosed between
2
+# \[ and \], as per the "PROMPTING" section of man bash:
3
+function declare_colors {
4
+	local ci cj cn
5
+	c_normal='\[\e[0m\]'
6
+	for cn in {,bright_}{black,red,green,yellow,blue,magenta,cyan,white}; do
7
+		eval "c_${cn}='\[\e[${ci:=0};${cj:=30}m\]'"
8
+		((++cj,(cj==38)?(cj=30,++ci):1))
9
+	done
10
+}
11
+
12
+function disable_colors {
13
+	local cn
14
+	for cn in normal {,bright_}{black,red,green,yellow,blue,magenta,cyan,white}; do
15
+		eval "c_${cn}=''"
16
+	done
17
+}
18
+
19
+# Generate the PS1 variable; doing that in a function allows to reflect almost
20
+# anything in the prompt.
21
+function generate_ps1 {
22
+	# Save the return code value (rcv)
23
+	local rcv=$?
24
+
25
+	# Color for brackets and braces
26
+	local bc="${c_red}"
27
+
28
+	# Pick the return code color (rcc)
29
+	local rcc='c_bright_green'
30
+	[ $rcv -ne 0 ] && rcc='c_bright_red'
31
+
32
+	local rc dstamp tstamp user host cwdir es
33
+	rc=$(     printf "%s[%s${rcv}%s]" "${bc}" "${!rcc}" "${bc}")
34
+	tstamp=$( printf '[%s\\t%s]'      "${c_bright_yellow}" "${bc}")
35
+	user=$(   printf '[%s\\u%s]'      "${c_cyan}" "${bc}")
36
+	host=$(   printf '{%s\\h%s}'      "${c_cyan}" "${bc}")
37
+	cwdir=$(  printf '%s\w%s '        "${c_green}" "${c_normal}")
38
+	es=$(     printf '%s\$%s '        "${c_red}" "${c_normal}")
39
+
40
+	if [ "${PS1_DISPLAY_DATE}" ]; then
41
+		dstamp=$(printf '[%s\\D{%%F}%s]' "${c_bright_yellow}" "${bc}")
42
+	fi
43
+
44
+	PS1="${rc}${dstamp}${tstamp}${user}${host}${cwdir}${es}"
45
+
46
+	# Also set PS2 and PS4:
47
+	PS2="${c['bright_green']}"
48
+	PS4="+ ${c['cyan']}[trace]${c['normal']} "
49
+
50
+	# It makes sense to export PS4 (so that e.g. "bash foobar.sh" benefit from
51
+	# it):
52
+	export PS4
53
+}
54
+
55
+# Provide a helper function to swiftly enable or disable the date in the
56
+# prompt:
57
+function ps1_toggle_date {
58
+	if [ "${PS1_DISPLAY_DATE}" ]; then
59
+		unset PS1_DISPLAY_DATE
60
+	else
61
+		PS1_DISPLAY_DATE='yes'
62
+	fi
63
+}
64
+
65
+# Provide a helper function to swiftly enable or disable colors in the
66
+# prompt:
67
+function ps1_toggle_colors {
68
+	if [ "${PS1_USE_COLORS}" ]; then
69
+		unset PS1_USE_COLORS
70
+		disable_colors
71
+	else
72
+		declare_colors
73
+		PS1_USE_COLORS='yes'
74
+	fi
75
+}
76
+
77
+# Provide a helper function to swiftly reduce the prompt to '$ ' -- this is
78
+# useful e.g. for documentation purposes, when one wants to run a few commands
79
+# and copy the whole terminal output without the prompt (which usually reflects
80
+# undesired details such as user and hostname):
81
+function ps1_toggle_prompt {
82
+	local min_prompt='\$ '
83
+	if [ "${PS1}" == "${min_prompt}" ]; then
84
+		PROMPT_COMMAND='generate_ps1'
85
+		generate_ps1
86
+	else
87
+		unset PROMPT_COMMAND
88
+		PS1="${min_prompt}"
89
+	fi
90
+}
91
+
92
+# By default, use colors but do not display the date:
93
+ps1_toggle_colors
94
+#ps1_toggle_date
95
+
96
+# Instruct bash to regenerate PS1 before printing the prompt:
97
+PROMPT_COMMAND='generate_ps1'
98
+
99
+# Generate PS1 at least once:
100
+generate_ps1