-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminiprompt
executable file
·398 lines (357 loc) · 10.6 KB
/
miniprompt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#!/bin/bash
# shellcheck disable=2154
# shellcheck disable=2034
# --> metadata
config="$([ -z "${XDG_CONFIG_HOME}" ] && echo "${HOME}/.config" || echo "${XDG_CONFIG_HOME}")/miniprompt/conf.toml"
# clrs
declare -A clrs=(
["spc"]=' '
["nc"]='\[\033[0m\]'
# normal
["gray"]='\[\033[0;30m\]'
["red"]='\[\033[0;31m\]'
["green"]='\[\033[0;32m\]'
["blue"]='\[\033[0;34m\]'
["cyan"]='\[\033[0;36m\]'
["yellow"]='\[\033[0;33m\]'
["magenta"]='\[\033[0;35m\]'
["white"]='\[\033[0;37m\]'
# bright
["gray_br"]='\[\033[1;30m\]'
["red_br"]='\[\033[1;31m\]'
["green_br"]='\[\033[1;32m\]'
["blue_br"]='\[\033[1;34m\]'
["cyan_br"]='\[\033[1;36m\]'
["yellow_br"]='\[\033[1;33m\]'
["magenta_br"]='\[\033[1;35m\]'
["white_br"]='\[\033[1;37m\]'
# dim
["gray_dm"]='\[\033[2;30m\]'
["red_dm"]='\[\033[2;31m\]'
["green_dm"]='\[\033[2;32m\]'
["blue_dm"]='\[\033[2;34m\]'
["cyan_dm"]='\[\033[2;36m\]'
["yellow_dm"]='\[\033[2;33m\]'
["magenta_dm"]='\[\033[2;35m\]'
["white_dm"]='\[\033[2;37m\]'
# italic
["gray_it"]='\[\033[3;30m\]'
["red_it"]='\[\033[3;31m\]'
["green_it"]='\[\033[3;32m\]'
["blue_it"]='\[\033[3;34m\]'
["cyan_it"]='\[\033[3;36m\]'
["yellow_it"]='\[\033[3;33m\]'
["magenta_it"]='\[\033[3;35m\]'
["white_it"]='\[\033[3;37m\]'
# underlined
["gray_un"]='\[\033[4;30m\]'
["red_un"]='\[\033[4;31m\]'
["green_un"]='\[\033[4;32m\]'
["blue_un"]='\[\033[4;34m\]'
["cyan_un"]='\[\033[4;36m\]'
["yellow_un"]='\[\033[4;33m\]'
["magenta_un"]='\[\033[4;35m\]'
["white_un"]='\[\033[4;37m\]'
# blinking
["gray_bl"]='\[\033[5;30m\]'
["red_bl"]='\[\033[5;31m\]'
["green_bl"]='\[\033[5;32m\]'
["blue_bl"]='\[\033[5;34m\]'
["cyan_bl"]='\[\033[5;36m\]'
["yellow_bl"]='\[\033[5;33m\]'
["magenta_bl"]='\[\033[5;35m\]'
["white_bl"]='\[\033[5;37m\]'
# reversed
["gray_re"]='\[\033[7;30m\]'
["red_re"]='\[\033[7;31m\]'
["green_re"]='\[\033[7;32m\]'
["blue_re"]='\[\033[7;34m\]'
["cyan_re"]='\[\033[7;36m\]'
["yellow_re"]='\[\033[7;33m\]'
["magenta_re"]='\[\033[7;35m\]'
["white_re"]='\[\033[7;37m\]'
)
declare -A final_extensions=(
["git"]=''
["ssh"]=''
["package"]=''
["ip"]=''
["wifi"]=''
["kube"]=''
)
# help
script_help=$(
cat <<EOF
MiniPrompt is a minimal and versatile prompt suitable for any type of machine.
You can also use it to toggle extensions on and off 'on the fly'!
Args:
gt, git handle the git extension
sh, ssh handle the ssh extension
pk, package handle the package extension
ip handle the ip extension
wf, wifi handle the wifi extension
kb, kube, kubernetes handle the kubernetes extensions
Modifiers for the args:
t, toggle Toggle extension on and off
e, enable Enable extension
d, disable Disable extension
Flags:
-h,--help See this help message.
EOF
)
function load_defualt_settings() {
# behaviour
colored_exit_status="true"
dirtrim=2
enhance_utocompletion="true"
enhanced_history_format="true"
dircolors="true"
# primary_prompt
prefix="\[\e]0;\w\a\]"
ps_content="</yellow>\w</nc> "
ps_suffix="</green>➜</nc> "
normal_color="nc"
error_symbol="✗"
failed="red"
# extensions
ext_git="false"
ext_ssh="false"
ext_package="false"
ext_kubernetes="false"
ext_ip="false"
ext_wifi="false"
git_struct="</cyan> </unit> "
ssh_struct="</magenta> </unit> "
package_struct="</white> v</unit> "
ip_struct="</gray_br> </unit> "
wifi_struct="</blue>直 </unit> "
hierarchy="ssh, git, ip, wifi, package"
}
function load_config() {
load_defualt_settings
[[ -f $1 ]] || {
return 1
}
entry_regex="^[[:blank:]]*([[:alpha:]_][[:alnum:]_-]*)[[:blank:]]*=[[:blank:]]*('[^']+'|\"[^\"]+\"|[^#[:blank:]]+)[[:blank:]]*(#.*)*$"
while read -r line; do
[[ -n $line ]] || continue
[[ $line =~ $entry_regex ]] || continue
key=${BASH_REMATCH[1]}
value=${BASH_REMATCH[2]#[\'\"]} # strip quotes
value=${value%[\'\"]}
declare -g "${key}"="${value}"
done <"$1"
}
function export_ps1_vars() {
export PS1_previous_exit
export PS1_prefix
export PS1_content
export PS1_suffix
}
function enhance_autocompletion() {
if [[ "$enhanced_autocompletion" == "true" ]]; then
bind 'set colored-stats on'
bind 'set colored-completion-prefix on'
bind 'set completion-ignore-case on'
bind 'set completion-map-case on'
bind 'set expand-tilde on'
bind 'set mark-directories on'
bind 'set mark-symlinked-directories on'
bind 'set show-all-if-ambiguous on'
bind 'set show-all-if-unmodified on'
bind 'set skip-completed-text on'
shopt -s 'cdspell'
shopt -s 'checkwinsize'
shopt -s 'dirspell'
fi
}
function enhance_history_format() {
if [[ "$enhanced_history_format" == "true" ]]; then
export HISTCONTROL='ignoreboth:erasedups'
export HISTTIMEFORMAT='[%Y-%m-%d %T] '
shopt -s 'histappend'
fi
}
function load_dircolors() {
if [[ "$dircolors" == "true" ]]; then
if [ -x "$(command -v dircolors)" ]; then
if [ -r "$HOME/.dircolors" ]; then
eval "$(dircolors -b "$HOME/.dircolors")"
else
eval "$(dircolors -b)"
fi
fi
fi
}
function reset_prompt() {
PS1_previous_exit="$?"
PS1_prefix="${prefix}" # window title
for clr in "${!clrs[@]}"; do
ps_content=${ps_content//"</${clr}>"/"${clrs[$clr]}"}
ps_suffix=${ps_suffix//"</${clr}>"/"${clrs[$clr]}"}
done
PS1_content="${ps_content}"
PS1_suffix="${ps_suffix}${clrs[$normal_color]}"
}
function clean_variables() {
unset PS1_prefix \
PS1_content \
PS1_suffix \
PS1_previous_exit
}
function assert_extension() {
name=$1
enabled=$2
if [[ "$enabled" == "true" ]]; then
unit=""
case "$name" in
"git")
bin_cmd="$(command -v ${name})"
if [[ "$($bin_cmd rev-parse --is-inside-work-tree 2>/dev/null)" = 'true' ]]; then
unit=$($bin_cmd branch --show-current)
fi
;;
"package")
if [[ -f "package.json" ]]; then # npm
unit=$(sed 's/.*"version": "\(.*\)".*/\1/;t;d' ./package.json)
fi
;;
"ssh")
if [[ -n "$SSH_CONNECTION" ]]; then
unit="\u@\h"
fi
;;
"ip")
IFACE=$(ip -4 route | grep default | head -n1 | awk '{print $5}')
if [ ! -z "${IFACE}" ]; then
unit=$(ip -4 -o addr show scope global "${IFACE}" | awk '{gsub(/\/.*/, "",$4); print $4}' | paste -s -d "")
fi
;;
"wifi")
unit=$(nmcli -t -f name connection show --active)
;;
"kube")
bin_cmd="$(command -v ${name})"
local context
local namespace
context="$(${bin_cmd} config view -o=jsonpath='{.current-context}')"
namespace="$(${bin_cmd} config view -o=jsonpath="{.contexts[?(@.name==\"${context}\")].context.namespace}")"
PS1_content="${PS1_content:-} ${kube_symbol}\[\e[34m\]${context}${namespace:+:$namespace}\[\e[0m\]"
;;
esac
if ! [ -z "${unit}" ]; then
val="${name}_struct"
parsed_struct="${!val}"
for clr in "${!clrs[@]}"; do parsed_struct=${parsed_struct//"</${clr}>"/"${clrs[$clr]}"}; done
final_extensions[${name}]="${parsed_struct//"</unit>"/"${unit}"}"
else
final_extensions[${name}]=""
fi
fi
}
function turn_ext() {
task=$1
var=$2
if [[ "${task}" == "e" ]] || [[ "${task}" == "enable" ]]; then
echo "true"
elif [[ "${task}" == "d" ]] || [[ "${task}" == "disable" ]]; then
echo "false"
elif [[ "${task}" == "t" ]] || [[ "${task}" == "toggle" ]]; then
if [[ "${var}" == "false" ]]; then echo "true"; fi
echo "false"
fi
}
function on_da_fly() {
extension=$1
task=$2
case "$extension" in
"gt" | "git")
var="ext_git"
export ${var}="$(turn_ext "${task}" "${var}")"
;;
"sh" | "ssh")
var="ext_ssh"
export ${var}="$(turn_ext "${task}" "${var}")"
;;
"pk" | "package")
var="ext_package"
export ${var}="$(turn_ext "${task}" "${var}")"
;;
"ip")
var="ext_ip"
export ${var}="$(turn_ext "${task}" "${var}")"
;;
"wf" | "wifi")
var="ext_wifi"
export ${var}="$(turn_ext "${task}" "${var}")"
;;
"kp" | "kube" | "kubernetes")
var="ext_kubernetes"
export ${var}="$(turn_ext "${task}" "${var}")"
;;
"-h" | "--help")
echo "$script_help"
# exit 0 # causes shell to close
;;
*)
echo "Error: command was not recognized"
;;
esac
}
function pre_prompt() {
export_ps1_vars
IFS=', ' read -r -a order <<<"$hierarchy"
for name in "${!final_extensions[@]}"; do # append arbitrarily the missing extensions
if ! [[ ${order[*]} =~ ${name} ]]; then
order[${#order[@]}]="${name}"
fi
done
PROMPT_DIRTRIM=${dirtrim}
}
function append_extensions() {
prompt_extensions=""
for ((i = 0; i < ${#order[@]}; i++)); do
if ! [ -z "${final_extensions[${order[$i]}]}" ]; then
prompt_extensions="${prompt_extensions}${final_extensions[${order[$i]}]}"
fi
done
PS1_content="${PS1_content:-}${prompt_extensions}${clrs[$normal_color]}"
}
function assert_extensions() {
assert_extension "git" "${ext_git}"
assert_extension "ssh" "${ext_ssh}"
assert_extension "package" "${ext_package}"
assert_extension "ip" "${ext_ip}"
assert_extension "wifi" "${ext_wifi}"
# assert_extension "kubectl" "${ext_kubernetes}"
}
function make_behaviour() {
if [[ "$colored_exit_status" == "true" ]]; then
[ "$PS1_previous_exit" -ne 0 ] && PS1_suffix="${clrs[$failed]}${error_symbol} ${clrs[$normal_color]}"
fi
}
function make_prompt() {
reset_prompt
make_behaviour
assert_extensions
append_extensions
export PS1="${PS1_prefix:-}${PS1_content:-}${PS1_suffix:-}"
history -a
clean_variables
}
function post_prompt() {
enhance_autocompletion
enhance_history_format
load_dircolors
}
main() {
if [[ ! "$#" > 0 ]]; then
load_config "${config}"
pre_prompt
export PROMPT_COMMAND="make_prompt;$PROMPT_COMMAND"
post_prompt
else
on_da_fly "${1}" "${2}"
fi
}
main "$@"