-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.sh
More file actions
688 lines (567 loc) · 19.4 KB
/
common.sh
File metadata and controls
688 lines (567 loc) · 19.4 KB
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
#!/bin/bash
# grbl common functions
# collection of functions be sourced every time module needs it
# casa@ujo.guru 2019 - 2026
__config=$(readlink --canonicalize --no-newline $BASH_SOURCE)
## Add common variables here
export GRBL_FORMAT_TIMESTAMP='%Y%m%d%H%M%S'
export GRBL_FORMAT_LOGSTAMP='%Y%m%d_%H%M%S'
export GRBL_FORMAT_NICE='%-d.%-m.%Y %H:%M:%S'
export GRBL_FORMAT_STAMP='%Y-%m-%d %H:%M'
export GRBL_FORMAT_DATE='%-d.%-m.%Y'
export GRBL_FORMAT_TIME='%H:%M:%S'
export GRBL_FORMAT_FILE_DATE='%Y%m%d'
export GRBL_FORMAT_FILE_TIME='%H%M%S'
export GRBL_SOUND_ENABLED=
export GRBL_SPEECH_ENABLED=
## Common fucntions
gr.ts () {
# returns current timestamp in different formats
local _timestamp
case $1 in
epoch|-e) _timestamp="$(date -d now +"%s")" ;;
file|-f) _timestamp="$(date -d now +$GRBL_FORMAT_FILE_DATE-$GRBL_FORMAT_FILE_TIME)" ;;
human|-h) _timestamp=$(date -d now +"$GRBL_FORMAT_DATE $GRBL_FORMAT_TIME") ;;
nice|-n) _timestamp=$(date -d now +"$GRBL_FORMAT_NICE") ;;
*) _timestamp="$(date -d now +$GRBL_FORMAT_TIMESTAMP)" ;;
esac
printf "$_timestamp"
}
gr.msg () {
# function for output messages and make log notifications
# options
local _verbose_trigger=0
local _verbose_limiter=5 # maximum + 1 verbose level
local _newline="\n" # newline is on by default
local _pre_newline= # newline before text disable by default
local _timestamp=
local _datestamp= # timestamp is disabled by default
local _message= # message container
local _logging= # logging is disabled by default
local _say= # speak
local _color=
local _color_code= # default color if none
local _exit= # exit with code (exit not return!)
local _mqtt_topic=
local _indicator_key=
local _color_only=
local _c_var=
local _column_width=
local _debug=
local _error=
local _return=
local _function=
# shopt -s extdebug; declare -Ff quote; shopt -u extdebug
# https://askubuntu.com/questions/1146269/how-can-i-find-where-certain-bash-function-is-defined
# parse flags
TEMP=`getopt --long -o "tlsrnhNpde:x:H:w:V:v:c:C:q:k:m:" "$@"`
eval set -- "$TEMP"
while true ; do
case "$1" in
#-r ) _repeat="$2 " ; shift ;;
-t ) _timestamp="$(date +$GRBL_FORMAT_TIME) " ; shift ;;
-s ) _say=true ; shift ;;
-h ) _color_code="$C_HEADER" ; shift ;;
-n ) _newline= ; shift ;;
-p ) _newline="\n\n" ; shift ;;
-r ) _newline= ;_pre_newline="\r" ; shift ;;
-N ) _pre_newline="\n" ; shift ;;
-x ) _exit=$2 ; shift 2 ;;
-H ) _hihglite_word=$2 ; shift 2 ;; # TODO
-w ) _column_width=$2 ; shift 2 ;;
-V ) _verbose_limiter=$2 ; shift 2 ;;
-v ) _verbose_trigger=$2 ; shift 2 ;;
-m ) _mqtt_topic="$2" ; shift 2 ;;
-q ) _mqtt_topic="$(hostname)/$2" ; shift 2 ;;
-k ) _indicator_key=$2 ; shift 2 ;;
-l ) _logging=true
shift
;;
-d ) _debug=true
shift
;;
-e ) _error=$2
[[ $GRBL_VERBOSE -ge 2 ]] && logging=true
shift 2
;;
-C ) _color=$2
_color_only=true
_c_var="C_${_color^^}"
_color_code=${!_c_var}
shift 2 ;;
-c ) _color=$2
_c_var="C_${_color^^}"
_color_code=${!_c_var}
shift 2 ;;
* ) break
esac
done
# --) check message for long parameters (don't remember why like this)
local _arg="$@"
[[ "$_arg" != "--" ]] && _message="${_arg#* }"
# -C) print only color code
if [[ $_color_only ]] ; then
echo -n "$_color_code"
return 0
fi
if [[ $_logging ]]; then
_datestamp=$(date +$GRBL_FORMAT_LOGSTAMP)
_function=$(caller 0 | awk '{print $2}')
[[ -d ${GRBL_LOG%/*} ]] && printf '%s %s: %s\n' $_datestamp $_function "$_message" >> $GRBL_LOG
fi
# -d) debug messages to stderr
if [[ $_debug ]] ; then
_c_var="C_FUCHSIA"
_color_code=${!_c_var}
_function=$(caller 0 | awk '{print $2}')
>&2 printf "$_function $_color_code%s\033[0m" "${_message}"
# bash --debugger
# declare -Ff quote
# nl -ba /usr/share/bash-completion/bash_completion | sed -n 143,150p
return 0
fi
if [[ $_verbose_trigger -gt $GRBL_VERBOSE ]]; then
return 0
fi
# -v) given verbose level is lower than trigger level, do not print
# "print only if higher verbose level than this"
if [[ $_verbose_trigger -gt $GRBL_VERBOSE ]]; then
return 0
fi
# -V) given verbose level is higher than high limiter, do not print
# "do not print after this verbose level"
if [[ $_verbose_limiter -le $GRBL_VERBOSE ]]; then
return 0
fi
# -x) add exit code to message
if [[ $_exit -gt 0 ]]; then
_message="$_exit: $_message"
_datestamp=$(date +$GRBL_FORMAT_LOGSTAMP)
_function=$(caller 0 | awk '{print $2}')
printf '%s warning early exit (%s)' $_datestamp $_function $_exit >> $GRBL_LOG
fi
# -k) set corsair key is '-k <key>' used
if [[ $_indicator_key ]] && [[ $GRBL_CORSAIR_ENABLED ]]; then
source corsair.sh
if [[ "$_color" == "reset" ]] ; then
corsair.main reset "$_indicator_key"
else
corsair.main set "$_indicator_key" "$_color"
fi
[[ $_message ]] || return 0
fi
# -m) publish to mqtt if '-q|-m <topic>' used
if [[ $_mqtt_topic ]] && [[ $GRBL_MQTT_ENABLED ]]; then
source mqtt.sh
# mqtt.enabled || return 0
mqtt.pub "$_mqtt_topic" "$_message"
return $?
fi
# -w) fill message length to column limiter
if ! [[ $_column_width ]] ; then
_column_width=${#_message}
fi
_message_length=$_column_width
# [[ ${#_column_width} -gt 1 ]] && local _message_length=$(( $_column_width - 1))
# -e) error messages to stderr
if [[ $_error ]] ; then
# get caller function name
_function=$(caller 0 | awk '{print $2}')
[[ ${_function} ]] && printf "%s:" "${_function} "
if [[ $GRBL_COLOR ]] && [[ $GRBL_VERBOSE -gt 0 ]]; then
case $_error in
0) _c_var="C_WHITE" ;;
1) _c_var="C_YELLOW" ;;
2) _c_var="C_ORANGE" ;;
3) _c_var="C_RED" ;;
4) _c_var="C_FUCHSIA" ;;
""|*) _c_var="C_YELLOW" ; _message="undefined error" ; _column_width=${#_message}
;;
esac
_color_code=${!_c_var}
printf "$_color_code%-${_column_width}s$_newline\033[0m$_return" "${_message} " >&2
else
printf "%s: %-${_column_width}s$_newline$_return" "${_function}" "${_message:0:$_message_length}" >&2
fi
return 0
fi
# -c) color printout
if [[ $GRBL_COLOR ]] && [[ $GRBL_VERBOSE -gt 0 ]]; then
printf "$_pre_newline$_color_code%s%-${_column_width}s$_newline\033[0m$_return" "${_timestamp}" "${_message:0:$_message_length}"
else
# *) normal printout without formatting
printf "$_pre_newline%s%-${_column_width}s$_newline$_return" "${_timestamp}" "${_message:0:$_message_length}"
fi
if [[ $_say ]] && [[ $GRBL_SOUND_ENABLED ]] && [[ $GRBL_SPEECH_ENABLED ]] || [[ $GRBL_SPEAK ]]; then
# remove colors and stuff
local say_message=$(echo ${_message[@]} | sed $'s/\e\\[[0-9;:]*[a-zA-Z]//g' )
# remove options
say_message=${say_message//-}
# add pauses if more spaces than two.. not working
#say_message="$(echo $say_message | sed -r 's/ {2,}/, /g')"
#[[ $GRBL_VERBOSE -gt 0 ]] && printf "%s\n" "$_message"
#-p $GRBL_SPEECH_PITCH -s $GRBL_SPEECH_SPEED -v $GRBL_SPEECH_LANG "$_message"
speech
[[ $GRBL_DEBUG ]] || espeak -p $GRBL_SPEECH_PITCH -s $GRBL_SPEECH_SPEED -v $GRBL_SPEECH_LANG "${say_message//-}"
#return 0
fi
# -x) printout and exit for development use
[[ $_exit ]] && exit $_exit
return 0
}
gr.ask () {
# yes or no with y and n bling on keyboard
local _answer=
local _def_answer='n'
local _ano_answer='y'
local _options=
local _timeout=
local _read_it=
local _message=
local _box=
local _comment=
local _comment_box=
local _highlight=
local _highlight_word=
# parse arguments
TEMP=`getopt --long -o "chst:d:H:" "$@"`
eval set -- "$TEMP"
while true ; do
case "$1" in
-s) # speak question out
_read_it=true
shift
;;
-h) # highlight all
_highlight=true
shift
;;
-H) # highlight word
_highlight_word=$2
shift 2
;;
-t)
_timeout=$2
_options="-t $_timeout"
shift 2
;;
-c) # ask comment
_comment=true
shift
;;
-d)
_def_answer=$2
case $_def_answer in
y*|Y*) _ano_answer='n' ;;
n*|N*|*) _ano_answer='y' ;;
esac
_answer=$2
shift 2
;;
*)
break
esac
done
local _arg="$@"
[[ "$_arg" != "--" ]] && _message="${_arg#* }"
# format timeout box
if [[ $_timeout ]] ; then
_message="$_message ($_timeout sec timeout)"
fi
# make y and n blink on keyboard
if [[ $GRBL_CORSAIR_ENABLED ]] ; then
source corsair.sh
corsair.indicate yes y 2>/dev/null >/dev/null
sleep 0.75
corsair.indicate no n 2>/dev/null >/dev/null
fi
# format [Y/n]: box
if [[ $_comment ]]; then
_comment_box='/c'
fi
case $_def_answer in
y) _box="[${_def_answer^^}/${_ano_answer,,}$_comment_box]: " ;;
n) _box="[${_ano_answer,,}/${_def_answer^^}$_comment_box]: " ;;
esac
# speak
[[ $_read_it ]] && espeak "$_message" #& >/dev/null
# colorize message
if [[ $GRBL_COLOR ]]; then
# highlight whole question
if [[ $_highlight ]]; then
_message="$(gr.msg -n -c cyan $_message)"
# user specified hihglight
elif [[ $_highlight_word ]]; then
read -ra _words <<< "$_message"
_message=""
for (( i = 0; i < ${#_words[@]}; i++ )); do
# allows word to contain ONE special character after it
if [[ ${_words[$i]} =~ ^${_highlight_word}[[:punct:]]?$ ]]; then
_message+="$(gr.msg -n -c cyan ${_words[$i]}) "
else
_message+="$(gr.msg -n -c dark_cyan ${_words[$i]}) "
fi
done
# Normal
else
_message="$(gr.msg -n -c dark_cyan $_message)"
fi
fi
# clear keyboard buffer
while read -e -t 0.1; do : ; done
# ask the question
if [[ $GRBL_FORCE ]] ; then
_answer=y
else
read $_options -n 1 -p "${_message% } $_box" _answer
echo
fi
## commenting feature used used by testing module
# if [[ $_answer == c ]]; then
# read -p "comment: " _comment_ans
# gr.msg -v3 -c white "$_comment_ans"
# echo $_comment_ans >/tmp/$USER/ask.comment
# fi
# fulfill default answer
_answer="${_answer:-$_def_answer}"
# stop blinking the keys
if [[ $GRBL_CORSAIR_ENABLED ]] ; then
corsair.blink_stop y >/dev/null 2>/dev/null
corsair.blink_stop n >/dev/null 2>/dev/null
fi
# return for callers if statement
case ${_answer^^} in Y)
[[ $_read_it ]] && espeak "yes" #& >/dev/null
return 0
esac
[[ $_read_it ]] && espeak "no" #& >/dev/null
return 1
}
# TBD rename following three functions
gr.kv() {
# print key value pair
local variable="$1"; shift
IFS=
local value=$@ ; shift
local column=20
local colors=(light_blue aqua_marine deep_pink)
[[ $variable ]] || variable='NaN'
[[ $value ]] || value='NaN'
# cut and add '..'
if [[ $(expr length $variable) -ge $((column)) ]]; then
[[ $column -gt 6 ]] \
&& variable="$(head -c$((column - 3)) <<<$variable).." \
|| variable="$(head -c$((column - 1)) <<<$variable)"
fi
gr.msg -c ${colors[0]} -w$column -n "$variable"
[[ "$value" == *' '* ]] \
&& gr.msg -c ${colors[1]} "'$value'" \
|| gr.msg -c ${colors[1]} "$value"
}
gr.kvt () {
# print key value pair list
input=($@)
for (( i = 0; i < ${#input[@]}; i++ )); do
gr.kv "${input[$i]}" "${input[$(($i+1))]}"
i=$(( i + 1 ))
done
}
gr.varlist(){
# print key value pairs based list of variable names
local input=($@)
local _i=0
# if first argument is debug, print list only if debu mode is on
if [[ ${input%% *} == "debug" ]]; then
[[ $GRBL_DEBUG ]] || return 0
_i=1
fi
for (( i = $_i ; i < ${#input[@]} ; i++ )); do
gr.msg -n -c light_blue "${input[$i]}" >&2
gr.msg -n -c white " = " >&2
gr.msg -c aqua_marine "'$(eval echo '$'${input[$i]})'" >&2
done
}
gr.kvp () {
# compatibility alias
gr.kvp $@
}
gr.installed () {
# check is module installed
local i=0
local _to_find=$1
while [[ "$i" -lt "${#GRBL_MODULES[@]}" ]] ; do
if [[ "${GRBL_MODULES[$i]}" == "$_to_find" ]] ; then
return 0
fi
((i++))
done
return 100
}
gr.contain () {
# check item is in list
# shopt -s nocasematch # Can be useful to disable case-matching
local e
for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done
return 1
}
gr.debug () {
# printout debug messages
if [[ -z $GRBL_DEBUG ]] || [[ -z $1 ]]; then return 0; fi
local _c_var="C_FUCHSIA"
local _color_code=${!_c_var}
local _function=$(caller 0 | awk '{print $2}')
printf "$_function $_color_code%s\033[0m\n" "$@" >&2
}
gr.colors () {
# printout available colors
export GRBL_COLOR=true
export GRBL_VERBOSE=2
case $1 in
"")
for color in ${GRBL_COLOR_LIST[@]} ; do
gr.msg -n -c $color "$color "
done
;;
*)
for color in ${GRBL_COLOR_LIST[@]} ; do
gr.msg -n -c $color "$1"
done
;;
esac
echo
}
gr.install() {
# install requirements, -p package manager apt|apt-get|pip|flatpack|snap
local check=
local check_only=
local package_manager=apt
local install_required=
TEMP=`getopt --longoptions -o "fscp:" $@`
eval set -- "$TEMP"
while true ; do
case "$1" in
-p) # select package manager
package_manager=$2
shift 2
;;
-s) # be silent
GRBL_VERBOSE=0
shift
;;
-f) # do not ask every package
GRBL_FORCE=true
shift
;;
-c) # check only
check_only=true
shift
;;
*) break
esac
done
local required=
local left_overs="$@"
if [[ "$left_overs" != "--" ]] ; then
required="${left_overs#* }"
fi
if [[ ${#required[@]} -lt 1 ]]; then
gr.msg -e1 "required list empty"
return 0
fi
## 0) Install required package manager
case $package_manager in
pipx)
if ! apt list -i pipx 2>/dev/null >/dev/null | grep installed ; then
gr.ask "install pipx package manager?" || return 12
sudo apt install pipx
pipx ensurepath
#sudo pipx ensurepath --global # optional to allow pipx actions with --global argument
fi
;;
pip3|pip)
if ! apt list -i pip3 2>/dev/null >/dev/null | grep installed ; then
gr.ask "install pip3 package manager?" || return 12
sudo apt install python3-pip
fi
;;
esac
## 1) Check package
gr.msg -v2 -n "checking requirements.. "
for install in ${required[@]} ; do
case $package_manager in
apt|apt-get)
if apt list -i $install 2>/dev/null| grep -q installed; then
gr.msg -v2 -n -c green "$install "
else
gr.msg -v2 -n -c red "$install "
install_required=true
fi
;;
pip|pip3)
if $package_manager list 2>/dev/null | grep -q $install; then
gr.msg -v2 -n -c green "$install "
else
gr.msg -v2 -n -c red "$install "
install_required=true
fi
;;
flat*)
gr.msg "flatpack TBD"
return 100
;;
snap*)
gr.msg "snap not supported"
return 100
;;
esac
done
## Inform, exit or continue
if [[ -z $install_required ]] ; then
gr.msg -v2 -c green "fulfilled"
return 0
else
if [[ $check_only ]] ; then
gr.msg -v2 -N -c yellow "install required"
return 10
fi
echo
if gr.ask "install requirements?" ; then
GRBL_FORCE=true
else
gr.msg -v2 -c yellow "some packages will be missed"
return 11
fi
fi
## 2) install package
case $package_manager in
apt|apt-get)
gr.msg "sudo privileges needed to install"
sudo apt-get update
for install in ${apt_require[@]} ; do
apt list -i $install 2>/dev/null >/dev/null | grep installed && continue
gr.ask -h "install $install?" || continue
sudo $package_manager -y install $install
done
;;
pip|pip3)
pip3 install --upgrade pip
for install in ${apt_require[@]} ; do
$package_manager list 2>/dev/null | grep -q $install && continue
gr.ask -h "install $install?" || continue
$package_manager -y install $install
done
;;
flat*)
gr.msg "flatpack TBD"
return 100
;;
snap*)
gr.msg "snap not supported"
return 100
;;
esac
return 0
}
export -f gr.msg
export -f gr.ask
export -f gr.debug