ccdeck 3.82.4 → 4.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (145) hide show
  1. package/LICENSING.md +42 -4
  2. package/README.md +183 -72
  3. package/THIRD_PARTY_NOTICES.md +243 -17
  4. package/bin/cli/tui.js +58 -4
  5. package/bin/deck.js +27 -1
  6. package/dist/web/assets/AccountBridge-fp-fsMx-.js +1 -0
  7. package/dist/web/assets/AccountSection-Cu-96Q00.js +1 -0
  8. package/dist/web/assets/BrowserWatchModal-CvUx07C6.js +1 -0
  9. package/dist/web/assets/FeedbackConversations-BgVk0nkA.js +1 -0
  10. package/dist/web/assets/IntegrationModal-CEj5oJg4.js +7 -0
  11. package/dist/web/assets/IntegrationSkeleton-DD7YiFgd.js +1 -0
  12. package/dist/web/assets/RadioPanel-BhnzWRCT.js +1 -0
  13. package/dist/web/assets/StageShell-DmJu3dU1.js +4 -0
  14. package/dist/web/assets/TrafficRadar-C8-ieIw5.js +8 -0
  15. package/dist/web/assets/UsageHistoryModal-BThv9Kja.js +1 -0
  16. package/dist/web/assets/addon-unicode-graphemes-DW3WJwyh.js +18 -0
  17. package/dist/web/assets/addon-web-links-BWjmmSgS.js +12 -0
  18. package/dist/web/assets/addon-webgl-CLK4R-_u.js +75 -0
  19. package/dist/web/assets/app-DtosBis5.css +1 -0
  20. package/dist/web/assets/app-ZMmmEHxF.js +361 -0
  21. package/dist/web/assets/{index-B-l-VxLm.js → index-zSxC5ueJ.js} +1 -1
  22. package/dist/web/assets/inspector-status-pane-BgQG0bQi.js +1 -0
  23. package/dist/web/assets/screens-DD0uCtat.js +12 -0
  24. package/dist/web/assets/screens-DUniTTbC.css +1 -0
  25. package/dist/web/assets/stage-board-J2rKA9Pg.js +2 -0
  26. package/dist/web/assets/use-account-DvFObI4M.js +1 -0
  27. package/dist/web/assets/xterm-4jYizCpg.js +27 -0
  28. package/dist/web/assets/xterm-sheet-6GBZ9nXN.css +32 -0
  29. package/dist/web/index.html +16 -6
  30. package/package.json +13 -1
  31. package/release-notes.json +69 -1
  32. package/src/server/account-client.mjs +196 -0
  33. package/src/server/account-connect.mjs +238 -0
  34. package/src/server/account-flag.mjs +106 -0
  35. package/src/server/account-projects.mjs +1 -1
  36. package/src/server/account-service.mjs +532 -0
  37. package/src/server/account-status.mjs +293 -0
  38. package/src/server/account-store.mjs +137 -0
  39. package/src/server/account-sync.mjs +473 -0
  40. package/src/server/account-token-store.mjs +216 -0
  41. package/src/server/account-ui-fields.mjs +126 -0
  42. package/src/server/account-watch.mjs +1 -1
  43. package/src/server/browser-watch-log.mjs +1 -1
  44. package/src/server/browser-watch-store.mjs +1 -1
  45. package/src/server/ccdeck-account-routes.mjs +252 -0
  46. package/src/server/ccusage-install.mjs +2 -2
  47. package/src/server/ccusage-registry.mjs +4 -4
  48. package/src/server/ccusage-session.mjs +11 -0
  49. package/src/server/claude-accounts.mjs +1 -1
  50. package/src/server/claude-recovery.mjs +9 -4
  51. package/src/server/claude-settings-catalog.mjs +6 -6
  52. package/src/server/claude-settings.mjs +1 -1
  53. package/src/server/codex-auto.mjs +1 -1
  54. package/src/server/codex-cost-usage.mjs +7 -2
  55. package/src/server/codex-live-checkpoint.mjs +24 -9
  56. package/src/server/codex-live-engine.mjs +14 -4
  57. package/src/server/codex-live-handoff.mjs +53 -9
  58. package/src/server/codex-names.mjs +49 -0
  59. package/src/server/codex-translate.mjs +1 -1
  60. package/src/server/codex-ultrafast.d.mts +3 -0
  61. package/src/server/codex-ultrafast.mjs +5 -0
  62. package/src/server/codex-watch.mjs +25 -0
  63. package/src/server/command-panel-engine.mjs +300 -0
  64. package/src/server/command-panel-platform.mjs +99 -0
  65. package/src/server/cswap-auto-choice.mjs +1 -1
  66. package/src/server/cswap-auto-loop.mjs +1 -1
  67. package/src/server/cswap-install.mjs +21 -40
  68. package/src/server/deck-home.mjs +6 -0
  69. package/src/server/deck-prefs.mjs +53 -1
  70. package/src/server/detach.mjs +1 -1
  71. package/src/server/engine-download.mjs +277 -0
  72. package/src/server/engine-install.mjs +378 -0
  73. package/src/server/engine-package.mjs +267 -0
  74. package/src/server/event-log.mjs +7 -1
  75. package/src/server/event-pipeline.mjs +29 -1
  76. package/src/server/ghostty-handoff.mjs +81 -0
  77. package/src/server/index.mjs +143 -5
  78. package/src/server/installer.mjs +4 -1
  79. package/src/server/integrations/jira-write-guard.mjs +14 -208
  80. package/src/server/integrations/store.mjs +37 -19
  81. package/src/server/integrations/write-guard.mjs +223 -0
  82. package/src/server/intel-cpu-temperature.mjs +26 -0
  83. package/src/server/lifecycle.mjs +69 -6
  84. package/src/server/log-replay.mjs +4 -0
  85. package/src/server/login-service.mjs +7 -1
  86. package/src/server/macmon.mjs +1 -1
  87. package/src/server/npm-latest.mjs +1 -1
  88. package/src/server/org-telemetry.mjs +1 -1
  89. package/src/server/output-watch.mjs +4 -1
  90. package/src/server/panel-provenance.mjs +390 -0
  91. package/src/server/pinned-build.mjs +14 -0
  92. package/src/server/prefs-refusal.mjs +8 -1
  93. package/src/server/prefs-routes.mjs +60 -11
  94. package/src/server/pro-engine-client.mjs +339 -0
  95. package/src/server/pro-engine-install.mjs +294 -0
  96. package/src/server/pro-engine-terminal.mjs +308 -0
  97. package/src/server/project-discovery.mjs +139 -0
  98. package/src/server/provider-session-name.mjs +58 -0
  99. package/src/server/quota-closes.mjs +1 -1
  100. package/src/server/quota-history.mjs +1 -1
  101. package/src/server/reports.mjs +86 -21
  102. package/src/server/request-gates.mjs +34 -5
  103. package/src/server/restart-note.mjs +1 -1
  104. package/src/server/seed-boundary.mjs +489 -0
  105. package/src/server/session-counts.mjs +244 -0
  106. package/src/server/session-deck-names.mjs +143 -0
  107. package/src/server/session-enrichment.mjs +43 -6
  108. package/src/server/session-name-route.mjs +73 -0
  109. package/src/server/session-summary.mjs +12 -1
  110. package/src/server/session-transcript-routes.mjs +167 -0
  111. package/src/server/swap-log.mjs +1 -1
  112. package/src/server/system-metrics.mjs +57 -0
  113. package/src/server/terminal-bridge.mjs +485 -0
  114. package/src/server/terminal-folder.mjs +61 -0
  115. package/src/server/terminal-js-routes.mjs +752 -0
  116. package/src/server/terminal-routes.mjs +553 -0
  117. package/src/server/terminal-sessions.mjs +759 -0
  118. package/src/server/terminal-socket.mjs +403 -0
  119. package/src/server/terminal-target.mjs +928 -0
  120. package/src/server/thermal-metrics.mjs +13 -9
  121. package/src/server/tmux-control.mjs +1319 -0
  122. package/src/server/tmux-download.mjs +220 -0
  123. package/src/server/tmux.mjs +542 -0
  124. package/src/server/token-redact.mjs +42 -3
  125. package/src/server/tool-audit-report.mjs +44 -6
  126. package/src/server/tool-audit-routes.mjs +12 -1
  127. package/src/server/tool-audit.mjs +42 -8
  128. package/src/server/traffic-radar-monitor.mjs +1 -1
  129. package/src/server/traffic-radar-permission.mjs +1 -1
  130. package/src/server/transcript-gate.mjs +1 -1
  131. package/src/server/transcript-scan.mjs +15 -7
  132. package/src/server/transcript-turns.mjs +154 -0
  133. package/src/server/tui-network.mjs +1 -0
  134. package/src/server/tui-view.mjs +28 -0
  135. package/src/server/utf8-carry.mjs +159 -0
  136. package/src/server/uv-bootstrap.mjs +2 -2
  137. package/src/server/windows-acl.mjs +158 -0
  138. package/src/server/ws-frames.mjs +690 -0
  139. package/dist/web/assets/BrowserWatchModal-CWVooKxD.js +0 -1
  140. package/dist/web/assets/IntegrationModal-DLX43RIK.js +0 -7
  141. package/dist/web/assets/RadioPanel-Cj6PjrvQ.js +0 -1
  142. package/dist/web/assets/TrafficRadar-Bq1YcuHB.js +0 -8
  143. package/dist/web/assets/UsageHistoryModal-CMSCca5_.js +0 -1
  144. package/dist/web/assets/app-BHxsMz3-.js +0 -358
  145. package/dist/web/assets/app-CbXmr9rC.css +0 -1
@@ -0,0 +1,489 @@
1
+ // What tmux's parser holds at a seed's boundary, kept from the pane's own
2
+ // output so the seed can hand it on (tmux-control.mjs, "The seed boundary").
3
+ //
4
+ // WHY. A seed is tmux's SCREEN — capture-pane's cells and the modes a format
5
+ // reports — not tmux's PARSER. A capture lands between two of tmux's reads of
6
+ // the pane, and those can split one write: a macOS pty hands tmux a large
7
+ // frame in pieces of about a kilobyte. The parser then holds what the cut left
8
+ // it in: the pen the next cells are drawn with, the start of an escape
9
+ // sequence, the first bytes of a character. No format reports any of them,
10
+ // and the seed resets the emulator, so the output after the boundary used to
11
+ // land in a parser at rest with the default pen: the rest of a row came out
12
+ // uncoloured, the rest of a sequence printed as text ("6mCUT"), a character
13
+ // lost its first bytes. A seed that would need another one after it to be
14
+ // right is a flicker; this makes the one seed exact.
15
+ //
16
+ // WHAT. Every byte of the pane's output the client is sent goes through
17
+ // feed() — streamed or not: output that arrives before a seed's capture is
18
+ // dropped from the stream, not from here — and is read the way the emulator
19
+ // the panel runs reads it (xterm.js 6: its UTF-8 decoding, then its VT500
20
+ // parser's states). tail() is what the client writes right after the seed's
21
+ // own bytes:
22
+ //
23
+ // the pen the SGR sequences since the pen was last reset, less each one
24
+ // whose every attribute a later one sets again — an SGR
25
+ // parameter sets an attribute whole — so that replaying them
26
+ // ends in the same pen, in a few hundred bytes at most. A DECSC
27
+ // (ESC 7, CSI s, ?1048h, ?1049h) keeps a copy for the screen it
28
+ // is on, and a DECRC (ESC 8, CSI u, ?1048l, ?1049l) puts it back.
29
+ // the rest the sequence the output stopped inside, from its ESC (or C1
30
+ // introducer), less the C0 controls in it that took effect
31
+ // already; then the first bytes of a character the output
32
+ // stopped inside. The bytes that finish them come next in the
33
+ // stream, as they come to tmux.
34
+ //
35
+ // WHAT IT PRESUMES. At attach: a parser at rest, the default pen and the
36
+ // normal screen — what a pane set before the client attached is not in its
37
+ // stream, and the seed always presumed as much. After a %pause (lose()), tmux
38
+ // has skipped bytes: the pen is presumed default again, and no unfinished
39
+ // sequence is handed on until an ESC, CAN, SUB or C1 control says where the
40
+ // parser is. The client's quiet reseed covers that window, and presume() is
41
+ // that reseed taking a quiet pane as at rest.
42
+ //
43
+ // WHAT IT DOES NOT HAND ON: the character sets (G0-G3, SO/SI); an OSC 8
44
+ // hyperlink; what a DECSC saved before the seed, the cursor and its pen (no
45
+ // format reports the cursor, and the seed's reset clears the emulator's, so a
46
+ // DECRC after the seed restores what the seed left); a wrap pending at the
47
+ // right margin; a sequence longer than PENDING_MAX (a large OSC 52 or sixel
48
+ // image); the underline colour and style across a DECRC (xterm.js keeps the
49
+ // current ones there, this the saved ones); and a combining mark that is the
50
+ // first thing after the boundary: xterm.js joins a mark only to a character
51
+ // printed in the same run of text, and the seed's own bytes end that run.
52
+ //
53
+ // Pure: no I/O, no timers.
54
+ import { unfinishedTail } from "./utf8-carry.mjs";
55
+
56
+ /** The most bytes of an unfinished sequence handed on; past it, none are. */
57
+ export const PENDING_MAX = 16 * 1024;
58
+
59
+ /** The most bytes of SGR sequences a pen is kept in, once compacted; past it
60
+ * the pen is presumed default. */
61
+ export const PEN_MAX = 4 * 1024;
62
+
63
+ // The pen is compacted once its sequences pass this many bytes.
64
+ const PEN_COMPACT = 256;
65
+
66
+ // How many SGR parameter strings a reader remembers what they set for.
67
+ const KNOWN_MAX = 256;
68
+
69
+ // xterm.js keeps 32 parameters of a sequence and drops the rest; an SGR with
70
+ // more is replayed as it came, but never read for what it sets.
71
+ const MAX_PARAMS = 32;
72
+
73
+ // The VT500 parser's states, as xterm.js has them.
74
+ const GROUND = 0;
75
+ const ESCAPE = 1;
76
+ const ESC_INTER = 2;
77
+ const CSI_ENTRY = 3;
78
+ const CSI_PARAM = 4;
79
+ const CSI_INTER = 5;
80
+ const CSI_IGNORE = 6;
81
+ const SOS_PM_APC = 7;
82
+ const OSC = 8;
83
+ const DCS_ENTRY = 9;
84
+ const DCS_PARAM = 10;
85
+ const DCS_INTER = 11;
86
+ const DCS_IGNORE = 12;
87
+ const DCS_PASS = 13;
88
+
89
+ // The attributes an SGR parameter sets, each one whole.
90
+ const BOLD = 1 << 0;
91
+ const DIM = 1 << 1;
92
+ const ITALIC = 1 << 2;
93
+ const UNDERLINE = 1 << 3; // the flag and the style (4, 4:n, 21, 24)
94
+ const BLINK = 1 << 4;
95
+ const INVERSE = 1 << 5;
96
+ const INVISIBLE = 1 << 6;
97
+ const STRIKE = 1 << 7;
98
+ const OVERLINE = 1 << 8;
99
+ const FG = 1 << 9;
100
+ const BG = 1 << 10;
101
+ const UL_COLOUR = 1 << 11;
102
+ const ALL = (1 << 12) - 1;
103
+
104
+ const SIMPLE = new Map([
105
+ [1, BOLD], [2, DIM], [3, ITALIC], [4, UNDERLINE], [5, BLINK], [7, INVERSE], [8, INVISIBLE], [9, STRIKE],
106
+ [21, UNDERLINE], [22, BOLD | DIM], [23, ITALIC], [24, UNDERLINE], [25, BLINK], [27, INVERSE], [28, INVISIBLE],
107
+ [29, STRIKE], [39, FG], [49, BG], [53, OVERLINE], [55, OVERLINE], [59, UL_COLOUR],
108
+ ]);
109
+
110
+ /** C0 controls xterm.js executes where they stand, inside a sequence too. */
111
+ const executable = (c) => c <= 0x17 || c === 0x19 || (c >= 0x1c && c <= 0x1f);
112
+
113
+ /** An SGR's parameter string as xterm.js reads it: numbers, each with the
114
+ * colon sub-parameters after it, an empty one as 0 (or -1 as a sub). */
115
+ function sgrParams(text) {
116
+ return text.split(";").map((part) => {
117
+ const [main, ...subs] = part.split(":");
118
+ return { v: Number(main || 0), subs: subs.length ? subs.map(s => (s === "" ? -1 : Number(s))) : null };
119
+ });
120
+ }
121
+
122
+ /** How many parameters after `pos` a 38, 48 or 58 at `pos` takes, and the
123
+ * colour mode it names, read as xterm.js reads it (InputHandler's
124
+ * _extractColor). Only modes 2 and 5 change the colour. */
125
+ function colourAdvance(ps, pos) {
126
+ const accu = [0, 0, -1, 0, 0, 0];
127
+ let cSpace = 0;
128
+ let advance = 0;
129
+ do {
130
+ accu[advance + cSpace] = ps[pos + advance].v;
131
+ const subs = ps[pos + advance].subs;
132
+ if (subs) {
133
+ let i = 0;
134
+ do {
135
+ if (accu[1] === 5) cSpace = 1;
136
+ accu[advance + i + 1 + cSpace] = subs[i];
137
+ } while (++i < subs.length && i + advance + 1 + cSpace < accu.length);
138
+ break;
139
+ }
140
+ if ((accu[1] === 5 && advance + cSpace >= 2) || (accu[1] === 2 && advance + cSpace >= 5)) break;
141
+ if (accu[1]) cSpace = 1;
142
+ } while (++advance + pos < ps.length && advance + cSpace < accu.length);
143
+ return { advance, mode: accu[1] };
144
+ }
145
+
146
+ /** The attributes an SGR's parameters set, or -1 when one of them is not
147
+ * understood here (such a sequence is kept, and never counted as setting
148
+ * anything again). */
149
+ export function sgrSets(text) {
150
+ const ps = sgrParams(text);
151
+ if (ps.length > MAX_PARAMS) return -1;
152
+ let sets = 0;
153
+ for (let i = 0; i < ps.length; i++) {
154
+ const p = ps[i].v;
155
+ if (p === 0) sets |= ALL;
156
+ else if ((p >= 30 && p <= 37) || (p >= 90 && p <= 97)) sets |= FG;
157
+ else if ((p >= 40 && p <= 47) || (p >= 100 && p <= 107)) sets |= BG;
158
+ else if (p === 38 || p === 48 || p === 58) {
159
+ const { advance, mode } = colourAdvance(ps, i);
160
+ if (mode === 2 || mode === 5) sets |= p === 38 ? FG : p === 48 ? BG : UL_COLOUR;
161
+ i += advance;
162
+ } else if (SIMPLE.has(p)) sets |= SIMPLE.get(p);
163
+ else return -1;
164
+ }
165
+ return sets;
166
+ }
167
+
168
+ /**
169
+ * A new reader of one pane's output: feed(bytes) for every %output of the
170
+ * pane, in order; tail() for what a seed taken now hands on; lose() at a
171
+ * %pause; presume() when the client takes a quiet pane as at rest.
172
+ */
173
+ export function seedBoundary() {
174
+ let state = GROUND;
175
+ let placed = true; // whether `state` is where the emulator's parser is
176
+ const pending = []; // the unfinished sequence's bytes, its introducer first
177
+ let overflow = false; // the unfinished sequence passed PENDING_MAX
178
+ const u8 = [0, 0, 0, 0]; // the bytes of a character begun and not finished
179
+ let u8len = 0;
180
+ let need = 0; // how many more that character takes
181
+ let pen = []; // { seq, sets } in order
182
+ let penBytes = 0;
183
+ let screen = "normal";
184
+ let saved = { normal: null, alt: null };
185
+ const known = new Map(); // an SGR's parameters -> what they set
186
+
187
+ const toGround = () => {
188
+ state = GROUND;
189
+ pending.length = 0;
190
+ overflow = false;
191
+ };
192
+ /** Keep the character just read: `c` itself, or the bytes of a character
193
+ * decoded from several. */
194
+ const keep = (c, multi) => {
195
+ if (overflow) return;
196
+ if (pending.length + (multi ? u8len : 1) > PENDING_MAX) {
197
+ overflow = true;
198
+ pending.length = 0;
199
+ return;
200
+ }
201
+ if (multi) for (let k = 0; k < u8len; k++) pending.push(u8[k]);
202
+ else pending.push(c);
203
+ };
204
+ const begin = (next, c, multi) => {
205
+ toGround();
206
+ state = next;
207
+ keep(c, multi);
208
+ };
209
+
210
+ const setPen = (entries) => {
211
+ pen = entries;
212
+ penBytes = entries.reduce((n, e) => n + e.seq.length, 0);
213
+ };
214
+ const compact = () => {
215
+ let covered = 0;
216
+ const kept = [];
217
+ for (let k = pen.length - 1; k >= 0; k--) {
218
+ const e = pen[k];
219
+ if (e.sets >= 0 && (e.sets & ~covered) === 0) continue;
220
+ kept.push(e);
221
+ if (e.sets >= 0) covered |= e.sets;
222
+ }
223
+ setPen(kept.reverse());
224
+ if (penBytes > PEN_MAX) setPen([]);
225
+ };
226
+ const sgr = (text) => {
227
+ // A reset and nothing else leaves the pen the seed leaves.
228
+ if (/^[0;]*$/.test(text)) return setPen([]);
229
+ // A program sets the same few colours over and over.
230
+ let sets = known.get(text);
231
+ if (sets === undefined) {
232
+ if (known.size >= KNOWN_MAX) known.clear();
233
+ sets = sgrSets(text);
234
+ known.set(text, sets);
235
+ }
236
+ const seq = `\x1b[${text}m`;
237
+ if (sets === ALL) setPen([]);
238
+ pen.push({ seq, sets });
239
+ penBytes += seq.length;
240
+ if (penBytes > PEN_COMPACT) compact();
241
+ };
242
+ const save = () => { saved[screen] = pen.slice(); };
243
+ const restore = () => { setPen(saved[screen] ? saved[screen].slice() : []); };
244
+ const reset = () => {
245
+ setPen([]);
246
+ screen = "normal";
247
+ saved = { normal: null, alt: null };
248
+ };
249
+
250
+ // A sequence is read whole at its final byte, from `pending`: its
251
+ // introducer (ESC and one byte, or a two-byte C1), what came between, and
252
+ // the final. The C0 controls in it are not kept.
253
+ const escDispatch = (final) => {
254
+ if (pending.length !== 2) return; // intermediates, or past PENDING_MAX
255
+ if (final === 0x37) save(); // ESC 7, DECSC
256
+ else if (final === 0x38) restore(); // ESC 8, DECRC
257
+ else if (final === 0x63) reset(); // ESC c, RIS
258
+ };
259
+ /** Only these CSI finals touch the pen or the screen it is saved for. */
260
+ const touches = (final) => final === 0x6d || final === 0x73 || final === 0x75 || final === 0x70 || final === 0x68 || final === 0x6c;
261
+ /** A CSI from what lies between its introducer and its final, read as the
262
+ * parser's states read it: a prefix (< = > ?), parameters (0-9 : ;),
263
+ * intermediates (space to /), in that order, or the sequence is ignored. */
264
+ const csi = (body, final) => {
265
+ let prefix = "";
266
+ let at = 0;
267
+ const p0 = body.charCodeAt(0);
268
+ if (p0 >= 0x3c && p0 <= 0x3f) { prefix = body[0]; at = 1; }
269
+ let end = at;
270
+ while (end < body.length && body.charCodeAt(end) <= 0x3b && body.charCodeAt(end) >= 0x30) end++;
271
+ let last = end;
272
+ while (last < body.length && body.charCodeAt(last) <= 0x2f && body.charCodeAt(last) >= 0x20) last++;
273
+ if (last !== body.length) return;
274
+ const params = body.slice(at, end);
275
+ const inter = body.slice(end);
276
+ if (prefix === "" && inter === "") {
277
+ if (final === 0x6d) sgr(params);
278
+ else if (final === 0x73) save(); // CSI s
279
+ else if (final === 0x75) restore(); // CSI u
280
+ } else if (prefix === "" && inter === "!" && final === 0x70) { // CSI ! p, DECSTR
281
+ setPen([]);
282
+ saved[screen] = [];
283
+ } else if (prefix === "?" && inter === "" && (final === 0x68 || final === 0x6c)) {
284
+ const on = final === 0x68;
285
+ for (const { v } of sgrParams(params)) {
286
+ if (v === 1048) on ? save() : restore();
287
+ else if (v === 1049) {
288
+ if (on) { save(); screen = "alt"; } else { screen = "normal"; restore(); }
289
+ } else if (v === 47 || v === 1047) screen = on ? "alt" : "normal";
290
+ }
291
+ }
292
+ };
293
+ const csiDispatch = (final) => {
294
+ if (overflow || !touches(final)) return;
295
+ csi(String.fromCharCode.apply(null, pending.slice(2, -1)), final);
296
+ };
297
+
298
+ /** One character `c` through the parser's states; `multi` when its bytes
299
+ * are the character just decoded (u8), not the byte `c` itself. */
300
+ const step = (c, multi) => {
301
+ if (!placed) {
302
+ // After skipped bytes, only these say where the parser is.
303
+ if (c === 0x1b || c === 0x18 || c === 0x1a || (c >= 0x80 && c <= 0x9f)) placed = true;
304
+ else return;
305
+ }
306
+ // From any state.
307
+ if (c === 0x1b) return begin(ESCAPE, c, multi);
308
+ if (c === 0x9b) return begin(CSI_ENTRY, c, multi);
309
+ if (c === 0x9d) return begin(OSC, c, multi);
310
+ if (c === 0x90) return begin(DCS_ENTRY, c, multi);
311
+ if (c === 0x98 || c === 0x9e || c === 0x9f) return begin(SOS_PM_APC, c, multi);
312
+ if (c === 0x18 || c === 0x1a || (c >= 0x80 && c <= 0x9f)) return toGround();
313
+ const wide = c >= 0xa0;
314
+ switch (state) {
315
+ case GROUND:
316
+ return;
317
+ case ESCAPE:
318
+ if (executable(c) || c === 0x7f) return;
319
+ if (wide) return toGround();
320
+ keep(c, multi);
321
+ if (c <= 0x2f) { state = ESC_INTER; return; }
322
+ if (c === 0x5b) { state = CSI_ENTRY; return; }
323
+ if (c === 0x5d) { state = OSC; return; }
324
+ if (c === 0x50) { state = DCS_ENTRY; return; }
325
+ if (c === 0x58 || c === 0x5e || c === 0x5f) { state = SOS_PM_APC; return; }
326
+ escDispatch(c);
327
+ return toGround();
328
+ case ESC_INTER:
329
+ if (executable(c) || c === 0x7f) return;
330
+ if (wide) return toGround();
331
+ keep(c, multi);
332
+ if (c <= 0x2f) return;
333
+ escDispatch(c);
334
+ return toGround();
335
+ case CSI_ENTRY:
336
+ case CSI_PARAM:
337
+ case CSI_INTER:
338
+ if (executable(c) || c === 0x7f) return;
339
+ if (wide) return toGround();
340
+ keep(c, multi);
341
+ if (c >= 0x40) { csiDispatch(c); return toGround(); }
342
+ if (c <= 0x2f) { state = CSI_INTER; return; }
343
+ if (state === CSI_INTER) { state = CSI_IGNORE; return; }
344
+ if (c <= 0x3b) { state = CSI_PARAM; return; }
345
+ // A prefix (< = > ?) opens a sequence; anywhere else it spoils it.
346
+ state = state === CSI_ENTRY ? CSI_PARAM : CSI_IGNORE;
347
+ return;
348
+ case CSI_IGNORE:
349
+ if (executable(c)) return;
350
+ if (c >= 0x40 && c <= 0x7e) return toGround();
351
+ return keep(c, multi);
352
+ case SOS_PM_APC:
353
+ if (wide) return toGround();
354
+ return keep(c, multi);
355
+ case OSC:
356
+ if (c === 0x07) return toGround();
357
+ if (executable(c)) return;
358
+ return keep(c, multi);
359
+ case DCS_ENTRY:
360
+ case DCS_PARAM:
361
+ case DCS_INTER:
362
+ if (executable(c) || c === 0x7f) return;
363
+ if (wide) return toGround();
364
+ keep(c, multi);
365
+ if (c >= 0x40) { state = DCS_PASS; return; }
366
+ if (c <= 0x2f) { state = DCS_INTER; return; }
367
+ if (state === DCS_INTER) { state = DCS_IGNORE; return; }
368
+ state = c <= 0x3b || state === DCS_ENTRY ? DCS_PARAM : DCS_IGNORE;
369
+ return;
370
+ case DCS_IGNORE:
371
+ case DCS_PASS:
372
+ return keep(c, multi);
373
+ default:
374
+ }
375
+ };
376
+
377
+ // UTF-8 as xterm.js decodes it: a character broken off by a byte that
378
+ // cannot continue it is dropped and that byte read afresh; a byte no
379
+ // character begins with, and a character that decodes to an overlong form,
380
+ // a surrogate, past U+10FFFF or to U+FEFF (a byte order mark), reaches no
381
+ // parser state at all.
382
+ const decode = () => {
383
+ const n = u8len;
384
+ let cp = u8[0] & (n === 2 ? 0x1f : n === 3 ? 0x0f : 0x07);
385
+ for (let i = 1; i < n; i++) cp = (cp << 6) | (u8[i] & 0x3f);
386
+ const min = n === 2 ? 0x80 : n === 3 ? 0x800 : 0x10000;
387
+ if (cp < min || (cp >= 0xd800 && cp <= 0xdfff) || cp > 0x10ffff || cp === 0xfeff) return -1;
388
+ return cp;
389
+ };
390
+ const byte = (b) => {
391
+ if (need) {
392
+ if ((b & 0xc0) === 0x80) {
393
+ u8[u8len++] = b;
394
+ if (--need === 0) {
395
+ const cp = decode();
396
+ if (cp >= 0) step(cp, true);
397
+ u8len = 0;
398
+ }
399
+ return;
400
+ }
401
+ u8len = 0;
402
+ need = 0;
403
+ }
404
+ if (b < 0x80) {
405
+ // Text at rest changes nothing kept here.
406
+ if (state === GROUND && placed && b !== 0x1b && b !== 0x18 && b !== 0x1a) return;
407
+ step(b, false);
408
+ return;
409
+ }
410
+ const len = b >= 0xc2 && b <= 0xdf ? 2 : b >= 0xe0 && b <= 0xef ? 3 : b >= 0xf0 && b <= 0xf4 ? 4 : 0;
411
+ if (!len) return;
412
+ u8[0] = b;
413
+ u8len = 1;
414
+ need = len - 1;
415
+ };
416
+
417
+ return {
418
+ /** Read the next bytes the pane wrote (a Buffer). */
419
+ feed(bytes) {
420
+ const n = bytes.length;
421
+ // At rest, only an ESC, or a C1 control (U+0080-U+009F, whose UTF-8
422
+ // begins with C2), can leave the ground: CAN and SUB keep it there, and
423
+ // text changes nothing. So a run without either is skipped whole, save
424
+ // its last three bytes: whether it ends inside a character depends on
425
+ // those alone, UTF-8 finding its footing again within three. The
426
+ // positions of the next of each are kept, so no byte is searched twice.
427
+ let esc = -2;
428
+ let c1 = -2;
429
+ for (let i = 0; i < n; i++) {
430
+ if (need === 0 && state === GROUND && placed) {
431
+ if (esc !== -1 && esc < i) esc = bytes.indexOf(0x1b, i);
432
+ if (c1 !== -1 && c1 < i) c1 = bytes.indexOf(0xc2, i);
433
+ const j = esc === -1 ? (c1 === -1 ? n : c1) : (c1 === -1 ? esc : Math.min(esc, c1));
434
+ if (j === n) {
435
+ for (let k = Math.max(i, n - 3); k < n; k++) byte(bytes[k]);
436
+ return;
437
+ }
438
+ i = j;
439
+ // Most CSIs come whole in one write, with nothing in them the
440
+ // states would act on before the final: such a one is read here at
441
+ // once, and only a cut or odd one goes through the states.
442
+ if (bytes[i] === 0x1b && bytes[i + 1] === 0x5b) {
443
+ let k = i + 2;
444
+ while (k < n && bytes[k] <= 0x3f && bytes[k] >= 0x20) k++;
445
+ if (k < n && bytes[k] >= 0x40 && bytes[k] <= 0x7e) {
446
+ if (touches(bytes[k])) csi(bytes.toString("latin1", i + 2, k), bytes[k]);
447
+ i = k;
448
+ continue;
449
+ }
450
+ }
451
+ }
452
+ byte(bytes[i]);
453
+ }
454
+ },
455
+
456
+ /** What a seed taken now writes after its own bytes, so that an emulator
457
+ * it reset reads what follows as tmux's parser does. */
458
+ tail() {
459
+ const parts = pen.map(e => Buffer.from(e.seq, "latin1"));
460
+ if (placed && !overflow) {
461
+ if (pending.length) parts.push(Buffer.from(pending));
462
+ if (need) {
463
+ const begun = Buffer.from(u8.slice(0, u8len));
464
+ if (unfinishedTail(begun) === u8len) parts.push(begun);
465
+ }
466
+ }
467
+ return Buffer.concat(parts);
468
+ },
469
+
470
+ /** tmux skipped bytes (a %pause): presume the pen default, and hand on no
471
+ * unfinished sequence until the stream says where the parser is. */
472
+ lose() {
473
+ placed = false;
474
+ toGround();
475
+ u8len = 0;
476
+ need = 0;
477
+ setPen([]);
478
+ saved = { normal: null, alt: null };
479
+ },
480
+
481
+ /** The pane has been quiet: take its parser as at rest. */
482
+ presume() {
483
+ placed = true;
484
+ toGround();
485
+ u8len = 0;
486
+ need = 0;
487
+ },
488
+ };
489
+ }