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
@@ -9,8 +9,18 @@
9
9
  import { existsSync, readFileSync } from "node:fs";
10
10
  import { dirname, join } from "node:path";
11
11
  import { fileURLToPath } from "node:url";
12
- import type { ExtensionAPI, ExtensionContext, Theme } from "@earendil-works/pi-coding-agent";
13
- import { type Component, matchesKey, truncateToWidth, wrapTextWithAnsi } from "@earendil-works/pi-tui";
12
+ import type { ExtensionAPI, Theme } from "@earendil-works/pi-coding-agent";
13
+ import {
14
+ type Component,
15
+ type Keybinding,
16
+ type KeyId,
17
+ matchesKey,
18
+ type TuiMouseEvent,
19
+ type TuiMouseEventResult,
20
+ truncateToWidth,
21
+ wrapTextWithAnsi,
22
+ } from "@earendil-works/pi-tui";
23
+ import { isInteractiveTui, normalizeWidth } from "../shared/tui-context";
14
24
 
15
25
  export interface ChangelogSection {
16
26
  title: string;
@@ -28,6 +38,26 @@ const packageRoot = join(dirname(fileURLToPath(import.meta.url)), "..", "..");
28
38
  // ---------------------------------------------------------------------------
29
39
  // Parsing (pure)
30
40
 
41
+ const FENCE_OPEN = /^\s*(`{3,}|~{3,})/;
42
+ const FENCE_CLOSE = /^\s*(`{3,}|~{3,})[ \t]*$/;
43
+
44
+ function matchFenceOpen(line: string): string | undefined {
45
+ const match = FENCE_OPEN.exec(line);
46
+ if (!match) return undefined;
47
+ const marker = match[1]!;
48
+ // CommonMark §4.5: backtick fence info strings cannot contain backticks
49
+ // (single-line inline spans like ```code``` do not open a fenced block).
50
+ if (marker[0] === "`" && line.indexOf("`", match.index + match[0].length) !== -1) {
51
+ return undefined;
52
+ }
53
+ return marker;
54
+ }
55
+
56
+ function closesFence(line: string, fence: string): boolean {
57
+ const match = FENCE_CLOSE.exec(line);
58
+ return !!match && match[1]![0] === fence[0] && match[1]!.length >= fence.length;
59
+ }
60
+
31
61
  /** Parses `## [1.2.0] - 2026-09-30` / `## 1.2.0` headings with `###` sections and `-` bullets. */
32
62
  export function parseChangelog(markdown: string): ChangelogEntry[] {
33
63
  const entries: ChangelogEntry[] = [];
@@ -36,8 +66,36 @@ export function parseChangelog(markdown: string): ChangelogEntry[] {
36
66
  // Markdown soft line breaks: a hard-wrapped bullet or paragraph continues on the next
37
67
  // non-bullet line until a blank line or heading ends it.
38
68
  let open = false;
69
+ let fence: string | undefined;
70
+
71
+ const pushLineToSection = (line: string) => {
72
+ if (!entry) return;
73
+ if (!section) {
74
+ section = { title: "", lines: [] };
75
+ entry.sections.push(section);
76
+ }
77
+ section.lines.push(line);
78
+ };
79
+
39
80
  for (const raw of markdown.split(/\r?\n/)) {
40
81
  const line = raw.trimEnd();
82
+ if (fence !== undefined) {
83
+ if (closesFence(line, fence)) {
84
+ fence = undefined;
85
+ open = false;
86
+ }
87
+ pushLineToSection(line);
88
+ continue;
89
+ }
90
+
91
+ const fenceOpen = matchFenceOpen(line);
92
+ if (fenceOpen !== undefined) {
93
+ fence = fenceOpen;
94
+ open = false;
95
+ pushLineToSection(line);
96
+ continue;
97
+ }
98
+
41
99
  const heading = line.match(/^##\s+\[?v?(\d+\.\d+\.\d+[^\]\s]*)\]?(?:\s+[-–—]\s+(.+))?\s*$/);
42
100
  if (heading) {
43
101
  entry = { version: heading[1]!, date: heading[2]?.trim(), sections: [] };
@@ -69,11 +127,7 @@ export function parseChangelog(markdown: string): ChangelogEntry[] {
69
127
  continue;
70
128
  }
71
129
  open = true;
72
- if (!section) {
73
- section = { title: "", lines: [] };
74
- entry.sections.push(section);
75
- }
76
- section.lines.push(line);
130
+ pushLineToSection(line);
77
131
  }
78
132
  return entries;
79
133
  }
@@ -97,28 +151,51 @@ function bulletText(line: string): string {
97
151
 
98
152
  /** Full changelog lines (used by the scrollable viewer). */
99
153
  export function renderChangelogLines(entries: ChangelogEntry[], width: number, theme: Paint): string[] {
154
+ const safeWidth = normalizeWidth(width);
155
+ if (safeWidth <= 0) return [];
156
+ const compact = safeWidth < 16;
157
+ const baseIndent = compact ? "" : " ";
100
158
  const out: string[] = [];
101
159
  const wrap = (text: string, indent: string, hanging: string) => {
102
- const body = wrapTextWithAnsi(text, Math.max(10, width - hanging.length));
160
+ const body = wrapTextWithAnsi(text, Math.max(1, safeWidth - hanging.length));
103
161
  body.forEach((part, index) => out.push(`${index === 0 ? indent : hanging}${part}`));
104
162
  };
105
163
  for (const entry of entries) {
106
164
  if (out.length) out.push("");
107
165
  out.push(theme.bold(theme.fg("accent", `✿ ${entry.version}`)) + (entry.date ? theme.fg("dim", ` ${entry.date}`) : ""));
108
166
  for (const section of entry.sections) {
109
- if (section.title) out.push(theme.fg("mdHeading", ` ${section.title}`));
167
+ if (section.title) out.push(theme.fg("mdHeading", compact ? section.title : ` ${section.title}`));
168
+ let fence: string | undefined;
110
169
  for (const line of section.lines) {
170
+ if (fence !== undefined) {
171
+ if (closesFence(line, fence)) {
172
+ fence = undefined;
173
+ wrap(theme.fg("dim", line.trim()), baseIndent, baseIndent);
174
+ } else if (!line.trim()) {
175
+ out.push("");
176
+ } else {
177
+ wrap(line, baseIndent, baseIndent);
178
+ }
179
+ continue;
180
+ }
181
+ const fenceOpen = matchFenceOpen(line);
182
+ if (fenceOpen !== undefined) {
183
+ fence = fenceOpen;
184
+ wrap(theme.fg("dim", line.trim()), baseIndent, baseIndent);
185
+ continue;
186
+ }
111
187
  const nested = /^\s{2,}[-*]\s+/.test(line);
112
188
  if (isBullet(line)) {
113
- const indent = nested ? " " : " ";
114
- wrap(`${theme.fg("dim", nested ? "◦" : "•")} ${bulletText(line)}`, indent, `${indent} `);
189
+ const indent = compact ? "" : nested ? " " : " ";
190
+ const hanging = compact ? "" : `${indent} `;
191
+ wrap(`${theme.fg("dim", nested ? "◦" : "•")} ${bulletText(line)}`, indent, hanging);
115
192
  } else {
116
- wrap(stripInlineMarkdown(line.trim()), " ", " ");
193
+ wrap(stripInlineMarkdown(line.trim()), baseIndent, baseIndent);
117
194
  }
118
195
  }
119
196
  }
120
197
  }
121
- return out.map((line) => truncateToWidth(line, width, ""));
198
+ return out.map((line) => truncateToWidth(line, safeWidth, ""));
122
199
  }
123
200
 
124
201
  export type Paint = Pick<Theme, "fg" | "bold">;
@@ -139,83 +216,149 @@ function readChangelog(): ChangelogEntry[] {
139
216
  // ---------------------------------------------------------------------------
140
217
  // Scrollable viewer
141
218
 
142
- class ChangelogViewer implements Component {
219
+ type KeybindingsMatcher = {
220
+ matches?: (data: string, keybinding: Keybinding) => boolean;
221
+ };
222
+
223
+ export class ChangelogViewer implements Component {
224
+ readonly capturesViewportInput = true;
143
225
  private offset = 0;
144
226
  private lastWidth = 0;
145
227
  private cache: string[] = [];
228
+ private lastFrameKey = "";
229
+ private lastFrameLines: string[] = [];
146
230
  private readonly entries: ChangelogEntry[];
147
- private readonly theme: Theme;
231
+ private readonly theme: Paint;
148
232
  private readonly rows: () => number;
149
233
  private readonly requestRender: () => void;
150
234
  private readonly done: () => void;
235
+ private readonly keybindings: KeybindingsMatcher | undefined;
151
236
 
152
237
  constructor(
153
238
  entries: ChangelogEntry[],
154
- theme: Theme,
239
+ theme: Paint,
155
240
  rows: () => number,
156
241
  requestRender: () => void,
157
242
  done: () => void,
243
+ keybindings?: KeybindingsMatcher,
158
244
  ) {
159
245
  this.entries = entries;
160
246
  this.theme = theme;
161
247
  this.rows = rows;
162
248
  this.requestRender = requestRender;
163
249
  this.done = done;
250
+ this.keybindings = keybindings;
164
251
  }
165
252
 
166
253
  private pageSize(): number {
167
- return Math.max(5, Math.min(40, this.rows() - 8));
254
+ const rawRows = this.rows();
255
+ const safeRows = typeof rawRows === "number" && Number.isFinite(rawRows) && rawRows > 0 ? Math.floor(rawRows) : 24;
256
+ return Math.max(5, Math.min(40, safeRows - 8));
168
257
  }
169
258
 
170
259
  private maxOffset(): number {
171
260
  return Math.max(0, this.cache.length - this.pageSize());
172
261
  }
173
262
 
263
+ private matchesBinding(data: string, binding: Keybinding, fallbackKeys: readonly KeyId[]): boolean {
264
+ if (typeof this.keybindings?.matches === "function") {
265
+ return this.keybindings.matches(data, binding);
266
+ }
267
+ return fallbackKeys.some((key) => matchesKey(data, key));
268
+ }
269
+
270
+ private isCloseInput(data: string): boolean {
271
+ return (
272
+ this.matchesBinding(data, "tui.select.cancel", ["escape", "ctrl+c"]) ||
273
+ matchesKey(data, "ctrl+c") ||
274
+ data === "q" ||
275
+ matchesKey(data, "return")
276
+ );
277
+ }
278
+
279
+ private nextScrollOffset(data: string, max: number): number | undefined {
280
+ if (this.matchesBinding(data, "tui.select.up", ["up"]) || data === "k") return this.offset - 1;
281
+ if (this.matchesBinding(data, "tui.select.down", ["down"]) || data === "j") return this.offset + 1;
282
+ if (this.matchesBinding(data, "tui.select.pageUp", ["pageUp"])) return this.offset - this.pageSize();
283
+ if (this.matchesBinding(data, "tui.select.pageDown", ["pageDown"]) || data === " ") return this.offset + this.pageSize();
284
+ if (matchesKey(data, "home") || data === "g") return 0;
285
+ if (matchesKey(data, "end") || data === "G") return max;
286
+ return undefined;
287
+ }
288
+
174
289
  handleInput(data: string): void {
175
- const max = this.maxOffset();
176
- if (matchesKey(data, "escape") || data === "q" || matchesKey(data, "return")) {
290
+ if (this.isCloseInput(data)) {
177
291
  this.done();
178
292
  return;
179
293
  }
180
- if (matchesKey(data, "up") || data === "k") this.offset -= 1;
181
- else if (matchesKey(data, "down") || data === "j") this.offset += 1;
182
- else if (matchesKey(data, "pageUp")) this.offset -= this.pageSize();
183
- else if (matchesKey(data, "pageDown") || data === " ") this.offset += this.pageSize();
184
- else if (matchesKey(data, "home") || data === "g") this.offset = 0;
185
- else if (matchesKey(data, "end") || data === "G") this.offset = max;
186
- this.offset = Math.max(0, Math.min(max, this.offset));
187
- this.requestRender();
294
+ const max = this.maxOffset();
295
+ const target = this.nextScrollOffset(data, max);
296
+ if (target === undefined) return;
297
+ const previous = this.offset;
298
+ this.offset = Math.max(0, Math.min(max, target));
299
+ if (this.offset !== previous) this.requestRender();
300
+ }
301
+
302
+ handleMouse(event: TuiMouseEvent): TuiMouseEventResult | undefined {
303
+ if (event.type !== "wheel") return undefined;
304
+ const rawDelta = event.wheelDelta;
305
+ if (typeof rawDelta !== "number" || !Number.isFinite(rawDelta) || rawDelta === 0) {
306
+ return { handled: true, render: false };
307
+ }
308
+ const step = rawDelta < 0 ? Math.min(-1, Math.trunc(rawDelta)) : Math.max(1, Math.trunc(rawDelta));
309
+ const previous = this.offset;
310
+ this.offset = Math.max(0, Math.min(this.maxOffset(), this.offset + step));
311
+ const changed = this.offset !== previous;
312
+ if (changed) this.requestRender();
313
+ return { handled: true, render: changed };
188
314
  }
189
315
 
190
316
  render(width: number): string[] {
191
- if (width !== this.lastWidth) {
192
- this.cache = renderChangelogLines(this.entries, width, this.theme);
193
- this.lastWidth = width;
317
+ const safeWidth = normalizeWidth(width);
318
+ if (safeWidth <= 0) return [];
319
+ if (safeWidth !== this.lastWidth) {
320
+ this.cache = renderChangelogLines(this.entries, safeWidth, this.theme);
321
+ this.lastWidth = safeWidth;
322
+ this.lastFrameKey = "";
194
323
  }
195
324
  // A wider width shortens the text and a taller terminal grows the page, so the offset
196
325
  // a key press clamped earlier can point past the end: clamp again on every render.
197
326
  this.offset = Math.max(0, Math.min(this.maxOffset(), this.offset));
198
327
  const size = this.pageSize();
328
+ const frameKey = `${safeWidth}:${this.offset}:${size}`;
329
+ if (frameKey === this.lastFrameKey) return this.lastFrameLines;
199
330
  const visible = this.cache.slice(this.offset, this.offset + size);
200
331
  const end = Math.min(this.cache.length, this.offset + size);
332
+ const range = this.cache.length === 0 ? "0/0" : `${this.offset + 1}-${end}/${this.cache.length}`;
201
333
  const footer = this.theme.fg(
202
334
  "muted",
203
- ` ${this.offset + 1}-${end}/${this.cache.length} · ↑↓ j/k scroll · PgUp/PgDn · q/Esc close`,
335
+ ` ${range} · ↑↓ j/k scroll · PgUp/PgDn · q/Esc close`,
204
336
  );
205
- return [...visible, "", truncateToWidth(footer, width, "…")];
337
+ this.lastFrameLines = [...visible, "", truncateToWidth(footer, safeWidth, "…")];
338
+ this.lastFrameKey = frameKey;
339
+ return this.lastFrameLines;
206
340
  }
207
341
 
208
342
  invalidate(): void {
209
343
  this.lastWidth = 0;
344
+ this.lastFrameKey = "";
210
345
  }
211
346
  }
212
347
 
213
348
  // ---------------------------------------------------------------------------
214
349
  // Extension
215
350
 
216
- function isTui(ctx: ExtensionContext): boolean {
217
- const mode = (ctx as { mode?: string }).mode;
218
- return mode ? mode === "tui" : ctx.hasUI;
351
+ function safeNotify(
352
+ ctx: { hasUI?: boolean; ui?: { notify?: (message: string, type?: "info" | "warning" | "error") => void } } | null | undefined,
353
+ message: string,
354
+ type: "info" | "warning" | "error",
355
+ ): void {
356
+ if (!ctx?.hasUI || typeof ctx.ui?.notify !== "function") return;
357
+ try {
358
+ ctx.ui.notify(message, type);
359
+ } catch {
360
+ // Headless/disposed UI: fail soft.
361
+ }
219
362
  }
220
363
 
221
364
  export default function sakuraChangelog(pi: ExtensionAPI) {
@@ -223,26 +366,27 @@ export default function sakuraChangelog(pi: ExtensionAPI) {
223
366
  description: "Show the pi-sakura-cyberdeck changelog (optionally for one version)",
224
367
  handler: async (args, ctx) => {
225
368
  const all = readChangelog();
226
- const wanted = args.trim().replace(/^v/, "");
369
+ const wanted = (typeof args === "string" ? args : "").trim().replace(/^v/, "");
227
370
  const entries = wanted ? all.filter((e) => e.version === wanted) : all;
228
371
  if (!entries.length) {
229
- ctx.ui.notify(
372
+ safeNotify(
373
+ ctx,
230
374
  wanted ? `No changelog entry for ${wanted}.` : "Changelog not found in this installation.",
231
375
  "warning",
232
376
  );
233
377
  return;
234
378
  }
235
- if (!isTui(ctx)) {
379
+ if (!isInteractiveTui(ctx)) {
236
380
  const plain = renderChangelogLines(entries, 100, { fg: (_c: string, t: string) => t, bold: (t: string) => t });
237
- ctx.ui.notify(plain.slice(0, 40).join("\n"), "info");
381
+ safeNotify(ctx, plain.slice(0, 40).join("\n"), "info");
238
382
  return;
239
383
  }
240
- await ctx.ui.custom<void>((tui, theme, _kb, done) => {
384
+ await ctx.ui.custom<void>((tui, theme, kb, done) => {
241
385
  const rows = () => {
242
386
  const value = (tui as { terminal?: { rows?: number } }).terminal?.rows;
243
- return typeof value === "number" && value > 0 ? value : 24;
387
+ return typeof value === "number" && Number.isFinite(value) && value > 0 ? Math.floor(value) : 24;
244
388
  };
245
- return new ChangelogViewer(entries, theme, rows, () => tui.requestRender(), () => done());
389
+ return new ChangelogViewer(entries, theme, rows, () => tui.requestRender(), () => done(), kb);
246
390
  });
247
391
  },
248
392
  });