-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpwb_template
executable file
·136 lines (107 loc) · 2.98 KB
/
pwb_template
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
#!/usr/bin/env bash
enable ate
enable pwb
source <( pwb_sources \
ate_exit_on_error \
pwb_exit_on_error \
pwb_newline_head \
)
declare MARGIN_COLOR=$'\e[48;2;64;64;64m'
##########################
### TABLE MAINTNENANCE ###
##########################
load_table()
{
local table_name="$1"
# Confirm table configuration
local -i expected_row_size=1
ate get_row_size "$table_name"
if (( ATE_VALUE != expected_row_size )); then
printf "Misconfigured table has %d instead of %d fields.\n" \
"$ATE_VALUE" "$expected_row_size"
exit 1
fi
# Get access to ate-hosted array
ate get_array_name "$table_name"
ate_exit_on_error
local -n lt_array="$ATE_VALUE"
lt_array=( * )
ate index_rows "$table_name"
}
#############################
### PWB EXECUTION SECTION ###
#############################
# Recognize extra keys, prototype an exec function
# Keypresses should be linked to '7', which will result in
# these keys triggering the common exec function
declare -a extra_keys=(
$'\e' 7 # Escape key declaration
E 7 # upper- and
e 7 # lower-case for same letter must be
# declared individually
)
pwb declare_keymap ek_map extra_keys
pwb_exit_on_error
common_exec()
{
local keyp="$1"
local -i index="$2"
local table_name="$3"
local pwb_name="$4"
# ensure same set of keys are in the key_map and the 'case'
case "$keyp" in
$'\cm' ) echo "Pressed ENTER!" ;;
$'\e' ) echo "Pressed ESCAPE!" ;;
E|e ) echo "Pressed 'e'" ;;
esac
return 0
}
############################
### PWB PRINTING SECTION ###
############################
print_line()
{
local -i index="$1"
local table_name="$2"
local -i char_limit="$3"
declare -a row
ate get_row "$table_name" "$index" -a row
ate_exit_on_error
declare trow
printf -v trow "%-*s" "$char_limit" "${row[0]}"
printf "%s" "${trow:0:$char_limit}"
}
###########################
### PWB SIGNAL HANDLING ###
###########################
# PWB messes with the terminal, and politeness dictates returning
# the terminal to the original state before exiting. Responding to
# signals is the only reliable way to restore the terminal.
trap_for_pwb_cleanup()
{
pwb restore
echo "Signal trapped $# arguments, '$*'"
}
#############################
### EXECUTION BEGINS HERE ###
#############################
declare ls_table
ate declare ls_table 1
ate_exit_on_error
if ! load_table ls_table; then
echo "load_table failed!"
exit 1
fi
ate get_row_count ls_table
ate_exit_on_error
declare ls_display
pwb declare ls_display ls_table "$ATE_VALUE" print_line \
-e common_exec
pwb_exit_on_error
# Set traps to ensure cleanup
trap "trap_for_pwb_cleanup EXIT" EXIT
trap "trap_for_pwb_cleanup SIGINT" SIGINT
trap "trap_for_pwb_cleanup SIGQUIT" SIGQUIT
trap "trap_for_pwb_cleanup SIGABRT" SIGABRT
pwb init
pwb start ls_display -a ek_map