Skip to content

Commit a68d37d

Browse files
committed
theme/brainy: work-in-progress
Handle all unbound parameters, even colors!
1 parent 750b97c commit a68d37d

File tree

1 file changed

+119
-115
lines changed

1 file changed

+119
-115
lines changed

themes/brainy/brainy.theme.bash

Lines changed: 119 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# shellcheck shell=bash
22
# shellcheck disable=SC2034 # Expected behavior for themes.
3-
# shellcheck disable=SC2154 #TODO: fix these all.
43

54
# Brainy Bash Prompt for Bash-it
65
# by MunifTanjim
@@ -9,78 +8,80 @@
98
## Parsers ##
109
#############
1110

12-
____brainy_top_left_parse() {
13-
ifs_old="${IFS}"
14-
IFS="|"
11+
function ____brainy_top_left_parse() {
12+
local ifs_old="${IFS}"
13+
local IFS="|"
1514
read -r -a args <<< "$@"
1615
IFS="${ifs_old}"
17-
if [ -n "${args[3]}" ]; then
16+
if [[ -n "${args[3]:-}" ]]; then
1817
_TOP_LEFT+="${args[2]}${args[3]}"
1918
fi
20-
_TOP_LEFT+="${args[0]}${args[1]}"
21-
if [ -n "${args[4]}" ]; then
19+
_TOP_LEFT+="${args[0]?}${args[1]?}"
20+
if [[ -n "${args[4]:-}" ]]; then
2221
_TOP_LEFT+="${args[2]}${args[4]}"
2322
fi
2423
_TOP_LEFT+=" "
2524
}
2625

27-
____brainy_top_right_parse() {
28-
ifs_old="${IFS}"
29-
IFS="|"
26+
function ____brainy_top_right_parse() {
27+
local ifs_old="${IFS}"
28+
local IFS="|"
3029
read -r -a args <<< "$@"
3130
IFS="${ifs_old}"
3231
_TOP_RIGHT+=" "
33-
if [ -n "${args[3]}" ]; then
32+
if [ -n "${args[3]:-}" ]; then
3433
_TOP_RIGHT+="${args[2]}${args[3]}"
3534
fi
36-
_TOP_RIGHT+="${args[0]}${args[1]}"
37-
if [ -n "${args[4]}" ]; then
35+
_TOP_RIGHT+="${args[0]?}${args[1]?}"
36+
if [ -n "${args[4]:-}" ]; then
3837
_TOP_RIGHT+="${args[2]}${args[4]}"
3938
fi
40-
__TOP_RIGHT_LEN=$((__TOP_RIGHT_LEN + ${#args[1]} + ${#args[3]} + ${#args[4]} + 1))
39+
__TOP_RIGHT_LEN="$((__TOP_RIGHT_LEN + ${#args[1]} + ${#args[3]} + ${#args[4]} + 1))"
4140
((__SEG_AT_RIGHT += 1))
4241
}
4342

44-
____brainy_bottom_parse() {
45-
ifs_old="${IFS}"
46-
IFS="|"
43+
function ____brainy_bottom_parse() {
44+
local ifs_old="${IFS}"
45+
local IFS="|"
4746
read -r -a args <<< "$@"
4847
IFS="${ifs_old}"
49-
_BOTTOM+="${args[0]}${args[1]}"
50-
[ ${#args[1]} -gt 0 ] && _BOTTOM+=" "
48+
_BOTTOM+="${args[0]?}${args[1]?}"
49+
[[ ${#args[1]} -gt 0 ]] && _BOTTOM+=" "
5150
}
5251

53-
____brainy_top() {
52+
function ____brainy_top() {
53+
local info ___cursor_right ___cursor_adjust seg
5454
_TOP_LEFT=""
5555
_TOP_RIGHT=""
5656
__TOP_RIGHT_LEN=0
5757
__SEG_AT_RIGHT=0
5858

5959
for seg in ${___BRAINY_TOP_LEFT}; do
6060
info="$(___brainy_prompt_"${seg}")"
61-
[ -n "${info}" ] && ____brainy_top_left_parse "${info}"
61+
[[ -n "${info}" ]] && ____brainy_top_left_parse "${info}"
6262
done
6363

6464
___cursor_right="\033[500C"
6565
_TOP_LEFT+="${___cursor_right}"
6666

6767
for seg in ${___BRAINY_TOP_RIGHT}; do
6868
info="$(___brainy_prompt_"${seg}")"
69-
[ -n "${info}" ] && ____brainy_top_right_parse "${info}"
69+
[[ -n "${info}" ]] && ____brainy_top_right_parse "${info}"
7070
done
7171

72-
[ $__TOP_RIGHT_LEN -gt 0 ] && __TOP_RIGHT_LEN=$((__TOP_RIGHT_LEN - 1))
72+
[[ ${__TOP_RIGHT_LEN} -gt 0 ]] && __TOP_RIGHT_LEN=$((__TOP_RIGHT_LEN - 1))
7373
___cursor_adjust="\033[${__TOP_RIGHT_LEN}D"
7474
_TOP_LEFT+="${___cursor_adjust}"
7575

7676
printf "%s%s" "${_TOP_LEFT}" "${_TOP_RIGHT}"
7777
}
7878

79-
____brainy_bottom() {
79+
function ____brainy_bottom() {
80+
local info seg
8081
_BOTTOM=""
8182
for seg in $___BRAINY_BOTTOM; do
8283
info="$(___brainy_prompt_"${seg}")"
83-
[ -n "${info}" ] && ____brainy_bottom_parse "${info}"
84+
[[ -n "${info}" ]] && ____brainy_bottom_parse "${info}"
8485
done
8586
printf "\n%s" "${_BOTTOM}"
8687
}
@@ -89,119 +90,119 @@ ____brainy_bottom() {
8990
## Segments ##
9091
##############
9192

92-
___brainy_prompt_user_info() {
93-
color=$bold_blue
94-
if [ "${THEME_SHOW_SUDO}" == "true" ]; then
93+
function ___brainy_prompt_user_info() {
94+
local color="${bold_blue?}"
95+
local box="[|]"
96+
local info="\u@\H"
97+
if [[ "${THEME_SHOW_SUDO:-}" == "true" ]]; then
9598
if sudo -vn 1> /dev/null 2>&1; then
96-
color=$bold_red
99+
color="${bold_red?}"
97100
fi
98101
fi
99-
box="[|]"
100-
info="\u@\H"
101-
if [ -n "${SSH_CLIENT}" ]; then
102-
printf "%s|%s|%s|%s" "${color}" "${info}" "${bold_white}" "${box}"
102+
if [[ -n "${SSH_CLIENT:-}" ]]; then
103+
printf "%s|%s|%s|%s" "${color}" "${info}" "${bold_white?}" "${box}"
103104
else
104105
printf "%s|%s" "${color}" "${info}"
105106
fi
106107
}
107108

108-
___brainy_prompt_dir() {
109-
color=$bold_yellow
110-
box="[|]"
111-
info="\w"
112-
printf "%s|%s|%s|%s" "${color}" "${info}" "${bold_white}" "${box}"
109+
function ___brainy_prompt_dir() {
110+
local color="${bold_yellow?}"
111+
local box="[|]"
112+
local info="\w"
113+
printf "%s|%s|%s|%s" "${color}" "${info}" "${bold_white?}" "${box}"
113114
}
114115

115-
___brainy_prompt_scm() {
116-
[ "${THEME_SHOW_SCM}" != "true" ] && return
117-
color=$bold_green
116+
function ___brainy_prompt_scm() {
117+
[[ "${THEME_SHOW_SCM:-}" != "true" ]] && return
118+
local color="${bold_green?}" box info
118119
box="$(scm_char) "
119120
info="$(scm_prompt_info)"
120-
printf "%s|%s|%s|%s" "${color}" "${info}" "${bold_white}" "${box}"
121+
printf "%s|%s|%s|%s" "${color}" "${info}" "${bold_white?}" "${box}"
121122
}
122123

123-
___brainy_prompt_python() {
124-
[ "${THEME_SHOW_PYTHON}" != "true" ] && return
125-
color=$bold_yellow
126-
box="[|]"
124+
function ___brainy_prompt_python() {
125+
[[ "${THEME_SHOW_PYTHON:-}" != "true" ]] && return
126+
local color="${bold_yellow?}" info
127+
local box="[|]"
127128
info="$(python_version_prompt)"
128-
printf "%s|%s|%s|%s" "${color}" "${info}" "${bold_blue}" "${box}"
129+
printf "%s|%s|%s|%s" "${color}" "${info}" "${bold_blue?}" "${box}"
129130
}
130131

131-
___brainy_prompt_ruby() {
132-
[ "${THEME_SHOW_RUBY}" != "true" ] && return
133-
color=$bold_white
134-
box="[|]"
132+
function ___brainy_prompt_ruby() {
133+
[[ "${THEME_SHOW_RUBY:-}" != "true" ]] && return
134+
local color="${bold_white?}" info
135+
local box="[|]"
135136
info="rb-$(ruby_version_prompt)"
136-
printf "%s|%s|%s|%s" "${color}" "${info}" "${bold_red}" "${box}"
137+
printf "%s|%s|%s|%s" "${color}" "${info}" "${bold_red?}" "${box}"
137138
}
138139

139-
___brainy_prompt_todo() {
140-
[ "${THEME_SHOW_TODO}" != "true" ] \
141-
|| [ -z "$(which todo.sh)" ] && return
142-
color=$bold_white
143-
box="[|]"
140+
function ___brainy_prompt_todo() {
141+
[[ "${THEME_SHOW_TODO:-}" != "true" ||
142+
-z "$(which todo.sh)" ]] && return
143+
local color="${bold_white?}" info
144+
local box="[|]"
144145
info="t:$(todo.sh ls | grep -E "TODO: [0-9]+ of ([0-9]+)" | awk '{ print $4 }')"
145-
printf "%s|%s|%s|%s" "${color}" "${info}" "${bold_green}" "${box}"
146+
printf "%s|%s|%s|%s" "${color}" "${info}" "${bold_green?}" "${box}"
146147
}
147148

148-
___brainy_prompt_clock() {
149-
[ "${THEME_SHOW_CLOCK}" != "true" ] && return
150-
color=$THEME_CLOCK_COLOR
151-
box="[|]"
152-
info="$(date +"${THEME_CLOCK_FORMAT}")"
153-
printf "%s|%s|%s|%s" "${color}" "${info}" "${bold_purple}" "${box}"
149+
function ___brainy_prompt_clock() {
150+
[[ "${THEME_SHOW_CLOCK}" != "true" ]] && return
151+
local color="${THEME_CLOCK_COLOR:-}" info
152+
local box="[|]"
153+
info="$(date +"${THEME_CLOCK_FORMAT?}")"
154+
printf "%s|%s|%s|%s" "${color}" "${info}" "${bold_purple?}" "${box}"
154155
}
155156

156-
___brainy_prompt_battery() {
157+
function ___brainy_prompt_battery() {
157158
! _command_exists battery_percentage \
158-
|| [ "${THEME_SHOW_BATTERY}" != "true" ] \
159-
|| [ "$(battery_percentage)" = "no" ] && return
159+
|| [[ "${THEME_SHOW_BATTERY:-}" != "true" ]] \
160+
|| [[ "$(battery_percentage)" == "no" ]] && return
160161

162+
local color="${bold_green?}" info
161163
info=$(battery_percentage)
162-
color=$bold_green
163164
if [ "$info" -lt 50 ]; then
164-
color=$bold_yellow
165+
color="${bold_yellow?}"
165166
elif [ "$info" -lt 25 ]; then
166-
color=$bold_red
167+
color="${bold_red?}"
167168
fi
168-
box="[|]"
169+
local box="[|]" charging
169170
ac_adapter_connected && charging="+"
170171
ac_adapter_disconnected && charging="-"
171-
info+=$charging
172+
info+="${charging}"
172173
[ "$info" == "100+" ] && info="AC"
173-
printf "%s|%s|%s|%s" "${color}" "${info}" "${bold_white}" "${box}"
174+
printf "%s|%s|%s|%s" "${color}" "${info}" "${bold_white?}" "${box}"
174175
}
175176

176-
___brainy_prompt_exitcode() {
177-
[ "${THEME_SHOW_EXITCODE}" != "true" ] && return
178-
color=$bold_purple
179-
[ "$exitcode" -ne 0 ] && printf "%s|%s" "${color}" "${exitcode}"
177+
function ___brainy_prompt_exitcode() {
178+
[[ "${THEME_SHOW_EXITCODE}" != "true" ]] && return
179+
local color="${bold_purple?}"
180+
[[ "${exitcode?}" -ne 0 ]] && printf "%s|%s" "${color}" "${exitcode}"
180181
}
181182

182-
___brainy_prompt_char() {
183-
color=$bold_white
184-
prompt_char="${__BRAINY_PROMPT_CHAR_PS1}"
183+
function ___brainy_prompt_char() {
184+
local color="${bold_white?}"
185+
local prompt_char="${__BRAINY_PROMPT_CHAR_PS1?}"
185186
printf "%s|%s" "${color}" "${prompt_char}"
186187
}
187188

188189
#########
189190
## cli ##
190191
#########
191192

192-
__brainy_show() {
193-
typeset _seg=${1:-}
193+
function __brainy_show() {
194+
local _seg="${1?}"
194195
shift
195196
export "THEME_SHOW_${_seg}"=true
196197
}
197198

198-
__brainy_hide() {
199-
typeset _seg=${1:-}
199+
function __brainy_hide() {
200+
local _seg="${1?}"
200201
shift
201202
export "THEME_SHOW_${_seg}"=false
202203
}
203204

204-
_brainy_completion() {
205+
function _brainy_completion() {
205206
local cur _action actions segments
206207
COMPREPLY=()
207208
cur="${COMP_WORDS[COMP_CWORD]}"
@@ -221,22 +222,25 @@ _brainy_completion() {
221222
return 0
222223
}
223224

224-
brainy() {
225-
typeset action=${1:-}
225+
function brainy() {
226+
local action="${1?}" func
226227
shift
227-
typeset segs=${*:-}
228-
typeset func
229-
case $action in
228+
local segs=("${@?}")
229+
case "${action}" in
230230
show)
231231
func=__brainy_show
232232
;;
233233
hide)
234234
func=__brainy_hide
235235
;;
236+
*)
237+
_log_error "${FUNCNAME[0]}: unknown action '$action'"
238+
return 1
239+
;;
236240
esac
237-
for seg in ${segs}; do
238-
seg=$(printf "%s" "${seg}" | tr '[:lower:]' '[:upper:]')
239-
$func "${seg}"
241+
for seg in "${segs[@]}"; do
242+
seg="$(printf "%s" "${seg}" | tr '[:lower:]' '[:upper:]')"
243+
"${func}" "${seg}"
240244
done
241245
}
242246

@@ -258,42 +262,42 @@ export RVM_THEME_PROMPT_SUFFIX=""
258262
export VIRTUALENV_THEME_PROMPT_PREFIX=""
259263
export VIRTUALENV_THEME_PROMPT_SUFFIX=""
260264

261-
export SCM_THEME_PROMPT_DIRTY=" ${bold_red}${normal}"
262-
export SCM_THEME_PROMPT_CLEAN=" ${bold_green}${normal}"
265+
export SCM_THEME_PROMPT_DIRTY=" ${bold_red?}${normal?}"
266+
export SCM_THEME_PROMPT_CLEAN=" ${bold_green?}${normal?}"
263267

264-
THEME_SHOW_SUDO=${THEME_SHOW_SUDO:-"true"}
265-
THEME_SHOW_SCM=${THEME_SHOW_SCM:-"true"}
266-
THEME_SHOW_RUBY=${THEME_SHOW_RUBY:-"false"}
267-
THEME_SHOW_PYTHON=${THEME_SHOW_PYTHON:-"false"}
268-
THEME_SHOW_CLOCK=${THEME_SHOW_CLOCK:-"true"}
269-
THEME_SHOW_TODO=${THEME_SHOW_TODO:-"false"}
270-
THEME_SHOW_BATTERY=${THEME_SHOW_BATTERY:-"false"}
271-
THEME_SHOW_EXITCODE=${THEME_SHOW_EXITCODE:-"true"}
268+
: "${THEME_SHOW_SUDO:="true"}"
269+
: "${THEME_SHOW_SCM:="true"}"
270+
: "${THEME_SHOW_RUBY:="false"}"
271+
: "${THEME_SHOW_PYTHON:="false"}"
272+
: "${THEME_SHOW_CLOCK:="true"}"
273+
: "${THEME_SHOW_TODO:="false"}"
274+
: "${THEME_SHOW_BATTERY:="false"}"
275+
: "${THEME_SHOW_EXITCODE:="true"}"
272276

273-
THEME_CLOCK_COLOR=${THEME_CLOCK_COLOR:-"$bold_white"}
274-
THEME_CLOCK_FORMAT=${THEME_CLOCK_FORMAT:-"%H:%M:%S"}
277+
: "${THEME_CLOCK_COLOR:="${bold_white?}"}"
278+
: "${THEME_CLOCK_FORMAT:="%H:%M:%S"}"
275279

276-
__BRAINY_PROMPT_CHAR_PS1=${THEME_PROMPT_CHAR_PS1:-">"}
277-
__BRAINY_PROMPT_CHAR_PS2=${THEME_PROMPT_CHAR_PS2:-"\\"}
280+
__BRAINY_PROMPT_CHAR_PS1="${THEME_PROMPT_CHAR_PS1:=">"}"
281+
__BRAINY_PROMPT_CHAR_PS2="${THEME_PROMPT_CHAR_PS2:="\\"}"
278282

279-
___BRAINY_TOP_LEFT=${___BRAINY_TOP_LEFT:-"user_info dir scm"}
280-
___BRAINY_TOP_RIGHT=${___BRAINY_TOP_RIGHT:-"python ruby todo clock battery"}
281-
___BRAINY_BOTTOM=${___BRAINY_BOTTOM:-"exitcode char"}
283+
: "${___BRAINY_TOP_LEFT:="user_info dir scm"}"
284+
: "${___BRAINY_TOP_RIGHT:="python ruby todo clock battery"}"
285+
: "${___BRAINY_BOTTOM:="exitcode char"}"
282286

283287
############
284288
## Prompt ##
285289
############
286290

287-
__brainy_ps1() {
288-
printf "%s%s%s" "$(____brainy_top)" "$(____brainy_bottom)" "${normal}"
291+
function __brainy_ps1() {
292+
printf "%s%s%s" "$(____brainy_top)" "$(____brainy_bottom)" "${normal?}"
289293
}
290294

291-
__brainy_ps2() {
295+
function __brainy_ps2() {
292296
color=$bold_white
293-
printf "%s%s%s" "${color}" "${__BRAINY_PROMPT_CHAR_PS2} " "${normal}"
297+
printf "%s%s%s" "${color}" "${__BRAINY_PROMPT_CHAR_PS2?} " "${normal?}"
294298
}
295299

296-
_brainy_prompt() {
300+
function _brainy_prompt() {
297301
exitcode="$?"
298302

299303
PS1="$(__brainy_ps1)"

0 commit comments

Comments
 (0)