-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpwb_tutorial
executable file
·300 lines (247 loc) · 6.59 KB
/
pwb_tutorial
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
#!/usr/bin/env bash
# This example extends pwb_example_03_centering with an explanation
# of source utilities made available to scripts using the PWB and
# ATE builtins.
#
# These source utilites were created to fill needs related to using
# the builtins but were not appropriate to add the the builtins
# themselves
enable pwb
enable ate
source <( pwb_sources \
ate_exit_on_error \
pwb_exit_on_error \
pwb_get_regions \
pwb_print_array \
pwb_show_message \
pwb_erase_dims \
)
##############################
### DATA ACQUISITION ###
##############################
declare FILE_PREFIX="pwb_tutorial_"
declare -i FILE_PREFIX_LEN=$(( ${#FILE_PREFIX} + 3 ))
fill_main_table()
{
local table_name="$1"
local -a tfiles=( "$FILE_PREFIX"* )
ate get_array_name "$table_name"
local -n fmt_array="$ATE_VALUE"
fmt_array=( "${tfiles[@]}" )
ate index_rows "$table_name"
}
get_max_line_width()
{
local name_return="$1"
local table_name="$2"
local -n gmlw_maxlen="$name_return"
gmlw_maxlen=0
stepper()
{
local -n s_row="$1"
local -i curlen=$(( ${#s_row[0]} - FILE_PREFIX_LEN ))
local -n s_maxlen="$name_return"
(( s_maxlen = curlen > s_maxlen ? curlen : s_maxlen ))
}
ate walk_rows "$table_name" stepper
ate_exit_on_error
}
read_tutorial_summary()
{
local -n rts_array="$1"
local file_name="$2"
local -i doc_status=0
while read -r; do
case "$doc_status" in
0 )
if [[ "$REPLY" == \#\#\ BEGIN_DOC ]]; then
(( ++doc_status ))
fi
;;
1 )
if [[ "$REPLY" == \#\#\ END_DOC ]]; then
(( ++doc_status ))
else
rts_array+=( "${REPLY:3}" )
fi
;;
esac
done < "$file_name"
}
####################################
### PWB CALLBACK FUNCTIONS ###
####################################
print_line()
{
local -i index="$1"
local data_name="$2"
local -i chars_limit="$3"
ate get_row "$data_name" "$index"
local line="${ATE_ARRAY[0]:$FILE_PREFIX_LEN}"
local type="${ATE_ARRAY[1]}"
local color
if [[ "$type" == "directory" ]]; then
color="$COL_DIR"
elif [[ "$type" == "symbolic link" ]]; then
color="$COL_LINK"
fi
printf $'%s%-*s\e[m' "$color" "$chars_limit" "${line:0:$chars_limit}"
}
execute_id_file()
{
printf -v msg $'File \e[32;1m%s\e[39m' "$1"
local -a lines=(
"$msg"
""
"Press any key to continue"
)
local -a dims
pwb_show_message -a dims lines
read -sn1
pwb_erase_dims dims
}
execute_view_file()
{
local fname="$1"
emacs "$fname"
}
execute_run_file()
{
local fname="$1"
"./$fname"
}
show_help()
{
local -a msg=(
" Arrow and page keys to navigate "
""
$' \e[32;1mENTER\e[39m to execute script '
$' \e[32;1mv\e[39m to view source code '
$' \e[32;1mi\e[39m for script information '
""
$' \e[32;1m?\e[39m to show this help page '
$' \e[32;1mq\e[39m to quit '
""
" Press any key to continue "
)
local -a dims
pwb_show_message -a dims msg
read -sn1
pwb_erase_dims dims
}
execute_line()
{
local keyp="$1"
local -i index="$2"
local data_name="$3"
local pwb_name="$4"
ate get_row "$data_name" "$index"
declare fname="${ATE_ARRAY[0]}"
case "$keyp" in
$'\cm') ./"$fname" ;;
'i' ) execute_id_file "$fname" ;;
'v' ) emacs "$fname" ;;
'?' ) show_help ;;
esac
restore_window "$pwb_name"
}
print_right_margin()
{
local -i index="$1"
local tname="$2"
ate get_row "$tname" "$index"
ate_exit_on_error
local filename="${ATE_ARRAY[0]}"
local -a prm_array=( "Script Summary" "" )
read_tutorial_summary prm_array "$filename"
printf -v prm_array["${#prm_array[*]}"] "%*s" 60 " "
# printf -v longstr "%*s" 60 ' '
# prm_array+=( "$longstr" )
pwb_print_array prm_array \
-t "${REGION_DIMS[RM_TOP]}" \
-l "${REGION_DIMS[RM_LEFT]}" \
-w "${REGION_DIMS[RM_WIDE]}" \
-c "${REGION_DIMS[RM_LINES]}" \
-j l
}
print_head()
{
print_centered()
{
local -i row="$1"
local msg="$2"
local -i left
(( left = ( REGION_DIMS[SC_WIDE] - ${#msg} ) / 2 ))
printf $'\e[%d;%dH\e[2K%s' "$row" "$left" "$msg"
}
printf $'\e[48;2;32;32;32m'
print_centered 1 "PWB Development Tutorial"
print_centered 2 "Press '?' for help, 'q' to quit"
printf $'\e[49m'
}
#########################
### MISCELLANEOUS ###
#########################
# Prepare the pwb handle with margins to center the content:
prepare_margins()
{
local pwb_handle="$1"
local table_name="$2"
local -i maxlen
get_max_line_width maxlen "$table_name"
pwb get_dimensions "$pwb_handle"
local -i swide="${PWB_ASSOC[screen_cols]}"
local -i left_margin=2
local -i right_margin=$(( swide - maxlen - 2 ))
pwb set_margins "$pwb_handle" 3 "$right_margin" 1 "$left_margin"
pwb_exit_on_error
}
# Non managed margins will not be automatically reploted with
# `pwb print_all`, so this function restores the static top
# margin after a full screen erase. The content and managed
# right margin will be handled by PWB.
restore_window()
{
local pname="$1"
printf $'\e[2J'
print_head "$pname"
pwb print_all "$pname"
}
exit_trap() { pwb restore; }
#############################
### BEGIN EXECUTION ###
#############################
## Color constants
declare COL_DIR=$'\e[34;1m'
declare COL_LINK=$'\e[38;2;64;255;192m'
## Declare and populate the table
declare FTABLE
ate declare FTABLE 1
fill_main_table FTABLE
declare PHANDLE
ate get_row_count FTABLE
pwb declare PHANDLE \
FTABLE "$ATE_VALUE" \
print_line \
-e execute_line \
-r print_right_margin
prepare_margins PHANDLE FTABLE
# Prepare global hash table for margin dimensions
declare -A REGION_DIMS
pwb_get_regions REGION_DIMS PHANDLE
pwb_exit_on_error
# Keymap for special action
declare -a keymap=(
$'\cm' 7 # enter key to execute
$'i' 7 # i key for information
$'v' 7 # v key to view
$'?' 7 # show help
)
declare KEYMAP
pwb declare_keymap KEYMAP keymap
pwb_exit_on_error
trap exit_trap EXIT SIGINT SIGABRT
#5 Start the interaction
pwb init
print_head
pwb start PHANDLE -a KEYMAP