-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpwb_tutorial_04_centering
executable file
·66 lines (54 loc) · 1.5 KB
/
pwb_tutorial_04_centering
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
#!/usr/bin/env bash
## BEGIN_DOC
## This example introduces PWB actions for
## determining screen dimensions and for setting
## centering margins around the content.
##
## The source lines are surveyed to identify
## the longest line. The longest line value
## is used to set margins that will center the
## just-large-enough content area on the console.
## END_DOC
# 1. Enable the builtin(s)
enable pwb
enable ate
# 2. Create line-printing function
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]}"
printf "%-*s" "$chars_limit" "${line:0:$chars_limit}"
}
# 3. Get data
declare ftable
ate declare ftable 1
ate get_array_name ftable
declare -n ftarray="$ATE_VALUE"
# Diff #1: Note longest file name while adding to the table
# This could also be done using 'ate walk_rows'
declare -i curlen maxlen=0
for fname in *; do
ftarray+=( "$fname" )
curlen="${#fname}"
(( maxlen = curlen > maxlen ? curlen : maxlen ))
done
ftarray=( * )
unset -n ftarray
ate index_rows ftable
declare -i file_count
ate get_row_count ftable -v file_count
#4 Declare the pwb handle
declare phandle
pwb declare phandle ftable "$file_count" print_line
# Diff #2 Set margins to center the content
pwb get_dimensions
declare -i swide="${PWB_ASSOC[screen_cols]}"
declare -i lrmargin=$(( (swide - maxlen) / 2 ))
pwb set_margins phandle 5 "$lrmargin"
#5 Start the interaction
pwb init
pwb start phandle
pwb restore