@xynogen/pix-pretty 3.1.0 → 3.3.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.1.0",
3
+ "version": "3.3.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",
@@ -77,7 +77,7 @@
77
77
  "access": "public"
78
78
  },
79
79
  "dependencies": {
80
- "@xynogen/pix-runtime": "^0.22.4",
80
+ "@xynogen/pix-runtime": "^0.23.0",
81
81
  "chalk": "^4.1.2",
82
82
  "cli-highlight": "^2.1.11",
83
83
  "@ff-labs/fff-node": "^0.10.6",
@@ -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 { ListFilter } from "@xynogen/pix-runtime/list-filter";
21
22
  import { isSubTabKey, nextTab, renderSubTabBar, renderTabRule } from "@xynogen/pix-runtime/tab-bar";
22
23
  import {
23
24
  frameModal,
@@ -198,6 +199,44 @@ export function filterChoices(choices: SettingsChoice[], query: string): Setting
198
199
 
199
200
  export type SettingsAction = { key: string; value?: string };
200
201
 
202
+ const COLUMN_GAP = 3;
203
+
204
+ /**
205
+ * Plain toggle rows (no headings, choices or text fields) in column-major columns, so a long
206
+ * list fills the modal's width instead of paging down one narrow column. One column when the
207
+ * rows do not fit two. Exported for tests.
208
+ */
209
+ export function settingsColumns(rows: SettingsRow[], width: number): number {
210
+ if (rows.length < 8 || rows.some((row) => row.section || row.choices?.length || row.editable))
211
+ return 1;
212
+ const labelWidth = Math.max(...rows.map((row) => visibleWidth(row.label)));
213
+ const valueWidth = Math.max(...rows.map((row) => visibleWidth(row.value)));
214
+ const cell = 2 + labelWidth + 2 + valueWidth;
215
+ return Math.max(1, Math.min(4, Math.floor((width + COLUMN_GAP) / (cell + COLUMN_GAP))));
216
+ }
217
+
218
+ function renderColumns(
219
+ rows: SettingsRow[],
220
+ theme: Pick<PickerTheme, "fg">,
221
+ selected: number,
222
+ columns: number,
223
+ ): { lines: string[]; rowLines: number[] } {
224
+ const labelWidth = Math.max(...rows.map((row) => visibleWidth(row.label)));
225
+ const valueWidth = Math.max(...rows.map((row) => visibleWidth(row.value)));
226
+ const perColumn = Math.ceil(rows.length / columns);
227
+ const lines: string[] = Array.from({ length: perColumn }, () => "");
228
+ const rowLines: number[] = [];
229
+ rows.forEach((row, index) => {
230
+ const line = index % perColumn;
231
+ const active = index === selected;
232
+ const labelRole = active ? "accent" : row.dim ? "dim" : "text";
233
+ const cell = `${active ? theme.fg("accent", "→") : " "} ${theme.fg(labelRole, row.label.padEnd(labelWidth))} ${theme.fg(row.tone ?? "success", row.value.padEnd(valueWidth))}`;
234
+ lines[line] += (index >= perColumn ? " ".repeat(COLUMN_GAP) : "") + cell;
235
+ rowLines[index] = line;
236
+ });
237
+ return { lines, rowLines };
238
+ }
239
+
201
240
  /** Render the sectioned settings overview body. Exported for tests. */
202
241
  export function renderSettingsRows(
203
242
  rows: SettingsRow[],
@@ -207,6 +246,8 @@ export function renderSettingsRows(
207
246
  width = 80,
208
247
  list?: { choices: SettingsChoice[]; cursor: number; query: string; max?: number },
209
248
  ): { lines: string[]; rowLines: number[]; listLine?: number } {
249
+ const columns = field || list ? 1 : settingsColumns(rows, width);
250
+ if (columns > 1) return renderColumns(rows, theme, selected, columns);
210
251
  const labelWidth = Math.max(...rows.map((row) => row.label.length));
211
252
  const lines: string[] = [];
212
253
  const rowLines: number[] = [];
@@ -280,6 +321,8 @@ export interface SettingsPickerOptions {
280
321
  * Off by default; one section shows no tab bar.
281
322
  */
282
323
  sectionTabs?: boolean;
324
+ /** The shared `/pix` search line: `/` filters rows by label or section across every sub-tab. */
325
+ search?: boolean;
283
326
  }
284
327
 
285
328
  /**
@@ -298,7 +341,14 @@ export async function showSettingsPicker(
298
341
  const sectionsOf = (list: SettingsRow[]) => [...new Set(list.map((row) => row.section))];
299
342
  // Start on the section of the requested row, so a reopened panel keeps its place.
300
343
  let tab = opts.sectionTabs ? (all[opts.selected ?? 0]?.section ?? all[0]?.section ?? "") : "";
301
- const visible = () => (tab ? all.filter((row) => row.section === tab) : all);
344
+ const filter = opts.search ? new ListFilter() : undefined;
345
+ // A query searches every sub-tab, like the /pix Settings tab.
346
+ const visible = () =>
347
+ filter?.query
348
+ ? all.filter((row) => filter.matches(row.label, row.section))
349
+ : tab
350
+ ? all.filter((row) => row.section === tab)
351
+ : all;
302
352
  let rows = visible();
303
353
  let selected = Math.max(
304
354
  0,
@@ -352,6 +402,28 @@ export async function showSettingsPicker(
352
402
  const index = (row.choices ?? []).findIndex((choice) => choice.value === row.value);
353
403
  list = { row, query: "", cursor: Math.max(0, index) };
354
404
  };
405
+ // Columns the last frame drew: ←→ jump a column, ↑↓ walk down one and on into the next.
406
+ let columns = 1;
407
+ const move = (step: -1 | 1) => {
408
+ if (rows.length) selected = (selected + step + rows.length) % rows.length;
409
+ };
410
+ const moveColumn = (step: -1 | 1) => {
411
+ const perColumn = Math.ceil(rows.length / columns);
412
+ const next = selected + step * perColumn;
413
+ if (next >= 0 && next < rows.length) selected = next;
414
+ else if (step > 0) selected = rows.length - 1;
415
+ };
416
+ /** Search input open: ↑↓ move through the matches, every other key edits the query. */
417
+ const searchInput = (search: ListFilter, data: string) => {
418
+ if (keybindings.matches(data, "tui.select.up")) move(-1);
419
+ else if (keybindings.matches(data, "tui.select.down")) move(1);
420
+ else {
421
+ search.handleInput(data);
422
+ rows = visible();
423
+ selected = Math.min(selected, Math.max(0, rows.length - 1));
424
+ }
425
+ redraw();
426
+ };
355
427
  const listInput = (data: string) => {
356
428
  if (!list) return;
357
429
  const choices = listChoices();
@@ -384,14 +456,20 @@ export async function showSettingsPicker(
384
456
  render(width: number) {
385
457
  const mw = modalWidth(width);
386
458
  const tabs = tab ? sectionsOf(all) : [];
459
+ const shown =
460
+ tabs.length > 1 && !filter?.query ? rows.map((row) => ({ ...row, section: "" })) : rows;
461
+ columns = field || list ? 1 : settingsColumns(shown, mw - 4);
387
462
  const body = renderSettingsRows(
388
- tabs.length > 1 ? rows.map((row) => ({ ...row, section: "" })) : rows,
463
+ // Sub-tabs name the section; search results span sections, so they keep headings.
464
+ shown,
389
465
  theme,
390
466
  selected,
391
467
  field,
392
468
  mw - 4,
393
469
  list && { choices: listChoices(), cursor: list.cursor, query: list.query },
394
470
  );
471
+ if (filter && rows.length === 0) body.lines.push(filter.noMatch(theme));
472
+ const searchLine = filter ? [filter.line(theme, mw - 4, rows.length, all.length)] : [];
395
473
  const status = opts.status?.() ?? [];
396
474
  if (error) status.push(theme.fg("error", error));
397
475
  if (status.length) body.lines.push("", ...status);
@@ -403,8 +481,14 @@ export async function showSettingsPicker(
403
481
  titleColor: (text) => theme.fg("accent", theme.bold(text)),
404
482
  header:
405
483
  tabs.length > 1
406
- ? [renderSubTabBar(tabs, tab, theme, mw - 4), renderTabRule(theme, mw - 4)]
407
- : [""],
484
+ ? [
485
+ renderSubTabBar(tabs, tab, theme, mw - 4),
486
+ renderTabRule(theme, mw - 4),
487
+ ...searchLine,
488
+ ]
489
+ : searchLine.length
490
+ ? searchLine
491
+ : [""],
408
492
  body: body.lines,
409
493
  footer: [
410
494
  "",
@@ -414,7 +498,9 @@ export async function showSettingsPicker(
414
498
  ? "enter save · esc cancel"
415
499
  : list
416
500
  ? "type to filter · ↑↓ move · enter pick · esc cancel"
417
- : `↑↓ move · space/enter${all.some((row) => row.choices?.length) ? "/←→" : ""} change${tabs.length > 1 ? " · ` sub-tab" : ""} · esc close`,
501
+ : filter?.typing
502
+ ? "↑↓ move · enter keep · esc clear"
503
+ : `↑↓${columns > 1 ? "←→" : ""} move · space/enter${all.some((row) => row.choices?.length) ? "/←→" : ""} change${tabs.length > 1 ? " · ` sub-tab" : ""}${filter ? " · / search" : ""} · esc close`,
418
504
  ),
419
505
  ],
420
506
  selectedBodyLine: body.listLine ?? body.rowLines[selected],
@@ -430,10 +516,22 @@ export async function showSettingsPicker(
430
516
  return redraw();
431
517
  }
432
518
  if (list) return listInput(data);
433
- if (keybindings.matches(data, "tui.select.cancel")) return close(null);
519
+ if (filter?.typing) return searchInput(filter, data);
520
+ if (keybindings.matches(data, "tui.select.cancel")) {
521
+ if (!filter?.clear()) return close(null);
522
+ rows = visible();
523
+ selected = 0;
524
+ return redraw();
525
+ }
434
526
  if (busy) return;
527
+ if (filter && matchesKey(data, "/")) {
528
+ filter.open();
529
+ selected = 0;
530
+ return redraw();
531
+ }
435
532
  if (tab && isSubTabKey(data)) {
436
533
  tab = nextTab(sectionsOf(all), tab);
534
+ filter?.clear();
437
535
  rows = visible();
438
536
  selected = 0;
439
537
  return redraw();
@@ -447,6 +545,10 @@ export async function showSettingsPicker(
447
545
  else void run({ key: row.key });
448
546
  return redraw();
449
547
  }
548
+ if (columns > 1 && (matchesKey(data, Key.left) || matchesKey(data, Key.right))) {
549
+ moveColumn(matchesKey(data, Key.left) ? -1 : 1);
550
+ return redraw();
551
+ }
450
552
  if (row?.choices?.length && (matchesKey(data, Key.left) || matchesKey(data, Key.right))) {
451
553
  const at = row.choices.findIndex((choice) => choice.value === row.value);
452
554
  const step = matchesKey(data, Key.left) ? -1 : 1;
@@ -454,10 +556,8 @@ export async function showSettingsPicker(
454
556
  if (next && next.value !== row.value) void run({ key: row.key, value: next.value });
455
557
  return redraw();
456
558
  }
457
- if (keybindings.matches(data, "tui.select.up"))
458
- selected = (selected - 1 + rows.length) % rows.length;
459
- else if (keybindings.matches(data, "tui.select.down"))
460
- selected = (selected + 1) % rows.length;
559
+ if (keybindings.matches(data, "tui.select.up")) move(-1);
560
+ else if (keybindings.matches(data, "tui.select.down")) move(1);
461
561
  else return;
462
562
  redraw();
463
563
  },
package/src/renderers.ts CHANGED
@@ -2,7 +2,7 @@ import { truncateToWidth, visibleWidth, wrapTextWithAnsi } from "@earendil-works
2
2
  import { config } from "@xynogen/pix-runtime/config";
3
3
  import { prettySection } from "@xynogen/pix-runtime/sections";
4
4
 
5
- import { FG_DIM, FG_LNUM, FG_RULE, RST } from "./ansi.ts";
5
+ import { FG_DIM, FG_RULE, RST } from "./ansi.ts";
6
6
  import { MAX_PREVIEW_LINES } from "./config.ts";
7
7
  import { BODY_PAD, type ExpandHintCtx, expandHint, rule } from "./frame.ts";
8
8
  import { hlBlock } from "./highlight.ts";
@@ -10,7 +10,7 @@ import { dirIcon, fileColor, fileIcon } from "./icons.ts";
10
10
  import { lang } from "./lang.ts";
11
11
  import { type FadeTheme, fadeRowsOf, normalizeLineEndings, pluralize } from "./text.ts";
12
12
  import type { FgTheme } from "./types.ts";
13
- import { lineNumber, terminalWidth } from "./utils.ts";
13
+ import { gutterWidth, lineGutter, terminalWidth } from "./utils.ts";
14
14
 
15
15
  /** Layout controls for {@link renderFileContent}. */
16
16
  export interface RenderFileContentOptions {
@@ -49,7 +49,7 @@ export async function renderFileContent(
49
49
  const startLine = offset;
50
50
  const endLine = startLine + show.length - 1;
51
51
  const nw = Math.max(3, String(endLine).length);
52
- const gw = nw + 3; // num + " │ "
52
+ const gw = gutterWidth(nw);
53
53
  const cw = Math.max(1, tw - gw);
54
54
 
55
55
  const out: string[] = [];
@@ -62,15 +62,13 @@ export async function renderFileContent(
62
62
  // Expanded view: no tail is hidden. First row carries the line number,
63
63
  // continuation rows indent under the gutter so the code column aligns.
64
64
  const rows = wrapTextWithAnsi(code, cw);
65
- const pad = " ".repeat(nw);
66
65
  rows.forEach((rowText, r) => {
67
- const gutter = r === 0 ? lineNumber(ln, nw) : `${FG_LNUM}${pad}${RST}`;
68
- out.push(`${gutter} ${FG_RULE}│${RST} ${rowText}${RST}`);
66
+ out.push(`${lineGutter(r === 0 ? ln : "", nw)}${rowText}${RST}`);
69
67
  });
70
68
  continue;
71
69
  }
72
70
  const display = truncateToWidth(code, cw, `${FG_DIM}›`);
73
- out.push(`${lineNumber(ln, nw)} ${FG_RULE}│${RST} ${display}${RST}`);
71
+ out.push(`${lineGutter(ln, nw)}${display}${RST}`);
74
72
  }
75
73
 
76
74
  out.push(rule(tw));
package/src/utils.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { isAbsolute, relative, resolve } from "node:path";
2
2
  import { pathToFileURL } from "node:url";
3
3
  import { getCapabilities, hyperlink, truncateToWidth, visibleWidth } from "@earendil-works/pi-tui";
4
- import { ANSI_CAPTURE_RE, BG_BASE, FG_LNUM, RST } from "./ansi.ts";
4
+ import { ANSI_CAPTURE_RE, BG_BASE, FG_LNUM, FG_RULE, RST } from "./ansi.ts";
5
5
  import { MAX_PREVIEW_LINES } from "./config.ts";
6
6
  import type { TextComponentCtor, TextComponentLike } from "./types.ts";
7
7
 
@@ -290,3 +290,14 @@ export function lineNumber(n: number, w: number): string {
290
290
  const v = String(n);
291
291
  return `${FG_LNUM}${" ".repeat(Math.max(0, w - v.length))}${v}${RST}`;
292
292
  }
293
+
294
+ /** Columns taken by a numbered gutter: the number, a space, the rule, a space. */
295
+ export function gutterWidth(numberWidth: number): number {
296
+ return numberWidth + 3;
297
+ }
298
+
299
+ /** One gutter cell in front of a code row; pass "" to blank a continuation row. */
300
+ export function lineGutter(n: number | "", numberWidth: number): string {
301
+ const cell = n === "" ? `${FG_LNUM}${" ".repeat(numberWidth)}${RST}` : lineNumber(n, numberWidth);
302
+ return `${cell} ${FG_RULE}│${RST} `;
303
+ }