@silvery/examples 0.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. package/bin/cli.ts +286 -0
  2. package/examples/apps/aichat/components.tsx +469 -0
  3. package/examples/apps/aichat/index.tsx +207 -0
  4. package/examples/apps/aichat/script.ts +460 -0
  5. package/examples/apps/aichat/state.ts +326 -0
  6. package/examples/apps/aichat/types.ts +19 -0
  7. package/examples/apps/app-todo.tsx +201 -0
  8. package/examples/apps/async-data.tsx +208 -0
  9. package/examples/apps/cli-wizard.tsx +332 -0
  10. package/examples/apps/clipboard.tsx +183 -0
  11. package/examples/apps/components.tsx +463 -0
  12. package/examples/apps/data-explorer.tsx +490 -0
  13. package/examples/apps/dev-tools.tsx +379 -0
  14. package/examples/apps/explorer.tsx +731 -0
  15. package/examples/apps/gallery.tsx +653 -0
  16. package/examples/apps/inline-bench.tsx +136 -0
  17. package/examples/apps/kanban.tsx +267 -0
  18. package/examples/apps/layout-ref.tsx +185 -0
  19. package/examples/apps/outline.tsx +171 -0
  20. package/examples/apps/panes/index.tsx +205 -0
  21. package/examples/apps/paste-demo.tsx +198 -0
  22. package/examples/apps/scroll.tsx +77 -0
  23. package/examples/apps/search-filter.tsx +240 -0
  24. package/examples/apps/task-list.tsx +271 -0
  25. package/examples/apps/terminal.tsx +800 -0
  26. package/examples/apps/textarea.tsx +103 -0
  27. package/examples/apps/theme.tsx +515 -0
  28. package/examples/apps/transform.tsx +242 -0
  29. package/examples/apps/virtual-10k.tsx +405 -0
  30. package/examples/components/counter.tsx +45 -0
  31. package/examples/components/hello.tsx +34 -0
  32. package/examples/components/progress-bar.tsx +48 -0
  33. package/examples/components/select-list.tsx +50 -0
  34. package/examples/components/spinner.tsx +40 -0
  35. package/examples/components/text-input.tsx +57 -0
  36. package/examples/components/virtual-list.tsx +52 -0
  37. package/package.json +27 -0
@@ -0,0 +1,379 @@
1
+ /**
2
+ * Dev Tools — Log Viewer Example
3
+ *
4
+ * A live log viewer demonstrating:
5
+ * - VirtualList for efficient rendering of thousands of log entries
6
+ * - Keyboard shortcuts to add log entries at different severity levels
7
+ * - Color-coded severity levels (DEBUG, INFO, WARN, ERROR)
8
+ * - j/k navigation through log history
9
+ * - Auto-scroll to latest entry
10
+ *
11
+ * Usage: bun run examples/apps/dev-tools.tsx
12
+ *
13
+ * Controls:
14
+ * j/k or Up/Down - Navigate through log entries
15
+ * g/G - Jump to first/last entry
16
+ * d - Add DEBUG entry
17
+ * i - Add INFO entry
18
+ * w - Add WARN entry
19
+ * e - Add ERROR entry
20
+ * c - Clear all logs
21
+ * q or Esc - Quit
22
+ */
23
+
24
+ import React, { useState, useCallback, useMemo } from "react"
25
+ import {
26
+ render,
27
+ Box,
28
+ Text,
29
+ VirtualList,
30
+ Divider,
31
+ useContentRect,
32
+ useInput,
33
+ useApp,
34
+ createTerm,
35
+ H1,
36
+ Strong,
37
+ Kbd,
38
+ Muted,
39
+ type Key,
40
+ } from "../../src/index.js"
41
+ import { ExampleBanner, type ExampleMeta } from "../_banner.js"
42
+
43
+ export const meta: ExampleMeta = {
44
+ name: "Dev Tools",
45
+ description: "Log viewer with severity levels, VirtualList, and keyboard-driven log injection",
46
+ features: ["VirtualList", "useInput()", "useContentRect()", "keyboard navigation"],
47
+ }
48
+
49
+ // ============================================================================
50
+ // Types
51
+ // ============================================================================
52
+
53
+ type LogLevel = "DEBUG" | "INFO" | "WARN" | "ERROR"
54
+
55
+ interface LogEntry {
56
+ id: number
57
+ timestamp: Date
58
+ level: LogLevel
59
+ source: string
60
+ message: string
61
+ }
62
+
63
+ // ============================================================================
64
+ // Data Generation
65
+ // ============================================================================
66
+
67
+ const SOURCES = ["http", "db", "auth", "cache", "worker", "api", "scheduler", "queue", "metrics", "ws"]
68
+
69
+ const LOG_TEMPLATES: Record<LogLevel, string[]> = {
70
+ DEBUG: [
71
+ "Cache miss for key user:session:{{id}}",
72
+ "Query plan: sequential scan on events ({{n}} rows)",
73
+ "WebSocket frame received: {{n}} bytes",
74
+ "GC pause: {{n}}ms (minor collection)",
75
+ "Connection pool stats: {{n}} active, {{n}} idle",
76
+ "Route matched: GET /api/v2/resources/{{id}}",
77
+ ],
78
+ INFO: [
79
+ "Request completed: 200 OK ({{n}}ms)",
80
+ "User {{id}} authenticated via OAuth",
81
+ "Background job processed: email_dispatch #{{id}}",
82
+ "Server listening on port {{n}}",
83
+ "Database migration applied: v{{n}}",
84
+ "Health check passed (latency: {{n}}ms)",
85
+ ],
86
+ WARN: [
87
+ "Slow query detected: {{n}}ms (threshold: 200ms)",
88
+ "Rate limit approaching: {{n}}/1000 requests",
89
+ "Memory usage: {{n}}% of allocated heap",
90
+ "Retry attempt {{n}}/3 for external API call",
91
+ "Certificate expires in {{n}} days",
92
+ "Connection pool near capacity: {{n}}/100",
93
+ ],
94
+ ERROR: [
95
+ "Unhandled exception in request handler: TypeError",
96
+ "Database connection refused: ECONNREFUSED",
97
+ "Authentication failed for user {{id}}: invalid token",
98
+ "Timeout after {{n}}ms waiting for upstream service",
99
+ "Disk usage critical: {{n}}% on /var/data",
100
+ "Failed to process message from queue: malformed payload",
101
+ ],
102
+ }
103
+
104
+ let nextLogId = 1
105
+
106
+ function seededRandom(seed: number): () => number {
107
+ let s = seed
108
+ return () => {
109
+ s = (s * 1664525 + 1013904223) & 0x7fffffff
110
+ return s / 0x7fffffff
111
+ }
112
+ }
113
+
114
+ function generateMessage(level: LogLevel, rng: () => number): string {
115
+ const templates = LOG_TEMPLATES[level]
116
+ const template = templates[Math.floor(rng() * templates.length)]!
117
+ return template
118
+ .replace(/\{\{id\}\}/g, () => String(Math.floor(rng() * 99999)))
119
+ .replace(/\{\{n\}\}/g, () => String(Math.floor(rng() * 999)))
120
+ }
121
+
122
+ function createLogEntry(level: LogLevel, rng: () => number): LogEntry {
123
+ return {
124
+ id: nextLogId++,
125
+ timestamp: new Date(),
126
+ level,
127
+ source: SOURCES[Math.floor(rng() * SOURCES.length)]!,
128
+ message: generateMessage(level, rng),
129
+ }
130
+ }
131
+
132
+ function generateInitialLogs(count: number): LogEntry[] {
133
+ const rng = seededRandom(42)
134
+ const levels: LogLevel[] = ["DEBUG", "INFO", "INFO", "INFO", "WARN", "ERROR"]
135
+ const entries: LogEntry[] = []
136
+ const now = Date.now()
137
+
138
+ for (let i = 0; i < count; i++) {
139
+ const level = levels[Math.floor(rng() * levels.length)]!
140
+ const entry = createLogEntry(level, rng)
141
+ // Spread timestamps over the last hour
142
+ entry.timestamp = new Date(now - (count - i) * 1200)
143
+ entries.push(entry)
144
+ }
145
+ return entries
146
+ }
147
+
148
+ // ============================================================================
149
+ // Constants
150
+ // ============================================================================
151
+
152
+ const LEVEL_COLORS: Record<LogLevel, string> = {
153
+ DEBUG: "$muted",
154
+ INFO: "$primary",
155
+ WARN: "$warning",
156
+ ERROR: "$error",
157
+ }
158
+
159
+ const LEVEL_BADGES: Record<LogLevel, string> = {
160
+ DEBUG: "DBG",
161
+ INFO: "INF",
162
+ WARN: "WRN",
163
+ ERROR: "ERR",
164
+ }
165
+
166
+ // ============================================================================
167
+ // Components
168
+ // ============================================================================
169
+
170
+ function formatTime(date: Date): string {
171
+ return date.toLocaleTimeString("en-US", {
172
+ hour: "2-digit",
173
+ minute: "2-digit",
174
+ second: "2-digit",
175
+ hour12: false,
176
+ })
177
+ }
178
+
179
+ function LogRow({ entry, isSelected }: { entry: LogEntry; isSelected: boolean }) {
180
+ const badge = LEVEL_BADGES[entry.level]
181
+ const color = LEVEL_COLORS[entry.level]
182
+
183
+ return (
184
+ <Box paddingX={1} backgroundColor={isSelected ? "$primary" : undefined}>
185
+ <Muted>{formatTime(entry.timestamp)} </Muted>
186
+ <Strong color={color}>{badge}</Strong>
187
+ <Muted> [{entry.source.padEnd(9)}] </Muted>
188
+ <Text>{entry.message}</Text>
189
+ </Box>
190
+ )
191
+ }
192
+
193
+ function LevelCounts({ entries }: { entries: LogEntry[] }) {
194
+ const counts = useMemo(() => {
195
+ const c = { DEBUG: 0, INFO: 0, WARN: 0, ERROR: 0 }
196
+ for (const e of entries) c[e.level]++
197
+ return c
198
+ }, [entries])
199
+
200
+ return (
201
+ <Box gap={2}>
202
+ <Strong color="$muted">
203
+ {LEVEL_BADGES.DEBUG}:{counts.DEBUG}
204
+ </Strong>
205
+ <Strong color="$primary">
206
+ {LEVEL_BADGES.INFO}:{counts.INFO}
207
+ </Strong>
208
+ <Strong color="$warning">
209
+ {LEVEL_BADGES.WARN}:{counts.WARN}
210
+ </Strong>
211
+ <Strong color="$error">
212
+ {LEVEL_BADGES.ERROR}:{counts.ERROR}
213
+ </Strong>
214
+ </Box>
215
+ )
216
+ }
217
+
218
+ /** Inner component that reads the flex container's height via useContentRect */
219
+ function LogListArea({ entries, cursor }: { entries: LogEntry[]; cursor: number }) {
220
+ const { height } = useContentRect()
221
+
222
+ return (
223
+ <VirtualList
224
+ items={entries}
225
+ height={height}
226
+ itemHeight={1}
227
+ scrollTo={cursor}
228
+ overscan={5}
229
+ renderItem={(entry, index) => <LogRow key={entry.id} entry={entry} isSelected={index === cursor} />}
230
+ />
231
+ )
232
+ }
233
+
234
+ // ============================================================================
235
+ // Main App
236
+ // ============================================================================
237
+
238
+ const INITIAL_COUNT = 200
239
+ const rng = seededRandom(12345)
240
+
241
+ export function DevTools() {
242
+ const { exit } = useApp()
243
+ const { width } = useContentRect()
244
+ const [entries, setEntries] = useState<LogEntry[]>(() => generateInitialLogs(INITIAL_COUNT))
245
+ const [cursor, setCursor] = useState(INITIAL_COUNT - 1)
246
+ const [autoScroll, setAutoScroll] = useState(true)
247
+
248
+ const addEntry = useCallback(
249
+ (level: LogLevel) => {
250
+ const entry = createLogEntry(level, rng)
251
+ setEntries((prev) => [...prev, entry])
252
+ if (autoScroll) {
253
+ setCursor((prev) => prev + 1)
254
+ }
255
+ },
256
+ [autoScroll],
257
+ )
258
+
259
+ useInput(
260
+ useCallback(
261
+ (input: string, key: Key) => {
262
+ // Quit
263
+ if (input === "q" || key.escape) {
264
+ exit()
265
+ return
266
+ }
267
+
268
+ // Navigation
269
+ if (input === "j" || key.downArrow) {
270
+ setCursor((c) => Math.min(entries.length - 1, c + 1))
271
+ setAutoScroll(false)
272
+ return
273
+ }
274
+ if (input === "k" || key.upArrow) {
275
+ setCursor((c) => Math.max(0, c - 1))
276
+ setAutoScroll(false)
277
+ return
278
+ }
279
+
280
+ // Jump to start/end
281
+ if (input === "g" || key.home) {
282
+ setCursor(0)
283
+ setAutoScroll(false)
284
+ return
285
+ }
286
+ if (input === "G" || key.end) {
287
+ setCursor(entries.length - 1)
288
+ setAutoScroll(true)
289
+ return
290
+ }
291
+
292
+ // Add log entries
293
+ if (input === "d") {
294
+ addEntry("DEBUG")
295
+ return
296
+ }
297
+ if (input === "i") {
298
+ addEntry("INFO")
299
+ return
300
+ }
301
+ if (input === "w") {
302
+ addEntry("WARN")
303
+ return
304
+ }
305
+ if (input === "e") {
306
+ addEntry("ERROR")
307
+ return
308
+ }
309
+
310
+ // Clear
311
+ if (input === "c") {
312
+ setEntries([])
313
+ setCursor(0)
314
+ setAutoScroll(true)
315
+ return
316
+ }
317
+ },
318
+ [entries.length, exit, addEntry],
319
+ ),
320
+ )
321
+
322
+ return (
323
+ <Box flexDirection="column" flexGrow={1}>
324
+ {/* Header */}
325
+ <Box paddingX={1} justifyContent="space-between">
326
+ <Box gap={2}>
327
+ <H1>Log Viewer</H1>
328
+ <LevelCounts entries={entries} />
329
+ </Box>
330
+ <Box gap={1}>
331
+ <Strong color="$primary">{cursor + 1}</Strong>
332
+ <Muted>/ {entries.length}</Muted>
333
+ {autoScroll && (
334
+ <Text color="$success" bold>
335
+ {" "}
336
+ LIVE
337
+ </Text>
338
+ )}
339
+ </Box>
340
+ </Box>
341
+
342
+ <Box paddingX={1}>
343
+ <Divider />
344
+ </Box>
345
+
346
+ {/* Log list in a flex-grow container */}
347
+ <Box flexGrow={1} flexDirection="column">
348
+ <LogListArea entries={entries} cursor={cursor} />
349
+ </Box>
350
+
351
+ {/* Help bar */}
352
+ <Box paddingX={1} justifyContent="space-between">
353
+ <Muted>
354
+ <Kbd>j/k</Kbd> navigate <Kbd>g/G</Kbd> start/end <Kbd>d/i/w/e</Kbd> add log <Kbd>c</Kbd> clear{" "}
355
+ <Kbd>Esc/q</Kbd> quit
356
+ </Muted>
357
+ </Box>
358
+ </Box>
359
+ )
360
+ }
361
+
362
+ // ============================================================================
363
+ // Main
364
+ // ============================================================================
365
+
366
+ async function main() {
367
+ using term = createTerm()
368
+ const { waitUntilExit } = await render(
369
+ <ExampleBanner meta={meta} controls="j/k navigate g/G start/end d/i/w/e add log c clear Esc/q quit">
370
+ <DevTools />
371
+ </ExampleBanner>,
372
+ term,
373
+ )
374
+ await waitUntilExit()
375
+ }
376
+
377
+ if (import.meta.main) {
378
+ main().catch(console.error)
379
+ }