nish 0.35.0 → 0.37.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.
- package/docs/AI.md +50 -4
- package/docs/INSTALL.md +73 -59
- package/package.json +19 -5
- package/runtime/async.d.mts +2 -0
- package/runtime/async.mjs +7 -0
- package/runtime/nish.d.ts +4849 -430
- package/runtime/nish.h +25 -0
- package/runtime/page.d.ts +53 -0
- package/runtime/page.mjs +146 -0
- package/runtime/runtime-async.c +557 -0
- package/runtime/runtime-wasm.c +63 -7
- package/runtime/vite.d.ts +30 -0
- package/runtime/vite.mjs +150 -0
- package/scripts/build.sh +252 -15
- package/scripts/fuzz.sh +1 -0
- package/scripts/undocumented.txt +14 -0
- package/std/README.md +107 -82
- package/std/base64.ts +34 -5
- package/std/bytes.ts +279 -59
- package/std/cli-args.ts +296 -70
- package/std/cli-flags.ts +184 -19
- package/std/cli.ts +475 -54
- package/std/collections.ts +121 -23
- package/std/compress/checksum.ts +44 -14
- package/std/compress/deflate.ts +224 -111
- package/std/compress/gzip.ts +217 -53
- package/std/compress/zlib.ts +63 -7
- package/std/crypto/aes.ts +128 -52
- package/std/crypto/base64url.ts +29 -5
- package/std/crypto/bcrypt-batch.ts +101 -34
- package/std/crypto/bcrypt.ts +93 -27
- package/std/crypto/chacha20poly1305.ts +69 -14
- package/std/crypto/ct.ts +32 -13
- package/std/crypto/ed25519.ts +66 -16
- package/std/crypto/hkdf.ts +198 -13
- package/std/crypto/hmac.ts +242 -37
- package/std/crypto/jwt.ts +204 -72
- package/std/crypto/p256.ts +222 -72
- package/std/crypto/p384.ts +51 -23
- package/std/crypto/pki-roots.ts +32 -2
- package/std/crypto/pki.ts +99 -30
- package/std/crypto/rsa.ts +56 -22
- package/std/crypto/sha1.ts +3 -0
- package/std/crypto/sha256.ts +24 -1
- package/std/crypto/sha512.ts +99 -14
- package/std/crypto/x25519.ts +163 -24
- package/std/crypto/x509-random.ts +10 -0
- package/std/crypto/x509.ts +133 -66
- package/std/host/ring.ts +124 -0
- package/std/json-equal.ts +31 -11
- package/std/json-format.ts +112 -13
- package/std/json-number.ts +76 -26
- package/std/json-pointer.ts +23 -1
- package/std/json-tokenizer.ts +126 -51
- package/std/json-typed.ts +757 -96
- package/std/json-value.ts +438 -157
- package/std/json.ts +170 -38
- package/std/map.ts +25 -7
- package/std/math.ts +36 -4
- package/std/net/buffer-pool.ts +42 -11
- package/std/net/dns-config.ts +109 -23
- package/std/net/dns-message.ts +62 -7
- package/std/net/dns-resolver.ts +247 -53
- package/std/net/dns.ts +203 -35
- package/std/net/fetch.ts +1267 -290
- package/std/net/handler.ts +296 -80
- package/std/net/hpack.ts +290 -77
- package/std/net/http-compress.ts +152 -36
- package/std/net/http-fields.ts +85 -9
- package/std/net/http1-client-codec.ts +129 -44
- package/std/net/http1-codec.ts +279 -114
- package/std/net/http1-server.ts +409 -139
- package/std/net/http2-frame.ts +289 -51
- package/std/net/http2-tls.ts +152 -49
- package/std/net/http2.ts +314 -160
- package/std/net/http3-frame.ts +165 -27
- package/std/net/http3-server.ts +282 -131
- package/std/net/http3.ts +377 -173
- package/std/net/outbound.ts +37 -3
- package/std/net/postgres-carrier.ts +144 -8
- package/std/net/postgres.ts +898 -260
- package/std/net/prefork.ts +202 -65
- package/std/net/qpack.ts +120 -32
- package/std/net/quic-conn-ack.ts +60 -12
- package/std/net/quic-conn-cid.ts +135 -27
- package/std/net/quic-conn-params.ts +109 -22
- package/std/net/quic-datagram.ts +50 -3
- package/std/net/quic-frame.ts +333 -23
- package/std/net/quic-listener.ts +161 -75
- package/std/net/quic-packet.ts +373 -73
- package/std/net/quic-recovery.ts +287 -65
- package/std/net/quic-stream.ts +436 -146
- package/std/net/quic.ts +693 -323
- package/std/net/router.ts +87 -40
- package/std/net/tls/client.ts +167 -51
- package/std/net/tls/codec.ts +494 -68
- package/std/net/tls/record-client.ts +140 -73
- package/std/net/tls/record-server.ts +233 -105
- package/std/net/tls/record.ts +142 -36
- package/std/net/tls/schedule.ts +235 -26
- package/std/net/tls/ticket.ts +161 -40
- package/std/net/tls/verify.ts +57 -10
- package/std/net/tls-tcp.ts +308 -95
- package/std/net/tls.ts +276 -102
- package/std/net/upstream.ts +81 -45
- package/std/net/url.ts +109 -22
- package/std/net/websocket.ts +140 -26
- package/std/net/webtransport.ts +243 -106
- package/std/pair.ts +10 -2
- package/std/random.ts +42 -15
- package/std/regex.ts +422 -257
- package/std/region.ts +70 -15
- package/std/secret.ts +52 -11
- package/std/sort.ts +17 -2
- package/std/sqlite.ts +256 -35
- package/std/sync/ring.ts +229 -56
- package/std/sync/shm.ts +203 -23
- package/std/term-io.ts +154 -78
- package/std/term-pixels.ts +144 -19
- package/std/term-probe.ts +77 -35
- package/std/term-rate.ts +199 -118
- package/std/term.ts +400 -175
- package/std/testing.ts +130 -32
- package/std/text.ts +84 -17
- package/std/threads.ts +248 -56
- package/std/yaml-json.ts +111 -97
- package/std/yaml-lines.ts +47 -15
- package/std/yaml-write.ts +45 -30
- package/std/yaml.ts +764 -384
package/std/term-io.ts
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* `
|
|
2
|
+
* `nish/term-io` — opening a terminal, drawing frames, reading events and
|
|
3
3
|
* leaving it as it was: the one entry point that uses the other term modules
|
|
4
4
|
* and does the I/O.
|
|
5
5
|
*
|
|
6
|
-
*
|
|
7
|
-
* import { defaultTerminalOptions, runTerminal, TermInput, Frame } from "nish/term-io";
|
|
8
|
-
* ```
|
|
6
|
+
* import { defaultTerminalOptions, runTerminal, TermInput, Frame } from "nish/term-io"
|
|
9
7
|
*
|
|
10
8
|
* - `runTerminal(options, state, update, render)` is the whole program loop:
|
|
11
9
|
* it opens the terminal, runs `update(state, input)` at
|
|
@@ -24,24 +22,27 @@
|
|
|
24
22
|
* terminal has: the kitty keyboard protocol (`CSI > 11 u`: key releases and
|
|
25
23
|
* Ctrl-C as a key), mouse reports (1000, 1002 or 1003, in SGR form 1006, with
|
|
26
24
|
* pixel coordinates 1016 when the terminal has them), bracketed paste (2004)
|
|
27
|
-
* and focus reports (1004).
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
25
|
+
* and focus reports (1004).
|
|
26
|
+
*
|
|
27
|
+
* Before each change reaches the terminal, the runtime's restore string
|
|
28
|
+
* (`setTerminalRestore`) is set to undo everything — cancel any half-written
|
|
29
|
+
* escape (CAN), end synchronized output, delete the kitty image, pop the kitty
|
|
30
|
+
* keyboard flags, turn mouse, paste and focus reports off, reset the style,
|
|
31
|
+
* show the cursor and leave the alternate screen — so a panic or a signal
|
|
32
|
+
* leaves the terminal as it was.
|
|
33
33
|
*
|
|
34
34
|
* `present()` draws the `Screen` and the `Framebuffer`, if the options gave
|
|
35
35
|
* one, with the best backend the terminal has capped by the controller
|
|
36
36
|
* (`min(caps.graphics, decision.backendCap)`), at the controller's resolution
|
|
37
|
-
* divisor and palette, wrapped in synchronized output when the terminal has
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
37
|
+
* divisor and palette, wrapped in synchronized output when the terminal has it,
|
|
38
|
+
* as one write. It follows the frame with a DSR 5 query (`CSI 5 n`), whose
|
|
39
|
+
* answer (`CSI 0 n`) acknowledges the frame to the controller and marks the
|
|
40
|
+
* input before it as delivered to `KeyState`.
|
|
41
|
+
*
|
|
42
|
+
* No answer from the terminal reaches the program: one that arrives after the
|
|
43
|
+
* probe gave up (a slow link) is taken, and a cell size among them is learned.
|
|
44
|
+
* `close` reads the acknowledgements still in flight before it leaves raw mode,
|
|
45
|
+
* so none is echoed onto the shell's line.
|
|
45
46
|
*
|
|
46
47
|
* A frame's cost to the controller is the program's `update` and `render`
|
|
47
48
|
* since the last frame plus encoding it, not the write: a write that blocks
|
|
@@ -143,8 +144,9 @@ export class TerminalOptions {
|
|
|
143
144
|
quitOnCtrlC: boolean
|
|
144
145
|
/** How long after a held key's last repeat it counts as released, without the kitty protocol (100). */
|
|
145
146
|
releaseGapMs: f64
|
|
146
|
-
/** The framebuffer's
|
|
147
|
+
/** The framebuffer's width in pixels, or 0 for none (0). Either side 0 means no framebuffer. */
|
|
147
148
|
framebufferWidth: i32
|
|
149
|
+
/** The framebuffer's height in pixels, or 0 for none (0). */
|
|
148
150
|
framebufferHeight: i32
|
|
149
151
|
/** The corner (`CORNER_*` from `nish/term-rate`) to draw the stats overlay in, or -1 for none (-1). */
|
|
150
152
|
overlay: i32
|
|
@@ -156,6 +158,7 @@ export class TerminalOptions {
|
|
|
156
158
|
*/
|
|
157
159
|
graphics: string
|
|
158
160
|
|
|
161
|
+
/** The defaults each field's comment gives in brackets. */
|
|
159
162
|
constructor() {
|
|
160
163
|
this.stepsPerSecond = 60
|
|
161
164
|
this.minFps = 10
|
|
@@ -176,7 +179,11 @@ export class TerminalOptions {
|
|
|
176
179
|
}
|
|
177
180
|
}
|
|
178
181
|
|
|
179
|
-
/**
|
|
182
|
+
/**
|
|
183
|
+
* The options a program starts from and changes what it needs in.
|
|
184
|
+
*
|
|
185
|
+
* @returns A fresh `TerminalOptions` holding the default each field's comment gives.
|
|
186
|
+
*/
|
|
180
187
|
export const defaultTerminalOptions = (): TerminalOptions => new TerminalOptions()
|
|
181
188
|
|
|
182
189
|
/** The monotonic clock in milliseconds. */
|
|
@@ -258,68 +265,91 @@ const termIoParam = (e: TermEvent, i: i32): i32 => (i < toI32(e.params.length) ?
|
|
|
258
265
|
* Made by `openTerminal`.
|
|
259
266
|
*/
|
|
260
267
|
export class Terminal {
|
|
268
|
+
/** The options it was opened with. */
|
|
261
269
|
options: TerminalOptions
|
|
270
|
+
/** What the probe found the terminal can do. */
|
|
262
271
|
caps: TermCaps
|
|
263
272
|
/** The cell grid the program draws into, the terminal's size. */
|
|
264
273
|
screen: Screen
|
|
274
|
+
/** Half-block pixels drawn on `screen`, two to a cell. */
|
|
265
275
|
canvas: Canvas
|
|
266
276
|
/** The pixels the program draws into, `options.framebufferWidth × framebufferHeight`. */
|
|
267
277
|
framebuffer: Framebuffer
|
|
268
278
|
/** The framebuffer shrunk by the controller's resolution divisor. */
|
|
269
279
|
scaled: Framebuffer
|
|
280
|
+
/** The buffer a kitty or sixel frame is encoded into before it is written. */
|
|
270
281
|
pixels: PixelBytes
|
|
282
|
+
/** Turns the bytes read from stdin into events. */
|
|
271
283
|
decoder: KeyDecoder
|
|
284
|
+
/** Which keys are down, and the releases made up from silence without the kitty protocol. */
|
|
272
285
|
keys: KeyState
|
|
286
|
+
/** Paces the frames and chooses their detail from how fast the terminal acknowledges them. */
|
|
273
287
|
rate: RateController
|
|
274
288
|
/** The events read during the probe, handed to the program first. */
|
|
275
|
-
held: TermEvent[]
|
|
289
|
+
private held: TermEvent[]
|
|
276
290
|
/** The send times of the frames not yet acknowledged, a ring of `TERM_IO_ACKS`. */
|
|
277
|
-
ackSent: f64[]
|
|
278
|
-
ackHead: i32
|
|
291
|
+
private ackSent: f64[]
|
|
292
|
+
private ackHead: i32
|
|
293
|
+
/** The frames sent whose acknowledgement has not come back, up to the most the ring holds. */
|
|
279
294
|
ackCount: i32
|
|
280
|
-
readBuf: u8[]
|
|
295
|
+
private readBuf: u8[]
|
|
281
296
|
/** Where the framebuffer is drawn, for `rectCols × rectRows` cells and `rectBackend`. */
|
|
282
|
-
rect: PixelRect
|
|
297
|
+
private rect: PixelRect
|
|
283
298
|
/** When the last frame acknowledgement arrived, or -1. */
|
|
284
|
-
lastAckMs: f64
|
|
299
|
+
private lastAckMs: f64
|
|
285
300
|
/** The last acknowledged frame's own round trip, in milliseconds, which the controller's smoothed one trails. */
|
|
286
|
-
lastRttMs: f64
|
|
287
|
-
/**
|
|
301
|
+
private lastRttMs: f64
|
|
302
|
+
/**
|
|
303
|
+
* The program's work toward the next frame (its `update` and `render`), in nanoseconds, which `present` adds to the frame's cost.
|
|
304
|
+
* @internal Used by `runTerminal`.
|
|
305
|
+
*/
|
|
288
306
|
workNs: f64
|
|
289
307
|
/** When the last frame's write began, on the monotonic clock. */
|
|
290
|
-
sendStartNs: i64
|
|
308
|
+
private sendStartNs: i64
|
|
291
309
|
/** The cells, backend and framebuffer size `rect` was fitted to. */
|
|
292
|
-
rectCols: i32
|
|
293
|
-
rectRows: i32
|
|
294
|
-
rectBackend: i32
|
|
295
|
-
rectWidth: i32
|
|
296
|
-
rectHeight: i32
|
|
310
|
+
private rectCols: i32
|
|
311
|
+
private rectRows: i32
|
|
312
|
+
private rectBackend: i32
|
|
313
|
+
private rectWidth: i32
|
|
314
|
+
private rectHeight: i32
|
|
297
315
|
/** The best backend the terminal has, a `GRAPHICS_*`. */
|
|
298
316
|
graphics: i32
|
|
299
317
|
/** The backend the last frame used. */
|
|
300
318
|
backend: i32
|
|
319
|
+
/** A cell's width in pixels as the terminal reported it, at most 256; 0 when it did not say. */
|
|
301
320
|
cellWidthPx: i32
|
|
321
|
+
/** A cell's height in pixels as the terminal reported it, at most 256; 0 when it did not say. */
|
|
302
322
|
cellHeightPx: i32
|
|
303
323
|
/** The size `terminalSize` last answered. */
|
|
304
|
-
sizeCols: i32
|
|
305
|
-
sizeRows: i32
|
|
324
|
+
private sizeCols: i32
|
|
325
|
+
private sizeRows: i32
|
|
306
326
|
/** The size last given to the `Screen`, before it clamped it, which `checkResize` compares. */
|
|
307
|
-
shownCols: i32
|
|
308
|
-
shownRows: i32
|
|
327
|
+
private shownCols: i32
|
|
328
|
+
private shownRows: i32
|
|
309
329
|
/** The mouse mode turned on, or 0. */
|
|
310
|
-
mouseMode: i32
|
|
311
|
-
open: boolean
|
|
312
|
-
kittyKeyboardOn: boolean
|
|
313
|
-
pixelMouseOn: boolean
|
|
314
|
-
pasteOn: boolean
|
|
315
|
-
focusOn: boolean
|
|
330
|
+
private mouseMode: i32
|
|
331
|
+
private open: boolean
|
|
332
|
+
private kittyKeyboardOn: boolean
|
|
333
|
+
private pixelMouseOn: boolean
|
|
334
|
+
private pasteOn: boolean
|
|
335
|
+
private focusOn: boolean
|
|
316
336
|
/** Whether a kitty image may be on the screen, so closing deletes it. */
|
|
317
|
-
kittyImage: boolean
|
|
337
|
+
private kittyImage: boolean
|
|
318
338
|
/** Whether the last frame sent pixels, so a framebuffer emptied since takes them off the screen. */
|
|
319
|
-
pixelsShown: boolean
|
|
339
|
+
private pixelsShown: boolean
|
|
320
340
|
/** Whether `screen` was drawn in 256 colours last frame. */
|
|
321
341
|
palette256: boolean
|
|
322
342
|
|
|
343
|
+
/**
|
|
344
|
+
* A terminal already in raw mode and probed, before anything is turned on;
|
|
345
|
+
* `openTerminal` makes one and turns on what the options ask for.
|
|
346
|
+
*
|
|
347
|
+
* @param options The program's options.
|
|
348
|
+
* @param caps What the probe found.
|
|
349
|
+
* @param cols The terminal's columns.
|
|
350
|
+
* @param rows The terminal's rows.
|
|
351
|
+
* @param held The events that arrived during the probe, which `readEvents` hands over first.
|
|
352
|
+
*/
|
|
323
353
|
constructor(options: TerminalOptions, caps: TermCaps, cols: i32, rows: i32, held: TermEvent[]) {
|
|
324
354
|
this.options = options
|
|
325
355
|
this.caps = caps
|
|
@@ -388,7 +418,7 @@ export class Terminal {
|
|
|
388
418
|
}
|
|
389
419
|
|
|
390
420
|
/** The bytes that undo everything turned on: what `close` writes and the runtime writes on a panic or signal. */
|
|
391
|
-
restoreString(): string {
|
|
421
|
+
private restoreString(): string {
|
|
392
422
|
let s: string = "\x18\x1b[?2026l"
|
|
393
423
|
if (this.kittyImage) {
|
|
394
424
|
s = s + kittyDelete(TERM_IO_IMAGE_ID)
|
|
@@ -409,12 +439,15 @@ export class Terminal {
|
|
|
409
439
|
}
|
|
410
440
|
|
|
411
441
|
/** Set the runtime's restore string to `restoreString()`, then write `bytes`, which turn something on. */
|
|
412
|
-
turnOn(bytes: string): void {
|
|
442
|
+
private turnOn(bytes: string): void {
|
|
413
443
|
setTerminalRestore(this.restoreString())
|
|
414
444
|
write(bytes)
|
|
415
445
|
}
|
|
416
446
|
|
|
417
|
-
/**
|
|
447
|
+
/**
|
|
448
|
+
* Turn on what the options ask for and the terminal has, the restore string kept ahead of each.
|
|
449
|
+
* @internal Called by `openTerminal`.
|
|
450
|
+
*/
|
|
418
451
|
enable(): void {
|
|
419
452
|
const o: TerminalOptions = this.options
|
|
420
453
|
if (o.kittyKeyboard && this.caps.kittyKeyboard) {
|
|
@@ -468,7 +501,7 @@ export class Terminal {
|
|
|
468
501
|
* round trip's bound passes. Before any answer the probe's round trip is
|
|
469
502
|
* the one known.
|
|
470
503
|
*/
|
|
471
|
-
drainAcks(): void {
|
|
504
|
+
private drainAcks(): void {
|
|
472
505
|
let owed: i32 = this.rate.unacked() + this.rate.stale
|
|
473
506
|
let rtt: f64 = this.rate.rttMs > this.lastRttMs ? this.rate.rttMs : this.lastRttMs
|
|
474
507
|
rtt = rtt > this.caps.probeRttMs ? rtt : this.caps.probeRttMs
|
|
@@ -500,7 +533,13 @@ export class Terminal {
|
|
|
500
533
|
}
|
|
501
534
|
}
|
|
502
535
|
|
|
503
|
-
/**
|
|
536
|
+
/**
|
|
537
|
+
* What the controller measured, with the backend the last frame actually
|
|
538
|
+
* used.
|
|
539
|
+
*
|
|
540
|
+
* @returns The controller's own stats object, updated in place on every tick, not a copy: a
|
|
541
|
+
* caller who keeps it past the next tick copies the fields it needs.
|
|
542
|
+
*/
|
|
504
543
|
stats(): TermStats {
|
|
505
544
|
const s: TermStats = this.rate.stats()
|
|
506
545
|
s.backend = this.backend
|
|
@@ -508,7 +547,7 @@ export class Terminal {
|
|
|
508
547
|
}
|
|
509
548
|
|
|
510
549
|
/** Read `terminalSize` into `sizeCols` and `sizeRows`; its array dies here. */
|
|
511
|
-
readSize(): void {
|
|
550
|
+
private readSize(): void {
|
|
512
551
|
const size: i32[] = terminalSize()
|
|
513
552
|
if (toI32(size.length) >= 2 && size[0] > 0 && size[1] > 0) {
|
|
514
553
|
this.sizeCols = size[0]
|
|
@@ -517,10 +556,12 @@ export class Terminal {
|
|
|
517
556
|
}
|
|
518
557
|
|
|
519
558
|
/**
|
|
520
|
-
*
|
|
521
|
-
*
|
|
522
|
-
*
|
|
523
|
-
*
|
|
559
|
+
* Take a new terminal size, if there is one: the `Screen` takes it and the
|
|
560
|
+
* framebuffer is fitted again. It compares the size the terminal answers, not
|
|
561
|
+
* the `Screen`'s, which clamps a huge one, so a huge terminal is not resized
|
|
562
|
+
* every frame.
|
|
563
|
+
*
|
|
564
|
+
* @returns True when the size changed since the last call.
|
|
524
565
|
*/
|
|
525
566
|
checkResize(): boolean {
|
|
526
567
|
this.readSize()
|
|
@@ -537,7 +578,7 @@ export class Terminal {
|
|
|
537
578
|
}
|
|
538
579
|
|
|
539
580
|
/** Forget the send times of the frames the controller gave up on, oldest first, so the ring holds only frames it still waits for. */
|
|
540
|
-
dropLost(): void {
|
|
581
|
+
private dropLost(): void {
|
|
541
582
|
while (this.ackCount > this.rate.unacked()) {
|
|
542
583
|
this.ackHead = (this.ackHead + 1) % TERM_IO_ACKS
|
|
543
584
|
this.ackCount = this.ackCount - 1
|
|
@@ -545,7 +586,7 @@ export class Terminal {
|
|
|
545
586
|
}
|
|
546
587
|
|
|
547
588
|
/** Remember that a frame acknowledgement was asked for at `nowMs`. */
|
|
548
|
-
ackPush(nowMs: f64): void {
|
|
589
|
+
private ackPush(nowMs: f64): void {
|
|
549
590
|
if (this.ackCount === TERM_IO_ACKS) {
|
|
550
591
|
this.ackHead = (this.ackHead + 1) % TERM_IO_ACKS
|
|
551
592
|
this.ackCount = this.ackCount - 1
|
|
@@ -559,7 +600,7 @@ export class Terminal {
|
|
|
559
600
|
* and, unless it is the late answer of a frame the controller gave up on,
|
|
560
601
|
* marks its frame's send time delivered.
|
|
561
602
|
*/
|
|
562
|
-
acked(count: i32, nowMs: f64): void {
|
|
603
|
+
private acked(count: i32, nowMs: f64): void {
|
|
563
604
|
for (let k: i32 = 0; k < count; k++) {
|
|
564
605
|
this.dropLost()
|
|
565
606
|
if (this.rate.stale === 0 && this.ackCount > 0) {
|
|
@@ -576,7 +617,7 @@ export class Terminal {
|
|
|
576
617
|
}
|
|
577
618
|
|
|
578
619
|
/** A reply other than an acknowledgement, which came after the probe: learn the cell size from it if the probe did not. */
|
|
579
|
-
lateReply(e: TermEvent): void {
|
|
620
|
+
private lateReply(e: TermEvent): void {
|
|
580
621
|
if (
|
|
581
622
|
this.cellWidthPx > 0 ||
|
|
582
623
|
e.introducer !== 91 ||
|
|
@@ -617,7 +658,7 @@ export class Terminal {
|
|
|
617
658
|
* `KeyState` into `out`, then the acknowledgements count. Every reply is
|
|
618
659
|
* the terminal's and is taken here, a late cell size learned.
|
|
619
660
|
*/
|
|
620
|
-
take(events: TermEvent[], nowMs: f64, out: TermEvent[]): void {
|
|
661
|
+
private take(events: TermEvent[], nowMs: f64, out: TermEvent[]): void {
|
|
621
662
|
let replies: i32 = 0
|
|
622
663
|
for (const e of events) {
|
|
623
664
|
if (e.type === EVENT_REPLY) {
|
|
@@ -645,11 +686,13 @@ export class Terminal {
|
|
|
645
686
|
}
|
|
646
687
|
|
|
647
688
|
/**
|
|
648
|
-
* Wait
|
|
649
|
-
* and
|
|
650
|
-
*
|
|
651
|
-
*
|
|
652
|
-
*
|
|
689
|
+
* Wait for input and append what it means for the program: keys, mouse, paste
|
|
690
|
+
* and focus events, the releases made up from silence, `EVENT_QUIT` for
|
|
691
|
+
* Ctrl-C. Frame acknowledgements are taken here and never appended.
|
|
692
|
+
*
|
|
693
|
+
* @param waitMs The longest wait in milliseconds; a negative wait has no limit.
|
|
694
|
+
* @param out Where the events go, after what it holds.
|
|
695
|
+
* @returns False once stdin has ended.
|
|
653
696
|
*/
|
|
654
697
|
readEvents(waitMs: i32, out: TermEvent[]): boolean {
|
|
655
698
|
if (toI32(this.held.length) > 0) {
|
|
@@ -693,7 +736,7 @@ export class Terminal {
|
|
|
693
736
|
}
|
|
694
737
|
|
|
695
738
|
/** Fit the framebuffer to the screen for `backend`, unless the sizes and backend are those of the last fit. */
|
|
696
|
-
fit(backend: i32): void {
|
|
739
|
+
private fit(backend: i32): void {
|
|
697
740
|
const fb: Framebuffer = this.framebuffer
|
|
698
741
|
if (
|
|
699
742
|
this.rectCols === this.screen.width &&
|
|
@@ -727,7 +770,7 @@ export class Terminal {
|
|
|
727
770
|
* began in `sendStartNs` and answers the bytes written. Its strings die
|
|
728
771
|
* here, so a frame loop does not keep them.
|
|
729
772
|
*/
|
|
730
|
-
send(withPixels: boolean): i32 {
|
|
773
|
+
private send(withPixels: boolean): i32 {
|
|
731
774
|
const body: string = this.screen.flush()
|
|
732
775
|
const image: string = withPixels ? this.pixels.text() : ""
|
|
733
776
|
const frame: string = this.caps.sync
|
|
@@ -819,8 +862,11 @@ const termIoRefusal = (): string => {
|
|
|
819
862
|
/**
|
|
820
863
|
* Open the terminal: refuse a pipe, enter raw mode and the alternate screen,
|
|
821
864
|
* hide the cursor, probe what the terminal can do and turn on what `options`
|
|
822
|
-
* asks for and it has. The restore string undoes all of it should the
|
|
823
|
-
*
|
|
865
|
+
* asks for and it has. The restore string undoes all of it should the program
|
|
866
|
+
* panic or be signalled before `close`.
|
|
867
|
+
*
|
|
868
|
+
* @param options What to turn on, and how to pace frames.
|
|
869
|
+
* @returns The open terminal, or an `Err` saying why not: stdin or stdout is not a terminal, or raw mode was refused.
|
|
824
870
|
*/
|
|
825
871
|
export const openTerminal = (options: TerminalOptions): Result<Terminal, string> => {
|
|
826
872
|
const refused: string = termIoRefusal()
|
|
@@ -868,15 +914,29 @@ export const openTerminal = (options: TerminalOptions): Result<Terminal, string>
|
|
|
868
914
|
|
|
869
915
|
/** What one `update` step gets: the events since the last step and which keys are down. */
|
|
870
916
|
export class TermInput {
|
|
917
|
+
/** The events since the last step, in the order they arrived. */
|
|
871
918
|
events: TermEvent[]
|
|
919
|
+
/** Which keys are down, for `isDown`. */
|
|
872
920
|
keys: KeyState
|
|
873
921
|
|
|
922
|
+
/**
|
|
923
|
+
* An input with no events yet.
|
|
924
|
+
*
|
|
925
|
+
* @param keys The terminal's key state, which `isDown` reads.
|
|
926
|
+
*/
|
|
874
927
|
constructor(keys: KeyState) {
|
|
875
928
|
this.events = []
|
|
876
929
|
this.keys = keys
|
|
877
930
|
}
|
|
878
931
|
|
|
879
|
-
/**
|
|
932
|
+
/**
|
|
933
|
+
* Whether a key is down.
|
|
934
|
+
*
|
|
935
|
+
* @param key The key's `KEY_` kind.
|
|
936
|
+
* @param code The code point for `KEY_CHAR`; it tells `KEY_CONTROL` and `KEY_UNKNOWN` keys apart
|
|
937
|
+
* too, and is ignored for the other kinds.
|
|
938
|
+
* @returns True from the key's press until its release, real or synthetic, or a loss of focus.
|
|
939
|
+
*/
|
|
880
940
|
isDown(key: i32, code: i32): boolean {
|
|
881
941
|
return this.keys.isDown(key, code)
|
|
882
942
|
}
|
|
@@ -884,13 +944,25 @@ export class TermInput {
|
|
|
884
944
|
|
|
885
945
|
/** What `render` draws into: the cells, the pixels, how far into the next step the clock is, and the stats. */
|
|
886
946
|
export class Frame {
|
|
947
|
+
/** The cell grid, the terminal's size. */
|
|
887
948
|
screen: Screen
|
|
949
|
+
/** The pixels, `options.framebufferWidth × framebufferHeight`; 0 by 0 when the options ask for none. */
|
|
888
950
|
framebuffer: Framebuffer
|
|
889
951
|
/** From 0 up to 1: how far past the last `update` step this frame is, to interpolate with. */
|
|
890
952
|
alpha: f64
|
|
953
|
+
/**
|
|
954
|
+
* What the controller measured: its live stats object, which the next tick
|
|
955
|
+
* updates in place, so copy a field to keep it past this frame.
|
|
956
|
+
*/
|
|
891
957
|
stats: TermStats
|
|
958
|
+
/** What the probe found the terminal can do. */
|
|
892
959
|
caps: TermCaps
|
|
893
960
|
|
|
961
|
+
/**
|
|
962
|
+
* A frame drawing into `term`'s screen and framebuffer, at `alpha` 0.
|
|
963
|
+
*
|
|
964
|
+
* @param term The open terminal.
|
|
965
|
+
*/
|
|
894
966
|
constructor(term: Terminal) {
|
|
895
967
|
this.screen = term.screen
|
|
896
968
|
this.framebuffer = term.framebuffer
|
|
@@ -939,14 +1011,18 @@ const termIoWait = (term: Terminal, step: FixedStep, nowMs: f64): i32 => {
|
|
|
939
1011
|
}
|
|
940
1012
|
|
|
941
1013
|
/**
|
|
942
|
-
* Open the terminal and run the program: `update
|
|
943
|
-
*
|
|
944
|
-
* `input.events`, and `render(state, frame)` whenever the controller allows a
|
|
1014
|
+
* Open the terminal and run the program: `update` once per fixed step
|
|
1015
|
+
* (`options.stepsPerSecond`) and `render` whenever the controller allows a
|
|
945
1016
|
* frame, which is then presented. A resize is an `EVENT_RESIZE` (`x` the new
|
|
946
|
-
* columns, `y` the rows) with the `Screen` already resized. The
|
|
947
|
-
*
|
|
948
|
-
*
|
|
949
|
-
*
|
|
1017
|
+
* columns, `y` the rows) with the `Screen` already resized. The terminal is
|
|
1018
|
+
* closed however the loop ends.
|
|
1019
|
+
*
|
|
1020
|
+
* @param options What to turn on, and how to pace steps and frames.
|
|
1021
|
+
* @param state The program's state, handed to every call.
|
|
1022
|
+
* @param update Runs one step on the events since the last in `input.events`; answers false to stop.
|
|
1023
|
+
* @param render Draws `state` into `frame`.
|
|
1024
|
+
* @returns `Ok(0)` once `update` answers false or the user quits (`EVENT_QUIT`); an `Err` for a
|
|
1025
|
+
* terminal that could not be opened, or stdin ending.
|
|
950
1026
|
*/
|
|
951
1027
|
export const runTerminal = <G>(
|
|
952
1028
|
options: TerminalOptions,
|