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.
Files changed (129) hide show
  1. package/docs/AI.md +50 -4
  2. package/docs/INSTALL.md +73 -59
  3. package/package.json +19 -5
  4. package/runtime/async.d.mts +2 -0
  5. package/runtime/async.mjs +7 -0
  6. package/runtime/nish.d.ts +4849 -430
  7. package/runtime/nish.h +25 -0
  8. package/runtime/page.d.ts +53 -0
  9. package/runtime/page.mjs +146 -0
  10. package/runtime/runtime-async.c +557 -0
  11. package/runtime/runtime-wasm.c +63 -7
  12. package/runtime/vite.d.ts +30 -0
  13. package/runtime/vite.mjs +150 -0
  14. package/scripts/build.sh +252 -15
  15. package/scripts/fuzz.sh +1 -0
  16. package/scripts/undocumented.txt +14 -0
  17. package/std/README.md +107 -82
  18. package/std/base64.ts +34 -5
  19. package/std/bytes.ts +279 -59
  20. package/std/cli-args.ts +296 -70
  21. package/std/cli-flags.ts +184 -19
  22. package/std/cli.ts +475 -54
  23. package/std/collections.ts +121 -23
  24. package/std/compress/checksum.ts +44 -14
  25. package/std/compress/deflate.ts +224 -111
  26. package/std/compress/gzip.ts +217 -53
  27. package/std/compress/zlib.ts +63 -7
  28. package/std/crypto/aes.ts +128 -52
  29. package/std/crypto/base64url.ts +29 -5
  30. package/std/crypto/bcrypt-batch.ts +101 -34
  31. package/std/crypto/bcrypt.ts +93 -27
  32. package/std/crypto/chacha20poly1305.ts +69 -14
  33. package/std/crypto/ct.ts +32 -13
  34. package/std/crypto/ed25519.ts +66 -16
  35. package/std/crypto/hkdf.ts +198 -13
  36. package/std/crypto/hmac.ts +242 -37
  37. package/std/crypto/jwt.ts +204 -72
  38. package/std/crypto/p256.ts +222 -72
  39. package/std/crypto/p384.ts +51 -23
  40. package/std/crypto/pki-roots.ts +32 -2
  41. package/std/crypto/pki.ts +99 -30
  42. package/std/crypto/rsa.ts +56 -22
  43. package/std/crypto/sha1.ts +3 -0
  44. package/std/crypto/sha256.ts +24 -1
  45. package/std/crypto/sha512.ts +99 -14
  46. package/std/crypto/x25519.ts +163 -24
  47. package/std/crypto/x509-random.ts +10 -0
  48. package/std/crypto/x509.ts +133 -66
  49. package/std/host/ring.ts +124 -0
  50. package/std/json-equal.ts +31 -11
  51. package/std/json-format.ts +112 -13
  52. package/std/json-number.ts +76 -26
  53. package/std/json-pointer.ts +23 -1
  54. package/std/json-tokenizer.ts +126 -51
  55. package/std/json-typed.ts +757 -96
  56. package/std/json-value.ts +438 -157
  57. package/std/json.ts +170 -38
  58. package/std/map.ts +25 -7
  59. package/std/math.ts +36 -4
  60. package/std/net/buffer-pool.ts +42 -11
  61. package/std/net/dns-config.ts +109 -23
  62. package/std/net/dns-message.ts +62 -7
  63. package/std/net/dns-resolver.ts +247 -53
  64. package/std/net/dns.ts +203 -35
  65. package/std/net/fetch.ts +1267 -290
  66. package/std/net/handler.ts +296 -80
  67. package/std/net/hpack.ts +290 -77
  68. package/std/net/http-compress.ts +152 -36
  69. package/std/net/http-fields.ts +85 -9
  70. package/std/net/http1-client-codec.ts +129 -44
  71. package/std/net/http1-codec.ts +279 -114
  72. package/std/net/http1-server.ts +409 -139
  73. package/std/net/http2-frame.ts +289 -51
  74. package/std/net/http2-tls.ts +152 -49
  75. package/std/net/http2.ts +314 -160
  76. package/std/net/http3-frame.ts +165 -27
  77. package/std/net/http3-server.ts +282 -131
  78. package/std/net/http3.ts +377 -173
  79. package/std/net/outbound.ts +37 -3
  80. package/std/net/postgres-carrier.ts +144 -8
  81. package/std/net/postgres.ts +898 -260
  82. package/std/net/prefork.ts +202 -65
  83. package/std/net/qpack.ts +120 -32
  84. package/std/net/quic-conn-ack.ts +60 -12
  85. package/std/net/quic-conn-cid.ts +135 -27
  86. package/std/net/quic-conn-params.ts +109 -22
  87. package/std/net/quic-datagram.ts +50 -3
  88. package/std/net/quic-frame.ts +333 -23
  89. package/std/net/quic-listener.ts +161 -75
  90. package/std/net/quic-packet.ts +373 -73
  91. package/std/net/quic-recovery.ts +287 -65
  92. package/std/net/quic-stream.ts +436 -146
  93. package/std/net/quic.ts +693 -323
  94. package/std/net/router.ts +87 -40
  95. package/std/net/tls/client.ts +167 -51
  96. package/std/net/tls/codec.ts +494 -68
  97. package/std/net/tls/record-client.ts +140 -73
  98. package/std/net/tls/record-server.ts +233 -105
  99. package/std/net/tls/record.ts +142 -36
  100. package/std/net/tls/schedule.ts +235 -26
  101. package/std/net/tls/ticket.ts +161 -40
  102. package/std/net/tls/verify.ts +57 -10
  103. package/std/net/tls-tcp.ts +308 -95
  104. package/std/net/tls.ts +276 -102
  105. package/std/net/upstream.ts +81 -45
  106. package/std/net/url.ts +109 -22
  107. package/std/net/websocket.ts +140 -26
  108. package/std/net/webtransport.ts +243 -106
  109. package/std/pair.ts +10 -2
  110. package/std/random.ts +42 -15
  111. package/std/regex.ts +422 -257
  112. package/std/region.ts +70 -15
  113. package/std/secret.ts +52 -11
  114. package/std/sort.ts +17 -2
  115. package/std/sqlite.ts +256 -35
  116. package/std/sync/ring.ts +229 -56
  117. package/std/sync/shm.ts +203 -23
  118. package/std/term-io.ts +154 -78
  119. package/std/term-pixels.ts +144 -19
  120. package/std/term-probe.ts +77 -35
  121. package/std/term-rate.ts +199 -118
  122. package/std/term.ts +400 -175
  123. package/std/testing.ts +130 -32
  124. package/std/text.ts +84 -17
  125. package/std/threads.ts +248 -56
  126. package/std/yaml-json.ts +111 -97
  127. package/std/yaml-lines.ts +47 -15
  128. package/std/yaml-write.ts +45 -30
  129. package/std/yaml.ts +764 -384
package/std/term.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * `std/term` — a terminal drawn as a grid of coloured cells, and the keys a
2
+ * `nish/term` — a terminal drawn as a grid of coloured cells, and the keys a
3
3
  * terminal sends turned into events.
4
4
  *
5
5
  * import { Screen, Canvas, rgb, COLOR_DEFAULT, enterAltScreen, hideCursor } from "nish/term";
@@ -79,33 +79,40 @@
79
79
  * sends no releases, so while `releasesAreSynthetic` is set (until the first
80
80
  * real release) a key counts as let go when its repeats stop: `firstRepeatMs`
81
81
  * (700 ms) after a press that has not repeated and `releaseGapMs` (100 ms)
82
- * after its last repeat. Silence is measured in stream time when the caller has
83
- * one: term-io sends an in-order query after each frame and calls `delivered`
84
- * with the send time of each one answered, since by then everything the
85
- * terminal generated before it has arrived. Measured that way a stall, a burst
86
- * or jittery reads delay the watermark as much as the keys, so a held key never
87
- * flickers and a tap comes up 700 ms plus one round trip after its press. With
88
- * no watermark yet, silence is wall-clock time between `apply`s, which a link
89
- * that reads in bursts more than `releaseGapMs` apart can mistake for a key let
90
- * go. While acks stop, nothing is released: there is no wall-clock backstop,
91
- * and a caller that stops getting answers clears `watermarked` to fall back to
92
- * the wall clock. term-io does so once 1000 ms pass with no query answered,
93
- * frames sent or not, which no working link reaches while it draws.
94
- * Within one read the caller `apply`s its events before calling `delivered` for
95
- * the answer it carries: a `tick` between the two would judge keys against a
96
- * watermark that already covers repeats not yet applied, and report a release
97
- * and then a press. A non-finite time is ignored. Under X11 and Windows only
98
- * the last key pressed auto-repeats, so holding W and then pressing D lets W go
99
- * `releaseGapMs` after its last repeat. A real release of a key not down is
100
- * dropped, so one arriving after its key was made up as released is not
101
- * reported twice. Under kitty a real release that is lost leaves its key down
102
- * until focus is lost, and for good without focus reporting. A key the batch
103
- * `apply` takes names stays down. A made-up release carries the code and
104
- * modifiers the key last came with. Losing focus lets every key go, and Ctrl-C
105
- * becomes `EVENT_QUIT` while `quitOnCtrlC` is set. Under the kitty protocol a
106
- * non-Latin layout sends Ctrl-C as its own letter (`ESC [ 1089 ; 5 u` for
107
- * Russian С), which is not read as a quit: the base-layout key that flag 4
108
- * (report alternate keys) adds is not read yet.
82
+ * after its last repeat.
83
+ *
84
+ * Silence is measured in stream time when the caller has one: term-io sends an
85
+ * in-order query after each frame and calls `delivered` with the send time of
86
+ * each one answered, since by then everything the terminal generated before it
87
+ * has arrived. Measured that way a stall, a burst or jittery reads delay the
88
+ * watermark as much as the keys, so a held key never flickers and a tap comes
89
+ * up 700 ms plus one round trip after its press.
90
+ *
91
+ * With no watermark yet, silence is wall-clock time between `apply`s, which a
92
+ * link that reads in bursts more than `releaseGapMs` apart can mistake for a
93
+ * key let go. While acks stop, nothing is released: there is no wall-clock
94
+ * backstop, and a caller that stops getting answers clears `watermarked` to
95
+ * fall back to the wall clock. term-io does so once 1000 ms pass with no query
96
+ * answered, frames sent or not, which no working link reaches while it draws.
97
+ *
98
+ * Within one read the caller `apply`s its events before calling `delivered` for
99
+ * the answer it carries: a `tick` between the two would judge keys against a
100
+ * watermark that already covers repeats not yet applied, and report a release
101
+ * and then a press. A non-finite time is ignored. Under X11 and Windows only
102
+ * the last key pressed auto-repeats, so holding W and then pressing D lets W go
103
+ * `releaseGapMs` after its last repeat.
104
+ *
105
+ * A real release of a key not down is dropped, so one arriving after its key
106
+ * was made up as released is not reported twice. Under kitty a real release
107
+ * that is lost leaves its key down until focus is lost, and for good without
108
+ * focus reporting. A key the batch `apply` takes names stays down. A made-up
109
+ * release carries the code and modifiers the key last came with. Losing focus
110
+ * lets every key go, and Ctrl-C becomes `EVENT_QUIT` while `quitOnCtrlC` is
111
+ * set.
112
+ *
113
+ * Under the kitty protocol a non-Latin layout sends Ctrl-C as its own letter
114
+ * (`ESC [ 1089 ; 5 u` for Russian С), which is not read as a quit: the
115
+ * base-layout key that flag 4 (report alternate keys) adds is not read yet.
109
116
  *
110
117
  * **The same bytes under Node.** Natively a string is UTF-8 bytes, and `flush`
111
118
  * builds its answer as bytes and makes one string of them. Under Node a string
@@ -114,10 +121,11 @@
114
121
  * way, which `tests/run.js` checks ("nish/term prints the same bytes under
115
122
  * runtime/nish.mjs"). `text` reads a string's code points as UTF-8 natively and
116
123
  * as UTF-16 under Node, so a string literal draws the same cells both ways.
117
- * A `TermEvent`'s `text` is made from bytes the terminal sent: natively it
118
- * holds them as they came, and under Node they are decoded as UTF-8 without
119
- * checking, so malformed UTF-8 in a paste or reply can differ between the two.
120
- * Integer division is spelled `(a / b) | 0`, which truncates in both.
124
+ *
125
+ * A `TermEvent`'s `text` is made from bytes the terminal sent: natively it
126
+ * holds them as they came, and under Node they are decoded as UTF-8 without
127
+ * checking, so malformed UTF-8 in a paste or reply can differ between the two.
128
+ * Integer division is spelled `(a / b) | 0`, which truncates in both.
121
129
  */
122
130
 
123
131
  /** The terminal's own colour, foreground or background: SGR 39 and 49. */
@@ -412,7 +420,14 @@ const termLeadBits = (c: i32, width: i32): i32 => {
412
420
  return width === 3 ? c & 15 : c & 7
413
421
  }
414
422
 
415
- /** The colour with red, green and blue each in 0–255, as `0xRRGGBB`; a component outside is clamped. */
423
+ /**
424
+ * A colour from its red, green and blue.
425
+ *
426
+ * @param r Red, 0–255; a value outside is clamped.
427
+ * @param g Green, 0–255; a value outside is clamped.
428
+ * @param b Blue, 0–255; a value outside is clamped.
429
+ * @returns The colour as `0xRRGGBB`, for any `fg`, `bg` or pixel.
430
+ */
416
431
  export const rgb = (r: i32, g: i32, b: i32): i32 =>
417
432
  (termClamp(r, 0, 255) << 16) | (termClamp(g, 0, 255) << 8) | termClamp(b, 0, 255)
418
433
 
@@ -431,9 +446,11 @@ const termCubeLevel = (v: i32): i32 => {
431
446
  const termCubeValue = (q: i32): i32 => (q === 0 ? 0 : q * 40 + 55)
432
447
 
433
448
  /**
434
- * The 256-colour palette index nearest `colour`: one of the 6×6×6 cube
435
- * (16–231) or of the 24 greys (232–255), whichever is closer in RGB.
436
- * `COLOR_DEFAULT` stays itself.
449
+ * The 256-colour palette entry nearest a colour: one of the 6×6×6 cube (16–231)
450
+ * or of the 24 greys (232–255), whichever is closer in RGB.
451
+ *
452
+ * @param colour An `0xRRGGBB` colour, or `COLOR_DEFAULT`.
453
+ * @returns The palette index; `COLOR_DEFAULT` stays itself.
437
454
  */
438
455
  export const paletteIndex = (colour: i32): i32 => {
439
456
  if (colour < 0) {
@@ -460,35 +477,73 @@ export const paletteIndex = (colour: i32): i32 => {
460
477
  return greyDistance < cubeDistance ? step + 232 : cube
461
478
  }
462
479
 
463
- /** Switch to the alternate screen, which the terminal restores the old contents from on leaving. */
480
+ /**
481
+ * Switch to the alternate screen, which the terminal restores the old contents
482
+ * from on leaving.
483
+ *
484
+ * @returns The escape sequence, to `write`.
485
+ */
464
486
  export const enterAltScreen = (): string => "\x1b[?1049h"
465
487
 
466
- /** Leave the alternate screen and show what was there before `enterAltScreen`. */
488
+ /**
489
+ * Leave the alternate screen and show what was there before `enterAltScreen`.
490
+ *
491
+ * @returns The escape sequence, to `write`.
492
+ */
467
493
  export const leaveAltScreen = (): string => "\x1b[?1049l"
468
494
 
469
- /** Hide the cursor, so a redraw does not flicker it across the screen. */
495
+ /**
496
+ * Hide the cursor, so a redraw does not flicker it across the screen.
497
+ *
498
+ * @returns The escape sequence, to `write`.
499
+ */
470
500
  export const hideCursor = (): string => "\x1b[?25l"
471
501
 
472
- /** Show the cursor again. */
502
+ /**
503
+ * Show the cursor again.
504
+ *
505
+ * @returns The escape sequence, to `write`.
506
+ */
473
507
  export const showCursor = (): string => "\x1b[?25h"
474
508
 
475
- /** Move the cursor home and erase the whole screen; a `Screen` that was drawn on wants `invalidate` after it. */
509
+ /**
510
+ * Move the cursor home and erase the whole screen; a `Screen` that was drawn on
511
+ * wants `invalidate` after it.
512
+ *
513
+ * @returns The escape sequences, to `write`.
514
+ */
476
515
  export const clearScreen = (): string => "\x1b[H\x1b[2J"
477
516
 
478
- /** Reset colour and attributes to the terminal's defaults: SGR 0. */
517
+ /**
518
+ * Reset colour and attributes to the terminal's defaults: SGR 0.
519
+ *
520
+ * @returns The escape sequence, to `write`.
521
+ */
479
522
  export const resetStyle = (): string => "\x1b[0m"
480
523
 
481
- /** Move the cursor to column `x`, row `y`, both from 0. */
524
+ /**
525
+ * Move the cursor to a cell.
526
+ *
527
+ * @param x The column, from 0 at the left.
528
+ * @param y The row, from 0 at the top.
529
+ * @returns The escape sequence, to `write`.
530
+ */
482
531
  export const moveCursor = (x: i32, y: i32): string => `\x1b[${y + 1};${x + 1}H`
483
532
 
484
533
  /**
485
534
  * Begin a synchronized update (DEC mode 2026): a terminal that knows the mode
486
535
  * holds what follows until `endSync`, then paints it in one go, so a frame is
487
536
  * never seen half drawn. One that does not ignores it.
537
+ *
538
+ * @returns The escape sequence, to `write`.
488
539
  */
489
540
  export const beginSync = (): string => "\x1b[?2026h"
490
541
 
491
- /** End a synchronized update, so the terminal paints what `beginSync` held. */
542
+ /**
543
+ * End a synchronized update, so the terminal paints what `beginSync` held.
544
+ *
545
+ * @returns The escape sequence, to `write`.
546
+ */
492
547
  export const endSync = (): string => "\x1b[?2026l"
493
548
 
494
549
  /**
@@ -598,30 +653,34 @@ export class Screen {
598
653
  /** The frame's attributes, laid out as `glyphs`: `ATTR_BOLD` and `ATTR_INVERSE`, or'd. */
599
654
  attrs: i32[]
600
655
  /** The code points the terminal was last sent; -1 is a cell not yet sent. */
601
- sentGlyphs: i32[]
656
+ private sentGlyphs: i32[]
602
657
  /** The foregrounds the terminal was last sent. */
603
- sentFgs: i32[]
658
+ private sentFgs: i32[]
604
659
  /** The backgrounds the terminal was last sent. */
605
- sentBgs: i32[]
660
+ private sentBgs: i32[]
606
661
  /** The attributes the terminal was last sent. */
607
- sentAttrs: i32[]
662
+ private sentAttrs: i32[]
608
663
  /** The bytes `flush` builds, sized by `resize` and reused from frame to frame. */
609
- out: u8[]
664
+ private out: u8[]
610
665
  /** Columns. */
611
666
  width: i32
612
667
  /** Rows. */
613
668
  height: i32
614
669
  /** How many of `out` the flush in progress has written. */
615
- outLen: i32
670
+ private outLen: i32
616
671
  /** Whether colours go out as 256-colour palette indices rather than 24-bit RGB. */
617
672
  palette256: boolean
618
673
  /** Whether a `flush` that changes anything is wrapped in `beginSync` and `endSync`; off until set. */
619
674
  sync: boolean
620
675
 
621
676
  /**
622
- * A `width × height` screen of blank cells in the terminal's colours, none
623
- * of them sent yet, so the first `flush` draws them all. With `palette256`
624
- * set, every colour is sent as its nearest 256-colour index (`paletteIndex`).
677
+ * A screen of blank cells in the terminal's colours, none of them sent yet,
678
+ * so the first `flush` draws them all.
679
+ *
680
+ * @param width Columns, as the terminal reports them.
681
+ * @param height Rows, as the terminal reports them.
682
+ * @param palette256 Whether to send every colour as its nearest 256-colour index
683
+ * (`paletteIndex`), for a terminal without 24-bit colour.
625
684
  */
626
685
  constructor(width: i32, height: i32, palette256: boolean) {
627
686
  this.width = width < 0 ? 0 : width
@@ -642,10 +701,13 @@ export class Screen {
642
701
  }
643
702
 
644
703
  /**
645
- * Make the screen `width × height`, blank and unsent: a terminal that changed
646
- * size is redrawn in full. Each side is at most 4,096 cells and the screen at
647
- * most 1,048,576, rows dropped to fit, so no size a terminal reports makes it
704
+ * Make the screen a new size, blank and unsent: a terminal that changed size
705
+ * is redrawn in full. Each side is at most 4,096 cells and the screen at most
706
+ * 1,048,576, rows dropped to fit, so no size a terminal reports makes it
648
707
  * overflow or run out of memory.
708
+ *
709
+ * @param width The new columns.
710
+ * @param height The new rows.
649
711
  */
650
712
  resize(width: i32, height: i32): void {
651
713
  this.width = termClamp(width, 0, TERM_MAX_SIDE)
@@ -669,7 +731,11 @@ export class Screen {
669
731
  this.invalidate()
670
732
  }
671
733
 
672
- /** Blank every cell of the frame being drawn: a space on `background`, default foreground, no attributes. */
734
+ /**
735
+ * Blank every cell of the frame being drawn.
736
+ *
737
+ * @param background The colour each cell's space is drawn on; the foreground is the default and no attribute is set.
738
+ */
673
739
  clear(background: i32): void {
674
740
  this.glyphs.fill(TERM_SPACE)
675
741
  this.fgs.fill(COLOR_DEFAULT)
@@ -686,9 +752,17 @@ export class Screen {
686
752
  }
687
753
 
688
754
  /**
689
- * Draw code point `glyph` at column `x`, row `y`; a cell off the screen is
690
- * not drawn. Bits of `attrs` other than `ATTR_BOLD` and `ATTR_INVERSE` are
691
- * dropped, so `flush` never has an attribute it cannot write.
755
+ * Draw one character in a cell; a cell off the screen is not drawn.
756
+ *
757
+ * @param x The column, from 0.
758
+ * @param y The row, from 0.
759
+ * @param glyph The character's code point, taken to be one column wide. A C0 or C1 control, DEL,
760
+ * a surrogate or a value that is no code point is sent as U+FFFD, so a cell cannot move the
761
+ * cursor or open an escape.
762
+ * @param fg The character's colour, `0xRRGGBB` or `COLOR_DEFAULT`.
763
+ * @param bg The cell's colour behind it, `0xRRGGBB` or `COLOR_DEFAULT`.
764
+ * @param attrs `ATTR_BOLD` and `ATTR_INVERSE` bits; any other bit is dropped, so `flush` never
765
+ * has an attribute it cannot write.
692
766
  */
693
767
  set(x: i32, y: i32, glyph: i32, fg: i32, bg: i32, attrs: i32): void {
694
768
  if (x < 0 || y < 0 || x >= this.width || y >= this.height) {
@@ -705,8 +779,15 @@ export class Screen {
705
779
  }
706
780
 
707
781
  /**
708
- * Draw `s` from column `x` of row `y`, one cell per character, clipped at
709
- * the edges, and answer the column after its last character.
782
+ * Draw a string along a row, one cell per character, clipped at the edges.
783
+ *
784
+ * @param x The column of the first character, from 0; it may be off the screen.
785
+ * @param y The row, from 0.
786
+ * @param s The text. A control character in it, a line break included, takes a cell and is sent as U+FFFD.
787
+ * @param fg The characters' colour.
788
+ * @param bg The colour behind them.
789
+ * @param attrs `ATTR_BOLD` and `ATTR_INVERSE` bits, as `set` takes them.
790
+ * @returns The column after the last character, `x` plus the characters in `s`, whether or not they were clipped.
710
791
  */
711
792
  text(x: i32, y: i32, s: string, fg: i32, bg: i32, attrs: i32): i32 {
712
793
  const n: i32 = toI32(s.length)
@@ -720,7 +801,13 @@ export class Screen {
720
801
  return column
721
802
  }
722
803
 
723
- /** The code point drawn at column `x`, row `y` of the frame being drawn, or -1 off the screen. */
804
+ /**
805
+ * What the frame being drawn holds in a cell.
806
+ *
807
+ * @param x The column, from 0.
808
+ * @param y The row, from 0.
809
+ * @returns The code point drawn there, or -1 off the screen.
810
+ */
724
811
  glyphAt(x: i32, y: i32): i32 {
725
812
  if (x < 0 || y < 0 || x >= this.width || y >= this.height) {
726
813
  return -1
@@ -729,13 +816,13 @@ export class Screen {
729
816
  }
730
817
 
731
818
  /** Append one byte to the flush in progress; `flush` has reserved the room. */
732
- put(b: i32): void {
819
+ private put(b: i32): void {
733
820
  this.out[this.outLen] = toU8(b)
734
821
  this.outLen = this.outLen + 1
735
822
  }
736
823
 
737
824
  /** Append `v`, at least 0, in decimal: the three digits a colour channel has without recursing. */
738
- putDecimal(v: i32): void {
825
+ private putDecimal(v: i32): void {
739
826
  if (v >= 100) {
740
827
  if (v >= 1000) {
741
828
  this.putDecimal((v / 1000) | 0)
@@ -749,7 +836,7 @@ export class Screen {
749
836
  }
750
837
 
751
838
  /** Append `ESC [ ? 2026` and `final`, which sets or resets synchronized output. */
752
- putSyncMode(final: i32): void {
839
+ private putSyncMode(final: i32): void {
753
840
  this.put(TERM_ESC)
754
841
  this.put(TERM_BRACKET)
755
842
  this.put(TERM_PRIVATE)
@@ -763,7 +850,7 @@ export class Screen {
763
850
  * negative number — goes as U+FFFD, so a cell cannot move the cursor, open
764
851
  * an escape or send bytes that are not UTF-8.
765
852
  */
766
- putGlyph(cp: i32): void {
853
+ private putGlyph(cp: i32): void {
767
854
  if (cp >= TERM_SPACE && cp < TERM_DELETE) {
768
855
  this.put(cp)
769
856
  } else if (cp >= 160 && cp < 2048) {
@@ -783,7 +870,7 @@ export class Screen {
783
870
  }
784
871
 
785
872
  /** Append the SGR parameters for colour `key` (an RGB or a palette index) on base 30 (foreground) or 40. */
786
- putColour(base: i32, key: i32): void {
873
+ private putColour(base: i32, key: i32): void {
787
874
  if (key < 0) {
788
875
  this.putDecimal(base + 9)
789
876
  return
@@ -807,7 +894,7 @@ export class Screen {
807
894
  }
808
895
 
809
896
  /** The value a colour is sent as and compared by: itself, or its palette index under `palette256`. */
810
- colourKey(colour: i32): i32 {
897
+ private colourKey(colour: i32): i32 {
811
898
  if (colour < 0) {
812
899
  return COLOR_DEFAULT
813
900
  }
@@ -815,20 +902,22 @@ export class Screen {
815
902
  }
816
903
 
817
904
  /**
818
- * The escapes and glyphs that turn the frame last sent into the frame drawn,
819
- * and the drawn frame becomes the sent one. Write the answer with `write`;
820
- * an empty string means nothing changed.
905
+ * Turn the frame last sent into the frame drawn, and make the drawn frame the
906
+ * sent one.
821
907
  *
822
908
  * Changed cells are visited row by row. The cursor is moved only when it is
823
909
  * not already at the cell — `ESC [ n C` along the row, `ESC [ row ; col H`
824
910
  * otherwise — and a gap of up to three unchanged ASCII cells in the current
825
- * style is rewritten instead, which is shorter than the move. A style is
826
- * sent only when it differs from the previous cell's, and only the parts
827
- * that differ, from a reset when an attribute has to be turned off. A flush
828
- * that leaves the terminal in a style other than the default ends with
829
- * `ESC [ 0 m`, so what the caller writes next starts plain. With `sync` set,
830
- * a flush that changes anything is wrapped in `beginSync` and `endSync`, in
831
- * the same string, so the terminal paints the frame whole.
911
+ * style is rewritten instead, which is shorter than the move. A style is sent
912
+ * only when it differs from the previous cell's, and only the parts that
913
+ * differ, from a reset when an attribute has to be turned off.
914
+ *
915
+ * A flush that leaves the terminal in a style other than the default ends
916
+ * with `ESC [ 0 m`, so what the caller writes next starts plain. With `sync`
917
+ * set, a flush that changes anything is wrapped in `beginSync` and `endSync`,
918
+ * in the same string, so the terminal paints the frame whole.
919
+ *
920
+ * @returns The escapes and glyphs, to `write`; `""` when nothing changed.
832
921
  */
833
922
  flush(): string {
834
923
  const w: i32 = this.width
@@ -1021,7 +1110,12 @@ export class Canvas {
1021
1110
  /** One colour per pixel, row by row. */
1022
1111
  pixels: i32[]
1023
1112
 
1024
- /** A canvas of `screen`'s size, every pixel `COLOR_DEFAULT`. Make a new one after the screen is resized. */
1113
+ /**
1114
+ * A canvas of `screen`'s size, every pixel `COLOR_DEFAULT`. Make a new one
1115
+ * after the screen is resized.
1116
+ *
1117
+ * @param screen The screen it draws on, two pixels to a cell, one above the other.
1118
+ */
1025
1119
  constructor(screen: Screen) {
1026
1120
  this.screen = screen
1027
1121
  this.width = screen.width
@@ -1030,7 +1124,11 @@ export class Canvas {
1030
1124
  this.pixels.fill(COLOR_DEFAULT)
1031
1125
  }
1032
1126
 
1033
- /** Set every pixel to `colour`. */
1127
+ /**
1128
+ * Set every pixel to one colour.
1129
+ *
1130
+ * @param colour The colour, `0xRRGGBB` or `COLOR_DEFAULT` for none.
1131
+ */
1034
1132
  clear(colour: i32): void {
1035
1133
  this.pixels.fill(colour)
1036
1134
  const rows: i32 = this.height >> 1
@@ -1041,7 +1139,13 @@ export class Canvas {
1041
1139
  }
1042
1140
  }
1043
1141
 
1044
- /** The colour of the pixel at `x`, `y`, or `COLOR_DEFAULT` off the canvas. */
1142
+ /**
1143
+ * What colour a pixel is.
1144
+ *
1145
+ * @param x The pixel column, from 0.
1146
+ * @param y The pixel row, from 0; two to a screen row.
1147
+ * @returns The colour, or `COLOR_DEFAULT` off the canvas.
1148
+ */
1045
1149
  pixel(x: i32, y: i32): i32 {
1046
1150
  if (x < 0 || y < 0 || x >= this.width || y >= this.height) {
1047
1151
  return COLOR_DEFAULT
@@ -1049,7 +1153,13 @@ export class Canvas {
1049
1153
  return this.pixels[y * this.width + x]
1050
1154
  }
1051
1155
 
1052
- /** Set the pixel at `x`, `y` to `colour`; a pixel off the canvas is not drawn. */
1156
+ /**
1157
+ * Set one pixel; a pixel off the canvas is not drawn.
1158
+ *
1159
+ * @param x The pixel column, from 0.
1160
+ * @param y The pixel row, from 0; two to a screen row.
1161
+ * @param colour The colour, `0xRRGGBB` or `COLOR_DEFAULT` for none.
1162
+ */
1053
1163
  setPixel(x: i32, y: i32, colour: i32): void {
1054
1164
  if (x < 0 || y < 0 || x >= this.width || y >= this.height) {
1055
1165
  return
@@ -1058,7 +1168,15 @@ export class Canvas {
1058
1168
  this.paintCell(x, y >> 1)
1059
1169
  }
1060
1170
 
1061
- /** Fill the `w × h` rectangle whose top-left pixel is `x`, `y`, clipped to the canvas. */
1171
+ /**
1172
+ * Fill a rectangle, clipped to the canvas.
1173
+ *
1174
+ * @param x The top-left pixel's column.
1175
+ * @param y The top-left pixel's row.
1176
+ * @param w The width in pixels; 0 or less fills nothing.
1177
+ * @param h The height in pixels; 0 or less fills nothing.
1178
+ * @param colour The colour, `0xRRGGBB` or `COLOR_DEFAULT` for none.
1179
+ */
1062
1180
  fillRect(x: i32, y: i32, w: i32, h: i32, colour: i32): void {
1063
1181
  const left: i32 = x < 0 ? 0 : x
1064
1182
  const top: i32 = y < 0 ? 0 : y
@@ -1077,9 +1195,15 @@ export class Canvas {
1077
1195
  }
1078
1196
 
1079
1197
  /**
1080
- * Draw a line from `x0`, `y0` to `x1`, `y1`, both ends included (Bresenham's).
1081
- * Any `i32` ends will do: the walk stops once it leaves the canvas for good,
1082
- * though one that starts far outside steps all the way in.
1198
+ * Draw a line, both ends included (Bresenham's). Any `i32` ends will do: the
1199
+ * walk stops once it leaves the canvas for good, though one that starts far
1200
+ * outside steps all the way in.
1201
+ *
1202
+ * @param x0 The first end's pixel column.
1203
+ * @param y0 The first end's pixel row.
1204
+ * @param x1 The other end's pixel column.
1205
+ * @param y1 The other end's pixel row.
1206
+ * @param colour The colour, `0xRRGGBB` or `COLOR_DEFAULT` for none.
1083
1207
  */
1084
1208
  line(x0: i32, y0: i32, x1: i32, y1: i32, colour: i32): void {
1085
1209
  // The error terms are f64, exact here, because a span past 2^30 overflows `2 * err` in i32.
@@ -1117,9 +1241,15 @@ export class Canvas {
1117
1241
  }
1118
1242
 
1119
1243
  /**
1120
- * Draw `s` in cells from column `col` of cell row `row` in `fg`, each
1121
- * character on the colour of the pixels behind it (the top one, or the
1122
- * bottom one where the top is unset), and answer the column after it.
1244
+ * Draw a string in screen cells over the pixels, each character on the colour
1245
+ * of the pixels behind it (the top one, or the bottom one where the top is
1246
+ * unset).
1247
+ *
1248
+ * @param col The cell column of the first character, from 0.
1249
+ * @param row The cell row, from 0: pixel rows `2 * row` and `2 * row + 1`.
1250
+ * @param s The text.
1251
+ * @param fg The characters' colour.
1252
+ * @returns The column after the last character, whether or not it was clipped.
1123
1253
  */
1124
1254
  text(col: i32, row: i32, s: string, fg: i32): i32 {
1125
1255
  const n: i32 = toI32(s.length)
@@ -1135,7 +1265,10 @@ export class Canvas {
1135
1265
  return column
1136
1266
  }
1137
1267
 
1138
- /** Redraw the screen cell at `col`, `row` from its two pixels. */
1268
+ /**
1269
+ * Redraw the screen cell at `col`, `row` from its two pixels.
1270
+ * @internal Called by `drawHalfBlocks` in term-pixels.ts.
1271
+ */
1139
1272
  paintCell(col: i32, row: i32): void {
1140
1273
  const top: i32 = this.pixel(col, row * 2)
1141
1274
  const bottom: i32 = this.pixel(col, row * 2 + 1)
@@ -1156,7 +1289,12 @@ export class KeyEvent {
1156
1289
  /** The character's code point, the control byte, or the escape sequence's final byte. */
1157
1290
  code: i32
1158
1291
 
1159
- /** The event of kind `kind` with code `code`. */
1292
+ /**
1293
+ * A key event.
1294
+ *
1295
+ * @param kind One of the `KEY_` constants.
1296
+ * @param code The code point for `KEY_CHAR`, otherwise the byte that named the key.
1297
+ */
1160
1298
  constructor(kind: i32, code: i32) {
1161
1299
  this.kind = kind
1162
1300
  this.code = code
@@ -1205,7 +1343,11 @@ export class TermEvent {
1205
1343
  /** For `EVENT_FOCUS`, whether the terminal gained focus. */
1206
1344
  gained: boolean
1207
1345
 
1208
- /** An event of type `type` with every other field at its default. */
1346
+ /**
1347
+ * An event with every field but its type at its default.
1348
+ *
1349
+ * @param type One of the `EVENT_` constants.
1350
+ */
1209
1351
  constructor(type: i32) {
1210
1352
  this.type = type
1211
1353
  this.key = 0
@@ -1228,16 +1370,18 @@ export class TermEvent {
1228
1370
  }
1229
1371
 
1230
1372
  /**
1231
- * Bytes from a terminal in raw mode, turned into key events. A read can end
1232
- * in the middle of an escape sequence or a UTF-8 character; the decoder keeps
1233
- * the start and finishes it with the next `feed`. An ESC on its own is both the
1373
+ * Bytes from a terminal in raw mode, turned into key events. A read can end in
1374
+ * the middle of an escape sequence or a UTF-8 character; the decoder keeps the
1375
+ * start and finishes it with the next `feed`. An ESC on its own is both the
1234
1376
  * escape key and the start of every arrow, so it is held until either more
1235
1377
  * bytes complete a sequence or `escapeTimeoutMs` passes with none, and then it
1236
- * is `KEY_ESCAPE`. Whatever is held past the timeout is given up the same way:
1237
- * the ESC as `KEY_ESCAPE`, the bytes after it as keys of their own, and an
1238
- * unfinished UTF-8 character as U+FFFD. The clock is the caller's: each call
1239
- * takes the time now in milliseconds, from any origin, so long as it does not
1240
- * go backwards.
1378
+ * is `KEY_ESCAPE`.
1379
+ *
1380
+ * Whatever is held past the timeout is given up the same way: the ESC as
1381
+ * `KEY_ESCAPE`, the bytes after it as keys of their own, and an unfinished
1382
+ * UTF-8 character as U+FFFD. The clock is the caller's: each call takes the
1383
+ * time now in milliseconds, from any origin, so long as it does not go
1384
+ * backwards.
1241
1385
  *
1242
1386
  * `feedEvents` and `pollEvents` decode the same bytes into `TermEvent`s
1243
1387
  * instead: keys with their modifiers and press, repeat or release, the mouse,
@@ -1249,10 +1393,13 @@ export class KeyDecoder {
1249
1393
  /** How long a held ESC waits for the rest of a sequence, in milliseconds. */
1250
1394
  escapeTimeoutMs: f64
1251
1395
  /** The bytes of an unfinished sequence, carried to the next call. */
1252
- pending: u8[]
1396
+ private pending: u8[]
1253
1397
  /** When the first held byte arrived (`feed`), or the last (`feedEvents`). */
1254
- pendingSince: f64
1255
- /** The bytes of the paste so far. */
1398
+ private pendingSince: f64
1399
+ /**
1400
+ * The bytes of the paste so far.
1401
+ * @internal Used by `termDecodePaste` and `termCsiEvent`.
1402
+ */
1256
1403
  pasteBytes: u8[]
1257
1404
  /**
1258
1405
  * How many cursor position reports (`ESC [ 6 n`) are awaited. `ESC [ r ; c R`
@@ -1266,10 +1413,18 @@ export class KeyDecoder {
1266
1413
  cellHeightPx: i32
1267
1414
  /** Whether SGR mouse reports are in pixels (mode 1016), so `px` and `py` are set and `x`, `y` derived. */
1268
1415
  pixelMouse: boolean
1269
- /** Whether a bracketed paste has begun and not ended. */
1416
+ /**
1417
+ * Whether a bracketed paste has begun and not ended.
1418
+ * @internal Used by `termDecodePaste`, `termCsiEvent` and `termDecodeEvents`.
1419
+ */
1270
1420
  pasting: boolean
1271
1421
 
1272
- /** A decoder that gives up on an unfinished sequence after `escapeTimeoutMs` (25–50 is usual). */
1422
+ /**
1423
+ * A decoder with nothing held.
1424
+ *
1425
+ * @param escapeTimeoutMs How long an unfinished sequence is held before it is given up, in
1426
+ * milliseconds; 25–50 is usual.
1427
+ */
1273
1428
  constructor(escapeTimeoutMs: f64) {
1274
1429
  this.escapeTimeoutMs = escapeTimeoutMs
1275
1430
  this.pending = []
@@ -1282,15 +1437,25 @@ export class KeyDecoder {
1282
1437
  this.pasteBytes = []
1283
1438
  }
1284
1439
 
1285
- /** Whether bytes are held, so the caller should `poll` once the timeout has passed. */
1440
+ /**
1441
+ * Whether bytes are held, so the caller should `poll` once the timeout has
1442
+ * passed.
1443
+ *
1444
+ * @returns True while the bytes of an unfinished sequence are held.
1445
+ */
1286
1446
  waiting(): boolean {
1287
1447
  return toI32(this.pending.length) > 0
1288
1448
  }
1289
1449
 
1290
1450
  /**
1291
- * The keys in `bytes[0, count)`, read at time `nowMs`, after whatever was
1292
- * held: held bytes older than the timeout are given up first, as keys of
1293
- * their own, and the rest continue into these.
1451
+ * Decode what was read, after whatever was held: held bytes older than the
1452
+ * timeout are given up first, as keys of their own, and the rest continue
1453
+ * into these.
1454
+ *
1455
+ * @param bytes The buffer the read filled.
1456
+ * @param count How many of its bytes the read filled, from the start.
1457
+ * @param nowMs When the bytes were read, on the clock `poll` is given.
1458
+ * @returns The keys, in the order they were typed; an unfinished sequence at the end is held.
1294
1459
  */
1295
1460
  feed(bytes: u8[], count: i32, nowMs: f64): KeyEvent[] {
1296
1461
  const events: KeyEvent[] = this.poll(nowMs)
@@ -1305,7 +1470,12 @@ export class KeyDecoder {
1305
1470
  return events
1306
1471
  }
1307
1472
 
1308
- /** The keys held bytes become once `escapeTimeoutMs` has passed since they arrived: none before then. */
1473
+ /**
1474
+ * Give up held bytes once the timeout has passed since they arrived.
1475
+ *
1476
+ * @param nowMs The time now, on the clock `feed` was given.
1477
+ * @returns The keys the held bytes become, or none before the timeout.
1478
+ */
1309
1479
  poll(nowMs: f64): KeyEvent[] {
1310
1480
  const events: KeyEvent[] = []
1311
1481
  const held: i32 = toI32(this.pending.length)
@@ -1318,9 +1488,14 @@ export class KeyDecoder {
1318
1488
  }
1319
1489
 
1320
1490
  /**
1321
- * The events in `bytes[0, count)`, read at time `nowMs`, after whatever was
1322
- * held, as `feed` takes them. The timeout counts from the last byte that
1323
- * arrived, so a reply split across reads is not given up halfway.
1491
+ * Decode what was read into events of every kind, after whatever was held, as
1492
+ * `feed` takes them. The timeout counts from the last byte that arrived, so a
1493
+ * reply split across reads is not given up halfway.
1494
+ *
1495
+ * @param bytes The buffer the read filled.
1496
+ * @param count How many of its bytes the read filled, from the start.
1497
+ * @param nowMs When the bytes were read, on the clock `pollEvents` is given.
1498
+ * @returns The events, in the order they were sent; an unfinished sequence or paste at the end is held.
1324
1499
  */
1325
1500
  feedEvents(bytes: u8[], count: i32, nowMs: f64): TermEvent[] {
1326
1501
  const events: TermEvent[] = this.pollEvents(nowMs)
@@ -1335,9 +1510,12 @@ export class KeyDecoder {
1335
1510
  }
1336
1511
 
1337
1512
  /**
1338
- * The events held bytes become once `escapeTimeoutMs` has passed: an ESC as
1339
- * `KEY_ESCAPE`, an unterminated DCS, APC or OSC as nothing. A paste waits
1340
- * for its end however long it takes.
1513
+ * Give up held bytes once the timeout has passed: an ESC as `KEY_ESCAPE`, an
1514
+ * unterminated DCS, APC or OSC as nothing. A paste waits for its end however
1515
+ * long it takes.
1516
+ *
1517
+ * @param nowMs The time now, on the clock `feedEvents` was given.
1518
+ * @returns The events the held bytes become, or none before the timeout.
1341
1519
  */
1342
1520
  pollEvents(nowMs: f64): TermEvent[] {
1343
1521
  const events: TermEvent[] = []
@@ -1351,7 +1529,7 @@ export class KeyDecoder {
1351
1529
  }
1352
1530
 
1353
1531
  /** The held bytes followed by `bytes[0, count)`. */
1354
- merged(bytes: u8[], count: i32): u8[] {
1532
+ private merged(bytes: u8[], count: i32): u8[] {
1355
1533
  const held: i32 = toI32(this.pending.length)
1356
1534
  const n: i32 = termClamp(count, 0, toI32(bytes.length))
1357
1535
  const work: u8[] = new Array<u8>(held + n)
@@ -1366,7 +1544,7 @@ export class KeyDecoder {
1366
1544
  }
1367
1545
 
1368
1546
  /** Hold `work` from `used` on; when `restart` is set, the held bytes count as arriving at `nowMs`. */
1369
- keep(work: u8[], used: i32, restart: boolean, nowMs: f64): void {
1547
+ private keep(work: u8[], used: i32, restart: boolean, nowMs: f64): void {
1370
1548
  const rest: u8[] = []
1371
1549
  for (let k: i32 = used; k < toI32(work.length); k++) {
1372
1550
  if (!(k >= 0)) {
@@ -1384,21 +1562,24 @@ export class KeyDecoder {
1384
1562
  /**
1385
1563
  * Which keys are down, from the events a `KeyDecoder` made — what a game asks
1386
1564
  * each frame. Under the kitty protocol with event types (flag 2) a terminal
1387
- * sends real releases. Without it a terminal sends nothing when a key comes
1388
- * up, only the press again as it auto-repeats, so a key is let go once its
1389
- * repeats stop: `releaseGapMs` after its last repeat, and `firstRepeatMs` after
1390
- * a press that has not repeated. The system waits before its first repeat —
1391
- * 660 ms under X11 by default, 500 under Windows, shorter under macOS — and
1392
- * repeats every 30–40 ms; a longer delay needs `firstRepeatMs` raised, and a
1393
- * slower rate `releaseGapMs`. Input arrives late and in bursts (over SSH, reads
1394
- * 150–250 ms apart are usual), so when the reads alone are known the silence
1395
- * is not: a caller that sends in-order queries calls `delivered(sentAtMs)` as
1396
- * each is answered, and silence is then measured against that watermark rather
1397
- * than the clock. A key last seen in a read at `A` is let go once the watermark
1398
- * reaches `A + gap`: had it still been down, the terminal would have generated
1399
- * a repeat before then, and it would have arrived ahead of the answer. Until
1400
- * the first `delivered`, and after the caller clears `watermarked`, the gap is
1401
- * measured against `nowMs`. Pure, with the caller's clock, as `KeyDecoder` is.
1565
+ * sends real releases. Without it a terminal sends nothing when a key comes up,
1566
+ * only the press again as it auto-repeats, so a key is let go once its repeats
1567
+ * stop: `releaseGapMs` after its last repeat, and `firstRepeatMs` after a press
1568
+ * that has not repeated.
1569
+ *
1570
+ * The system waits before its first repeat — 660 ms under X11 by default, 500
1571
+ * under Windows, shorter under macOS — and repeats every 30–40 ms; a longer
1572
+ * delay needs `firstRepeatMs` raised, and a slower rate `releaseGapMs`. Input
1573
+ * arrives late and in bursts (over SSH, reads 150–250 ms apart are usual), so
1574
+ * when the reads alone are known the silence is not: a caller that sends
1575
+ * in-order queries calls `delivered(sentAtMs)` as each is answered, and silence
1576
+ * is then measured against that watermark rather than the clock.
1577
+ *
1578
+ * A key last seen in a read at `A` is let go once the watermark reaches `A +
1579
+ * gap`: had it still been down, the terminal would have generated a repeat
1580
+ * before then, and it would have arrived ahead of the answer. Until the first
1581
+ * `delivered`, and after the caller clears `watermarked`, the gap is measured
1582
+ * against `nowMs`. Pure, with the caller's clock, as `KeyDecoder` is.
1402
1583
  */
1403
1584
  export class KeyState {
1404
1585
  /** How long after its last repeat a key is let go, in milliseconds (100 by default). */
@@ -1407,17 +1588,22 @@ export class KeyState {
1407
1588
  firstRepeatMs: f64
1408
1589
  /** The latest send time of an answered query that `delivered` was given: everything the terminal generated before it has arrived. */
1409
1590
  watermarkMs: f64
1410
- /** The keys down, each as `termKeyId` names it. */
1591
+ /**
1592
+ * The keys down, each as `termKeyId` names it.
1593
+ * @internal Used by `Terminal` in term-io.ts.
1594
+ */
1411
1595
  downKeys: i32[]
1412
1596
  /** When each key down last pressed or repeated. */
1413
- downSince: f64[]
1597
+ private downSince: f64[]
1414
1598
  /** Whether each key down has repeated. */
1415
- downRepeated: boolean[]
1599
+ private downRepeated: boolean[]
1416
1600
  /** The code each key down last came with, which its synthesized release carries. */
1417
- downCodes: i32[]
1601
+ private downCodes: i32[]
1418
1602
  /** The `MOD_` bits each key down last came with, which its synthesized release carries. */
1419
- downMods: i32[]
1420
- /** Whether releases are made up from silence; false from the first real release, or when the caller knows kitty flag 2 is on. */
1603
+ private downMods: i32[]
1604
+ /**
1605
+ * Whether releases are made up from silence; false from the first real release, or when the caller knows kitty flag 2 is on.
1606
+ */
1421
1607
  releasesAreSynthetic: boolean
1422
1608
  /** Whether silence is measured against `watermarkMs` rather than the clock; set by `delivered`, which the caller may clear when answers stop. */
1423
1609
  watermarked: boolean
@@ -1440,27 +1626,33 @@ export class KeyState {
1440
1626
  }
1441
1627
 
1442
1628
  /**
1443
- * Whether the key of kind `key` is down. `code` tells a `KEY_CHAR`,
1444
- * `KEY_CONTROL` or `KEY_UNKNOWN` apart and is ignored for the rest; a
1445
- * letter is the same key in either case, since Shift changes the code a
1446
- * terminal sends.
1629
+ * Whether a key is down. A letter is the same key in either case, since Shift
1630
+ * changes the code a terminal sends.
1631
+ *
1632
+ * @param key The key's `KEY_` kind.
1633
+ * @param code Tells a `KEY_CHAR`, `KEY_CONTROL` or `KEY_UNKNOWN` apart, as `TermEvent.code` does;
1634
+ * ignored for the other kinds.
1635
+ * @returns True from the key's press until its release, real or synthetic, or a loss of focus.
1447
1636
  */
1448
1637
  isDown(key: i32, code: i32): boolean {
1449
1638
  return this.find(termKeyId(key, code)) >= 0
1450
1639
  }
1451
1640
 
1452
1641
  /**
1453
- * Take `events`, which arrived at `nowMs`, and answer what the program
1454
- * should see: first the releases `tick` makes for the keys `events` does
1455
- * not mention (a held key's repeats can wait in a read longer than the
1456
- * gap), then each event, with
1457
- * Ctrl-C as `EVENT_QUIT` (its repeats and release dropped) and a press of a
1458
- * key already down marked `ACTION_REPEAT` while releases are synthetic.
1459
- * A real release of a key not down is dropped: it was let go already, made
1460
- * up from silence or by losing focus. Losing focus lets every key go, since
1461
- * its release goes elsewhere. A non-finite `nowMs` releases nothing; a key
1462
- * it presses keeps its last time, and a new one is timed from the watermark
1463
- * (0 without one).
1642
+ * Take the events of one read and answer what the program should see: first
1643
+ * the releases `tick` makes for the keys the events do not mention (a held
1644
+ * key's repeats can wait in a read longer than the gap), then each event,
1645
+ * with Ctrl-C as `EVENT_QUIT` (its repeats and release dropped) and a press
1646
+ * of a key already down marked `ACTION_REPEAT` while releases are synthetic.
1647
+ *
1648
+ * A real release of a key not down is dropped: it was let go already, made up
1649
+ * from silence or by losing focus. Losing focus lets every key go, since its
1650
+ * release goes elsewhere.
1651
+ *
1652
+ * @param events The events, as `KeyDecoder.feedEvents` or `pollEvents` gave them.
1653
+ * @param nowMs When they arrived. A non-finite time releases nothing; a key it presses keeps its
1654
+ * last time, and a new one is timed from the watermark (0 without one).
1655
+ * @returns The events to act on, in order.
1464
1656
  */
1465
1657
  apply(events: TermEvent[], nowMs: f64): TermEvent[] {
1466
1658
  const out: TermEvent[] = this.releaseDue(nowMs, events)
@@ -1485,10 +1677,11 @@ export class KeyState {
1485
1677
  }
1486
1678
 
1487
1679
  /**
1488
- * Everything the terminal generated before `sentAtMs`, on the caller's
1489
- * clock, has arrived: the answer to an in-order query sent then has come
1490
- * back. From now on silence is measured against the latest such time. A
1491
- * non-finite `sentAtMs` is ignored.
1680
+ * Note that everything the terminal generated before a time has arrived: the
1681
+ * answer to an in-order query sent then has come back. From now on silence is
1682
+ * measured against the latest such time.
1683
+ *
1684
+ * @param sentAtMs When the query was sent, on the caller's clock; a non-finite time is ignored.
1492
1685
  */
1493
1686
  delivered(sentAtMs: f64): void {
1494
1687
  if (!termFinite(sentAtMs)) {
@@ -1501,21 +1694,30 @@ export class KeyState {
1501
1694
  }
1502
1695
 
1503
1696
  /**
1504
- * How long before `nowMs` the latest answered query was sent, or -1 with no
1505
- * watermark: the caller clears `watermarked` once this passes its bound.
1697
+ * How long ago the latest answered query was sent; the caller clears
1698
+ * `watermarked` once this passes its bound.
1699
+ *
1700
+ * @param nowMs The time now, on the clock `delivered` was given.
1701
+ * @returns The milliseconds since that query was sent, or -1 with no watermark.
1506
1702
  */
1507
1703
  deliveredAgoMs(nowMs: f64): f64 {
1508
1704
  return this.watermarked ? nowMs - this.watermarkMs : toF64(-1)
1509
1705
  }
1510
1706
 
1511
- /** The releases due by `nowMs` while releases are synthetic: each key whose gap has passed, let go. */
1707
+ /**
1708
+ * The releases due while releases are synthetic: each key whose gap has
1709
+ * passed, let go.
1710
+ *
1711
+ * @param nowMs The time now, on the clock `apply` was given.
1712
+ * @returns The synthetic releases, one per key let go; none while releases are real.
1713
+ */
1512
1714
  tick(nowMs: f64): TermEvent[] {
1513
1715
  const none: TermEvent[] = []
1514
1716
  return this.releaseDue(nowMs, none)
1515
1717
  }
1516
1718
 
1517
1719
  /** The releases due by `nowMs`, as `tick` makes them, for the keys no key event in `batch` names. */
1518
- releaseDue(nowMs: f64, batch: TermEvent[]): TermEvent[] {
1720
+ private releaseDue(nowMs: f64, batch: TermEvent[]): TermEvent[] {
1519
1721
  const out: TermEvent[] = []
1520
1722
  if (!this.releasesAreSynthetic || !termFinite(nowMs)) {
1521
1723
  return out
@@ -1534,7 +1736,7 @@ export class KeyState {
1534
1736
  }
1535
1737
 
1536
1738
  /** Record key event `e` at `nowMs`; false for a release of a key not down, which `apply` drops. */
1537
- press(e: TermEvent, nowMs: f64): boolean {
1739
+ private press(e: TermEvent, nowMs: f64): boolean {
1538
1740
  const at: i32 = this.find(termKeyId(e.key, e.code))
1539
1741
  if (e.action === ACTION_RELEASE) {
1540
1742
  this.releasesAreSynthetic = false
@@ -1570,7 +1772,7 @@ export class KeyState {
1570
1772
  }
1571
1773
 
1572
1774
  /** Let key `at` go, appending its release to `out`. */
1573
- release(at: i32, out: TermEvent[]): void {
1775
+ private release(at: i32, out: TermEvent[]): void {
1574
1776
  const id: i32 = this.downKeys[at]
1575
1777
  const e: TermEvent = new TermEvent(EVENT_KEY)
1576
1778
  e.key = id >> 21
@@ -1582,7 +1784,7 @@ export class KeyState {
1582
1784
  }
1583
1785
 
1584
1786
  /** Where key `id` is in `downKeys`, or -1. */
1585
- find(id: i32): i32 {
1787
+ private find(id: i32): i32 {
1586
1788
  for (let k: i32 = 0; k < toI32(this.downKeys.length); k++) {
1587
1789
  if (this.downKeys[k] === id) {
1588
1790
  return k
@@ -1592,7 +1794,7 @@ export class KeyState {
1592
1794
  }
1593
1795
 
1594
1796
  /** Drop key `at`, keeping the others in order. */
1595
- remove(at: i32): void {
1797
+ private remove(at: i32): void {
1596
1798
  const last: i32 = toI32(this.downKeys.length) - 1
1597
1799
  for (let k: i32 = at; k < last; k++) {
1598
1800
  this.downKeys[k] = this.downKeys[k + 1]
@@ -1623,16 +1825,32 @@ export class KeyState {
1623
1825
  export class FixedStep {
1624
1826
  /** The length of one step, in milliseconds. */
1625
1827
  stepMs: f64
1626
- /** The time since the last step, in milliseconds, under one step after each `advance`. */
1828
+ /**
1829
+ * The time since the last step, in milliseconds, under one step after each `advance`.
1830
+ * @internal Used by `termIoDue` in term-io.ts.
1831
+ */
1627
1832
  carry: f64
1628
- /** The time the last `advance` was given. */
1833
+ /**
1834
+ * The time the last `advance` was given.
1835
+ * @internal Used by `termIoDue` in term-io.ts.
1836
+ */
1629
1837
  lastMs: f64
1630
1838
  /** The most steps one `advance` answers. */
1631
1839
  maxSteps: i32
1632
- /** Whether `advance` has been called, so `lastMs` is a time. */
1840
+ /**
1841
+ * Whether `advance` has been called, so `lastMs` is a time.
1842
+ * @internal Used by `termIoWait` in term-io.ts.
1843
+ */
1633
1844
  started: boolean
1634
1845
 
1635
- /** A clock of `stepsPerSecond` steps a second (1 at least), running at most `maxSteps` (1 at least) per `advance`. */
1846
+ /**
1847
+ * A clock for a simulation that steps at a fixed rate whatever the frame
1848
+ * rate.
1849
+ *
1850
+ * @param stepsPerSecond The rate; 1 at least.
1851
+ * @param maxSteps The most steps one `advance` answers, 1 at least; time past them is dropped, so
1852
+ * a stall does not have to be caught up.
1853
+ */
1636
1854
  constructor(stepsPerSecond: i32, maxSteps: i32) {
1637
1855
  const rate: i32 = stepsPerSecond < 1 ? 1 : stepsPerSecond
1638
1856
  this.stepMs = 1000.0 / toF64(rate)
@@ -1643,9 +1861,12 @@ export class FixedStep {
1643
1861
  }
1644
1862
 
1645
1863
  /**
1646
- * The steps to run for the time between the last call and `nowMs`. The first
1647
- * call only starts the clock and answers 0, as does a time earlier than the
1648
- * last one.
1864
+ * The steps to run for the time between the last call and now. The first call
1865
+ * only starts the clock and answers 0, as does a time earlier than the last
1866
+ * one.
1867
+ *
1868
+ * @param nowMs The time now, in milliseconds.
1869
+ * @returns How many steps to run, 0 to `maxSteps`.
1649
1870
  */
1650
1871
  advance(nowMs: f64): i32 {
1651
1872
  if (!this.started || !(nowMs >= this.lastMs)) {
@@ -1670,7 +1891,11 @@ export class FixedStep {
1670
1891
  return steps
1671
1892
  }
1672
1893
 
1673
- /** How far into the next step the clock is, from 0 up to but not including 1. */
1894
+ /**
1895
+ * How far into the next step the clock is, for drawing between two steps.
1896
+ *
1897
+ * @returns A fraction from 0 up to but not including 1.
1898
+ */
1674
1899
  alpha(): f64 {
1675
1900
  return this.carry / this.stepMs
1676
1901
  }