silvery 0.0.1 → 0.3.0

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 (77) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +174 -11
  3. package/bin/silvery.ts +258 -0
  4. package/examples/CLAUDE.md +75 -0
  5. package/examples/_banner.tsx +60 -0
  6. package/examples/cli.ts +228 -0
  7. package/examples/index.md +101 -0
  8. package/examples/inline/inline-nontty.tsx +98 -0
  9. package/examples/inline/inline-progress.tsx +79 -0
  10. package/examples/inline/inline-simple.tsx +63 -0
  11. package/examples/inline/scrollback.tsx +185 -0
  12. package/examples/interactive/_input-debug.tsx +110 -0
  13. package/examples/interactive/_stdin-test.ts +71 -0
  14. package/examples/interactive/_textarea-bare.tsx +45 -0
  15. package/examples/interactive/aichat/components.tsx +468 -0
  16. package/examples/interactive/aichat/index.tsx +207 -0
  17. package/examples/interactive/aichat/script.ts +460 -0
  18. package/examples/interactive/aichat/state.ts +326 -0
  19. package/examples/interactive/aichat/types.ts +19 -0
  20. package/examples/interactive/app-todo.tsx +198 -0
  21. package/examples/interactive/async-data.tsx +208 -0
  22. package/examples/interactive/cli-wizard.tsx +332 -0
  23. package/examples/interactive/clipboard.tsx +183 -0
  24. package/examples/interactive/components.tsx +463 -0
  25. package/examples/interactive/data-explorer.tsx +506 -0
  26. package/examples/interactive/dev-tools.tsx +379 -0
  27. package/examples/interactive/explorer.tsx +747 -0
  28. package/examples/interactive/gallery.tsx +652 -0
  29. package/examples/interactive/inline-bench.tsx +136 -0
  30. package/examples/interactive/kanban.tsx +267 -0
  31. package/examples/interactive/layout-ref.tsx +185 -0
  32. package/examples/interactive/outline.tsx +171 -0
  33. package/examples/interactive/paste-demo.tsx +198 -0
  34. package/examples/interactive/scroll.tsx +77 -0
  35. package/examples/interactive/search-filter.tsx +240 -0
  36. package/examples/interactive/task-list.tsx +279 -0
  37. package/examples/interactive/terminal.tsx +798 -0
  38. package/examples/interactive/textarea.tsx +103 -0
  39. package/examples/interactive/theme.tsx +336 -0
  40. package/examples/interactive/transform.tsx +256 -0
  41. package/examples/interactive/virtual-10k.tsx +413 -0
  42. package/examples/kitty/canvas.tsx +519 -0
  43. package/examples/kitty/generate-samples.ts +236 -0
  44. package/examples/kitty/image-component.tsx +273 -0
  45. package/examples/kitty/images.tsx +604 -0
  46. package/examples/kitty/input.tsx +371 -0
  47. package/examples/kitty/keys.tsx +378 -0
  48. package/examples/kitty/paint.tsx +1017 -0
  49. package/examples/layout/dashboard.tsx +551 -0
  50. package/examples/layout/live-resize.tsx +290 -0
  51. package/examples/layout/overflow.tsx +51 -0
  52. package/examples/playground/README.md +69 -0
  53. package/examples/playground/build.ts +61 -0
  54. package/examples/playground/index.html +420 -0
  55. package/examples/playground/playground-app.tsx +416 -0
  56. package/examples/runtime/elm-counter.tsx +206 -0
  57. package/examples/runtime/hello-runtime.tsx +73 -0
  58. package/examples/runtime/pipe-composition.tsx +184 -0
  59. package/examples/runtime/run-counter.tsx +78 -0
  60. package/examples/runtime/runtime-counter.tsx +197 -0
  61. package/examples/screenshots/generate.tsx +563 -0
  62. package/examples/scrollback-perf.tsx +230 -0
  63. package/examples/viewer.tsx +654 -0
  64. package/examples/web/build.ts +365 -0
  65. package/examples/web/canvas-app.tsx +80 -0
  66. package/examples/web/canvas.html +89 -0
  67. package/examples/web/dom-app.tsx +81 -0
  68. package/examples/web/dom.html +113 -0
  69. package/examples/web/showcase-app.tsx +107 -0
  70. package/examples/web/showcase.html +34 -0
  71. package/examples/web/showcases/index.tsx +56 -0
  72. package/examples/web/viewer-app.tsx +555 -0
  73. package/examples/web/viewer.html +30 -0
  74. package/examples/web/xterm-app.tsx +105 -0
  75. package/examples/web/xterm.html +118 -0
  76. package/package.json +106 -5
  77. package/src/index.ts +5 -0
@@ -0,0 +1,230 @@
1
+ /**
2
+ * Benchmark: ScrollbackList typing performance
3
+ * Measures per-keystroke cost across React reconciliation + silvery pipeline.
4
+ *
5
+ * Usage: bun examples/scrollback-perf.tsx
6
+ *
7
+ * Modes:
8
+ * --simple Simple items (Box + 2 Text nodes)
9
+ * --complex Complex items matching ai-chat (default)
10
+ * --timers Add pulse/elapsed timers like the real demo
11
+ */
12
+ import React, { useState, useEffect, useRef } from "react"
13
+ import { createRenderer } from "../src/testing/index.js"
14
+ import { Box, Text, ScrollbackList, TextInput, Spinner } from "../src/index.js"
15
+
16
+ interface Item {
17
+ id: string
18
+ text: string
19
+ role: string
20
+ frozen: boolean
21
+ }
22
+
23
+ const useSimple = process.argv.includes("--simple")
24
+ const useTimers = process.argv.includes("--timers")
25
+
26
+ /** Simple item — matches original benchmark (Box + 2-3 Text nodes) */
27
+ function SimpleItem({ item, isLatest }: { item: Item; isLatest: boolean }) {
28
+ return (
29
+ <Box flexDirection="column" borderStyle={item.role === "assistant" ? "single" : undefined}>
30
+ <Text bold color={item.role === "user" ? "green" : "blue"}>
31
+ {item.role === "user" ? "❯" : "◆"} {item.role}
32
+ </Text>
33
+ <Text>{item.text}</Text>
34
+ {isLatest && <Text dimColor>Latest item</Text>}
35
+ </Box>
36
+ )
37
+ }
38
+
39
+ /** Complex item — matches ai-chat's ExchangeItem */
40
+ function ComplexItem({ item }: { item: Item }) {
41
+ if (item.role === "user") {
42
+ return (
43
+ <Box flexDirection="column">
44
+ <Text> </Text>
45
+ <Box paddingX={1}>
46
+ <Text>
47
+ <Text bold color="blue">
48
+ {"❯ "}
49
+ </Text>
50
+ {item.text}
51
+ </Text>
52
+ </Box>
53
+ <Text> </Text>
54
+ </Box>
55
+ )
56
+ }
57
+ return (
58
+ <Box flexDirection="column" borderStyle="round" borderColor="green" paddingX={1}>
59
+ <Text>
60
+ <Text bold color="green">
61
+ ◆ Agent
62
+ </Text>
63
+ <Text color="gray" dim>
64
+ {" "}
65
+ 624 tokens
66
+ </Text>
67
+ </Text>
68
+ <Text> </Text>
69
+ <Text>{item.text}</Text>
70
+ <Text> </Text>
71
+ <Box flexDirection="column">
72
+ <Text>
73
+ <Text color="green">{"✓ "}</Text>
74
+ <Text color="cyan" bold>
75
+ Read
76
+ </Text>{" "}
77
+ src/auth.ts
78
+ </Text>
79
+ <Box
80
+ borderStyle="bold"
81
+ borderColor="green"
82
+ borderLeft
83
+ borderRight={false}
84
+ borderTop={false}
85
+ borderBottom={false}
86
+ paddingLeft={1}
87
+ >
88
+ <Text>export async function login(token: string)</Text>
89
+ </Box>
90
+ </Box>
91
+ </Box>
92
+ )
93
+ }
94
+
95
+ function StatusBar() {
96
+ return (
97
+ <Box flexDirection="row" justifyContent="space-between" paddingX={1}>
98
+ <Text color="gray" dim>
99
+ <Text color="blue">0:00</Text>
100
+ {" ⏎ send tab auto ^L clear esc quit"}
101
+ </Text>
102
+ <Text color="gray" dim>
103
+ ctx {"█".repeat(4)}
104
+ {"░".repeat(16)} 20% · $0.12
105
+ </Text>
106
+ </Box>
107
+ )
108
+ }
109
+
110
+ /** Footer that owns its own inputText state (lifted down from parent). */
111
+ function LiftedFooter() {
112
+ const [inputText, setInputText] = useState("")
113
+ return (
114
+ <Box flexDirection="column">
115
+ <Box borderStyle="round" paddingX={1}>
116
+ <TextInput value={inputText} onChange={setInputText} prompt="> " isActive={true} />
117
+ </Box>
118
+ {!useSimple && <StatusBar />}
119
+ </Box>
120
+ )
121
+ }
122
+
123
+ const useLifted = process.argv.includes("--lifted")
124
+
125
+ function TestApp({ itemCount }: { itemCount: number }) {
126
+ const [items] = useState<Item[]>(() =>
127
+ Array.from({ length: itemCount }, (_, i) => ({
128
+ id: `item-${i}`,
129
+ text:
130
+ i % 2 === 0
131
+ ? `Fix the login bug in auth.ts — expired tokens throw instead of refreshing.`
132
+ : `Found it. The expiry check compares seconds (jwt.exp) to milliseconds (Date.now()). Fixing now.`,
133
+ role: i % 2 === 0 ? "user" : "assistant",
134
+ frozen: false,
135
+ })),
136
+ )
137
+ // When NOT lifted: inputText lives in parent (causes full re-render)
138
+ const [inputText, setInputText] = useState("")
139
+
140
+ // Optional timers (like ai-chat)
141
+ const [_pulse, setPulse] = useState(false)
142
+ const [_elapsed, setElapsed] = useState(0)
143
+ const startRef = useRef(Date.now())
144
+
145
+ useEffect(() => {
146
+ if (!useTimers) return
147
+ const t1 = setInterval(() => setPulse((p) => !p), 800)
148
+ const t2 = setInterval(() => setElapsed(Math.floor((Date.now() - startRef.current) / 1000)), 1000)
149
+ return () => {
150
+ clearInterval(t1)
151
+ clearInterval(t2)
152
+ }
153
+ }, [])
154
+
155
+ return (
156
+ <Box flexDirection="column">
157
+ <ScrollbackList
158
+ items={items}
159
+ keyExtractor={(item) => item.id}
160
+ isFrozen={(item) => item.frozen}
161
+ footer={
162
+ useLifted ? (
163
+ <LiftedFooter />
164
+ ) : (
165
+ <Box flexDirection="column">
166
+ <Box borderStyle="round" paddingX={1}>
167
+ <TextInput value={inputText} onChange={setInputText} prompt="> " isActive={true} />
168
+ </Box>
169
+ {!useSimple && <StatusBar />}
170
+ </Box>
171
+ )
172
+ }
173
+ footerHeight={useSimple ? 3 : 4}
174
+ width={120}
175
+ stdout={{ write: () => true }}
176
+ >
177
+ {(item, index) => {
178
+ const isLatest = index === items.length - 1
179
+ return useSimple ? <SimpleItem item={item} isLatest={isLatest} /> : <ComplexItem item={item} />
180
+ }}
181
+ </ScrollbackList>
182
+ </Box>
183
+ )
184
+ }
185
+
186
+ async function benchmark(itemCount: number, label: string) {
187
+ const render = createRenderer({ cols: 120, rows: 40 })
188
+ const app = render(<TestApp itemCount={itemCount} />)
189
+
190
+ // Warm up
191
+ await app.press("x")
192
+ await app.press("Backspace")
193
+
194
+ const chars = "the quick brown fox jumps over the lazy dog"
195
+ const times: number[] = []
196
+
197
+ for (const char of chars) {
198
+ const t0 = performance.now()
199
+ await app.press(char)
200
+ times.push(performance.now() - t0)
201
+ }
202
+
203
+ times.sort((a, b) => a - b)
204
+ const avg = times.reduce((a, b) => a + b) / times.length
205
+ const p50 = times[Math.floor(times.length * 0.5)]!
206
+ const p95 = times[Math.floor(times.length * 0.95)]!
207
+ const max = times[times.length - 1]!
208
+
209
+ // Pipeline breakdown from last frame
210
+ const pipeline = (globalThis as any).__silvery_last_pipeline
211
+ const pipelineStr = pipeline
212
+ ? Object.entries(pipeline)
213
+ .filter(([, v]) => typeof v === "number" && (v as number) > 0.05)
214
+ .map(([k, v]) => `${k}=${(v as number).toFixed(1)}ms`)
215
+ .join(" ")
216
+ : "n/a"
217
+
218
+ console.log(
219
+ `${label.padEnd(20)} avg=${avg.toFixed(1)}ms p50=${p50.toFixed(1)}ms p95=${p95.toFixed(1)}ms max=${max.toFixed(1)}ms pipeline=[${pipelineStr}]`,
220
+ )
221
+ }
222
+
223
+ console.log("=== ScrollbackList Typing Performance ===\n")
224
+ await benchmark(1, "1 item")
225
+ await benchmark(5, "5 items")
226
+ await benchmark(10, "10 items")
227
+ await benchmark(20, "20 items")
228
+ await benchmark(50, "50 items")
229
+ await benchmark(100, "100 items")
230
+ console.log("\nDone.")