Skip to content

Commit aedb498

Browse files
committed
cleanup and fixed api
1 parent 65a24aa commit aedb498

File tree

1 file changed

+130
-84
lines changed

1 file changed

+130
-84
lines changed

bright.bash

Lines changed: 130 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -9,164 +9,210 @@
99
# file distributed with this source code.
1010
##
1111

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()
1330
{
1431
local -n map="$1"
1532
local key="$2"
1633

1734
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 $?
2036
fi
2137

2238
echo ${map[$key]}
2339
}
2440

25-
function _bright_escape
41+
function bright_out_builder()
2642
{
27-
local esc="\033["
28-
local str="$1"
43+
local out="$1"
2944

3045
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 $?
3453
fi
3554

36-
str="${esc}${2}m${str}${esc}$(bright_get_control escape)m"
55+
out="\033[${ret}m${out}\033[0m"
3756

3857
shift || break
3958
done
4059

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
5862
}
5963

6064
function bright_get_control()
6165
{
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 $?
6681
}
6782

68-
function bright_get_default()
83+
function bright_get_color()
6984
{
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 $?
74120
}
75121

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"
78124
}
79125

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"
82128
}
83129

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"
86132
}
87133

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"
90136
}
91137

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"
94140
}
95141

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"
98144
}
99145

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"
102148
}
103149

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"
106152
}
107153

108-
function bright_black() {
109-
_bright_escape "$1" $(bright_get_color black)
154+
function bright_out_black() {
155+
bright_out_builder "$1" "color:black"
110156
}
111157

112-
function bright_red() {
113-
_bright_escape "$1" $(bright_get_color red)
158+
function bright_out_red() {
159+
bright_out_builder "$1" "color:red"
114160
}
115161

116-
function bright_green() {
117-
_bright_escape "$1" $(bright_get_color green)
162+
function bright_out_green() {
163+
bright_out_builder "$1" "color:green"
118164
}
119165

120-
function bright_brown() {
121-
_bright_escape "$1" $(bright_get_color brown)
166+
function bright_out_brown() {
167+
bright_out_builder "$1" "color:brown"
122168
}
123169

124-
function bright_blue() {
125-
_bright_escape "$1" $(bright_get_color blue)
170+
function bright_out_blue() {
171+
bright_out_builder "$1" "color:blue"
126172
}
127173

128-
function bright_magenta() {
129-
_bright_escape "$1" $(bright_get_color magenta)
174+
function bright_out_magenta() {
175+
bright_out_builder "$1" "color:magenta"
130176
}
131177

132-
function bright_cyan() {
133-
_bright_escape "$1" $(bright_get_color cyan)
178+
function bright_out_cyan() {
179+
bright_out_builder "$1" "color:cyan"
134180
}
135181

136-
function bright_white() {
137-
_bright_escape "$1" $(bright_get_color white)
182+
function bright_out_white() {
183+
bright_out_builder "$1" "color:white"
138184
}
139185

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"
142188
}
143189

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"
146192
}
147193

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"
150196
}
151197

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"
154200
}
155201

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"
158204
}
159205

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"
162208
}
163209

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"
166212
}
167213

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"
170216
}
171217

172218
# EOF

0 commit comments

Comments
 (0)