@amritk/nish-x86_64-linux 0.35.0 → 0.36.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/INSTALL.md +6 -1
- package/bin/nish +0 -0
- package/docs/AI.md +9 -2
- package/package.json +1 -1
- package/runtime/nish.d.ts +4612 -430
- package/runtime/vite.d.ts +30 -0
- package/runtime/vite.mjs +150 -0
- package/scripts/build.sh +108 -13
- package/std/base64.ts +34 -5
- package/std/bytes.ts +256 -40
- 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 +80 -9
- package/std/crypto/base64url.ts +22 -0
- package/std/crypto/bcrypt-batch.ts +88 -21
- package/std/crypto/bcrypt.ts +60 -0
- package/std/crypto/chacha20poly1305.ts +53 -0
- package/std/crypto/ct.ts +32 -13
- package/std/crypto/ed25519.ts +47 -0
- package/std/crypto/hkdf.ts +198 -13
- package/std/crypto/hmac.ts +235 -31
- package/std/crypto/jwt.ts +192 -63
- package/std/crypto/p256.ts +159 -16
- package/std/crypto/p384.ts +28 -3
- package/std/crypto/pki-roots.ts +32 -2
- package/std/crypto/pki.ts +81 -15
- package/std/crypto/rsa.ts +33 -3
- 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 +152 -14
- package/std/crypto/x509-random.ts +10 -0
- package/std/crypto/x509.ts +67 -7
- package/std/json-equal.ts +19 -0
- package/std/json-format.ts +112 -13
- package/std/json-number.ts +64 -14
- package/std/json-pointer.ts +23 -1
- package/std/json-tokenizer.ts +100 -29
- package/std/json-typed.ts +713 -66
- package/std/json-value.ts +400 -124
- package/std/json.ts +138 -10
- 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 +151 -61
- package/std/net/handler.ts +278 -80
- package/std/net/hpack.ts +262 -52
- package/std/net/http-compress.ts +125 -15
- package/std/net/http-fields.ts +85 -9
- package/std/net/http1-client-codec.ts +100 -19
- package/std/net/http1-codec.ts +235 -27
- package/std/net/http1-server.ts +343 -85
- package/std/net/http2-frame.ts +272 -35
- package/std/net/http2-tls.ts +135 -34
- package/std/net/http2.ts +247 -106
- package/std/net/http3-frame.ts +154 -17
- package/std/net/http3-server.ts +246 -96
- package/std/net/http3.ts +307 -115
- package/std/net/outbound.ts +37 -3
- package/std/net/postgres-carrier.ts +144 -8
- package/std/net/postgres.ts +723 -103
- package/std/net/prefork.ts +164 -31
- package/std/net/qpack.ts +105 -18
- package/std/net/quic-conn-ack.ts +59 -12
- package/std/net/quic-conn-cid.ts +124 -27
- package/std/net/quic-conn-params.ts +70 -10
- package/std/net/quic-datagram.ts +43 -3
- package/std/net/quic-frame.ts +286 -12
- package/std/net/quic-listener.ts +101 -32
- package/std/net/quic-packet.ts +302 -21
- package/std/net/quic-recovery.ts +228 -44
- package/std/net/quic-stream.ts +353 -91
- package/std/net/quic.ts +489 -171
- package/std/net/router.ts +87 -40
- package/std/net/tls/client.ts +166 -51
- package/std/net/tls/codec.ts +413 -56
- package/std/net/tls/record-client.ts +110 -55
- package/std/net/tls/record-server.ts +159 -59
- package/std/net/tls/record.ts +109 -12
- package/std/net/tls/schedule.ts +235 -26
- package/std/net/tls/ticket.ts +159 -40
- package/std/net/tls/verify.ts +49 -3
- package/std/net/tls-tcp.ts +250 -45
- package/std/net/tls.ts +221 -59
- package/std/net/upstream.ts +81 -45
- package/std/net/url.ts +97 -13
- package/std/net/websocket.ts +122 -12
- package/std/net/webtransport.ts +220 -89
- package/std/pair.ts +10 -2
- package/std/random.ts +34 -9
- package/std/regex.ts +352 -198
- 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 +205 -35
- package/std/sync/shm.ts +194 -16
- package/std/term-io.ts +137 -64
- package/std/term-pixels.ts +144 -19
- package/std/term-probe.ts +66 -25
- package/std/term-rate.ts +128 -57
- package/std/term.ts +332 -121
- package/std/testing.ts +130 -32
- package/std/text.ts +73 -7
- package/std/threads.ts +229 -41
- package/std/yaml-json.ts +94 -82
- package/std/yaml-lines.ts +47 -15
- package/std/yaml-write.ts +24 -16
- 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
|
|
@@ -143,8 +141,9 @@ export class TerminalOptions {
|
|
|
143
141
|
quitOnCtrlC: boolean
|
|
144
142
|
/** How long after a held key's last repeat it counts as released, without the kitty protocol (100). */
|
|
145
143
|
releaseGapMs: f64
|
|
146
|
-
/** The framebuffer's
|
|
144
|
+
/** The framebuffer's width in pixels, or 0 for none (0). Either side 0 means no framebuffer. */
|
|
147
145
|
framebufferWidth: i32
|
|
146
|
+
/** The framebuffer's height in pixels, or 0 for none (0). */
|
|
148
147
|
framebufferHeight: i32
|
|
149
148
|
/** The corner (`CORNER_*` from `nish/term-rate`) to draw the stats overlay in, or -1 for none (-1). */
|
|
150
149
|
overlay: i32
|
|
@@ -156,6 +155,7 @@ export class TerminalOptions {
|
|
|
156
155
|
*/
|
|
157
156
|
graphics: string
|
|
158
157
|
|
|
158
|
+
/** The defaults each field's comment gives in brackets. */
|
|
159
159
|
constructor() {
|
|
160
160
|
this.stepsPerSecond = 60
|
|
161
161
|
this.minFps = 10
|
|
@@ -176,7 +176,11 @@ export class TerminalOptions {
|
|
|
176
176
|
}
|
|
177
177
|
}
|
|
178
178
|
|
|
179
|
-
/**
|
|
179
|
+
/**
|
|
180
|
+
* The options a program starts from and changes what it needs in.
|
|
181
|
+
*
|
|
182
|
+
* @returns A fresh `TerminalOptions` holding the default each field's comment gives.
|
|
183
|
+
*/
|
|
180
184
|
export const defaultTerminalOptions = (): TerminalOptions => new TerminalOptions()
|
|
181
185
|
|
|
182
186
|
/** The monotonic clock in milliseconds. */
|
|
@@ -258,68 +262,91 @@ const termIoParam = (e: TermEvent, i: i32): i32 => (i < toI32(e.params.length) ?
|
|
|
258
262
|
* Made by `openTerminal`.
|
|
259
263
|
*/
|
|
260
264
|
export class Terminal {
|
|
265
|
+
/** The options it was opened with. */
|
|
261
266
|
options: TerminalOptions
|
|
267
|
+
/** What the probe found the terminal can do. */
|
|
262
268
|
caps: TermCaps
|
|
263
269
|
/** The cell grid the program draws into, the terminal's size. */
|
|
264
270
|
screen: Screen
|
|
271
|
+
/** Half-block pixels drawn on `screen`, two to a cell. */
|
|
265
272
|
canvas: Canvas
|
|
266
273
|
/** The pixels the program draws into, `options.framebufferWidth × framebufferHeight`. */
|
|
267
274
|
framebuffer: Framebuffer
|
|
268
275
|
/** The framebuffer shrunk by the controller's resolution divisor. */
|
|
269
276
|
scaled: Framebuffer
|
|
277
|
+
/** The buffer a kitty or sixel frame is encoded into before it is written. */
|
|
270
278
|
pixels: PixelBytes
|
|
279
|
+
/** Turns the bytes read from stdin into events. */
|
|
271
280
|
decoder: KeyDecoder
|
|
281
|
+
/** Which keys are down, and the releases made up from silence without the kitty protocol. */
|
|
272
282
|
keys: KeyState
|
|
283
|
+
/** Paces the frames and chooses their detail from how fast the terminal acknowledges them. */
|
|
273
284
|
rate: RateController
|
|
274
285
|
/** The events read during the probe, handed to the program first. */
|
|
275
|
-
held: TermEvent[]
|
|
286
|
+
private held: TermEvent[]
|
|
276
287
|
/** The send times of the frames not yet acknowledged, a ring of `TERM_IO_ACKS`. */
|
|
277
|
-
ackSent: f64[]
|
|
278
|
-
ackHead: i32
|
|
288
|
+
private ackSent: f64[]
|
|
289
|
+
private ackHead: i32
|
|
290
|
+
/** The frames sent whose acknowledgement has not come back, up to the most the ring holds. */
|
|
279
291
|
ackCount: i32
|
|
280
|
-
readBuf: u8[]
|
|
292
|
+
private readBuf: u8[]
|
|
281
293
|
/** Where the framebuffer is drawn, for `rectCols × rectRows` cells and `rectBackend`. */
|
|
282
|
-
rect: PixelRect
|
|
294
|
+
private rect: PixelRect
|
|
283
295
|
/** When the last frame acknowledgement arrived, or -1. */
|
|
284
|
-
lastAckMs: f64
|
|
296
|
+
private lastAckMs: f64
|
|
285
297
|
/** The last acknowledged frame's own round trip, in milliseconds, which the controller's smoothed one trails. */
|
|
286
|
-
lastRttMs: f64
|
|
287
|
-
/**
|
|
298
|
+
private lastRttMs: f64
|
|
299
|
+
/**
|
|
300
|
+
* The program's work toward the next frame (its `update` and `render`), in nanoseconds, which `present` adds to the frame's cost.
|
|
301
|
+
* @internal Used by `runTerminal`.
|
|
302
|
+
*/
|
|
288
303
|
workNs: f64
|
|
289
304
|
/** When the last frame's write began, on the monotonic clock. */
|
|
290
|
-
sendStartNs: i64
|
|
305
|
+
private sendStartNs: i64
|
|
291
306
|
/** 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
|
|
307
|
+
private rectCols: i32
|
|
308
|
+
private rectRows: i32
|
|
309
|
+
private rectBackend: i32
|
|
310
|
+
private rectWidth: i32
|
|
311
|
+
private rectHeight: i32
|
|
297
312
|
/** The best backend the terminal has, a `GRAPHICS_*`. */
|
|
298
313
|
graphics: i32
|
|
299
314
|
/** The backend the last frame used. */
|
|
300
315
|
backend: i32
|
|
316
|
+
/** A cell's width in pixels as the terminal reported it, at most 256; 0 when it did not say. */
|
|
301
317
|
cellWidthPx: i32
|
|
318
|
+
/** A cell's height in pixels as the terminal reported it, at most 256; 0 when it did not say. */
|
|
302
319
|
cellHeightPx: i32
|
|
303
320
|
/** The size `terminalSize` last answered. */
|
|
304
|
-
sizeCols: i32
|
|
305
|
-
sizeRows: i32
|
|
321
|
+
private sizeCols: i32
|
|
322
|
+
private sizeRows: i32
|
|
306
323
|
/** The size last given to the `Screen`, before it clamped it, which `checkResize` compares. */
|
|
307
|
-
shownCols: i32
|
|
308
|
-
shownRows: i32
|
|
324
|
+
private shownCols: i32
|
|
325
|
+
private shownRows: i32
|
|
309
326
|
/** 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
|
|
327
|
+
private mouseMode: i32
|
|
328
|
+
private open: boolean
|
|
329
|
+
private kittyKeyboardOn: boolean
|
|
330
|
+
private pixelMouseOn: boolean
|
|
331
|
+
private pasteOn: boolean
|
|
332
|
+
private focusOn: boolean
|
|
316
333
|
/** Whether a kitty image may be on the screen, so closing deletes it. */
|
|
317
|
-
kittyImage: boolean
|
|
334
|
+
private kittyImage: boolean
|
|
318
335
|
/** Whether the last frame sent pixels, so a framebuffer emptied since takes them off the screen. */
|
|
319
|
-
pixelsShown: boolean
|
|
336
|
+
private pixelsShown: boolean
|
|
320
337
|
/** Whether `screen` was drawn in 256 colours last frame. */
|
|
321
338
|
palette256: boolean
|
|
322
339
|
|
|
340
|
+
/**
|
|
341
|
+
* A terminal already in raw mode and probed, before anything is turned on;
|
|
342
|
+
* `openTerminal` makes one and turns on what the options ask for.
|
|
343
|
+
*
|
|
344
|
+
* @param options The program's options.
|
|
345
|
+
* @param caps What the probe found.
|
|
346
|
+
* @param cols The terminal's columns.
|
|
347
|
+
* @param rows The terminal's rows.
|
|
348
|
+
* @param held The events that arrived during the probe, which `readEvents` hands over first.
|
|
349
|
+
*/
|
|
323
350
|
constructor(options: TerminalOptions, caps: TermCaps, cols: i32, rows: i32, held: TermEvent[]) {
|
|
324
351
|
this.options = options
|
|
325
352
|
this.caps = caps
|
|
@@ -388,7 +415,7 @@ export class Terminal {
|
|
|
388
415
|
}
|
|
389
416
|
|
|
390
417
|
/** The bytes that undo everything turned on: what `close` writes and the runtime writes on a panic or signal. */
|
|
391
|
-
restoreString(): string {
|
|
418
|
+
private restoreString(): string {
|
|
392
419
|
let s: string = "\x18\x1b[?2026l"
|
|
393
420
|
if (this.kittyImage) {
|
|
394
421
|
s = s + kittyDelete(TERM_IO_IMAGE_ID)
|
|
@@ -409,12 +436,15 @@ export class Terminal {
|
|
|
409
436
|
}
|
|
410
437
|
|
|
411
438
|
/** Set the runtime's restore string to `restoreString()`, then write `bytes`, which turn something on. */
|
|
412
|
-
turnOn(bytes: string): void {
|
|
439
|
+
private turnOn(bytes: string): void {
|
|
413
440
|
setTerminalRestore(this.restoreString())
|
|
414
441
|
write(bytes)
|
|
415
442
|
}
|
|
416
443
|
|
|
417
|
-
/**
|
|
444
|
+
/**
|
|
445
|
+
* Turn on what the options ask for and the terminal has, the restore string kept ahead of each.
|
|
446
|
+
* @internal Called by `openTerminal`.
|
|
447
|
+
*/
|
|
418
448
|
enable(): void {
|
|
419
449
|
const o: TerminalOptions = this.options
|
|
420
450
|
if (o.kittyKeyboard && this.caps.kittyKeyboard) {
|
|
@@ -468,7 +498,7 @@ export class Terminal {
|
|
|
468
498
|
* round trip's bound passes. Before any answer the probe's round trip is
|
|
469
499
|
* the one known.
|
|
470
500
|
*/
|
|
471
|
-
drainAcks(): void {
|
|
501
|
+
private drainAcks(): void {
|
|
472
502
|
let owed: i32 = this.rate.unacked() + this.rate.stale
|
|
473
503
|
let rtt: f64 = this.rate.rttMs > this.lastRttMs ? this.rate.rttMs : this.lastRttMs
|
|
474
504
|
rtt = rtt > this.caps.probeRttMs ? rtt : this.caps.probeRttMs
|
|
@@ -500,7 +530,13 @@ export class Terminal {
|
|
|
500
530
|
}
|
|
501
531
|
}
|
|
502
532
|
|
|
503
|
-
/**
|
|
533
|
+
/**
|
|
534
|
+
* What the controller measured, with the backend the last frame actually
|
|
535
|
+
* used.
|
|
536
|
+
*
|
|
537
|
+
* @returns The controller's own stats object, updated in place on every tick, not a copy: a
|
|
538
|
+
* caller who keeps it past the next tick copies the fields it needs.
|
|
539
|
+
*/
|
|
504
540
|
stats(): TermStats {
|
|
505
541
|
const s: TermStats = this.rate.stats()
|
|
506
542
|
s.backend = this.backend
|
|
@@ -508,7 +544,7 @@ export class Terminal {
|
|
|
508
544
|
}
|
|
509
545
|
|
|
510
546
|
/** Read `terminalSize` into `sizeCols` and `sizeRows`; its array dies here. */
|
|
511
|
-
readSize(): void {
|
|
547
|
+
private readSize(): void {
|
|
512
548
|
const size: i32[] = terminalSize()
|
|
513
549
|
if (toI32(size.length) >= 2 && size[0] > 0 && size[1] > 0) {
|
|
514
550
|
this.sizeCols = size[0]
|
|
@@ -517,10 +553,12 @@ export class Terminal {
|
|
|
517
553
|
}
|
|
518
554
|
|
|
519
555
|
/**
|
|
520
|
-
*
|
|
521
|
-
*
|
|
522
|
-
*
|
|
523
|
-
*
|
|
556
|
+
* Take a new terminal size, if there is one: the `Screen` takes it and the
|
|
557
|
+
* framebuffer is fitted again. It compares the size the terminal answers, not
|
|
558
|
+
* the `Screen`'s, which clamps a huge one, so a huge terminal is not resized
|
|
559
|
+
* every frame.
|
|
560
|
+
*
|
|
561
|
+
* @returns True when the size changed since the last call.
|
|
524
562
|
*/
|
|
525
563
|
checkResize(): boolean {
|
|
526
564
|
this.readSize()
|
|
@@ -537,7 +575,7 @@ export class Terminal {
|
|
|
537
575
|
}
|
|
538
576
|
|
|
539
577
|
/** 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 {
|
|
578
|
+
private dropLost(): void {
|
|
541
579
|
while (this.ackCount > this.rate.unacked()) {
|
|
542
580
|
this.ackHead = (this.ackHead + 1) % TERM_IO_ACKS
|
|
543
581
|
this.ackCount = this.ackCount - 1
|
|
@@ -545,7 +583,7 @@ export class Terminal {
|
|
|
545
583
|
}
|
|
546
584
|
|
|
547
585
|
/** Remember that a frame acknowledgement was asked for at `nowMs`. */
|
|
548
|
-
ackPush(nowMs: f64): void {
|
|
586
|
+
private ackPush(nowMs: f64): void {
|
|
549
587
|
if (this.ackCount === TERM_IO_ACKS) {
|
|
550
588
|
this.ackHead = (this.ackHead + 1) % TERM_IO_ACKS
|
|
551
589
|
this.ackCount = this.ackCount - 1
|
|
@@ -559,7 +597,7 @@ export class Terminal {
|
|
|
559
597
|
* and, unless it is the late answer of a frame the controller gave up on,
|
|
560
598
|
* marks its frame's send time delivered.
|
|
561
599
|
*/
|
|
562
|
-
acked(count: i32, nowMs: f64): void {
|
|
600
|
+
private acked(count: i32, nowMs: f64): void {
|
|
563
601
|
for (let k: i32 = 0; k < count; k++) {
|
|
564
602
|
this.dropLost()
|
|
565
603
|
if (this.rate.stale === 0 && this.ackCount > 0) {
|
|
@@ -576,7 +614,7 @@ export class Terminal {
|
|
|
576
614
|
}
|
|
577
615
|
|
|
578
616
|
/** 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 {
|
|
617
|
+
private lateReply(e: TermEvent): void {
|
|
580
618
|
if (
|
|
581
619
|
this.cellWidthPx > 0 ||
|
|
582
620
|
e.introducer !== 91 ||
|
|
@@ -617,7 +655,7 @@ export class Terminal {
|
|
|
617
655
|
* `KeyState` into `out`, then the acknowledgements count. Every reply is
|
|
618
656
|
* the terminal's and is taken here, a late cell size learned.
|
|
619
657
|
*/
|
|
620
|
-
take(events: TermEvent[], nowMs: f64, out: TermEvent[]): void {
|
|
658
|
+
private take(events: TermEvent[], nowMs: f64, out: TermEvent[]): void {
|
|
621
659
|
let replies: i32 = 0
|
|
622
660
|
for (const e of events) {
|
|
623
661
|
if (e.type === EVENT_REPLY) {
|
|
@@ -645,11 +683,13 @@ export class Terminal {
|
|
|
645
683
|
}
|
|
646
684
|
|
|
647
685
|
/**
|
|
648
|
-
* Wait
|
|
649
|
-
* and
|
|
650
|
-
*
|
|
651
|
-
*
|
|
652
|
-
*
|
|
686
|
+
* Wait for input and append what it means for the program: keys, mouse, paste
|
|
687
|
+
* and focus events, the releases made up from silence, `EVENT_QUIT` for
|
|
688
|
+
* Ctrl-C. Frame acknowledgements are taken here and never appended.
|
|
689
|
+
*
|
|
690
|
+
* @param waitMs The longest wait in milliseconds; a negative wait has no limit.
|
|
691
|
+
* @param out Where the events go, after what it holds.
|
|
692
|
+
* @returns False once stdin has ended.
|
|
653
693
|
*/
|
|
654
694
|
readEvents(waitMs: i32, out: TermEvent[]): boolean {
|
|
655
695
|
if (toI32(this.held.length) > 0) {
|
|
@@ -693,7 +733,7 @@ export class Terminal {
|
|
|
693
733
|
}
|
|
694
734
|
|
|
695
735
|
/** Fit the framebuffer to the screen for `backend`, unless the sizes and backend are those of the last fit. */
|
|
696
|
-
fit(backend: i32): void {
|
|
736
|
+
private fit(backend: i32): void {
|
|
697
737
|
const fb: Framebuffer = this.framebuffer
|
|
698
738
|
if (
|
|
699
739
|
this.rectCols === this.screen.width &&
|
|
@@ -727,7 +767,7 @@ export class Terminal {
|
|
|
727
767
|
* began in `sendStartNs` and answers the bytes written. Its strings die
|
|
728
768
|
* here, so a frame loop does not keep them.
|
|
729
769
|
*/
|
|
730
|
-
send(withPixels: boolean): i32 {
|
|
770
|
+
private send(withPixels: boolean): i32 {
|
|
731
771
|
const body: string = this.screen.flush()
|
|
732
772
|
const image: string = withPixels ? this.pixels.text() : ""
|
|
733
773
|
const frame: string = this.caps.sync
|
|
@@ -819,8 +859,11 @@ const termIoRefusal = (): string => {
|
|
|
819
859
|
/**
|
|
820
860
|
* Open the terminal: refuse a pipe, enter raw mode and the alternate screen,
|
|
821
861
|
* 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
|
-
*
|
|
862
|
+
* asks for and it has. The restore string undoes all of it should the program
|
|
863
|
+
* panic or be signalled before `close`.
|
|
864
|
+
*
|
|
865
|
+
* @param options What to turn on, and how to pace frames.
|
|
866
|
+
* @returns The open terminal, or an `Err` saying why not: stdin or stdout is not a terminal, or raw mode was refused.
|
|
824
867
|
*/
|
|
825
868
|
export const openTerminal = (options: TerminalOptions): Result<Terminal, string> => {
|
|
826
869
|
const refused: string = termIoRefusal()
|
|
@@ -868,15 +911,29 @@ export const openTerminal = (options: TerminalOptions): Result<Terminal, string>
|
|
|
868
911
|
|
|
869
912
|
/** What one `update` step gets: the events since the last step and which keys are down. */
|
|
870
913
|
export class TermInput {
|
|
914
|
+
/** The events since the last step, in the order they arrived. */
|
|
871
915
|
events: TermEvent[]
|
|
916
|
+
/** Which keys are down, for `isDown`. */
|
|
872
917
|
keys: KeyState
|
|
873
918
|
|
|
919
|
+
/**
|
|
920
|
+
* An input with no events yet.
|
|
921
|
+
*
|
|
922
|
+
* @param keys The terminal's key state, which `isDown` reads.
|
|
923
|
+
*/
|
|
874
924
|
constructor(keys: KeyState) {
|
|
875
925
|
this.events = []
|
|
876
926
|
this.keys = keys
|
|
877
927
|
}
|
|
878
928
|
|
|
879
|
-
/**
|
|
929
|
+
/**
|
|
930
|
+
* Whether a key is down.
|
|
931
|
+
*
|
|
932
|
+
* @param key The key's `KEY_` kind.
|
|
933
|
+
* @param code The code point for `KEY_CHAR`; it tells `KEY_CONTROL` and `KEY_UNKNOWN` keys apart
|
|
934
|
+
* too, and is ignored for the other kinds.
|
|
935
|
+
* @returns True from the key's press until its release, real or synthetic, or a loss of focus.
|
|
936
|
+
*/
|
|
880
937
|
isDown(key: i32, code: i32): boolean {
|
|
881
938
|
return this.keys.isDown(key, code)
|
|
882
939
|
}
|
|
@@ -884,13 +941,25 @@ export class TermInput {
|
|
|
884
941
|
|
|
885
942
|
/** What `render` draws into: the cells, the pixels, how far into the next step the clock is, and the stats. */
|
|
886
943
|
export class Frame {
|
|
944
|
+
/** The cell grid, the terminal's size. */
|
|
887
945
|
screen: Screen
|
|
946
|
+
/** The pixels, `options.framebufferWidth × framebufferHeight`; 0 by 0 when the options ask for none. */
|
|
888
947
|
framebuffer: Framebuffer
|
|
889
948
|
/** From 0 up to 1: how far past the last `update` step this frame is, to interpolate with. */
|
|
890
949
|
alpha: f64
|
|
950
|
+
/**
|
|
951
|
+
* What the controller measured: its live stats object, which the next tick
|
|
952
|
+
* updates in place, so copy a field to keep it past this frame.
|
|
953
|
+
*/
|
|
891
954
|
stats: TermStats
|
|
955
|
+
/** What the probe found the terminal can do. */
|
|
892
956
|
caps: TermCaps
|
|
893
957
|
|
|
958
|
+
/**
|
|
959
|
+
* A frame drawing into `term`'s screen and framebuffer, at `alpha` 0.
|
|
960
|
+
*
|
|
961
|
+
* @param term The open terminal.
|
|
962
|
+
*/
|
|
894
963
|
constructor(term: Terminal) {
|
|
895
964
|
this.screen = term.screen
|
|
896
965
|
this.framebuffer = term.framebuffer
|
|
@@ -939,14 +1008,18 @@ const termIoWait = (term: Terminal, step: FixedStep, nowMs: f64): i32 => {
|
|
|
939
1008
|
}
|
|
940
1009
|
|
|
941
1010
|
/**
|
|
942
|
-
* Open the terminal and run the program: `update
|
|
943
|
-
*
|
|
944
|
-
* `input.events`, and `render(state, frame)` whenever the controller allows a
|
|
1011
|
+
* Open the terminal and run the program: `update` once per fixed step
|
|
1012
|
+
* (`options.stepsPerSecond`) and `render` whenever the controller allows a
|
|
945
1013
|
* 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
|
-
*
|
|
1014
|
+
* columns, `y` the rows) with the `Screen` already resized. The terminal is
|
|
1015
|
+
* closed however the loop ends.
|
|
1016
|
+
*
|
|
1017
|
+
* @param options What to turn on, and how to pace steps and frames.
|
|
1018
|
+
* @param state The program's state, handed to every call.
|
|
1019
|
+
* @param update Runs one step on the events since the last in `input.events`; answers false to stop.
|
|
1020
|
+
* @param render Draws `state` into `frame`.
|
|
1021
|
+
* @returns `Ok(0)` once `update` answers false or the user quits (`EVENT_QUIT`); an `Err` for a
|
|
1022
|
+
* terminal that could not be opened, or stdin ending.
|
|
950
1023
|
*/
|
|
951
1024
|
export const runTerminal = <G>(
|
|
952
1025
|
options: TerminalOptions,
|