-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsheet.sh
More file actions
executable file
·211 lines (169 loc) · 6.66 KB
/
sheet.sh
File metadata and controls
executable file
·211 lines (169 loc) · 6.66 KB
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
#!/usr/bin/env bash
# examples/sheet.sh — Two-step wizard using shellframe_sheet
#
# Demonstrates: sheet push from a parent shell screen, height change on
# transition, Back navigation, and Esc dismissal.
#
# Usage: ./examples/sheet.sh
# Prints submitted data to stdout on completion, or "Dismissed." if dismissed.
set -u
source "$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)/shellframe.sh"
# ── App state ─────────────────────────────────────────────────────────────────
_WZ_RESULT="" # set to "Submitted:name:city" on successful submit
# Field definitions: "label<TAB>cursor-ctx<TAB>type"
_WZ_STEP1_FIELDS=(
$'Name\twzs1_name\ttext'
$'Email\twzs1_email\ttext'
)
_WZ_STEP2_FIELDS=(
$'City\twzs2_city\ttext'
$'Zip\twzs2_zip\ttext'
)
# Initialize both forms at startup (each uses its own cursor contexts)
SHELLFRAME_FORM_FIELDS=("${_WZ_STEP1_FIELDS[@]}")
shellframe_form_init "step1"
SHELLFRAME_FORM_FIELDS=("${_WZ_STEP2_FIELDS[@]}")
shellframe_form_init "step2"
# ── Parent screen ─────────────────────────────────────────────────────────────
_wz_ROOT_render() {
local _rows _cols
_shellframe_shell_terminal_size _rows _cols
shellframe_shell_region "content" 1 1 "$_cols" "$(( _rows - 1 ))"
shellframe_shell_region "footer" "$_rows" 1 "$_cols" 1 nofocus
}
_wz_ROOT_content_render() {
local _top="$1" _left="$2" _width="$3"
shellframe_fb_print "$_top" "$_left" "Welcome — press Enter to open the wizard"
}
_wz_ROOT_footer_render() {
local _top="$1" _left="$2" _width="$3"
shellframe_fb_fill "$_top" "$_left" "$_width" " " "${SHELLFRAME_GRAY:-}"
shellframe_fb_print "$_top" "$_left" " Enter open wizard q quit" "${SHELLFRAME_GRAY:-}"
}
_wz_ROOT_quit() { _SHELLFRAME_SHELL_NEXT="__QUIT__"; }
# Open the wizard sheet on Enter
_wz_ROOT_content_on_key() {
if [[ "$1" == $'\n' || "$1" == $'\r' ]]; then
shellframe_sheet_push "_wz" "STEP1"
return 0
fi
return 1
}
# ── Sheet — Step 1 ────────────────────────────────────────────────────────────
_wz_STEP1_render() {
SHELLFRAME_SHEET_HEIGHT=6
shellframe_shell_region "form" 1 1 "$SHELLFRAME_SHEET_WIDTH" 4
shellframe_shell_region "next" 5 1 "$SHELLFRAME_SHEET_WIDTH" 1
shellframe_shell_region "footer" 6 1 "$SHELLFRAME_SHEET_WIDTH" 1 nofocus
}
_wz_STEP1_form_render() {
SHELLFRAME_FORM_FIELDS=("${_WZ_STEP1_FIELDS[@]}")
shellframe_form_render "step1" "$@"
}
_wz_STEP1_form_on_key() {
SHELLFRAME_FORM_FIELDS=("${_WZ_STEP1_FIELDS[@]}")
shellframe_form_on_key "step1" "$1"
}
_wz_STEP1_form_on_focus() {
: # no-op; focus state tracked by form itself
}
_wz_STEP1_next_render() {
local _top="$1" _left="$2" _width="$3"
local _label=" [Next] "
shellframe_fb_print "$_top" "$(( _width - ${#_label} + 1 ))" "$_label"
}
_wz_STEP1_next_on_key() {
if [[ "$1" == $'\n' || "$1" == $'\r' || "$1" == " " ]]; then
return 2
fi
return 1
}
_wz_STEP1_next_action() {
_SHELLFRAME_SHEET_NEXT="STEP2"
}
_wz_STEP1_footer_render() {
local _top="$1" _left="$2" _width="$3"
shellframe_fb_fill "$_top" "$_left" "$_width" " " "${SHELLFRAME_GRAY:-}"
shellframe_fb_print "$_top" "$_left" \
" Step 1 of 2 Tab next field Enter select Esc cancel" "${SHELLFRAME_GRAY:-}"
}
_wz_STEP1_quit() { shellframe_sheet_pop; }
# form submit (Enter in last field) → transition to step 2
_wz_STEP1_form_action() {
_SHELLFRAME_SHEET_NEXT="STEP2"
}
# ── Sheet — Step 2 ────────────────────────────────────────────────────────────
_wz_STEP2_render() {
SHELLFRAME_SHEET_HEIGHT=7
local _half=$(( SHELLFRAME_SHEET_WIDTH / 2 ))
shellframe_shell_region "form" 1 1 "$SHELLFRAME_SHEET_WIDTH" 4
shellframe_shell_region "submit" 5 1 "$_half" 1
shellframe_shell_region "back" 5 "$(( _half + 1 ))" "$_half" 1
shellframe_shell_region "footer" 7 1 "$SHELLFRAME_SHEET_WIDTH" 1 nofocus
}
_wz_STEP2_form_render() {
SHELLFRAME_FORM_FIELDS=("${_WZ_STEP2_FIELDS[@]}")
shellframe_form_render "step2" "$@"
}
_wz_STEP2_form_on_key() {
SHELLFRAME_FORM_FIELDS=("${_WZ_STEP2_FIELDS[@]}")
shellframe_form_on_key "step2" "$1"
}
_wz_STEP2_form_on_focus() {
: # no-op
}
_wz_STEP2_form_action() {
# Enter in last field acts like Submit
_wz_STEP2_submit_action
}
_wz_STEP2_submit_render() {
local _top="$1" _left="$2"
shellframe_fb_print "$_top" "$_left" "[Submit]"
}
_wz_STEP2_submit_on_key() {
if [[ "$1" == $'\n' || "$1" == $'\r' || "$1" == " " ]]; then
return 2
fi
return 1
}
_wz_STEP2_submit_action() {
# Collect values from both steps
local _step1_vals=()
SHELLFRAME_FORM_FIELDS=("${_WZ_STEP1_FIELDS[@]}")
shellframe_form_values "step1" _step1_vals
local _name="${_step1_vals[0]:-}"
local _step2_vals=()
SHELLFRAME_FORM_FIELDS=("${_WZ_STEP2_FIELDS[@]}")
shellframe_form_values "step2" _step2_vals
local _city="${_step2_vals[0]:-}"
_WZ_RESULT="Submitted:${_name}:${_city}"
# NOTE: __QUIT__ is processed by shell.sh before __POP__ can run through
# shellframe_sheet_draw, so _SHELLFRAME_SHEET_ACTIVE stays 1 on exit.
# This is safe for single-run scripts; don't call shellframe_shell again
# in the same process after this pattern.
_SHELLFRAME_SHEET_NEXT="__POP__"
_SHELLFRAME_SHELL_NEXT="__QUIT__"
}
_wz_STEP2_back_render() {
local _top="$1" _left="$2"
shellframe_fb_print "$_top" "$_left" "[Back]"
}
_wz_STEP2_back_on_key() {
if [[ "$1" == $'\n' || "$1" == $'\r' || "$1" == " " ]]; then
return 2
fi
return 1
}
_wz_STEP2_back_action() {
_SHELLFRAME_SHEET_NEXT="STEP1"
}
_wz_STEP2_footer_render() {
local _top="$1" _left="$2" _width="$3"
shellframe_fb_fill "$_top" "$_left" "$_width" " " "${SHELLFRAME_GRAY:-}"
shellframe_fb_print "$_top" "$_left" \
" Step 2 of 2 Tab next field Enter select Esc cancel" "${SHELLFRAME_GRAY:-}"
}
_wz_STEP2_quit() { shellframe_sheet_pop; }
# ── Run ───────────────────────────────────────────────────────────────────────
shellframe_shell "_wz" "ROOT"
[[ -n "$_WZ_RESULT" ]] && printf '%s\n' "$_WZ_RESULT" || printf 'Dismissed.\n'