forked from anomalyco/opentui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput-demo.ts
More file actions
332 lines (282 loc) · 8.73 KB
/
Copy pathinput-demo.ts
File metadata and controls
332 lines (282 loc) · 8.73 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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import {
createCliRenderer,
InputRenderable,
InputRenderableEvents,
RenderableEvents,
type CliRenderer,
t,
bold,
fg,
BoxRenderable,
} from "../index"
import { setupCommonDemoKeys } from "./lib/standalone-keys"
import { TextRenderable } from "../renderables/Text"
let nameInput: InputRenderable | null = null
let emailInput: InputRenderable | null = null
let passwordInput: InputRenderable | null = null
let commentInput: InputRenderable | null = null
let renderer: CliRenderer | null = null
let keyLegendDisplay: TextRenderable | null = null
let statusDisplay: TextRenderable | null = null
let lastActionText: string = "Welcome to InputRenderable demo! Use Tab to navigate between fields."
let lastActionColor: string = "#FFCC00"
let activeInputIndex: number = 0
const inputElements: InputRenderable[] = []
function getActiveInput(): InputRenderable | null {
return inputElements[activeInputIndex] || null
}
function updateDisplays() {
if (inputElements.length === 0) return
const activeInput = getActiveInput()
const activeInputName = getInputName(activeInput)
const keyLegendText = t`${bold(fg("#FFFFFF")("Key Controls:"))}
Tab/Shift+Tab: Navigate between inputs
Left/Right: Move cursor within input
Home/End: Move to start/end of input
Backspace/Delete: Remove characters
Enter: Submit current input
Ctrl+F: Toggle focus on active input
Ctrl+C: Clear active input
Ctrl+R: Reset all inputs to defaults
Type: Enter text in focused field`
if (keyLegendDisplay) {
keyLegendDisplay.content = keyLegendText
}
const nameValue = nameInput?.value || ""
const emailValue = emailInput?.value || ""
const passwordValue = passwordInput?.value || ""
const commentValue = commentInput?.value || ""
const nameStatus = nameInput?.focused ? "FOCUSED" : "BLURRED"
const nameColor = nameInput?.focused ? "#00FF00" : "#FF0000"
const emailStatus = emailInput?.focused ? "FOCUSED" : "BLURRED"
const emailColor = emailInput?.focused ? "#00FF00" : "#FF0000"
const passwordStatus = passwordInput?.focused ? "FOCUSED" : "BLURRED"
const passwordColor = passwordInput?.focused ? "#00FF00" : "#FF0000"
const commentStatus = commentInput?.focused ? "FOCUSED" : "BLURRED"
const commentColor = commentInput?.focused ? "#00FF00" : "#FF0000"
const statusText = t`${bold(fg("#FFFFFF")("Input Values:"))}
Name: "${nameValue}" (${fg(nameColor)(nameStatus)})
Email: "${emailValue}" (${fg(emailColor)(emailStatus)})
Password: "${passwordValue.replace(/./g, "*")}" (${fg(passwordColor)(passwordStatus)})
Comment: "${commentValue}" (${fg(commentColor)(commentStatus)})
${bold(fg("#FFAA00")(`Active Input: ${activeInputName}`))}
${bold(fg("#CCCCCC")("Validation:"))}
Name: ${validateName(nameValue) ? fg("#00FF00")("✓ Valid") : fg("#FF0000")("✗ Invalid (min 2 chars)")}
Email: ${validateEmail(emailValue) ? fg("#00FF00")("✓ Valid") : fg("#FF0000")("✗ Invalid format")}
Password: ${validatePassword(passwordValue) ? fg("#00FF00")("✓ Valid") : fg("#FF0000")("✗ Invalid (min 6 chars)")}
${fg(lastActionColor)(lastActionText)}`
if (statusDisplay) {
statusDisplay.content = statusText
}
}
function getInputName(input: InputRenderable | null): string {
if (input === nameInput) return "Name"
if (input === emailInput) return "Email"
if (input === passwordInput) return "Password"
if (input === commentInput) return "Comment"
return "Unknown"
}
function validateName(value: string): boolean {
return value.length >= 2
}
function validateEmail(value: string): boolean {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
return emailRegex.test(value)
}
function validatePassword(value: string): boolean {
return value.length >= 6
}
function navigateToInput(index: number): void {
const currentActive = getActiveInput()
currentActive?.blur()
activeInputIndex = Math.max(0, Math.min(index, inputElements.length - 1))
const newActive = getActiveInput()
newActive?.focus()
lastActionText = `Switched to ${getInputName(newActive)} input`
lastActionColor = "#FFCC00"
updateDisplays()
}
function resetInputs(): void {
nameInput!.value = ""
emailInput!.value = ""
passwordInput!.value = ""
commentInput!.value = ""
lastActionText = "All inputs reset to empty values"
lastActionColor = "#FF00FF"
updateDisplays()
setTimeout(() => {
lastActionColor = "#FFCC00"
updateDisplays()
}, 1000)
}
export function run(rendererInstance: CliRenderer): void {
renderer = rendererInstance
renderer.setBackgroundColor("#001122")
const parentContainer = new BoxRenderable(renderer, {
id: "parent-container",
zIndex: 10,
})
renderer.root.add(parentContainer)
// Create input elements
nameInput = new InputRenderable(renderer, {
id: "name-input",
position: "absolute",
left: 5,
top: 2,
width: 40,
height: 3,
zIndex: 100,
backgroundColor: "#001122",
textColor: "#FFFFFF",
placeholder: "Enter your name...",
placeholderColor: "#666666",
cursorColor: "#FFFF00",
value: "",
maxLength: 50,
})
emailInput = new InputRenderable(renderer, {
id: "email-input",
position: "absolute",
left: 5,
top: 6,
width: 40,
height: 3,
zIndex: 100,
backgroundColor: "#001122",
textColor: "#FFFFFF",
placeholder: "Enter your email...",
placeholderColor: "#666666",
cursorColor: "#FFFF00",
value: "",
maxLength: 100,
})
passwordInput = new InputRenderable(renderer, {
id: "password-input",
position: "absolute",
left: 5,
top: 10,
width: 40,
height: 3,
zIndex: 100,
backgroundColor: "#001122",
textColor: "#FFFFFF",
placeholder: "Enter password...",
placeholderColor: "#666666",
cursorColor: "#FFFF00",
value: "",
maxLength: 50,
})
commentInput = new InputRenderable(renderer, {
id: "comment-input",
position: "absolute",
left: 5,
top: 14,
width: 60,
height: 3,
zIndex: 100,
backgroundColor: "#001122",
textColor: "#FFFFFF",
placeholder: "Enter a comment...",
placeholderColor: "#666666",
cursorColor: "#FFFF00",
value: "",
maxLength: 200,
})
inputElements.push(nameInput, emailInput, passwordInput, commentInput)
renderer.root.add(nameInput)
renderer.root.add(emailInput)
renderer.root.add(passwordInput)
renderer.root.add(commentInput)
keyLegendDisplay = new TextRenderable(renderer, {
id: "key-legend",
content: t``,
width: 50,
height: 12,
position: "absolute",
left: 50,
top: 2,
zIndex: 50,
fg: "#AAAAAA",
})
parentContainer.add(keyLegendDisplay)
statusDisplay = new TextRenderable(renderer, {
id: "status-display",
content: t``,
width: 80,
height: 18,
position: "absolute",
left: 5,
top: 19,
zIndex: 50,
})
parentContainer.add(statusDisplay)
// Set up event handlers for all inputs
inputElements.forEach((input, index) => {
input.on(InputRenderableEvents.INPUT, (value: string) => {
lastActionText = `${getInputName(input)} input: "${value}"`
lastActionColor = "#00FFFF"
updateDisplays()
})
input.on(InputRenderableEvents.CHANGE, (value: string) => {
lastActionText = `*** ${getInputName(input)} CHANGED: "${value}" ***`
lastActionColor = "#FF00FF"
updateDisplays()
setTimeout(() => {
lastActionColor = "#FFCC00"
updateDisplays()
}, 1000)
})
input.on(InputRenderableEvents.ENTER, (value: string) => {
const inputName = getInputName(input)
const isValid =
inputName === "Name"
? validateName(value)
: inputName === "Email"
? validateEmail(value)
: inputName === "Password"
? validatePassword(value)
: true
lastActionText = `*** ${inputName} SUBMITTED: "${value}" ${isValid ? "(Valid)" : "(Invalid)"} ***`
lastActionColor = isValid ? "#00FF00" : "#FF0000"
updateDisplays()
setTimeout(() => {
lastActionColor = "#FFCC00"
updateDisplays()
}, 1500)
})
input.on(RenderableEvents.FOCUSED, () => {
updateDisplays()
})
input.on(RenderableEvents.BLURRED, () => {
updateDisplays()
})
})
updateDisplays()
nameInput.focus()
}
export function destroy(rendererInstance: CliRenderer): void {
inputElements.forEach((input) => {
if (input) {
rendererInstance.root.remove(input.id)
input.destroy()
}
})
inputElements.length = 0
rendererInstance.root.remove("parent-container")
nameInput = null
emailInput = null
passwordInput = null
commentInput = null
keyLegendDisplay = null
statusDisplay = null
renderer = null
activeInputIndex = 0
}
if (import.meta.main) {
const renderer = await createCliRenderer({
exitOnCtrlC: true,
})
run(renderer)
setupCommonDemoKeys(renderer)
renderer.start()
}