pi-sakura-cyberdeck 1.5.2 → 1.5.4

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 (34) hide show
  1. package/CHANGELOG.md +124 -0
  2. package/README.md +36 -18
  3. package/README.zh-CN.md +7 -5
  4. package/extensions/changelog/index.ts +186 -42
  5. package/extensions/claude-shimmer/index.ts +336 -140
  6. package/extensions/header/index.ts +84 -41
  7. package/extensions/matrix/index.ts +221 -69
  8. package/extensions/shared/color.ts +90 -36
  9. package/extensions/shared/config-file.ts +324 -0
  10. package/extensions/shared/tui-context.ts +78 -0
  11. package/extensions/zentui/config.ts +87 -128
  12. package/extensions/zentui/extension-status.ts +158 -19
  13. package/extensions/zentui/footer-format.ts +279 -85
  14. package/extensions/zentui/footer-segments.ts +630 -0
  15. package/extensions/zentui/footer.ts +668 -525
  16. package/extensions/zentui/format.ts +43 -19
  17. package/extensions/zentui/git.ts +362 -167
  18. package/extensions/zentui/gradient.ts +100 -50
  19. package/extensions/zentui/index.ts +172 -59
  20. package/extensions/zentui/live-context.ts +2 -1
  21. package/extensions/zentui/package-version.ts +102 -14
  22. package/extensions/zentui/project-refresh.ts +42 -20
  23. package/extensions/zentui/project-state.ts +2 -2
  24. package/extensions/zentui/runtime.ts +140 -51
  25. package/extensions/zentui/selector-border.ts +96 -16
  26. package/extensions/zentui/session-lifecycle.ts +3 -1
  27. package/extensions/zentui/settings-command.ts +404 -246
  28. package/extensions/zentui/shell-call.ts +140 -26
  29. package/extensions/zentui/style.ts +109 -40
  30. package/extensions/zentui/thinking-trail.ts +234 -9
  31. package/extensions/zentui/tool-execution.ts +144 -64
  32. package/extensions/zentui/ui.ts +698 -145
  33. package/extensions/zentui/user-message.ts +24 -12
  34. package/package.json +2 -1
@@ -33,6 +33,7 @@ const NON_CONTENT_VARS = new Set(["sep", "separator"]);
33
33
 
34
34
  const PARSE_CACHE_LIMIT = 8;
35
35
  const parseCache = new Map<string, FormatToken[]>();
36
+ const formatVariablesCache = new WeakMap<readonly FormatToken[], ReadonlySet<string>>();
36
37
 
37
38
  /**
38
39
  * Tokenize a format string into text/var/fill/group tokens. Results are cached
@@ -45,8 +46,12 @@ const parseCache = new Map<string, FormatToken[]>();
45
46
  export function parseFooterFormat(format: string): FormatToken[] {
46
47
  if (!format) return [];
47
48
  const cached = parseCache.get(format);
48
- if (cached) return cached;
49
- const tokens = parseTokenSlice(format, 0, true).tokens;
49
+ if (cached) {
50
+ parseCache.delete(format);
51
+ parseCache.set(format, cached);
52
+ return cached;
53
+ }
54
+ const tokens = parseTokensIterative(format);
50
55
  if (parseCache.size >= PARSE_CACHE_LIMIT) {
51
56
  const oldest = parseCache.keys().next().value;
52
57
  if (oldest !== undefined) parseCache.delete(oldest);
@@ -55,6 +60,26 @@ export function parseFooterFormat(format: string): FormatToken[] {
55
60
  return tokens;
56
61
  }
57
62
 
63
+ /** Collect every variable name referenced in a parsed token tree (iterative, WeakMap-cached). */
64
+ export function collectFormatVariables(tokens: readonly FormatToken[]): ReadonlySet<string> {
65
+ const cached = formatVariablesCache.get(tokens);
66
+ if (cached) return cached;
67
+ const vars = new Set<string>();
68
+ const pending: Array<readonly FormatToken[]> = [tokens];
69
+ while (pending.length > 0) {
70
+ const slice = pending.pop()!;
71
+ for (const token of slice) {
72
+ if (token.kind === "var") {
73
+ vars.add(token.name);
74
+ } else if (token.kind === "group") {
75
+ pending.push(token.tokens);
76
+ }
77
+ }
78
+ }
79
+ formatVariablesCache.set(tokens, vars);
80
+ return vars;
81
+ }
82
+
58
83
  function makeGroup(tokens: FormatToken[]): GroupToken {
59
84
  let hasContent = false;
60
85
  let sawSeparator = false;
@@ -81,19 +106,16 @@ function makeGroup(tokens: FormatToken[]): GroupToken {
81
106
  };
82
107
  }
83
108
 
84
- function parseTokenSlice(
85
- format: string,
86
- start: number,
87
- topLevel = false,
88
- ): { tokens: FormatToken[]; nextIndex: number } {
89
- const tokens: FormatToken[] = [];
109
+ function parseTokensIterative(format: string): FormatToken[] {
110
+ const root: FormatToken[] = [];
111
+ const stack: FormatToken[][] = [root];
90
112
  const end = format.length;
91
- let index = start;
92
- let textStart = start;
113
+ let index = 0;
114
+ let textStart = 0;
93
115
 
94
116
  const flushText = (until: number) => {
95
117
  if (until > textStart) {
96
- tokens.push({ kind: "text", value: format.slice(textStart, until) });
118
+ stack[stack.length - 1]!.push({ kind: "text", value: format.slice(textStart, until) });
97
119
  }
98
120
  };
99
121
 
@@ -102,9 +124,8 @@ function parseTokenSlice(
102
124
 
103
125
  if (ch === "(") {
104
126
  flushText(index);
105
- const nested = parseTokenSlice(format, index + 1, false);
106
- tokens.push(makeGroup(nested.tokens));
107
- index = nested.nextIndex;
127
+ stack.push([]);
128
+ index += 1;
108
129
  textStart = index;
109
130
  continue;
110
131
  }
@@ -112,12 +133,16 @@ function parseTokenSlice(
112
133
  if (ch === ")") {
113
134
  // Nested groups close on `)`. Unmatched top-level `)` is literal text so
114
135
  // trailing tokens like `$cwd) $tokens` are not discarded.
115
- if (topLevel) {
136
+ if (stack.length === 1) {
116
137
  index += 1;
117
138
  continue;
118
139
  }
119
140
  flushText(index);
120
- return { tokens, nextIndex: index + 1 };
141
+ const groupTokens = stack.pop()!;
142
+ stack[stack.length - 1]!.push(makeGroup(groupTokens));
143
+ index += 1;
144
+ textStart = index;
145
+ continue;
121
146
  }
122
147
 
123
148
  if (ch === "$") {
@@ -126,7 +151,9 @@ function parseTokenSlice(
126
151
  if (match) {
127
152
  flushText(index);
128
153
  const name = match[1] ?? match[2] ?? "";
129
- tokens.push(name === "fill" ? { kind: "fill" } : { kind: "var", name });
154
+ stack[stack.length - 1]!.push(
155
+ name === "fill" ? { kind: "fill" } : { kind: "var", name },
156
+ );
130
157
  index += match[0].length;
131
158
  textStart = index;
132
159
  continue;
@@ -137,7 +164,11 @@ function parseTokenSlice(
137
164
  }
138
165
 
139
166
  flushText(end);
140
- return { tokens, nextIndex: end };
167
+ while (stack.length > 1) {
168
+ const groupTokens = stack.pop()!;
169
+ stack[stack.length - 1]!.push(makeGroup(groupTokens));
170
+ }
171
+ return root;
141
172
  }
142
173
 
143
174
  /**
@@ -157,7 +188,7 @@ function parseTokenSlice(
157
188
  * rendered at most once per call.
158
189
  */
159
190
  export function renderFormatSplit(
160
- tokens: FormatToken[],
191
+ tokens: readonly FormatToken[],
161
192
  renderVariable: (name: string) => string,
162
193
  ): { left: string; middle: string; right: string } {
163
194
  const fills: number[] = [];
@@ -166,7 +197,7 @@ export function renderFormatSplit(
166
197
  }
167
198
  const [first, second] = fills;
168
199
  const slice = (start: number, end: number) =>
169
- renderTokens(tokens, start, end, renderVariable).text;
200
+ renderTokensIterative(tokens, start, end, renderVariable).text;
170
201
  if (first === undefined) return { left: slice(0, tokens.length), middle: "", right: "" };
171
202
  if (second === undefined) {
172
203
  return { left: slice(0, first), middle: "", right: slice(first + 1, tokens.length) };
@@ -178,88 +209,236 @@ export function renderFormatSplit(
178
209
  };
179
210
  }
180
211
 
212
+ type RenderFrame = {
213
+ tokens: readonly FormatToken[];
214
+ index: number;
215
+ end: number;
216
+ group: GroupToken | undefined;
217
+ text: string;
218
+ hasVisibleContent: boolean;
219
+ };
220
+
181
221
  /**
182
- * Single pass: returns the rendered text plus whether any content variable or
183
- * nested group in the range produced content (drives group visibility).
222
+ * Iterative single-pass renderer: handles arbitrarily nested groups without
223
+ * call-stack recursion while preserving exact conditional visibility rules.
184
224
  */
185
- function renderTokens(
225
+ function renderTokensIterative(
186
226
  tokens: readonly FormatToken[],
187
227
  start: number,
188
228
  end: number,
189
229
  renderVariable: (name: string) => string,
190
230
  ): { text: string; hasVisibleContent: boolean } {
191
- let text = "";
192
- let hasVisibleContent = false;
193
- for (let i = start; i < end; i++) {
194
- const token = tokens[i];
231
+ const stack: RenderFrame[] = [
232
+ { tokens, index: start, end, group: undefined, text: "", hasVisibleContent: false },
233
+ ];
234
+
235
+ while (stack.length > 0) {
236
+ const frame = stack[stack.length - 1]!;
237
+ if (frame.index >= frame.end) {
238
+ stack.pop();
239
+ if (stack.length === 0) {
240
+ return { text: frame.text, hasVisibleContent: frame.hasVisibleContent };
241
+ }
242
+ const parent = stack[stack.length - 1]!;
243
+ const group = frame.group!;
244
+ // A group is dropped iff it has content vars/groups and none rendered.
245
+ // Text-only groups always show and mark the parent as having visible content.
246
+ if (!group.hasContent || frame.hasVisibleContent) {
247
+ parent.text += frame.text;
248
+ parent.hasVisibleContent = true;
249
+ }
250
+ continue;
251
+ }
252
+
253
+ const token = frame.tokens[frame.index++];
195
254
  if (!token || token.kind === "fill") continue;
196
255
  if (token.kind === "text") {
197
- text += token.value;
256
+ frame.text += token.value;
198
257
  continue;
199
258
  }
200
259
  if (token.kind === "var") {
201
260
  const rendered = renderVariable(token.name);
202
- text += rendered;
203
- if (rendered !== "" && !NON_CONTENT_VARS.has(token.name)) hasVisibleContent = true;
261
+ frame.text += rendered;
262
+ if (rendered !== "" && !NON_CONTENT_VARS.has(token.name)) {
263
+ frame.hasVisibleContent = true;
264
+ }
204
265
  continue;
205
266
  }
206
- const group = renderGroup(token, renderVariable);
207
- if (group !== undefined) {
208
- text += group;
209
- hasVisibleContent = true;
267
+ if (token.onlySeparators) continue;
268
+ stack.push({
269
+ tokens: token.tokens,
270
+ index: 0,
271
+ end: token.tokens.length,
272
+ group: token,
273
+ text: "",
274
+ hasVisibleContent: false,
275
+ });
276
+ }
277
+
278
+ return { text: "", hasVisibleContent: false };
279
+ }
280
+
281
+ /** One SGR sequence (`\x1b[…m`). */
282
+ const ANSI_ONE_GLOBAL = /\x1b\[[0-9;]*m/g;
283
+ const SGR_STICKY = /\x1b\[[0-9;]*m/y;
284
+ const WS_STICKY = /\s+/y;
285
+ /** An SGR made only of resets: full reset or attribute/colour "off" codes (22–29, 39, 49, 59). */
286
+ const CLOSE_SGR_EXACT = /^\x1b\[(?:(?:0|2[2-9]|39|49|59)(?:;(?:0|2[2-9]|39|49|59))*)?m$/;
287
+
288
+ const GLYPH_CACHE_LIMIT = 16;
289
+ const glyphListCache = new Map<string, readonly string[]>();
290
+
291
+ function getSeparatorGlyphs(glyph: string): readonly string[] {
292
+ const cached = glyphListCache.get(glyph);
293
+ if (cached) {
294
+ glyphListCache.delete(glyph);
295
+ glyphListCache.set(glyph, cached);
296
+ return cached;
297
+ }
298
+ // Always treat the classic pipe as a separator too (custom format text often uses it).
299
+ const trimmed = glyph.trim();
300
+ const glyphs =
301
+ trimmed && trimmed !== "|"
302
+ ? trimmed.length > 1
303
+ ? [trimmed, "|"]
304
+ : ["|", trimmed]
305
+ : ["|"];
306
+ if (glyphListCache.size >= GLYPH_CACHE_LIMIT) {
307
+ const oldest = glyphListCache.keys().next().value;
308
+ if (oldest !== undefined) glyphListCache.delete(oldest);
309
+ }
310
+ glyphListCache.set(glyph, glyphs);
311
+ return glyphs;
312
+ }
313
+
314
+ function matchGlyphAt(text: string, pos: number, glyphs: readonly string[]): number {
315
+ for (const glyph of glyphs) {
316
+ if (text.startsWith(glyph, pos)) return pos + glyph.length;
317
+ }
318
+ return -1;
319
+ }
320
+
321
+ function matchWsEnd(text: string, pos: number): number {
322
+ WS_STICKY.lastIndex = pos;
323
+ const match = WS_STICKY.exec(text);
324
+ return match ? WS_STICKY.lastIndex : -1;
325
+ }
326
+
327
+ function matchSgr(text: string, pos: number): { end: number; isClose: boolean } | undefined {
328
+ SGR_STICKY.lastIndex = pos;
329
+ const match = SGR_STICKY.exec(text);
330
+ if (!match) return undefined;
331
+ return { end: SGR_STICKY.lastIndex, isClose: CLOSE_SGR_EXACT.test(match[0]) };
332
+ }
333
+
334
+ function matchSpacedGlyph(
335
+ text: string,
336
+ pos: number,
337
+ glyphs: readonly string[],
338
+ splitAdjacentBare: boolean,
339
+ ): { end: number; wsEnd: number } | undefined {
340
+ const wsEnd = matchWsEnd(text, pos);
341
+ if (wsEnd === -1) return undefined;
342
+ const afterGlyph = matchGlyphAt(text, wsEnd, glyphs);
343
+ if (afterGlyph === -1) return { end: -1, wsEnd };
344
+ const trailEnd = matchWsEnd(text, afterGlyph);
345
+ if (trailEnd === -1) return { end: -1, wsEnd };
346
+ if (splitAdjacentBare && trailEnd - afterGlyph >= 2) {
347
+ const nextAfterGlyph = matchGlyphAt(text, trailEnd, glyphs);
348
+ if (nextAfterGlyph !== -1 && matchWsEnd(text, nextAfterGlyph) !== -1) {
349
+ return { end: trailEnd - 1, wsEnd };
210
350
  }
211
351
  }
212
- return { text, hasVisibleContent };
352
+ return { end: trailEnd, wsEnd };
353
+ }
354
+
355
+ type StripToken = { kind: "text" | "sep"; raw: string };
356
+
357
+ function scanSgrRun(text: string, start: number, wantClose: boolean): number {
358
+ const len = text.length;
359
+ let pos = start;
360
+ while (pos < len && text.charCodeAt(pos) === 0x1b) {
361
+ const sgr = matchSgr(text, pos);
362
+ if (!sgr || sgr.isClose !== wantClose) break;
363
+ pos = sgr.end;
364
+ }
365
+ return pos;
366
+ }
367
+
368
+ function scanEscapeToken(
369
+ rendered: string,
370
+ pos: number,
371
+ glyphs: readonly string[],
372
+ ): { token: StripToken; next: number } {
373
+ const openEnd = scanSgrRun(rendered, pos, false);
374
+ if (openEnd > pos) {
375
+ const spaced = matchSpacedGlyph(rendered, openEnd, glyphs, false);
376
+ if (spaced && spaced.end !== -1) {
377
+ const closeEnd = scanSgrRun(rendered, spaced.end, true);
378
+ if (closeEnd > spaced.end) {
379
+ return {
380
+ token: { kind: "sep", raw: rendered.slice(pos, closeEnd) },
381
+ next: closeEnd,
382
+ };
383
+ }
384
+ }
385
+ return { token: { kind: "text", raw: rendered.slice(pos, openEnd) }, next: openEnd };
386
+ }
387
+ const sgr = matchSgr(rendered, pos);
388
+ const next = sgr ? sgr.end : pos + 1;
389
+ return { token: { kind: "text", raw: rendered.slice(pos, next) }, next };
390
+ }
391
+
392
+ function scanPlainEnd(rendered: string, start: number): number {
393
+ const len = rendered.length;
394
+ let next = start + 1;
395
+ while (next < len) {
396
+ const c = rendered.charCodeAt(next);
397
+ if (c === 0x1b) break;
398
+ if ((c <= 0x20 || c === 0xa0 || c >= 0x1680) && matchWsEnd(rendered, next) !== -1) break;
399
+ next += 1;
400
+ }
401
+ return next;
213
402
  }
214
403
 
215
404
  /**
216
- * A group is dropped iff it has content vars/groups and none of them rendered,
217
- * or it holds nothing but `$sep` and whitespace. Text-only groups always show.
218
- * Returns `undefined` when the group is dropped.
405
+ * Single-pass linear O(N) tokenizer for bare and ANSI-wrapped separator units.
406
+ *
407
+ * ANSI only belongs to a separator unit as an open…close pair (e.g. `\x1b[2m › \x1b[22;39m`):
408
+ * a literal ` | ` between two styled segments sits between the left segment's reset and
409
+ * the right segment's opener, so removing either SGR would recolour a neighbouring segment.
219
410
  */
220
- function renderGroup(
221
- group: GroupToken,
222
- renderVariable: (name: string) => string,
223
- ): string | undefined {
224
- if (group.onlySeparators) return undefined;
225
- const inner = renderTokens(group.tokens, 0, group.tokens.length, renderVariable);
226
- if (group.hasContent && !inner.hasVisibleContent) return undefined;
227
- return inner.text;
228
- }
411
+ function tokenizeSeparators(rendered: string, glyphs: readonly string[]): StripToken[] {
412
+ const tokens: StripToken[] = [];
413
+ const len = rendered.length;
414
+ let pos = 0;
229
415
 
230
- /** One optional SGR sequence (`\x1b[…m`). */
231
- const ANSI_ONE_SRC = "\u001b\\[[0-9;]*m";
232
- const ANSI_ONE_GLOBAL = new RegExp(ANSI_ONE_SRC, "g");
233
- /** An SGR made only of resets: full reset or attribute/colour "off" codes (22–29, 39, 49, 59). */
234
- const ANSI_CLOSE_SRC = "\x1b\\[(?:(?:0|2[2-9]|39|49|59)(?:;(?:0|2[2-9]|39|49|59))*)?m";
235
- /** Any other SGR: it switches something on. */
236
- const ANSI_OPEN_SRC = `(?!${ANSI_CLOSE_SRC})${ANSI_ONE_SRC}`;
416
+ while (pos < len) {
417
+ if (rendered.charCodeAt(pos) === 0x1b) {
418
+ const scanned = scanEscapeToken(rendered, pos, glyphs);
419
+ tokens.push(scanned.token);
420
+ pos = scanned.next;
421
+ continue;
422
+ }
237
423
 
238
- type SeparatorPatterns = { consecutive: RegExp; leading: RegExp; trailing: RegExp };
239
- const separatorPatternCache = new Map<string, SeparatorPatterns | undefined>();
424
+ const spaced = matchSpacedGlyph(rendered, pos, glyphs, true);
425
+ if (spaced) {
426
+ if (spaced.end !== -1) {
427
+ tokens.push({ kind: "sep", raw: rendered.slice(pos, spaced.end) });
428
+ pos = spaced.end;
429
+ } else {
430
+ tokens.push({ kind: "text", raw: rendered.slice(pos, spaced.wsEnd) });
431
+ pos = spaced.wsEnd;
432
+ }
433
+ continue;
434
+ }
240
435
 
241
- function escapeRegExp(value: string): string {
242
- return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
243
- }
436
+ const next = scanPlainEnd(rendered, pos);
437
+ tokens.push({ kind: "text", raw: rendered.slice(pos, next) });
438
+ pos = next;
439
+ }
244
440
 
245
- function separatorPatterns(glyph: string): SeparatorPatterns | undefined {
246
- if (separatorPatternCache.has(glyph)) return separatorPatternCache.get(glyph);
247
- // Always treat the classic pipe as a separator too (custom format text often uses it).
248
- const glyphs = [...new Set(["|", glyph.trim()].filter(Boolean))].map(escapeRegExp);
249
- const spacedGlyph = `\\s+(?:${glyphs.join("|")})\\s+`;
250
- // One separator unit: the bare spaced glyph, or the glyph wrapped by its own
251
- // style (opening codes before, reset codes after, as `renderStyle(..., " › ")`
252
- // emits). ANSI only belongs to the unit as that open…close pair: a literal
253
- // ` | ` typed between two segments sits between the left segment's reset and
254
- // the right segment's opening code, and removing either recolours a segment.
255
- const unit = `(?:(?:${ANSI_OPEN_SRC})+${spacedGlyph}(?:${ANSI_CLOSE_SRC})+|${spacedGlyph})`;
256
- const patterns = {
257
- consecutive: new RegExp(`(${unit})(?:${unit})+`, "g"),
258
- leading: new RegExp(`^(?:${unit})+`),
259
- trailing: new RegExp(`(?:${unit})+$`),
260
- };
261
- separatorPatternCache.set(glyph, patterns);
262
- return patterns;
441
+ return tokens;
263
442
  }
264
443
 
265
444
  /**
@@ -271,17 +450,32 @@ function separatorPatterns(glyph: string): SeparatorPatterns | undefined {
271
450
  *
272
451
  * `separatorGlyph` is the active `$sep` glyph (e.g. `›`); `|` is always recognized.
273
452
  */
453
+ function isOuterTrimmable(token: StripToken): boolean {
454
+ return token.kind === "sep" || (token.kind === "text" && token.raw.trim() === "");
455
+ }
456
+
274
457
  export function stripOrphanSeparators(rendered: string, separatorGlyph = "|"): string {
275
458
  if (!rendered) return rendered;
276
- let result = rendered;
277
- const patterns = separatorPatterns(separatorGlyph);
278
- if (patterns) {
279
- // Collapse consecutive separator units, keeping the first (preserves themed color).
280
- result = result
281
- .replace(patterns.consecutive, "$1")
282
- .replace(patterns.leading, "")
283
- .replace(patterns.trailing, "");
459
+ const tokens = tokenizeSeparators(rendered, getSeparatorGlyphs(separatorGlyph));
460
+
461
+ let start = 0;
462
+ while (start < tokens.length && isOuterTrimmable(tokens[start]!)) start += 1;
463
+ let end = tokens.length;
464
+ while (end > start && isOuterTrimmable(tokens[end - 1]!)) end -= 1;
465
+
466
+ let result = "";
467
+ let prevWasSep = false;
468
+ for (let i = start; i < end; i++) {
469
+ const token = tokens[i]!;
470
+ if (token.kind === "sep") {
471
+ if (prevWasSep) continue;
472
+ prevWasSep = true;
473
+ } else {
474
+ prevWasSep = false;
475
+ }
476
+ result += token.raw;
284
477
  }
478
+
285
479
  // Strip leading / trailing plain whitespace left by empty groups.
286
480
  result = result.trim();
287
481
  // Pure ANSI (or empty) leftovers are not useful content.