-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpwb_showrgb
executable file
·152 lines (118 loc) · 3.08 KB
/
pwb_showrgb
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
#!/usr/bin/env bash
enable ate
enable pwb
source <( pwb_sources \
ate_confirm_functions \
ate_get_index_with_key \
ate_exit_on_error \
pwb_exit_on_error \
pwb_display_ate_table \
pwb_print_head \
pwb_print_foot \
)
ps_read_data()
{
local table_name="$1"
ate get_array_name "$table_name"
ate_exit_on_error
local -n tarray="$ATE_VALUE"
local -a re_arr=(
^ # beginning of string
[[:space:]]* # optional spaces before first number
\([[:digit:]]+\) # capture the number
[[:space:]]+ # at least one space before next number
\([[:digit:]]+\) # capture the number
[[:space:]]+ # at least one space before next number
\([[:digit:]]+\) # capture the number
[[:space:]]+ # at least one space before name
\(.*\) # color name
$ # end of string
)
local IFS=
local re="${re_arr[*]}"
while read -r; do
if [[ "$REPLY" =~ $re ]]; then
tarray+=( "${BASH_REMATCH[@]:1}" )
fi
done < <( showrgb )
ate index_rows "$table_name"
ate_exit_on_error
}
ps_get_max_label_len()
{
local return_name="$1"
local -n pgmll_return="$1"
local tname="$2"
pgmll_return=5
cb()
{
local -n max="$return_name"
local -n cb_row="$1"
local -i cur="${#cb_row[3]}"
(( max = ( max < cur ? cur : max) ))
return 0
}
ate walk_rows "$tname" cb
ate_exit_on_error
}
ps_print()
{
local -i index="$1"
local dsource="$2"
local -i char_limit="$3"
ate get_row "$dsource" "$index"
ate_exit_on_error
local color
printf -v color $'\e[48;2;%d;%d;%dm' "${ATE_ARRAY[@]:0:3}"
local -a pargs=(
"${ATE_ARRAY[@]:0:3}"
"$color"
"$MAXLABEL"
"${ATE_ARRAY[3]}"
)
local tline
printf -v tline $'%3d %3d %3d |%s \e[m| %-*s' "${pargs[@]}"
# printf -v tline $'%s\e[m' "${pargs[*]}"
printf "%s" "$tline"
}
print_head()
{
local phandle="$1"
local -a lines=(
""
$'\e[38;2;255;255;0mSHOWRGB\e[m Demonstration'
"View a list of colors with X names"
)
pwb_print_head "$phandle" lines
}
############################
### EXECUTION BEGINS ###
############################
declare CTABLE
ate declare CTABLE 4
ps_read_data CTABLE
# Max label length needed for formatted printing
declare MAXLABEL
ps_get_max_label_len MAXLABEL CTABLE
declare LINELEN=$(( 4*3 + 6 + MAXLABEL ))
ate get_row_count CTABLE
ate_exit_on_error
declare PHANDLE
pwb declare PHANDLE CTABLE "$ATE_VALUE" ps_print
pwb_exit_on_error
pwb get_dimensions
pwb_exit_on_error
declare -a MARGINS=(
5
$(( (PWB_ASSOC[screen_cols] - LINELEN) / 2 ))
5
)
pwb set_margins PHANDLE "${MARGINS[@]}"
# Set trap to ensure call to pwb restore:
trap_func() { pwb restore; }
trap trap_func EXIT SIGABRT SIGINT
pwb init
printf $'\e[48;2;45;45;45m'
print_head PHANDLE
printf $'\e[m'
pwb start PHANDLE