-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherr
79 lines (75 loc) · 1.7 KB
/
err
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
#! .desc:
# Print formatted text to stderr
#! .params:
# <$1> - color(
# '-' - none
# '-bk' - black
# '-r' - red
# '-g' - green
# '-y' - yellow
# '-be' - blue
# '-m' - magenta
# '-c' - cyan
# '-w' - white
# .
# )
# <$2> - format(
# '-' - raw
# '--' - raw; no <newline>
# '-date' - `date` %H%M%S
# '-date-' - `date` %H%M%S; no <newline>
# .
# )
# <"$3">+ - text
#! .uses.var:
# [NO_COLOR] $ - environment variable;
# disables colored output
#! .rc:
# (0) success
# (255) bad input
#! .caveats:
# > `ARG_MAX`.
# > `INT_MAX`.
#.
err() {
# Check if color codes should be used.
if [ "$NO_COLOR" ]; then
_reset=
_color=
else
_reset='\033[0m'
case $1 in
'-') _color=; _reset= ;;
'-bk') _color='\033[1;30m' ;;
'-r') _color='\033[1;31m' ;;
'-g') _color='\033[1;32m' ;;
'-y') _color='\033[1;33m' ;;
'-be') _color='\033[1;34m' ;;
'-m') _color='\033[1;35m' ;;
'-c') _color='\033[1;36m' ;;
'-w') _color='\033[1;37m' ;;
*) return 255 ;;
esac
fi
_format="$2"; shift 2
case "$_format" in
'-')
printf "%b%s%b\n" "$_color" "$*" "$_reset" >&2
;;
'--')
printf "%b%s%b" "$_color" "$*" "$_reset" >&2
;;
'-date')
printf "%b[%s] =>>: %s%b\n" "$_color" "$(date '+%H:%M:%S')" "$*" \
"$_reset" >&2
;;
'-date-')
printf "%b[%s] =>>: %s%b" "$_color" "$(date '+%H:%M:%S')" "$*" \
"$_reset" >&2
;;
*)
return 255
;;
esac
return 0
}