@xynogen/pix-pretty 3.0.2 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xynogen/pix-pretty",
3
- "version": "3.0.2",
3
+ "version": "3.1.0",
4
4
  "description": "Enhanced tool output rendering with syntax highlighting, file icons, tree views, diff rendering, and FFF search",
5
5
  "type": "module",
6
6
  "main": "src/index.ts",
@@ -17,6 +17,7 @@
17
17
  "./diff-render": "./src/diff-render.ts",
18
18
  "./highlight": "./src/highlight.ts",
19
19
  "./lang": "./src/lang.ts",
20
+ "./markdown-transform": "./src/markdown-transform.ts",
20
21
  "./icons": "./src/icons.ts",
21
22
  "./icon-catalog": "./src/icon-catalog.ts",
22
23
  "./icon-persist": "./src/icon-persist.ts",
@@ -76,7 +77,7 @@
76
77
  "access": "public"
77
78
  },
78
79
  "dependencies": {
79
- "@xynogen/pix-runtime": "^0.22.2",
80
+ "@xynogen/pix-runtime": "^0.22.4",
80
81
  "chalk": "^4.1.2",
81
82
  "cli-highlight": "^2.1.11",
82
83
  "@ff-labs/fff-node": "^0.10.6",
package/src/ansi.ts CHANGED
@@ -32,6 +32,35 @@ export function hasAnsi(s: string): boolean {
32
32
  return s.includes(ESC);
33
33
  }
34
34
 
35
+ /**
36
+ * Re-open `bgOpen`/`fgOpen` after every reset inside `s`: `\x1b[0m` clears both, `49`
37
+ * clears the background, `39` the foreground. Keeps a panel color solid behind
38
+ * embedded content that ends its own styles with a full reset.
39
+ */
40
+ export function reopenAfterResets(s: string, bgOpen: string, fgOpen = ""): string {
41
+ if (!bgOpen && !fgOpen) return s;
42
+ return s.replace(ANSI_CAPTURE_RE, (seq, params: string) => {
43
+ let bgReset = false;
44
+ let fgReset = false;
45
+ const codes = params === "" ? [0] : params.split(";").map(Number);
46
+ for (let i = 0; i < codes.length; i++) {
47
+ const code = codes[i];
48
+ // 38/48 carry color arguments (`5;n` or `2;r;g;b`); a 49 there is not a reset.
49
+ if (code === 38 || code === 48) i += codes[i + 1] === 5 ? 2 : 4;
50
+ else if (code === 0) bgReset = fgReset = true;
51
+ else if (code === 49) bgReset = true;
52
+ else if (code === 39) fgReset = true;
53
+ }
54
+ return `${seq}${bgReset ? bgOpen : ""}${fgReset ? fgOpen : ""}`;
55
+ });
56
+ }
57
+
58
+ /** A `Box` background function that stays solid behind content with embedded resets. */
59
+ export function solidBackground(bg: (text: string) => string): (text: string) => string {
60
+ const bgOpen = bg("\x00").split("\x00")[0] ?? "";
61
+ return (text) => bg(reopenAfterResets(text, bgOpen));
62
+ }
63
+
35
64
  // ---------------------------------------------------------------------------
36
65
  // Low-contrast fix (same as pi-diff)
37
66
  // ---------------------------------------------------------------------------
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Display transform for Markdown that Pi does not pass through its transformer chain,
3
+ * such as cards drawn by entry renderers (`/btw`). pix-display registers the same
4
+ * pipeline it gives assistant replies; without it the Markdown stays as written.
5
+ * The slot lives on globalThis: Pi re-evaluates extension modules on reload.
6
+ */
7
+ export type MarkdownTransform = (markdown: string, availableWidth: number) => string;
8
+
9
+ const KEY = Symbol.for("@xynogen/pix-pretty:markdown-transform");
10
+ type Slot = { [KEY]?: MarkdownTransform };
11
+
12
+ export function setMarkdownTransform(transform: MarkdownTransform | undefined): void {
13
+ (globalThis as Slot)[KEY] = transform;
14
+ }
15
+
16
+ /** Pass as `Markdown`'s `transform` option. A throwing transform keeps the source, as Pi does. */
17
+ export function transformMarkdown(markdown: string, availableWidth: number): string {
18
+ try {
19
+ return (globalThis as Slot)[KEY]?.(markdown, availableWidth) ?? markdown;
20
+ } catch {
21
+ return markdown;
22
+ }
23
+ }
@@ -18,6 +18,7 @@ import {
18
18
  } from "@earendil-works/pi-tui";
19
19
  import { config } from "@xynogen/pix-runtime/config";
20
20
  import { prettySection, type RenderSize } from "@xynogen/pix-runtime/sections";
21
+ import { reopenAfterResets } from "./ansi.ts";
21
22
 
22
23
  export { truncateToWidth, visibleWidth, wrapTextWithAnsi };
23
24
 
@@ -233,24 +234,12 @@ export function frameLines(opts: FrameOptions): string[] {
233
234
  // Expand before framing so a long command wraps instead of losing its tail.
234
235
  const { rows: lines } = fitModalLines(opts.lines, inner, wrap);
235
236
 
236
- // Derive the OPEN sequences so we can re-assert them after any embedded
237
- // reset. A full reset (\x1b[0m) clears both fg and bg; \x1b[49m clears bg;
238
- // \x1b[39m clears fg. Re-emitting the base opens after each keeps the modal
239
- // background solid AND gives raw text a readable foreground.
237
+ // Re-assert the base opens after any embedded reset so the modal background stays
238
+ // solid AND raw text keeps a readable foreground.
240
239
  const SENTINEL = "\x00";
241
240
  const bgOpen = bg(SENTINEL).split(SENTINEL)[0] ?? "";
242
241
  const fgOpen = fg ? (fg(SENTINEL).split(SENTINEL)[0] ?? "") : "";
243
- const reassert = (s: string): string =>
244
- bgOpen || fgOpen
245
- ? s.replace(/\x1b\[([0-9;]*)m/g, (seq, p: string) => {
246
- const parts = p.split(";");
247
- const isFull = p === "0";
248
- let tail = seq;
249
- if (isFull || parts.includes("49")) tail += bgOpen;
250
- if (isFull || parts.includes("39")) tail += fgOpen;
251
- return tail;
252
- })
253
- : s;
242
+ const reassert = (s: string): string => reopenAfterResets(s, bgOpen, fgOpen);
254
243
 
255
244
  const row = (content: string): string => {
256
245
  const pad = inner - visibleWidth(content);
@@ -18,6 +18,7 @@ import {
18
18
  visibleWidth,
19
19
  } from "@earendil-works/pi-tui";
20
20
  import { getErrorMessage } from "@xynogen/pix-runtime/errors";
21
+ import { isSubTabKey, nextTab, renderSubTabBar, renderTabRule } from "@xynogen/pix-runtime/tab-bar";
21
22
  import {
22
23
  frameModal,
23
24
  MIN_MODAL_HEIGHT,
@@ -165,6 +166,8 @@ export interface SettingsRow {
165
166
  value: string;
166
167
  /** Theme role for the value. Default `success`. */
167
168
  tone?: "success" | "warning" | "muted";
169
+ /** Dim the label too: the row is turned off. The cursor still highlights it. */
170
+ dim?: boolean;
168
171
  /** Enter edits `value` in a text field inside the row, not in a new dialog. */
169
172
  editable?: boolean;
170
173
  /**
@@ -210,14 +213,16 @@ export function renderSettingsRows(
210
213
  let listLine: number | undefined;
211
214
  let section = "";
212
215
  rows.forEach((row, index) => {
213
- if (row.section !== section) {
216
+ // An empty section (rows of one sub-tab) gets no heading.
217
+ if (row.section !== section && row.section) {
214
218
  if (section) lines.push("");
215
219
  lines.push(theme.fg("dim", ` ${row.section}`));
216
220
  section = row.section;
217
221
  }
218
222
  const active = index === selected;
219
223
  rowLines[index] = lines.length;
220
- const label = `${active ? theme.fg("accent", "→") : " "} ${theme.fg(active ? "accent" : "text", row.label.padEnd(labelWidth))} `;
224
+ const labelRole = active ? "accent" : row.dim ? "dim" : "text";
225
+ const label = `${active ? theme.fg("accent", "→") : " "} ${theme.fg(labelRole, row.label.padEnd(labelWidth))} `;
221
226
  const editing = active ? field : undefined;
222
227
  const valueWidth = Math.max(10, width - labelWidth - 4);
223
228
  lines.push(
@@ -270,10 +275,16 @@ export interface SettingsPickerOptions {
270
275
  onMount?: (redraw: () => void) => () => void;
271
276
  /** Start row. */
272
277
  selected?: number;
278
+ /**
279
+ * Show each row `section` as a sub-tab (`` ` `` switches) instead of a heading in one list.
280
+ * Off by default; one section shows no tab bar.
281
+ */
282
+ sectionTabs?: boolean;
273
283
  }
274
284
 
275
285
  /**
276
- * Show the sectioned settings overview. It stays open until esc, or until
286
+ * Show the sectioned settings overview, or one section per sub-tab with `sectionTabs`.
287
+ * It stays open until esc, or until
277
288
  * `onAction` returns `"close"`. For an `editable` row, enter opens a text field in
278
289
  * the row. Resolves with the closing action, or null on escape.
279
290
  */
@@ -283,8 +294,16 @@ export async function showSettingsPicker(
283
294
  ): Promise<SettingsAction | null> {
284
295
  const result = await ui.custom<SettingsAction | null>(
285
296
  (tui, theme, keybindings, done) => {
286
- let rows = opts.rows();
287
- let selected = Math.min(opts.selected ?? 0, Math.max(0, rows.length - 1));
297
+ let all = opts.rows();
298
+ const sectionsOf = (list: SettingsRow[]) => [...new Set(list.map((row) => row.section))];
299
+ // Start on the section of the requested row, so a reopened panel keeps its place.
300
+ let tab = opts.sectionTabs ? (all[opts.selected ?? 0]?.section ?? all[0]?.section ?? "") : "";
301
+ const visible = () => (tab ? all.filter((row) => row.section === tab) : all);
302
+ let rows = visible();
303
+ let selected = Math.max(
304
+ 0,
305
+ Math.min(rows.indexOf(all[opts.selected ?? 0] as SettingsRow), rows.length - 1),
306
+ );
288
307
  let field: Input | undefined;
289
308
  let list: { row: SettingsRow; query: string; cursor: number } | undefined;
290
309
  let busy = false;
@@ -304,7 +323,10 @@ export async function showSettingsPicker(
304
323
  error = getErrorMessage(cause);
305
324
  }
306
325
  busy = false;
307
- rows = opts.rows();
326
+ all = opts.rows();
327
+ if (tab && !sectionsOf(all).includes(tab)) tab = all[0]?.section ?? "";
328
+ rows = visible();
329
+ selected = Math.min(selected, Math.max(0, rows.length - 1));
308
330
  redraw();
309
331
  };
310
332
  const edit = (row: SettingsRow) => {
@@ -361,8 +383,9 @@ export async function showSettingsPicker(
361
383
  return {
362
384
  render(width: number) {
363
385
  const mw = modalWidth(width);
386
+ const tabs = tab ? sectionsOf(all) : [];
364
387
  const body = renderSettingsRows(
365
- rows,
388
+ tabs.length > 1 ? rows.map((row) => ({ ...row, section: "" })) : rows,
366
389
  theme,
367
390
  selected,
368
391
  field,
@@ -378,7 +401,10 @@ export async function showSettingsPicker(
378
401
  minHeight: MIN_MODAL_HEIGHT,
379
402
  title: opts.title,
380
403
  titleColor: (text) => theme.fg("accent", theme.bold(text)),
381
- header: [""],
404
+ header:
405
+ tabs.length > 1
406
+ ? [renderSubTabBar(tabs, tab, theme, mw - 4), renderTabRule(theme, mw - 4)]
407
+ : [""],
382
408
  body: body.lines,
383
409
  footer: [
384
410
  "",
@@ -388,7 +414,7 @@ export async function showSettingsPicker(
388
414
  ? "enter save · esc cancel"
389
415
  : list
390
416
  ? "type to filter · ↑↓ move · enter pick · esc cancel"
391
- : "↑↓ move · enter change · esc close",
417
+ : `↑↓ move · space/enter${all.some((row) => row.choices?.length) ? "/←→" : ""} change${tabs.length > 1 ? " · ` sub-tab" : ""} · esc close`,
392
418
  ),
393
419
  ],
394
420
  selectedBodyLine: body.listLine ?? body.rowLines[selected],
@@ -406,14 +432,28 @@ export async function showSettingsPicker(
406
432
  if (list) return listInput(data);
407
433
  if (keybindings.matches(data, "tui.select.cancel")) return close(null);
408
434
  if (busy) return;
435
+ if (tab && isSubTabKey(data)) {
436
+ tab = nextTab(sectionsOf(all), tab);
437
+ rows = visible();
438
+ selected = 0;
439
+ return redraw();
440
+ }
409
441
  const row = rows[selected];
410
- if (matchesKey(data, Key.enter)) {
442
+ // Standard /pix keys: enter or space acts on the row; ←→ step through its choices.
443
+ if (matchesKey(data, Key.enter) || matchesKey(data, Key.space)) {
411
444
  if (!row) return;
412
445
  if (row.choices?.length) openList(row);
413
446
  else if (row.editable) edit(row);
414
447
  else void run({ key: row.key });
415
448
  return redraw();
416
449
  }
450
+ if (row?.choices?.length && (matchesKey(data, Key.left) || matchesKey(data, Key.right))) {
451
+ const at = row.choices.findIndex((choice) => choice.value === row.value);
452
+ const step = matchesKey(data, Key.left) ? -1 : 1;
453
+ const next = row.choices[(at + step + row.choices.length) % row.choices.length];
454
+ if (next && next.value !== row.value) void run({ key: row.key, value: next.value });
455
+ return redraw();
456
+ }
417
457
  if (keybindings.matches(data, "tui.select.up"))
418
458
  selected = (selected - 1 + rows.length) % rows.length;
419
459
  else if (keybindings.matches(data, "tui.select.down"))
@@ -497,11 +537,11 @@ export async function showProviderPicker(
497
537
  "",
498
538
  field
499
539
  ? guide("enter", "submit") + sep + guide("esc", "cancel")
500
- : guide("\u2191\u2193", "navigate") +
540
+ : guide("↑↓", "move") +
501
541
  sep +
502
542
  guide("enter", "set default") +
503
543
  sep +
504
- guide("space", "open") +
544
+ guide("space", "expand") +
505
545
  sep +
506
546
  guide("esc", "close"),
507
547
  ],
package/src/tui.ts CHANGED
@@ -13,6 +13,7 @@ export {
13
13
  AssistantMessageComponent,
14
14
  CustomEditor,
15
15
  getMarkdownTheme,
16
+ InteractiveMode,
16
17
  Theme,
17
18
  } from "@earendil-works/pi-coding-agent";
18
19
  export type {