|
9 | 9 | # file distributed with this source code.
|
10 | 10 | ##
|
11 | 11 |
|
12 |
| -function _bright_resolve() |
| 12 | +function _bright_error() |
| 13 | +{ |
| 14 | + local mesg="[${1:-unset_function}] ${2:-unspecified exception}" |
| 15 | + local args |
| 16 | + |
| 17 | + while [ $# != 2 ] && [ "$3" != false ]; do |
| 18 | + args+="\"$3\"," |
| 19 | + shift |
| 20 | + done |
| 21 | + |
| 22 | + [ "$args" ] && mesg+=" (got ${args:0:-1})" |
| 23 | + echo "$mesg" >&2 |
| 24 | + |
| 25 | + [ "$stop" == "true" ] && exit $retint |
| 26 | + return $retint |
| 27 | +} |
| 28 | + |
| 29 | +function bright_map_resolver() |
13 | 30 | {
|
14 | 31 | local -n map="$1"
|
15 | 32 | local key="$2"
|
16 | 33 |
|
17 | 34 | if [ ! ${map[$key]+_} ]; then
|
18 |
| - echo "_bright_resolve: argument \"$key\" is out of range" >&2 |
19 |
| - return 1 |
| 35 | + _bright_error $FUNCNAME "argument is out of range" "$key" || return $? |
20 | 36 | fi
|
21 | 37 |
|
22 | 38 | echo ${map[$key]}
|
23 | 39 | }
|
24 | 40 |
|
25 |
| -function _bright_escape |
| 41 | +function bright_out_builder() |
26 | 42 | {
|
27 |
| - local esc="\033[" |
28 |
| - local str="$1" |
| 43 | + local out="$1" |
29 | 44 |
|
30 | 45 | until [ -z "$2" ]; do
|
31 |
| - if ! [ $2 -ge 0 -a $2 -le 47 ] 2>/dev/null; then |
32 |
| - echo "_bright_escape: argument \"$2\" is out of range" >&2 |
33 |
| - return 1 |
| 46 | + IFS=\: read -a part <<<"$2" |
| 47 | + |
| 48 | + cmd="bright_get_${part[0]}" |
| 49 | + ret="$($cmd "${part[1]}")" |
| 50 | + |
| 51 | + if ! [ $ret -ge 0 -a $ret -le 50 ]; then |
| 52 | + _bright_error $FUNCNAME "expected range 0 through 50" "$ret" || return $? |
34 | 53 | fi
|
35 | 54 |
|
36 |
| - str="${esc}${2}m${str}${esc}$(bright_get_control escape)m" |
| 55 | + out="\033[${ret}m${out}\033[0m" |
37 | 56 |
|
38 | 57 | shift || break
|
39 | 58 | done
|
40 | 59 |
|
41 |
| - echo -e "$str" |
42 |
| -} |
43 |
| - |
44 |
| -function bright_get_color() |
45 |
| -{ |
46 |
| - local _key="$1" |
47 |
| - local -A _color=( [black]=30 [red]=31 [green]=32 [brown]=33 [blue]=34 [magenta]=35 [cyan]=36 [white]=37 ) |
48 |
| - |
49 |
| - _bright_resolve _color $_key |
50 |
| -} |
51 |
| - |
52 |
| -function bright_get_bgcolor() |
53 |
| -{ |
54 |
| - local _key="$1" |
55 |
| - local -A _bgcolor=( [black]=40 [red]=41 [green]=42 [brown]=43 [blue]=44 [magenta]=45 [cyan]=46 [white]=47 ) |
56 |
| - |
57 |
| - _bright_resolve _bgcolor $_key |
| 60 | + echo -e "$out" |
| 61 | + return 0 |
58 | 62 | }
|
59 | 63 |
|
60 | 64 | function bright_get_control()
|
61 | 65 | {
|
62 |
| - local _key=$1 |
63 |
| - local -A _control=( [escape]=0 [underline]=24 [reverse]=27 [bold]=1 [bright]=2 [underscore]=4 [reverse]=7 ) |
64 |
| - |
65 |
| - _bright_resolve _control $_key |
| 66 | + local control_name="$1" |
| 67 | + local -A controls=( |
| 68 | + [control escape]=0 |
| 69 | + [style underline]=24 |
| 70 | + [style reverse]=27 |
| 71 | + [style bold]=1 |
| 72 | + [style bright]=2 |
| 73 | + [style underscore]=4 |
| 74 | + [style reverse]=7 |
| 75 | + [default foreground]=39 |
| 76 | + [default background]=49 |
| 77 | + ) |
| 78 | + |
| 79 | + bright_map_resolver controls "$control_name" |
| 80 | + return $? |
66 | 81 | }
|
67 | 82 |
|
68 |
| -function bright_get_default() |
| 83 | +function bright_get_color() |
69 | 84 | {
|
70 |
| - local _key=$1 |
71 |
| - local -A _default=( [fg]=39 [bg]=49 ) |
72 |
| - |
73 |
| - _bright_resolve _default $_key |
| 85 | + local color_name="$1" |
| 86 | + local color_type="${2:-foreground}" |
| 87 | + local -A colors=( |
| 88 | + [foreground black]=30 |
| 89 | + [foreground red]=31 |
| 90 | + [foreground green]=32 |
| 91 | + [foreground brown]=33 |
| 92 | + [foreground blue]=34 |
| 93 | + [foreground magenta]=35 |
| 94 | + [foreground cyan]=36 |
| 95 | + [foreground white]=37 |
| 96 | + [background black]=40 |
| 97 | + [background red]=41 |
| 98 | + [background green]=42 |
| 99 | + [background brown]=43 |
| 100 | + [background blue]=44 |
| 101 | + [background magenta]=45 |
| 102 | + [background cyan]=46 |
| 103 | + [background white]=47 |
| 104 | + ) |
| 105 | + |
| 106 | + case "$color_type" in |
| 107 | + foreground ) color_name="foreground $color_name" ;; |
| 108 | + background ) color_name="background $color_name" ;; |
| 109 | + * ) _bright_error $FUNCNAME "expected fore- or background but got" "$color_type" || return $? ;; |
| 110 | + esac |
| 111 | + |
| 112 | + bright_map_resolver colors "$color_name" |
| 113 | + return $? |
| 114 | +} |
| 115 | + |
| 116 | +function bright_get_color_bg() |
| 117 | +{ |
| 118 | + bright_get_color "$1" background |
| 119 | + return $? |
74 | 120 | }
|
75 | 121 |
|
76 |
| -function bright_escape() { |
77 |
| - _bright_escape "$1" $(bright_get_control escape) |
| 122 | +function bright_out_default() { |
| 123 | + bright_out_builder "$1" "control:default foreground" |
78 | 124 | }
|
79 | 125 |
|
80 |
| -function bright_default() { |
81 |
| - _bright_escape "$1" $(bright_get_default fg) |
| 126 | +function bright_out_default_bg() { |
| 127 | + bright_out_builder "$1" "control:default background" |
82 | 128 | }
|
83 | 129 |
|
84 |
| -function bright_bgdefault() { |
85 |
| - _bright_escape "$1" $(bright_get_default bg) |
| 130 | +function bright_out_escape() { |
| 131 | + bright_out_builder "$1" "control:control escape" |
86 | 132 | }
|
87 | 133 |
|
88 |
| -function bright_underline() { |
89 |
| - _bright_escape "$1" $(bright_get_control underline) |
| 134 | +function bright_out_underline() { |
| 135 | + bright_out_builder "$1" "control:style underline" |
90 | 136 | }
|
91 | 137 |
|
92 |
| -function bright_reverse() { |
93 |
| - _bright_escape "$1" $(bright_get_control reverse) |
| 138 | +function bright_out_reverse() { |
| 139 | + bright_out_builder "$1" "control:style reverse" |
94 | 140 | }
|
95 | 141 |
|
96 |
| -function bright_bold() { |
97 |
| - _bright_escape "$1" $(bright_get_control bold) |
| 142 | +function bright_out_bold() { |
| 143 | + bright_out_builder "$1" "control:style bold" |
98 | 144 | }
|
99 | 145 |
|
100 |
| -function bright_bright() { |
101 |
| - _bright_escape "$1" $(bright_get_control bright) |
| 146 | +function bright_out_bright() { |
| 147 | + bright_out_builder "$1" "control:style bright" |
102 | 148 | }
|
103 | 149 |
|
104 |
| -function bright_underscore() { |
105 |
| - _bright_escape "$1" $(bright_get_control underscore) |
| 150 | +function bright_out_underscore() { |
| 151 | + bright_out_builder "$1" "control:style underscore" |
106 | 152 | }
|
107 | 153 |
|
108 |
| -function bright_black() { |
109 |
| - _bright_escape "$1" $(bright_get_color black) |
| 154 | +function bright_out_black() { |
| 155 | + bright_out_builder "$1" "color:black" |
110 | 156 | }
|
111 | 157 |
|
112 |
| -function bright_red() { |
113 |
| - _bright_escape "$1" $(bright_get_color red) |
| 158 | +function bright_out_red() { |
| 159 | + bright_out_builder "$1" "color:red" |
114 | 160 | }
|
115 | 161 |
|
116 |
| -function bright_green() { |
117 |
| - _bright_escape "$1" $(bright_get_color green) |
| 162 | +function bright_out_green() { |
| 163 | + bright_out_builder "$1" "color:green" |
118 | 164 | }
|
119 | 165 |
|
120 |
| -function bright_brown() { |
121 |
| - _bright_escape "$1" $(bright_get_color brown) |
| 166 | +function bright_out_brown() { |
| 167 | + bright_out_builder "$1" "color:brown" |
122 | 168 | }
|
123 | 169 |
|
124 |
| -function bright_blue() { |
125 |
| - _bright_escape "$1" $(bright_get_color blue) |
| 170 | +function bright_out_blue() { |
| 171 | + bright_out_builder "$1" "color:blue" |
126 | 172 | }
|
127 | 173 |
|
128 |
| -function bright_magenta() { |
129 |
| - _bright_escape "$1" $(bright_get_color magenta) |
| 174 | +function bright_out_magenta() { |
| 175 | + bright_out_builder "$1" "color:magenta" |
130 | 176 | }
|
131 | 177 |
|
132 |
| -function bright_cyan() { |
133 |
| - _bright_escape "$1" $(bright_get_color cyan) |
| 178 | +function bright_out_cyan() { |
| 179 | + bright_out_builder "$1" "color:cyan" |
134 | 180 | }
|
135 | 181 |
|
136 |
| -function bright_white() { |
137 |
| - _bright_escape "$1" $(bright_get_color white) |
| 182 | +function bright_out_white() { |
| 183 | + bright_out_builder "$1" "color:white" |
138 | 184 | }
|
139 | 185 |
|
140 |
| -function bright_bg_black() { |
141 |
| - _bright_escape "$1" $(bright_get_bgcolor black) |
| 186 | +function bright_out_black_bg() { |
| 187 | + bright_out_builder "$1" "color_bg:black" |
142 | 188 | }
|
143 | 189 |
|
144 |
| -function bright_bg_red() { |
145 |
| - _bright_escape "$1" $(bright_get_bgcolor red) |
| 190 | +function bright_out_red_bg() { |
| 191 | + bright_out_builder "$1" "color_bg:red" |
146 | 192 | }
|
147 | 193 |
|
148 |
| -function bright_bg_green() { |
149 |
| - _bright_escape "$1" $(bright_get_bgcolor green) |
| 194 | +function bright_out_green_bg() { |
| 195 | + bright_out_builder "$1" "color_bg:green" |
150 | 196 | }
|
151 | 197 |
|
152 |
| -function bright_bg_brown() { |
153 |
| - _bright_escape "$1" $(bright_get_bgcolor brown) |
| 198 | +function bright_out_brown_bg() { |
| 199 | + bright_out_builder "$1" "color_bg:brown" |
154 | 200 | }
|
155 | 201 |
|
156 |
| -function bright_bg_blue() { |
157 |
| - _bright_escape "$1" $(bright_get_bgcolor blue) |
| 202 | +function bright_out_blubg() { |
| 203 | + bright_out_builder "$1" "color_bg:blue" |
158 | 204 | }
|
159 | 205 |
|
160 |
| -function bright_bg_magenta() { |
161 |
| - _bright_escape "$1" $(bright_get_bgcolor magenta) |
| 206 | +function bright_out_magenta_bg() { |
| 207 | + bright_out_builder "$1" "color_bg:magenta" |
162 | 208 | }
|
163 | 209 |
|
164 |
| -function bright_bg_cyan() { |
165 |
| - _bright_escape "$1" $(bright_get_bgcolor cyan) |
| 210 | +function bright_out_cyan_bg() { |
| 211 | + bright_out_builder "$1" "color_bg:cyan" |
166 | 212 | }
|
167 | 213 |
|
168 |
| -function bright_bg_white() { |
169 |
| - _bright_escape "$1" $(bright_get_bgcolor white) |
| 214 | +function bright_out_whitbg() { |
| 215 | + bright_out_builder "$1" "color_bg:white" |
170 | 216 | }
|
171 | 217 |
|
172 | 218 | # EOF
|
0 commit comments