@valyrianjs/terminal 0.1.0 → 0.1.1
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.
- package/README.md +22 -22
- package/docs/api-reference.md +103 -103
- package/docs/cookbook.md +23 -23
- package/docs/core-concepts.md +50 -50
- package/docs/getting-started.md +22 -22
- package/docs/interaction-model.md +55 -55
- package/docs/local-demo.md +10 -10
- package/docs/session-runtime.md +97 -97
- package/package.json +1 -1
package/docs/session-runtime.md
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
# Session Runtime
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
This guide documents the interactive runtime of `valyrianjs-terminal`: when to use `mountTerminal()`, how to configure a session, and which capabilities `TerminalSession` exposes during its lifecycle.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
If you are starting from scratch, read `docs/core-concepts.md` first. For the general per-primitive interaction model, see `docs/interaction-model.md`. The focus here is operational: mounting, rerendering, streams, focus, keyboard, clipboard, and mouse.
|
|
6
6
|
|
|
7
|
-
##
|
|
7
|
+
## When to use `mountTerminal()`
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
Use `mountTerminal()` when you need a live session that:
|
|
10
10
|
|
|
11
|
-
-
|
|
12
|
-
-
|
|
13
|
-
-
|
|
14
|
-
-
|
|
15
|
-
-
|
|
11
|
+
- reevaluates the tree when external state changes
|
|
12
|
+
- preserves focus across renders
|
|
13
|
+
- dispatches keyboard input per session
|
|
14
|
+
- connects `stdin` and `stdout`
|
|
15
|
+
- responds to coordinates and mouse events
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
If you only want to inspect layout, generate snapshots, or produce static output, `renderTerminal()` is still the simplest option.
|
|
18
18
|
|
|
19
19
|
```tsx
|
|
20
20
|
/** @jsx v */
|
|
@@ -47,17 +47,17 @@ console.log(session.output());
|
|
|
47
47
|
|
|
48
48
|
## `TerminalMountOptions`
|
|
49
49
|
|
|
50
|
-
`mountTerminal(input, options?)`
|
|
50
|
+
`mountTerminal(input, options?)` accepts `TerminalMountOptions` with four integration points:
|
|
51
51
|
|
|
52
52
|
### `ansi?: boolean`
|
|
53
53
|
|
|
54
|
-
|
|
54
|
+
Enables incremental ANSI output to `stdout`. `session.ansiOutput()` is still available even if you do not enable this option; what changes is the session's automatic write behavior when it rerenders. If you do not enable it, the primary output written to `stdout` is plain text.
|
|
55
55
|
|
|
56
56
|
### `clipboard?: TerminalClipboardAdapter | false`
|
|
57
57
|
|
|
58
|
-
-
|
|
59
|
-
-
|
|
60
|
-
-
|
|
58
|
+
- if you pass an adapter, the session uses `readText()` and `writeText()` when handling shortcuts such as `CTRL_C` or `CTRL_V`
|
|
59
|
+
- if you pass `false`, system clipboard integration is disabled
|
|
60
|
+
- even with `clipboard: false`, `session.clipboard()` and `session.setClipboard()` still work as the session buffer
|
|
61
61
|
|
|
62
62
|
```tsx
|
|
63
63
|
/** @jsx v */
|
|
@@ -95,21 +95,21 @@ session.setClipboard("fallback value");
|
|
|
95
95
|
|
|
96
96
|
### `stdin?`
|
|
97
97
|
|
|
98
|
-
|
|
98
|
+
Input stream for keyboard and mouse. The session subscribes to `data`, tries to enable raw mode with `setRawMode(true)` if available, and calls `resume()` if the stream supports it.
|
|
99
99
|
|
|
100
|
-
|
|
100
|
+
Expected contract:
|
|
101
101
|
|
|
102
|
-
- `on("data", listener)`
|
|
103
|
-
- `off(...)`
|
|
104
|
-
- `setRawMode?(boolean)`
|
|
105
|
-
- `resume?()`
|
|
102
|
+
- `on("data", listener)` is required
|
|
103
|
+
- `off(...)` or `removeListener(...)` is optional for cleanup
|
|
104
|
+
- `setRawMode?(boolean)` is optional
|
|
105
|
+
- `resume?()` and `pause?()` are optional
|
|
106
106
|
|
|
107
107
|
### `stdout?`
|
|
108
108
|
|
|
109
|
-
|
|
109
|
+
Write target for the output produced on each rerender.
|
|
110
110
|
|
|
111
|
-
-
|
|
112
|
-
-
|
|
111
|
+
- with `ansi: false`, writes plain text
|
|
112
|
+
- with `ansi: true`, writes incremental ANSI diffs
|
|
113
113
|
|
|
114
114
|
```tsx
|
|
115
115
|
/** @jsx v */
|
|
@@ -132,21 +132,21 @@ const session = mountTerminal(app, {
|
|
|
132
132
|
});
|
|
133
133
|
```
|
|
134
134
|
|
|
135
|
-
##
|
|
135
|
+
## `TerminalSession` lifecycle
|
|
136
136
|
|
|
137
|
-
|
|
137
|
+
The session keeps a current frame, a current output, and the interactive state associated with the focusable tree. In practice, the cycle is:
|
|
138
138
|
|
|
139
|
-
1.
|
|
140
|
-
2.
|
|
141
|
-
3.
|
|
142
|
-
4. rerender
|
|
143
|
-
5.
|
|
139
|
+
1. mount with `mountTerminal()`
|
|
140
|
+
2. read with `output()` or `ansiOutput()`
|
|
141
|
+
3. mutate external state or dispatch interactions
|
|
142
|
+
4. rerender with `update()` or through events that already trigger rerenders
|
|
143
|
+
5. close with `destroy()`
|
|
144
144
|
|
|
145
145
|
### `update()`
|
|
146
146
|
|
|
147
|
-
|
|
147
|
+
Reevaluates `input`, recalculates the frame, and returns the current plain-text output.
|
|
148
148
|
|
|
149
|
-
|
|
149
|
+
Use it when you changed state outside the session handlers.
|
|
150
150
|
|
|
151
151
|
```tsx
|
|
152
152
|
const state = { count: 0 };
|
|
@@ -165,19 +165,19 @@ console.log(session.output());
|
|
|
165
165
|
|
|
166
166
|
### `output()`
|
|
167
167
|
|
|
168
|
-
|
|
168
|
+
Returns the current frame as plain text. It is the most direct way to:
|
|
169
169
|
|
|
170
|
-
-
|
|
171
|
-
-
|
|
172
|
-
-
|
|
170
|
+
- inspect the current state
|
|
171
|
+
- validate snapshots
|
|
172
|
+
- test focus and content without ANSI
|
|
173
173
|
|
|
174
174
|
### `ansiOutput()`
|
|
175
175
|
|
|
176
|
-
|
|
176
|
+
Returns the current frame serialized as full ANSI output, including the cursor and visual spans. It is useful when you want to:
|
|
177
177
|
|
|
178
|
-
-
|
|
179
|
-
-
|
|
180
|
-
-
|
|
178
|
+
- inspect cursor position
|
|
179
|
+
- validate selection or focus in an ANSI terminal
|
|
180
|
+
- integrate with a layer that consumes full escape sequences
|
|
181
181
|
|
|
182
182
|
```tsx
|
|
183
183
|
/** @jsx v */
|
|
@@ -195,35 +195,35 @@ console.log(ansi);
|
|
|
195
195
|
|
|
196
196
|
### `destroy()`
|
|
197
197
|
|
|
198
|
-
|
|
198
|
+
Removes `stdin` listeners, tries to exit raw mode with `setRawMode(false)`, and calls `pause()` if available.
|
|
199
199
|
|
|
200
|
-
|
|
200
|
+
Always call it when you mounted the session with a real `stdin` or a connected emitter.
|
|
201
201
|
|
|
202
202
|
```ts
|
|
203
203
|
session.destroy();
|
|
204
204
|
```
|
|
205
205
|
|
|
206
|
-
##
|
|
206
|
+
## Focus and coordinates
|
|
207
207
|
|
|
208
|
-
|
|
208
|
+
Focus lives at the session level. To participate in this flow, interactive nodes need an `id`.
|
|
209
209
|
|
|
210
210
|
### `focus(id)`
|
|
211
211
|
|
|
212
|
-
|
|
212
|
+
Focuses a node by `id`. Returns `true` if it found one.
|
|
213
213
|
|
|
214
|
-
### `focusNext()`
|
|
214
|
+
### `focusNext()` and `focusPrev()`
|
|
215
215
|
|
|
216
|
-
|
|
216
|
+
Walk the current focusable-element order. `dispatchKey("TAB")` and `dispatchKey("SHIFT_TAB")` use the same flow.
|
|
217
217
|
|
|
218
218
|
### `focusAt(x, y)`
|
|
219
219
|
|
|
220
|
-
|
|
220
|
+
Looks up the hitbox at the current coordinates, updates semantic hover when applicable, and focuses that node.
|
|
221
221
|
|
|
222
222
|
### `clickAt(x, y)`
|
|
223
223
|
|
|
224
|
-
-
|
|
225
|
-
-
|
|
226
|
-
-
|
|
224
|
+
- if it lands on a `Button`, it triggers its action
|
|
225
|
+
- if it lands on an `Input`, it focuses it and places the cursor based on the coordinate
|
|
226
|
+
- if it lands on another focusable surface, it moves focus there
|
|
227
227
|
|
|
228
228
|
```tsx
|
|
229
229
|
const session = mountTerminal(() => (
|
|
@@ -237,14 +237,14 @@ session.focusAt(2, 1);
|
|
|
237
237
|
session.clickAt(3, 2);
|
|
238
238
|
```
|
|
239
239
|
|
|
240
|
-
##
|
|
240
|
+
## Per-session keyboard dispatch
|
|
241
241
|
|
|
242
|
-
`dispatchKey(key)`
|
|
242
|
+
`dispatchKey(key)` processes a normalized key against the focused node and returns the current plain-text output.
|
|
243
243
|
|
|
244
|
-
|
|
244
|
+
Shortcuts supported by the public API and observable in tests:
|
|
245
245
|
|
|
246
|
-
-
|
|
247
|
-
- `Input`:
|
|
246
|
+
- global: `TAB`, `SHIFT_TAB`
|
|
247
|
+
- `Input`: single-byte characters, `ENTER`, `LEFT`, `RIGHT`, `SHIFT_LEFT`, `SHIFT_RIGHT`, `ALT_LEFT`, `ALT_RIGHT`, `HOME`, `END`, `CTRL_A`, `CTRL_C`, `CTRL_X`, `CTRL_V`, `BACKSPACE`, `DELETE`
|
|
248
248
|
- `Button`: `ENTER`, `SPACE`
|
|
249
249
|
- `List`: `UP`, `DOWN`, `LEFT`, `RIGHT`, `ENTER`
|
|
250
250
|
- `ScrollView`: `UP`, `DOWN`
|
|
@@ -275,14 +275,14 @@ session.dispatchKey("C");
|
|
|
275
275
|
session.dispatchKey("ENTER");
|
|
276
276
|
```
|
|
277
277
|
|
|
278
|
-
## Clipboard adapter
|
|
278
|
+
## Clipboard adapter and `clipboard: false`
|
|
279
279
|
|
|
280
|
-
|
|
280
|
+
The session separates two concepts:
|
|
281
281
|
|
|
282
|
-
-
|
|
283
|
-
-
|
|
282
|
+
- the session clipboard buffer: `clipboard()` and `setClipboard()`
|
|
283
|
+
- system integration: `options.clipboard`
|
|
284
284
|
|
|
285
|
-
|
|
285
|
+
With an adapter, input shortcuts read from and write to that adapter. Without an adapter, the session keeps a local value. With `clipboard: false`, external integration is disabled, but you can still use the local session buffer for tests or manual integrations.
|
|
286
286
|
|
|
287
287
|
```tsx
|
|
288
288
|
const state = { value: "abcd" };
|
|
@@ -305,17 +305,17 @@ session.dispatchKey("END");
|
|
|
305
305
|
session.dispatchKey("CTRL_V");
|
|
306
306
|
```
|
|
307
307
|
|
|
308
|
-
##
|
|
308
|
+
## `stdin` and `stdout` streams
|
|
309
309
|
|
|
310
|
-
|
|
310
|
+
Connecting streams makes the session useful as a CLI runtime, not just as a test helper.
|
|
311
311
|
|
|
312
|
-
|
|
312
|
+
Observable behavior:
|
|
313
313
|
|
|
314
|
-
-
|
|
315
|
-
-
|
|
316
|
-
-
|
|
317
|
-
-
|
|
318
|
-
-
|
|
314
|
+
- on mount, the session subscribes to `stdin.on("data", listener)`
|
|
315
|
+
- if `stdin.setRawMode` exists, it tries `setRawMode(true)`
|
|
316
|
+
- if `stdin.resume` exists, it tries `resume()`
|
|
317
|
+
- on each rerender, if `stdout.write` exists, the session writes the current frame
|
|
318
|
+
- on destroy, it tries to unsubscribe and restore raw mode
|
|
319
319
|
|
|
320
320
|
```tsx
|
|
321
321
|
/** @jsx v */
|
|
@@ -351,12 +351,12 @@ session.destroy();
|
|
|
351
351
|
|
|
352
352
|
## ANSI output
|
|
353
353
|
|
|
354
|
-
|
|
354
|
+
There are two practical ways to work with ANSI:
|
|
355
355
|
|
|
356
|
-
- `ansi: true` + `stdout`:
|
|
357
|
-
- `session.ansiOutput()`:
|
|
356
|
+
- `ansi: true` + `stdout`: the session emits incremental diffs on each rerender
|
|
357
|
+
- `session.ansiOutput()`: retrieves the full ANSI frame for the current state
|
|
358
358
|
|
|
359
|
-
|
|
359
|
+
This is especially useful when the consuming terminal needs to minimize repaints or when you want to verify cursor, focus, and selection with real escape sequences.
|
|
360
360
|
|
|
361
361
|
```tsx
|
|
362
362
|
const state = { value: "AB" };
|
|
@@ -379,15 +379,15 @@ session.dispatchKey("LEFT");
|
|
|
379
379
|
console.log(session.ansiOutput());
|
|
380
380
|
```
|
|
381
381
|
|
|
382
|
-
##
|
|
382
|
+
## SGR mouse, wheel, and pointer capture
|
|
383
383
|
|
|
384
|
-
|
|
384
|
+
When you connect `stdin`, the session accepts SGR mouse sequences and translates them into practical interaction over the current frame.
|
|
385
385
|
|
|
386
|
-
### Press, drag
|
|
386
|
+
### Press, drag, and release
|
|
387
387
|
|
|
388
|
-
- `press`:
|
|
389
|
-
- `drag`:
|
|
390
|
-
- `release`:
|
|
388
|
+
- `press`: focuses and activates the hitbox at the coordinate
|
|
389
|
+
- `drag`: if an `Input` is active through the mouse, it extends selection; in `List` and `ScrollView`, it updates row hover
|
|
390
|
+
- `release`: ends mouse selection and closes capture when applicable
|
|
391
391
|
|
|
392
392
|
```ts
|
|
393
393
|
stdin.emit("data", "\u001b[<0;4;1M");
|
|
@@ -395,34 +395,34 @@ stdin.emit("data", "\u001b[<32;7;1M");
|
|
|
395
395
|
stdin.emit("data", "\u001b[<0;7;1m");
|
|
396
396
|
```
|
|
397
397
|
|
|
398
|
-
|
|
398
|
+
That pattern is enough to select text inside an `Input` by coordinates.
|
|
399
399
|
|
|
400
400
|
### Wheel
|
|
401
401
|
|
|
402
|
-
|
|
402
|
+
Wheel input is dispatched as vertical navigation on the node under the pointer:
|
|
403
403
|
|
|
404
|
-
- `wheel-up`
|
|
405
|
-
- `wheel-down`
|
|
404
|
+
- `wheel-up` is equivalent to `dispatchKey("UP")`
|
|
405
|
+
- `wheel-down` is equivalent to `dispatchKey("DOWN")`
|
|
406
406
|
|
|
407
407
|
```ts
|
|
408
408
|
stdin.emit("data", "\u001b[<65;2;1M");
|
|
409
409
|
```
|
|
410
410
|
|
|
411
|
-
|
|
411
|
+
In a focused `ScrollView`, or one resolved by coordinates, that scrolls the viewport.
|
|
412
412
|
|
|
413
|
-
### Pointer capture
|
|
413
|
+
### Pointer capture in practice
|
|
414
414
|
|
|
415
|
-
`pointerCapture`
|
|
415
|
+
`pointerCapture` currently applies to `List` and `ScrollView`.
|
|
416
416
|
|
|
417
|
-
|
|
417
|
+
Without `pointerCapture`, semantic hover depends on the drag staying inside the visible hitbox. With `pointerCapture`, the session keeps the interaction during the drag even if the pointer leaves the initial area, and triggers:
|
|
418
418
|
|
|
419
419
|
- `oncapturestart`
|
|
420
420
|
- `oncaptureend`
|
|
421
421
|
|
|
422
|
-
|
|
422
|
+
This is useful for:
|
|
423
423
|
|
|
424
|
-
-
|
|
425
|
-
- scroll views
|
|
424
|
+
- lists that need to keep tracking the active row during a drag
|
|
425
|
+
- scroll views that keep hover or release behavior even if the pointer ends outside the viewport
|
|
426
426
|
|
|
427
427
|
```tsx
|
|
428
428
|
const session = mountTerminal(() => (
|
|
@@ -442,10 +442,10 @@ stdin.emit("data", "\u001b[<32;99;99M");
|
|
|
442
442
|
stdin.emit("data", "\u001b[<0;99;99m");
|
|
443
443
|
```
|
|
444
444
|
|
|
445
|
-
##
|
|
445
|
+
## Practical recommendations
|
|
446
446
|
|
|
447
|
-
-
|
|
448
|
-
-
|
|
449
|
-
-
|
|
450
|
-
-
|
|
451
|
-
-
|
|
447
|
+
- use `renderTerminal()` for snapshots and `mountTerminal()` for interactive runtime
|
|
448
|
+
- give stable `id` values to `Input`, `Button`, `List`, and `ScrollView` if you plan to use focus, coordinates, or programmatic dispatch
|
|
449
|
+
- call `update()` only when you changed state outside handlers already connected to the session
|
|
450
|
+
- always call `destroy()` when mounting with `stdin`
|
|
451
|
+
- use `clipboard: false` in tests or environments where you do not want to depend on the system clipboard
|