@amritk/nish-aarch64-linux 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/INSTALL.md +73 -59
- package/bin/nish +0 -0
- package/docs/AI.md +50 -4
- package/package.json +1 -1
- 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/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-pixels.ts
CHANGED
|
@@ -4,11 +4,21 @@
|
|
|
4
4
|
* half-block cells or ASCII.
|
|
5
5
|
*
|
|
6
6
|
* A program draws `0xRRGGBB` pixels into a `Framebuffer` and never thinks about
|
|
7
|
-
* the terminal; `
|
|
7
|
+
* the terminal; `nish/term-io` picks the backend and calls one encoder per frame.
|
|
8
8
|
* Everything here is pure: no capability, no I/O, and the bytes are the same
|
|
9
9
|
* natively and under Node, because the kitty and Sixel output is ASCII and the
|
|
10
10
|
* cell drawers go through `Screen`, which already is.
|
|
11
11
|
*
|
|
12
|
+
* import { Framebuffer, fitFramebuffer, GRAPHICS_KITTY, kittyFrame, PixelBytes } from "nish/term-pixels"
|
|
13
|
+
*
|
|
14
|
+
* const fb = new Framebuffer(160, 90)
|
|
15
|
+
* fb.fill(0x000020)
|
|
16
|
+
* fb.set(80, 45, 0xffcc00)
|
|
17
|
+
* const rect = fitFramebuffer(fb.width, fb.height, 80, 24, 0, 0, GRAPHICS_KITTY)
|
|
18
|
+
* const out = new PixelBytes(1 << 16)
|
|
19
|
+
* kittyFrame(fb, rect, 1, out)
|
|
20
|
+
* // out.text() is the frame's bytes, for one write
|
|
21
|
+
*
|
|
12
22
|
* - `Framebuffer(width, height)` holds the pixels, with `set`, `get`, `fill`,
|
|
13
23
|
* `clear` and `resize`. `scaleFramebuffer(src, divisor, dst)` shrinks one by
|
|
14
24
|
* an integer divisor, nearest-neighbour, for the refresh controller's
|
|
@@ -159,7 +169,12 @@ export class Framebuffer {
|
|
|
159
169
|
/** One colour per pixel, `y * width + x`. */
|
|
160
170
|
pixels: i32[]
|
|
161
171
|
|
|
162
|
-
/**
|
|
172
|
+
/**
|
|
173
|
+
* A black framebuffer of `width × height`; a negative size, or more than 2^31 - 1 pixels, panics.
|
|
174
|
+
*
|
|
175
|
+
* @param width Pixels across.
|
|
176
|
+
* @param height Pixels down.
|
|
177
|
+
*/
|
|
163
178
|
constructor(width: i32, height: i32) {
|
|
164
179
|
pixelsCheckSize("Framebuffer", width, height)
|
|
165
180
|
this.width = width
|
|
@@ -168,7 +183,12 @@ export class Framebuffer {
|
|
|
168
183
|
this.pixels.fill(0)
|
|
169
184
|
}
|
|
170
185
|
|
|
171
|
-
/**
|
|
186
|
+
/**
|
|
187
|
+
* Make the framebuffer `width × height` and black; the array is replaced only when the size changes.
|
|
188
|
+
*
|
|
189
|
+
* @param width The new pixels across; negative panics, as in the constructor.
|
|
190
|
+
* @param height The new pixels down; negative panics, as in the constructor.
|
|
191
|
+
*/
|
|
172
192
|
resize(width: i32, height: i32): void {
|
|
173
193
|
pixelsCheckSize("Framebuffer.resize", width, height)
|
|
174
194
|
if (width !== this.width || height !== this.height) {
|
|
@@ -179,7 +199,13 @@ export class Framebuffer {
|
|
|
179
199
|
this.pixels.fill(0)
|
|
180
200
|
}
|
|
181
201
|
|
|
182
|
-
/**
|
|
202
|
+
/**
|
|
203
|
+
* Set the pixel at `x`, `y` to `colour`'s low 24 bits; a pixel off the framebuffer is not drawn.
|
|
204
|
+
*
|
|
205
|
+
* @param x The pixel column, from 0 at the left.
|
|
206
|
+
* @param y The pixel row, from 0 at the top.
|
|
207
|
+
* @param colour An `0xRRGGBB` colour; the bits above it are dropped.
|
|
208
|
+
*/
|
|
183
209
|
set(x: i32, y: i32, colour: i32): void {
|
|
184
210
|
if (x < 0 || y < 0 || x >= this.width || y >= this.height) {
|
|
185
211
|
return
|
|
@@ -187,7 +213,13 @@ export class Framebuffer {
|
|
|
187
213
|
this.pixels[y * this.width + x] = colour & 0xffffff
|
|
188
214
|
}
|
|
189
215
|
|
|
190
|
-
/**
|
|
216
|
+
/**
|
|
217
|
+
* The colour of the pixel at `x`, `y`, or black off the framebuffer.
|
|
218
|
+
*
|
|
219
|
+
* @param x The pixel column, from 0 at the left.
|
|
220
|
+
* @param y The pixel row, from 0 at the top.
|
|
221
|
+
* @returns The colour as `0xRRGGBB`; 0, black, off the framebuffer.
|
|
222
|
+
*/
|
|
191
223
|
get(x: i32, y: i32): i32 {
|
|
192
224
|
if (x < 0 || y < 0 || x >= this.width || y >= this.height) {
|
|
193
225
|
return 0
|
|
@@ -195,7 +227,11 @@ export class Framebuffer {
|
|
|
195
227
|
return this.pixels[y * this.width + x]
|
|
196
228
|
}
|
|
197
229
|
|
|
198
|
-
/**
|
|
230
|
+
/**
|
|
231
|
+
* Set every pixel to `colour`'s low 24 bits.
|
|
232
|
+
*
|
|
233
|
+
* @param colour An `0xRRGGBB` colour; the bits above it are dropped.
|
|
234
|
+
*/
|
|
199
235
|
fill(colour: i32): void {
|
|
200
236
|
this.pixels.fill(colour & 0xffffff)
|
|
201
237
|
}
|
|
@@ -213,6 +249,10 @@ export class Framebuffer {
|
|
|
213
249
|
* frame costs a quarter or a sixteenth of the bytes at divisor 2 or 4. A
|
|
214
250
|
* divisor below 1 panics; 1 copies. `dst` must be another framebuffer than
|
|
215
251
|
* `src`, which resizing it would blank.
|
|
252
|
+
*
|
|
253
|
+
* @param src The full-size framebuffer, read only.
|
|
254
|
+
* @param divisor How many pixels across and down become one, 1 or more; 2 or 4 for the controller's steps.
|
|
255
|
+
* @param dst Where the shrunk frame goes; resized to fit.
|
|
216
256
|
*/
|
|
217
257
|
export const scaleFramebuffer = (src: Framebuffer, divisor: i32, dst: Framebuffer): void => {
|
|
218
258
|
if (divisor < 1) {
|
|
@@ -251,7 +291,17 @@ export class PixelRect {
|
|
|
251
291
|
/** The backend's pixels per framebuffer pixel across; ASCII's rows are half that down. */
|
|
252
292
|
scale: f64
|
|
253
293
|
|
|
254
|
-
/**
|
|
294
|
+
/**
|
|
295
|
+
* A rect of the given cells and size; `fitFramebuffer` is how one is usually made.
|
|
296
|
+
*
|
|
297
|
+
* @param col The first column covered, from 0.
|
|
298
|
+
* @param row The first row covered, from 0.
|
|
299
|
+
* @param cols The columns covered.
|
|
300
|
+
* @param rows The rows covered.
|
|
301
|
+
* @param width The image's width in the backend's pixels.
|
|
302
|
+
* @param height The image's height in the backend's pixels.
|
|
303
|
+
* @param scale The backend's pixels per framebuffer pixel across.
|
|
304
|
+
*/
|
|
255
305
|
constructor(col: i32, row: i32, cols: i32, rows: i32, width: i32, height: i32, scale: f64) {
|
|
256
306
|
this.col = col
|
|
257
307
|
this.row = row
|
|
@@ -278,6 +328,15 @@ const pixelsCeilDiv = (a: i32, b: i32): i32 => (a === 0 ? 0 : (((a - 1) / b) | 0
|
|
|
278
328
|
* and ASCII take a cell as one pixel wide and two tall, the shape of a usual
|
|
279
329
|
* terminal font, so a square framebuffer stays square. An empty framebuffer or
|
|
280
330
|
* area answers an empty rect at 0, 0.
|
|
331
|
+
*
|
|
332
|
+
* @param fbW The framebuffer's width in pixels.
|
|
333
|
+
* @param fbH The framebuffer's height in pixels.
|
|
334
|
+
* @param cols The area's width in cells.
|
|
335
|
+
* @param rows The area's height in cells.
|
|
336
|
+
* @param cellW A cell's width in pixels as the terminal reported it, or 0 or less when it did not say.
|
|
337
|
+
* @param cellH A cell's height in pixels likewise; with either at 0 or less, kitty and Sixel take the cell as 8×16.
|
|
338
|
+
* @param backend The `GRAPHICS_` backend that will draw it, which decides what a pixel of the rect is.
|
|
339
|
+
* @returns Where to draw, in the area's cells and the backend's pixels.
|
|
281
340
|
*/
|
|
282
341
|
export const fitFramebuffer = (
|
|
283
342
|
fbW: i32,
|
|
@@ -342,22 +401,46 @@ export class PixelBytes {
|
|
|
342
401
|
bytes: u8[]
|
|
343
402
|
/** How many bytes are written. */
|
|
344
403
|
length: i32
|
|
345
|
-
/**
|
|
404
|
+
/**
|
|
405
|
+
* One kitty chunk's RGB bytes.
|
|
406
|
+
* @internal Used by `kittyFrame`.
|
|
407
|
+
*/
|
|
346
408
|
kittyRaw: u8[]
|
|
347
|
-
/**
|
|
409
|
+
/**
|
|
410
|
+
* The framebuffer column and row each destination pixel samples.
|
|
411
|
+
* @internal Used by `sixelFrame`.
|
|
412
|
+
*/
|
|
348
413
|
sampleXs: i32[]
|
|
414
|
+
/** @internal Used by `sixelFrame`. */
|
|
349
415
|
sampleYs: i32[]
|
|
350
|
-
/**
|
|
416
|
+
/**
|
|
417
|
+
* Every destination pixel's Sixel register, row by row.
|
|
418
|
+
* @internal Used by `sixelFrame`.
|
|
419
|
+
*/
|
|
351
420
|
sixelReg: i32[]
|
|
352
|
-
/**
|
|
421
|
+
/**
|
|
422
|
+
* Which registers a Sixel frame uses, 1 or 0.
|
|
423
|
+
* @internal Used by `sixelFrame`.
|
|
424
|
+
*/
|
|
353
425
|
sixelUsed: u8[]
|
|
354
|
-
/**
|
|
426
|
+
/**
|
|
427
|
+
* The first and last column of a band each register covers, -1 for none.
|
|
428
|
+
* @internal Used by `sixelFrame`.
|
|
429
|
+
*/
|
|
355
430
|
sixelFirst: i32[]
|
|
431
|
+
/** @internal Used by `sixelFrame`. */
|
|
356
432
|
sixelLast: i32[]
|
|
357
|
-
/**
|
|
433
|
+
/**
|
|
434
|
+
* Each register's sixels across a band, all zero between bands.
|
|
435
|
+
* @internal Used by `sixelFrame`.
|
|
436
|
+
*/
|
|
358
437
|
sixelBits: i32[]
|
|
359
438
|
|
|
360
|
-
/**
|
|
439
|
+
/**
|
|
440
|
+
* An empty buffer with room for `capacity` bytes before it grows.
|
|
441
|
+
*
|
|
442
|
+
* @param capacity The bytes to make room for at the start; a frame's worth saves growing.
|
|
443
|
+
*/
|
|
361
444
|
constructor(capacity: i32) {
|
|
362
445
|
this.bytes = new Array<u8>(Math.max(capacity, 16))
|
|
363
446
|
this.bytes.fill(0)
|
|
@@ -381,7 +464,11 @@ export class PixelBytes {
|
|
|
381
464
|
this.length = 0
|
|
382
465
|
}
|
|
383
466
|
|
|
384
|
-
/**
|
|
467
|
+
/**
|
|
468
|
+
* Make room for `n` more bytes, doubling the array as often as that takes.
|
|
469
|
+
*
|
|
470
|
+
* @param n How many bytes the caller is about to `put`.
|
|
471
|
+
*/
|
|
385
472
|
reserve(n: i32): void {
|
|
386
473
|
const need: i32 = this.length + n
|
|
387
474
|
if (need <= toI32(this.bytes.length)) {
|
|
@@ -398,14 +485,22 @@ export class PixelBytes {
|
|
|
398
485
|
this.bytes = grown
|
|
399
486
|
}
|
|
400
487
|
|
|
401
|
-
/**
|
|
488
|
+
/**
|
|
489
|
+
* Append the byte `b`.
|
|
490
|
+
*
|
|
491
|
+
* @param b A byte value, 0–255; only the low 8 bits are kept.
|
|
492
|
+
*/
|
|
402
493
|
put(b: i32): void {
|
|
403
494
|
this.reserve(PIXELS_ONE)
|
|
404
495
|
this.bytes[this.length] = toU8(b)
|
|
405
496
|
this.length = this.length + 1
|
|
406
497
|
}
|
|
407
498
|
|
|
408
|
-
/**
|
|
499
|
+
/**
|
|
500
|
+
* Append `s`, which is ASCII.
|
|
501
|
+
*
|
|
502
|
+
* @param s Text of ASCII characters only, such as a command's keys.
|
|
503
|
+
*/
|
|
409
504
|
putText(s: string): void {
|
|
410
505
|
const n: i32 = toI32(s.length)
|
|
411
506
|
this.reserve(n)
|
|
@@ -415,7 +510,11 @@ export class PixelBytes {
|
|
|
415
510
|
this.length = this.length + n
|
|
416
511
|
}
|
|
417
512
|
|
|
418
|
-
/**
|
|
513
|
+
/**
|
|
514
|
+
* Append `v`, at least 0, in decimal.
|
|
515
|
+
*
|
|
516
|
+
* @param v A count or size, 0 or more.
|
|
517
|
+
*/
|
|
419
518
|
putDecimal(v: i32): void {
|
|
420
519
|
if (v >= 10) {
|
|
421
520
|
this.putDecimal((v / 10) | 0)
|
|
@@ -423,7 +522,11 @@ export class PixelBytes {
|
|
|
423
522
|
this.put(PIXELS_DIGIT_0 + (v % 10))
|
|
424
523
|
}
|
|
425
524
|
|
|
426
|
-
/**
|
|
525
|
+
/**
|
|
526
|
+
* What was written, as one string.
|
|
527
|
+
*
|
|
528
|
+
* @returns The bytes appended since it was made or last `reset`, to `write`.
|
|
529
|
+
*/
|
|
427
530
|
text(): string {
|
|
428
531
|
return String.fromBytes(this.bytes, 0, this.length)
|
|
429
532
|
}
|
|
@@ -513,6 +616,11 @@ const pixelsStringEnd = (out: PixelBytes): void => {
|
|
|
513
616
|
*
|
|
514
617
|
* `imageId` is 1 to 2^31 - 1, since 0 is the protocol's "no id"; anything
|
|
515
618
|
* else panics. An empty framebuffer or rect writes nothing.
|
|
619
|
+
*
|
|
620
|
+
* @param fb The frame's pixels, at its own size.
|
|
621
|
+
* @param rect Where to draw it, from `fitFramebuffer` with `GRAPHICS_KITTY`.
|
|
622
|
+
* @param imageId The image's id, 1 or more (below 1 panics), the same for every frame of one program.
|
|
623
|
+
* @param out Where the command bytes are appended.
|
|
516
624
|
*/
|
|
517
625
|
export const kittyFrame = (fb: Framebuffer, rect: PixelRect, imageId: i32, out: PixelBytes): void => {
|
|
518
626
|
pixelsCheckImageId("kittyFrame", imageId)
|
|
@@ -572,6 +680,9 @@ const pixelsCheckImageId = (who: string, imageId: i32): void => {
|
|
|
572
680
|
* terminal does not answer. A program writes it when it stops drawing, and
|
|
573
681
|
* puts it in its terminal restore string, so the last frame does not stay
|
|
574
682
|
* behind. Panics, like `kittyFrame`, on an id below 1.
|
|
683
|
+
*
|
|
684
|
+
* @param imageId The id `kittyFrame` drew with.
|
|
685
|
+
* @returns The command, to `write`.
|
|
575
686
|
*/
|
|
576
687
|
export const kittyDelete = (imageId: i32): string => {
|
|
577
688
|
pixelsCheckImageId("kittyDelete", imageId)
|
|
@@ -636,6 +747,11 @@ const pixelsSixelRun = (out: PixelBytes, bits: i32, count: i32): void => {
|
|
|
636
747
|
* matrix's threshold before they are quantised (ordered dithering), so a
|
|
637
748
|
* gradient becomes a fine, stable pattern rather than steps. An empty rect
|
|
638
749
|
* writes nothing.
|
|
750
|
+
*
|
|
751
|
+
* @param fb The frame's pixels; shrunk or grown to the rect by nearest neighbour.
|
|
752
|
+
* @param rect Where to draw it, from `fitFramebuffer` with `GRAPHICS_SIXEL`.
|
|
753
|
+
* @param dither Whether to dither gradients instead of banding them.
|
|
754
|
+
* @param out Where the Sixel bytes are appended.
|
|
639
755
|
*/
|
|
640
756
|
export const sixelFrame = (fb: Framebuffer, rect: PixelRect, dither: boolean, out: PixelBytes): void => {
|
|
641
757
|
const w: i32 = rect.width
|
|
@@ -785,6 +901,10 @@ export const sixelFrame = (fb: Framebuffer, rect: PixelRect, dither: boolean, ou
|
|
|
785
901
|
* not reach is set to `COLOR_DEFAULT`. Each covered cell is repainted once,
|
|
786
902
|
* and `Screen.flush` sends only the cells whose glyph or colours changed, so
|
|
787
903
|
* a frame that barely moves costs almost nothing.
|
|
904
|
+
*
|
|
905
|
+
* @param fb The frame's pixels.
|
|
906
|
+
* @param canvas The half-block canvas of the screen to draw on.
|
|
907
|
+
* @param rect Where to draw it, from `fitFramebuffer` with `GRAPHICS_HALFBLOCK`.
|
|
788
908
|
*/
|
|
789
909
|
export const drawHalfBlocks = (fb: Framebuffer, canvas: Canvas, rect: PixelRect): void => {
|
|
790
910
|
if (rect.width <= 0 || rect.height <= 0 || fb.width <= 0 || fb.height <= 0) {
|
|
@@ -851,6 +971,11 @@ const pixelsNearestAnsi16 = (colour: i32): i32 => {
|
|
|
851
971
|
* nearest of xterm's 16 standard colours. `Screen` sends colours as 24-bit or
|
|
852
972
|
* 256-palette SGR only, and a console that reduces those to its 16 itself then
|
|
853
973
|
* lands on the colour chosen here, not on a neighbour of it.
|
|
974
|
+
*
|
|
975
|
+
* @param fb The frame's pixels.
|
|
976
|
+
* @param screen The screen to draw on.
|
|
977
|
+
* @param rect Where to draw it, from `fitFramebuffer` with `GRAPHICS_ASCII`.
|
|
978
|
+
* @param colors How many colours the terminal shows: 16 for the Linux console, anything else for more.
|
|
854
979
|
*/
|
|
855
980
|
export const drawAscii = (fb: Framebuffer, screen: Screen, rect: PixelRect, colors: i32): void => {
|
|
856
981
|
if (rect.width <= 0 || rect.height <= 0 || fb.width <= 0 || fb.height <= 0) {
|
package/std/term-probe.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* `
|
|
2
|
+
* `nish/term-probe` — what the terminal a program runs in can do (its colours,
|
|
3
3
|
* keyboard protocol, graphics and size), asked of it once at start and read
|
|
4
4
|
* from its answer.
|
|
5
5
|
*
|
|
@@ -10,16 +10,14 @@
|
|
|
10
10
|
* caller's milliseconds as `f64` from any origin, and nothing here reads,
|
|
11
11
|
* writes or waits.
|
|
12
12
|
*
|
|
13
|
-
*
|
|
14
|
-
* import { TermEnv, TermProbe, probeQueries } from "nish/term-probe";
|
|
13
|
+
* import { TermEnv, TermProbe, probeQueries } from "nish/term-probe"
|
|
15
14
|
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
* ```
|
|
15
|
+
* const env: TermEnv = new TermEnv()
|
|
16
|
+
* env.term = "xterm-256color"
|
|
17
|
+
* const probe: TermProbe = new TermProbe(env, 80, 24, 0.0)
|
|
18
|
+
* const queries: string = probeQueries(env) // write these to the terminal
|
|
19
|
+
* // …feed each read: probe.feed(bytes, count, nowMs), until probe.done() or probe.timedOut(nowMs)
|
|
20
|
+
* console.log(`${probe.result().colors}`)
|
|
23
21
|
*
|
|
24
22
|
* - `TermEnv` — the environment variables the probe reads, each "" when
|
|
25
23
|
* unset, one field each in camelCase: `term` (`TERM`), `colorterm`
|
|
@@ -50,8 +48,8 @@
|
|
|
50
48
|
* (`nish/term`), keeping the replies and holding back every other event (a
|
|
51
49
|
* key typed while the probe ran) for `heldEvents()`, in order. `done()` is
|
|
52
50
|
* true once DA1 is answered; `timedOut(nowMs)` once `timeoutMs` (250 by
|
|
53
|
-
* default) has passed since the send with no answer to DA1.
|
|
54
|
-
*
|
|
51
|
+
* default) has passed since the send with no answer to DA1. `timedOut`
|
|
52
|
+
* calls `finish(nowMs)` itself; after `done()` the program calls it once: it gives up whatever the
|
|
55
53
|
* decoder still holds (a key cut off at the end of the read with DA1, say)
|
|
56
54
|
* and returns `heldEvents()` complete, and later reads go to the program's
|
|
57
55
|
* own decoder. `rttMs()` is the time from the
|
|
@@ -65,19 +63,20 @@
|
|
|
65
63
|
* "ascii"), `name` and `overrides`; each field's comment says where it
|
|
66
64
|
* comes from.
|
|
67
65
|
*
|
|
68
|
-
* The environment decides where the terminal cannot answer, and where the
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
* `256color` in it means 256 colours, and anything else 16. `TERM=linux` is
|
|
72
|
-
*
|
|
66
|
+
* The environment decides where the terminal cannot answer, and where the user
|
|
67
|
+
* says so: `COLORTERM=truecolor` or `24bit` means true colour, as does a
|
|
68
|
+
* DECRQSS that reads the 24-bit background back; otherwise a `TERM` with
|
|
69
|
+
* `256color` in it means 256 colours, and anything else 16. `TERM=linux` is the
|
|
70
|
+
* Linux console: 16 colours, ASCII graphics, no kitty keyboard and no
|
|
73
71
|
* synchronized output, whatever it answered. `NO_COLOR` set to anything
|
|
74
72
|
* (no-color.org) means 16 colours and touches nothing but colour.
|
|
73
|
+
*
|
|
75
74
|
* `NISH_TERM_GRAPHICS` names a graphics backend to use instead of the one the
|
|
76
|
-
* answers pick; a pixel backend the terminal did not answer for is refused
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
*
|
|
80
|
-
* `
|
|
75
|
+
* answers pick; a pixel backend the terminal did not answer for is refused when
|
|
76
|
+
* it would come out as garbage or nothing — on the Linux console, under a
|
|
77
|
+
* multiplexer, or when the terminal answered nothing at all. `overrides` says
|
|
78
|
+
* what each variable did, refusals included. The match is exact: `kitty`,
|
|
79
|
+
* `sixel`, `halfblock` or `ascii`, lower case, nothing around it.
|
|
81
80
|
*
|
|
82
81
|
* Under a multiplexer the probe sees the multiplexer first. tmux and screen
|
|
83
82
|
* answer DA1 themselves, so `done()` comes on their answer, before any reply
|
|
@@ -218,7 +217,14 @@ const termPassthrough = (multiplexer: string, query: string): string => {
|
|
|
218
217
|
return multiplexer === "screen" ? `\x1bP${query}\x1b\\` : query
|
|
219
218
|
}
|
|
220
219
|
|
|
221
|
-
/**
|
|
220
|
+
/**
|
|
221
|
+
* Every query the probe asks, as one write; `TermProbe` reads the answers.
|
|
222
|
+
*
|
|
223
|
+
* @param env The environment, which picks the queries: under a multiplexer some are wrapped to
|
|
224
|
+
* reach the terminal outside it, and under `screen` or the Linux console the kitty graphics and
|
|
225
|
+
* DECRQSS queries, which answer in strings, are left out.
|
|
226
|
+
* @returns The queries, ending in DA1, to `write` at once.
|
|
227
|
+
*/
|
|
222
228
|
export const probeQueries = (env: TermEnv): string => {
|
|
223
229
|
const mux: string = termMultiplexer(env)
|
|
224
230
|
const parts: string[] = ["\x1b[>q", "\x1b[?2026$p", "\x1b[?1016$p", "\x1b[?u"]
|
|
@@ -276,15 +282,22 @@ export class TermProbe {
|
|
|
276
282
|
/** How long to wait for the answer to DA1 (`TERM_PROBE_TIMEOUT_MS` by default). */
|
|
277
283
|
timeoutMs: f64
|
|
278
284
|
/** The decoder the terminal's bytes go through. */
|
|
279
|
-
decoder: KeyDecoder
|
|
285
|
+
private decoder: KeyDecoder
|
|
280
286
|
/** The replies so far, in order. */
|
|
281
287
|
replies: TermEvent[]
|
|
282
288
|
/** Every other event so far, in order: what the user typed during the probe. */
|
|
283
|
-
held: TermEvent[]
|
|
289
|
+
private held: TermEvent[]
|
|
284
290
|
/** When DA1 was answered; -1 before. */
|
|
285
|
-
answeredAtMs: f64
|
|
291
|
+
private answeredAtMs: f64
|
|
286
292
|
|
|
287
|
-
/**
|
|
293
|
+
/**
|
|
294
|
+
* A probe whose queries were written at `sentAtMs` to a window of `cols` by `rows` cells.
|
|
295
|
+
*
|
|
296
|
+
* @param env The environment the queries were made for, also read for what replies cannot say.
|
|
297
|
+
* @param cols The window's width in cells, for working out a cell's pixels from the window's.
|
|
298
|
+
* @param rows The window's height in cells, likewise.
|
|
299
|
+
* @param sentAtMs When the queries were written, in milliseconds on the clock `feed` is given.
|
|
300
|
+
*/
|
|
288
301
|
constructor(env: TermEnv, cols: i32, rows: i32, sentAtMs: f64) {
|
|
289
302
|
this.env = env
|
|
290
303
|
this.cols = cols
|
|
@@ -298,7 +311,7 @@ export class TermProbe {
|
|
|
298
311
|
}
|
|
299
312
|
|
|
300
313
|
/** Take in `events`: replies kept, DA1 marking the probe done, the rest held. */
|
|
301
|
-
take(events: TermEvent[], nowMs: f64): void {
|
|
314
|
+
private take(events: TermEvent[], nowMs: f64): void {
|
|
302
315
|
for (const e of events) {
|
|
303
316
|
if (e.type !== EVENT_REPLY) {
|
|
304
317
|
this.held.push(e)
|
|
@@ -311,12 +324,22 @@ export class TermProbe {
|
|
|
311
324
|
}
|
|
312
325
|
}
|
|
313
326
|
|
|
314
|
-
/**
|
|
327
|
+
/**
|
|
328
|
+
* Decode `bytes[0, count)`, read at `nowMs`.
|
|
329
|
+
*
|
|
330
|
+
* @param bytes The bytes read from the terminal.
|
|
331
|
+
* @param count How many of `bytes`, from the first, were read.
|
|
332
|
+
* @param nowMs When they were read, in milliseconds on the clock `sentAtMs` used.
|
|
333
|
+
*/
|
|
315
334
|
feed(bytes: u8[], count: i32, nowMs: f64): void {
|
|
316
335
|
this.take(this.decoder.feedEvents(bytes, count, nowMs), nowMs)
|
|
317
336
|
}
|
|
318
337
|
|
|
319
|
-
/**
|
|
338
|
+
/**
|
|
339
|
+
* Whether DA1 has been answered, so every reply is in.
|
|
340
|
+
*
|
|
341
|
+
* @returns True once DA1 is answered.
|
|
342
|
+
*/
|
|
320
343
|
done(): boolean {
|
|
321
344
|
return this.answeredAtMs >= 0.0
|
|
322
345
|
}
|
|
@@ -324,6 +347,9 @@ export class TermProbe {
|
|
|
324
347
|
/**
|
|
325
348
|
* Whether `timeoutMs` has passed since the send with DA1 unanswered. Once it
|
|
326
349
|
* has, it calls `finish(nowMs)`, so `heldEvents` is complete.
|
|
350
|
+
*
|
|
351
|
+
* @param nowMs The time now, in milliseconds on the clock `sentAtMs` used.
|
|
352
|
+
* @returns True once the time is up with DA1 unanswered; false while waiting, and always once DA1 is answered.
|
|
327
353
|
*/
|
|
328
354
|
timedOut(nowMs: f64): boolean {
|
|
329
355
|
if (this.done() || nowMs - this.sentAtMs < this.timeoutMs) {
|
|
@@ -337,25 +363,41 @@ export class TermProbe {
|
|
|
337
363
|
* End the probe at `nowMs`: what the decoder still holds — an ESC or the
|
|
338
364
|
* start of a key that came in the read with DA1, or an unfinished reply on a
|
|
339
365
|
* timeout — is given up (the ESC as a key, the reply as nothing) and
|
|
340
|
-
* `heldEvents` returned complete. Call it once
|
|
341
|
-
*
|
|
366
|
+
* `heldEvents` returned complete. Call it once when `done()` ends the probe
|
|
367
|
+
* (`timedOut` calls it itself), and hand every later read to the program's own decoder.
|
|
368
|
+
*
|
|
369
|
+
* @param nowMs The time now, in milliseconds on the clock `sentAtMs` used.
|
|
370
|
+
* @returns The events that were not replies, in the order they came: the probe's own array that `heldEvents` gives,
|
|
371
|
+
* not a copy.
|
|
342
372
|
*/
|
|
343
373
|
finish(nowMs: f64): TermEvent[] {
|
|
344
374
|
this.take(this.decoder.pollEvents(nowMs + this.decoder.escapeTimeoutMs), nowMs)
|
|
345
375
|
return this.held
|
|
346
376
|
}
|
|
347
377
|
|
|
348
|
-
/**
|
|
378
|
+
/**
|
|
379
|
+
* The events that were not replies — keys typed, a paste — in the order they came, for the program to handle.
|
|
380
|
+
*
|
|
381
|
+
* @returns The held events, oldest first; complete only after `finish`. The probe's own array, not a copy.
|
|
382
|
+
*/
|
|
349
383
|
heldEvents(): TermEvent[] {
|
|
350
384
|
return this.held
|
|
351
385
|
}
|
|
352
386
|
|
|
353
|
-
/**
|
|
387
|
+
/**
|
|
388
|
+
* The time from the send to the answer to DA1, in milliseconds; -1 before it.
|
|
389
|
+
*
|
|
390
|
+
* @returns The round trip in milliseconds, or -1 while DA1 is unanswered.
|
|
391
|
+
*/
|
|
354
392
|
rttMs(): f64 {
|
|
355
393
|
return this.done() ? this.answeredAtMs - this.sentAtMs : -1.0
|
|
356
394
|
}
|
|
357
395
|
|
|
358
|
-
/**
|
|
396
|
+
/**
|
|
397
|
+
* What the terminal can do, from the replies so far and the environment.
|
|
398
|
+
*
|
|
399
|
+
* @returns The capabilities found; what no reply settled comes from the environment.
|
|
400
|
+
*/
|
|
359
401
|
result(): TermCaps {
|
|
360
402
|
const env: TermEnv = this.env
|
|
361
403
|
const caps: TermCaps = new TermCaps()
|