pi-better-background-tasks 0.11.0 → 0.12.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-better-background-tasks",
3
- "version": "0.11.0",
3
+ "version": "0.12.1",
4
4
  "description": "Pi extension for durable background shell tasks, watchers, logs, and status inspection.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -328,28 +328,115 @@ function themeFgFromTheme(theme: unknown): (color: string, value: string) => str
328
328
  return (color, value) => maybeTheme?.fg ? maybeTheme.fg(color, value) : value;
329
329
  }
330
330
 
331
- function createMainListWidget(tui: { requestRender?(): void }, theme: unknown, deps: HostDeps): Component & { dispose?(): void } {
331
+ // Structural typing keeps keyboard-only Pi hosts (no mouse API) loading this widget unchanged.
332
+ type WidgetMouseEvent = {
333
+ type: string;
334
+ button: string;
335
+ /** Coordinates local to this component, zero-based. */
336
+ x: number;
337
+ y: number;
338
+ width: number;
339
+ shift?: boolean;
340
+ alt?: boolean;
341
+ ctrl?: boolean;
342
+ };
343
+
344
+ type WidgetMouseResult = { handled: true; render: boolean };
345
+
346
+ type MainListWidget = Component & {
347
+ dispose?(): void;
348
+ handleMouse(event: WidgetMouseEvent): WidgetMouseResult | undefined;
349
+ };
350
+
351
+ const BACK_TO_MAIN = "← main (Esc)";
352
+ const BACK_TARGET = "back-to-main";
353
+
354
+ function highlightPointerTarget(theme: unknown, line: string, width: number): string {
355
+ const colors = theme as { bg?(color: string, value: string): string } | undefined;
356
+ return colors?.bg?.("selectedBg", line + " ".repeat(Math.max(0, width - visibleWidth(line))))
357
+ ?? themeFgFromTheme(theme)("accent", line);
358
+ }
359
+
360
+ function createPointerNavigation(targetAt: (event: WidgetMouseEvent) => string | undefined, activate: (target: string) => boolean) {
361
+ let hovered: string | undefined;
362
+ let pressed: { target: string; y: number } | undefined;
363
+ return {
364
+ get hovered() { return hovered; },
365
+ reset() { hovered = undefined; pressed = undefined; },
366
+ handleMouse(event: WidgetMouseEvent): WidgetMouseResult | undefined {
367
+ if (event.type === "move" || event.type === "leave") {
368
+ const target = event.type === "leave" ? undefined : targetAt(event);
369
+ const changed = hovered !== target;
370
+ hovered = target;
371
+ return changed || target ? { handled: true, render: changed } : undefined;
372
+ }
373
+ if (event.button !== "left" || event.shift || event.alt || event.ctrl) {
374
+ pressed = undefined;
375
+ return undefined;
376
+ }
377
+ if (event.type === "press") {
378
+ const target = targetAt(event);
379
+ pressed = target ? { target, y: event.y } : undefined;
380
+ return target ? { handled: true, render: false } : undefined;
381
+ }
382
+ if (event.type !== "click") return undefined;
383
+ const claim = pressed;
384
+ pressed = undefined;
385
+ if (!claim || claim.y !== event.y || targetAt(event) !== claim.target || !activate(claim.target)) return undefined;
386
+ return { handled: true, render: true };
387
+ },
388
+ };
389
+ }
390
+
391
+ function createMainListWidget(tui: { requestRender?(): void }, theme: unknown, deps: HostDeps): MainListWidget {
332
392
  const requestRender = () => tui.requestRender?.();
333
393
  state().mainListRequestRender = requestRender;
394
+ // navigatorId per rendered line, so a pointer row resolves by structure, never by row text.
395
+ let lineOwners: Array<string | undefined> = [];
396
+ let lineWidth = MAIN_LIST_FALLBACK_WIDTH;
397
+ const rowAt = (event: WidgetMouseEvent): string | undefined => {
398
+ const width = Number.isFinite(event.width) && event.width > 0 ? Math.floor(event.width) : lineWidth;
399
+ if (!Number.isFinite(event.x) || event.x < 0 || event.x >= width) return undefined;
400
+ if (!Number.isInteger(event.y) || event.y < 0 || event.y >= lineOwners.length) return undefined;
401
+ const id = lineOwners[event.y];
402
+ return id && listRows().some((row) => row.navigatorId === id) ? id : undefined;
403
+ };
404
+ const pointer = createPointerNavigation(rowAt, openNavigatorRow);
334
405
  return {
335
406
  render(width: number): string[] {
336
407
  const rows = listRows();
337
408
  syncMainListSelection(rows);
338
409
  if (!rows.length) {
339
410
  state().lastMainListLines = undefined;
411
+ lineOwners = [];
412
+ pointer.reset();
340
413
  return [];
341
414
  }
342
- const lines = ["", ...buildMainListLines(rows, renderWidth(width), deps.truncate, themeFgFromTheme(theme), {
415
+ const layout = buildMainListLayout(rows, renderWidth(width), deps.truncate, themeFgFromTheme(theme), {
343
416
  selectedId: state().mainListFocused ? state().mainListSelectedId : undefined,
344
417
  focused: state().mainListFocused === true,
345
- })];
418
+ });
419
+ lineWidth = renderWidth(width);
420
+ // The leading gap line belongs to the widget too, so its slot stays ownerless.
421
+ lineOwners = [undefined, ...layout.owners];
422
+ const lines = ["", ...layout.lines].map((line, index) =>
423
+ pointer.hovered && lineOwners[index] === pointer.hovered ? highlightPointerTarget(theme, line, lineWidth) : line);
346
424
  state().lastMainListLines = lines;
347
425
  return lines;
348
426
  },
349
- invalidate() { state().lastMainListLines = undefined; },
427
+ handleMouse(event: WidgetMouseEvent): WidgetMouseResult | undefined {
428
+ return pointer.handleMouse(event);
429
+ },
430
+ invalidate() {
431
+ state().lastMainListLines = undefined;
432
+ lineOwners = [];
433
+ pointer.reset();
434
+ },
350
435
  dispose() {
351
436
  const s = state();
352
437
  if (s.mainListRequestRender === requestRender) s.mainListRequestRender = undefined;
438
+ lineOwners = [];
439
+ pointer.reset();
353
440
  },
354
441
  };
355
442
  }
@@ -454,7 +541,26 @@ function buildMainListLines(
454
541
  fg: (color: string, value: string) => string,
455
542
  options: { selectedId?: string; focused?: boolean } = {},
456
543
  ): string[] {
544
+ return buildMainListLayout(rows, width, truncate, fg, options).lines;
545
+ }
546
+
547
+ /**
548
+ * The list's lines plus the navigatorId each line shows, so pointer input maps a rendered row to its
549
+ * stable row identity. Headers, separators and the shortcut line keep an undefined owner.
550
+ */
551
+ function buildMainListLayout(
552
+ rows: InternalRow[],
553
+ width: number,
554
+ truncate: (s: string, width: number) => string,
555
+ fg: (color: string, value: string) => string,
556
+ options: { selectedId?: string; focused?: boolean } = {},
557
+ ): { lines: string[]; owners: Array<string | undefined> } {
457
558
  const lines: string[] = [];
559
+ const owners: Array<string | undefined> = [];
560
+ const push = (line: string, owner?: string) => {
561
+ lines.push(line);
562
+ owners.push(owner);
563
+ };
458
564
  const grouped = new Map<string, InternalRow[]>();
459
565
  for (const row of rows) {
460
566
  const existing = grouped.get(row.providerLabel) ?? [];
@@ -466,16 +572,16 @@ function buildMainListLines(
466
572
  for (let i = 0; i < orderedLabels.length; i += 1) {
467
573
  const label = orderedLabels[i]!;
468
574
  const group = grouped.get(label)!;
469
- if (i > 0) lines.push("");
470
- lines.push(providerGroupLabel(label, fg));
575
+ if (i > 0) push("");
576
+ push(providerGroupLabel(label, fg));
471
577
  for (const row of group) {
472
578
  const selected = options.focused && row.navigatorId === options.selectedId;
473
- lines.push(formatMainListRow(row, selected === true, fg, width));
579
+ push(formatMainListRow(row, selected === true, fg, width), row.navigatorId);
474
580
  }
475
581
  }
476
- lines.push("");
477
- lines.push(shortcutsLine(options.focused === true, fg));
478
- return lines.map((line) => safeTruncate(line, width, truncate));
582
+ push("");
583
+ push(shortcutsLine(options.focused === true, fg));
584
+ return { lines: lines.map((line) => safeTruncate(line, width, truncate)), owners };
479
585
  }
480
586
 
481
587
  function shortcutsLine(focused: boolean, fg: (color: string, value: string) => string): string {
@@ -766,6 +872,20 @@ function handleMainListCloseKey(): void {
766
872
  refreshMainListWidget();
767
873
  }
768
874
 
875
+ /**
876
+ * Open the shared detail overlay on the row a pointer chose. Setting the selection first keeps the
877
+ * keyboard path identical: Enter, x and the overlay's own keys then act on the clicked row.
878
+ */
879
+ function openNavigatorRow(navigatorId: string): boolean {
880
+ const rows = listRows();
881
+ if (!rows.some((row) => row.navigatorId === navigatorId)) return false;
882
+ clearMainListCloseArm();
883
+ state().mainListSelectedId = navigatorId;
884
+ refreshMainListWidget();
885
+ openNavigator();
886
+ return true;
887
+ }
888
+
769
889
  function openNavigator(): void {
770
890
  const s = state();
771
891
  const ctx = s.uiCtx;
@@ -861,6 +981,22 @@ function createOverlayComponent(
861
981
  let closed = false;
862
982
  let transcriptDetail: BackgroundWorkDetail | null = null;
863
983
  let transcriptComponent: Component | null = null;
984
+ let pointerTargets: Array<string | undefined> = [];
985
+ let pointerWidth = 0;
986
+ const pointer = createPointerNavigation((event) => {
987
+ if (closed || !Number.isInteger(event.y) || !Number.isFinite(event.x) || event.x < 0 || event.x >= pointerWidth) return undefined;
988
+ const target = pointerTargets[event.y];
989
+ if (target === BACK_TARGET) return event.x >= 3 && event.x < 3 + visibleWidth(BACK_TO_MAIN) ? target : undefined;
990
+ return target && listRows().some((row) => row.navigatorId === target) ? target : undefined;
991
+ }, (target) => {
992
+ if (target === BACK_TARGET) { backToMain(); return true; }
993
+ refreshRows();
994
+ const index = overlayState.rows.findIndex((row) => row.navigatorId === target);
995
+ if (index < 0) return false;
996
+ selectOverlayRow(index);
997
+ openDetail();
998
+ return true;
999
+ });
864
1000
 
865
1001
  const fg = (color: string, value: string) => theme?.fg ? theme.fg(color, value) : value;
866
1002
 
@@ -939,6 +1075,11 @@ function createOverlayComponent(
939
1075
  try { done(null); } catch { /* ignore */ }
940
1076
  }
941
1077
 
1078
+ function backToMain(): void {
1079
+ unfocusMainList();
1080
+ close();
1081
+ }
1082
+
942
1083
  function handleCloseKey(): void {
943
1084
  const row = selectedRow();
944
1085
  if (!row || row.parentRow) return;
@@ -1004,14 +1145,17 @@ function createOverlayComponent(
1004
1145
  render(width: number) {
1005
1146
  closeIfEditorReplaced();
1006
1147
  refreshRows();
1007
- const railLines = buildMainListLines(overlayState.rows, width, deps.truncate, fg, {
1148
+ const rail = buildMainListLayout(overlayState.rows, width, deps.truncate, fg, {
1008
1149
  selectedId: selectedRow()?.navigatorId,
1009
1150
  focused: true,
1010
1151
  });
1152
+ const railLines = rail.lines;
1011
1153
  const editorLines = renderEditorLines(width);
1012
1154
  const bottomLines = [...railLines, ...editorLines];
1013
1155
  const overlayRows = state().detailOverlayRows;
1014
1156
  const detailRows = overlayRows === undefined ? undefined : Math.max(1, overlayRows - bottomLines.length);
1157
+ const backControl = pointer.hovered === BACK_TARGET
1158
+ ? highlightPointerTarget(theme, BACK_TO_MAIN, visibleWidth(BACK_TO_MAIN)) : undefined;
1015
1159
  let contentLines: string[];
1016
1160
  if (detail?.transcript && deps.createTranscriptComponent) {
1017
1161
  if (transcriptDetail !== detail || !transcriptComponent) {
@@ -1024,18 +1168,30 @@ function createOverlayComponent(
1024
1168
  width,
1025
1169
  deps.truncate,
1026
1170
  fg,
1027
- { minRows: detailRows, maxRows: detailRows, bottomFooter: false, logTailRows },
1171
+ { minRows: detailRows, maxRows: detailRows, bottomFooter: false, logTailRows, backControl },
1028
1172
  );
1029
1173
  } else {
1030
1174
  transcriptDetail = null;
1031
1175
  transcriptComponent = null;
1032
- contentLines = buildDetailLines(detail, width, deps.truncate, fg, { expandedSections, logTailRows, minRows: detailRows, maxRows: detailRows, bottomFooter: false });
1176
+ contentLines = buildDetailLines(detail, width, deps.truncate, fg, { expandedSections, logTailRows, minRows: detailRows, maxRows: detailRows, bottomFooter: false, backControl });
1033
1177
  }
1034
- if (overlayRows === undefined || detailRows === undefined) return [...contentLines, ...bottomLines];
1035
- const fittedContent = contentLines.slice(0, detailRows);
1036
- while (fittedContent.length < detailRows) fittedContent.push("");
1037
- // A terminal shorter than rail + editor keeps the bottom rows (the input frame) and drops the top.
1038
- return [...fittedContent, ...bottomLines].slice(-Math.max(1, overlayRows));
1178
+ const fittedContent = detailRows === undefined ? contentLines : contentLines.slice(0, detailRows);
1179
+ if (detailRows !== undefined) while (fittedContent.length < detailRows) fittedContent.push("");
1180
+ const contentTargets = fittedContent.map((_, index) => index === 1 ? BACK_TARGET : undefined);
1181
+ const allLines = [...fittedContent, ...bottomLines];
1182
+ const allTargets = [...contentTargets, ...rail.owners, ...editorLines.map(() => undefined)];
1183
+ // Match hit targets to the same viewport clipping as the visible lines.
1184
+ const start = overlayRows === undefined ? 0 : Math.max(0, allLines.length - Math.max(1, overlayRows));
1185
+ pointerTargets = allTargets.slice(start);
1186
+ pointerWidth = renderWidth(width);
1187
+ return allLines.slice(start).map((line, index) => {
1188
+ if (!pointer.hovered || pointerTargets[index] !== pointer.hovered) return line;
1189
+ if (pointer.hovered === BACK_TARGET) return line;
1190
+ return highlightPointerTarget(theme, line, width);
1191
+ });
1192
+ },
1193
+ handleMouse(event: WidgetMouseEvent): WidgetMouseResult | undefined {
1194
+ return pointer.handleMouse(event);
1039
1195
  },
1040
1196
  handleInput(data: string) {
1041
1197
  if (closed) return;
@@ -1054,7 +1210,7 @@ function createOverlayComponent(
1054
1210
  else if (deps.matchKey(data, "left")) {
1055
1211
  const mainIdx = overlayState.rows.findIndex((row) => row.parentRow && row.id === "main");
1056
1212
  if (mainIdx >= 0) selectOverlayRow(mainIdx);
1057
- close();
1213
+ backToMain();
1058
1214
  }
1059
1215
  else if (deps.matchKey(data, "enter")) {
1060
1216
  const row = selectedRow();
@@ -1082,8 +1238,7 @@ function createOverlayComponent(
1082
1238
  requestRender();
1083
1239
  }
1084
1240
  else if (deps.matchKey(data, "escape")) {
1085
- unfocusMainList();
1086
- close();
1241
+ backToMain();
1087
1242
  }
1088
1243
  },
1089
1244
  /** Show another row's detail in this overlay (the main list moved while it was unfocused). */
@@ -1098,8 +1253,10 @@ function createOverlayComponent(
1098
1253
  },
1099
1254
  /** Close the overlay (done), e.g. when the navigator is disposed. */
1100
1255
  dismiss() { close(); },
1101
- invalidate() { transcriptComponent?.invalidate(); },
1256
+ invalidate() { pointer.reset(); pointerTargets = []; transcriptComponent?.invalidate(); },
1102
1257
  dispose() {
1258
+ pointer.reset();
1259
+ pointerTargets = [];
1103
1260
  clearCloseArm();
1104
1261
  detailScheduler.dispose();
1105
1262
  },
@@ -1136,14 +1293,14 @@ function buildTranscriptDetailLines(
1136
1293
  width: number,
1137
1294
  truncate: (s: string, width: number) => string,
1138
1295
  fg: (color: string, value: string) => string,
1139
- options: { minRows?: number; maxRows?: number; bottomFooter?: boolean; logTailRows?: number } = {},
1296
+ options: { minRows?: number; maxRows?: number; bottomFooter?: boolean; logTailRows?: number; backControl?: string } = {},
1140
1297
  ): string[] {
1141
- const actions = [...(detail.footerActions ?? ["x close"]), "Esc close"].join(" · ");
1298
+ const actions = [...(detail.footerActions ?? ["x close"])].join(" · ");
1142
1299
  const tailRows = options.logTailRows ?? DEFAULT_LOG_TAIL_ROWS;
1143
1300
  let shownTranscriptLines = transcriptLines.length ? transcriptLines.slice(-tailRows) : [" (no transcript yet)"];
1144
1301
  const lines: string[] = [
1145
1302
  fg("accent", rule(detail.title, width)),
1146
- dim(` ← main · ${actions}`, fg),
1303
+ ` ${options.backControl ?? fg("accent", BACK_TO_MAIN)}${dim(` · ${actions}`, fg)}`,
1147
1304
  "",
1148
1305
  ` status ${fg(toneColor(detail.statusTone, detail.status), detail.status)}`,
1149
1306
  ];
@@ -1167,7 +1324,7 @@ function buildTranscriptDetailLines(
1167
1324
  lines.push(...shownTranscriptLines);
1168
1325
  lines.push("");
1169
1326
  if (options.bottomFooter === false) return lines.map((line) => safeTruncate(line, width, truncate));
1170
- const footerLines = [dim(` ← main · ${actions}`, fg), dim(rule("", width), fg)];
1327
+ const footerLines = [dim(` ${BACK_TO_MAIN} · ${actions}`, fg), dim(rule("", width), fg)];
1171
1328
  padBeforeFooter(lines, footerLines.length, options.minRows);
1172
1329
  lines.push(...footerLines);
1173
1330
  return lines.map((line) => safeTruncate(line, width, truncate));
@@ -1223,10 +1380,10 @@ function buildDetailLines(
1223
1380
  width: number,
1224
1381
  truncate: (s: string, width: number) => string,
1225
1382
  fg: (color: string, value: string) => string,
1226
- options: { expandedSections?: Set<string>; logTailRows?: number; minRows?: number; maxRows?: number; bottomFooter?: boolean } = {},
1383
+ options: { expandedSections?: Set<string>; logTailRows?: number; minRows?: number; maxRows?: number; bottomFooter?: boolean; backControl?: string } = {},
1227
1384
  ): string[] {
1228
1385
  if (!detail) {
1229
- const lines = [rule("Work unavailable", width), dim(" ← back · Esc close", fg), ""];
1386
+ const lines = [rule("Work unavailable", width), ` ${options.backControl ?? fg("accent", BACK_TO_MAIN)}`, ""];
1230
1387
  if (options.bottomFooter === false) return lines.map((line) => safeTruncate(line, width, truncate));
1231
1388
  const footerLines = [dim(rule("", width), fg)];
1232
1389
  padBeforeFooter(lines, footerLines.length, options.minRows);
@@ -1239,8 +1396,8 @@ function buildDetailLines(
1239
1396
  const foldedAction = toggleableSectionId
1240
1397
  ? (options.expandedSections?.has(toggleableSectionId) ? "Enter collapse" : "Enter expand")
1241
1398
  : null;
1242
- const actions = [foldedAction, ...(detail.footerActions?.length ? detail.footerActions : ["x close"]), "l 10/25", "Esc close"].filter(Boolean).join(" · ");
1243
- lines.push(dim(` ← back · ${actions}`, fg));
1399
+ const actions = [foldedAction, ...(detail.footerActions?.length ? detail.footerActions : ["x close"]), "l 10/25"].filter(Boolean).join(" · ");
1400
+ lines.push(` ${options.backControl ?? fg("accent", BACK_TO_MAIN)}${dim(` · ${actions}`, fg)}`);
1244
1401
  lines.push("");
1245
1402
  lines.push(` status ${fg(toneColor(detail.statusTone, detail.status), detail.status)}`);
1246
1403
  if (detail.subtitle) lines.push(` summary ${detail.subtitle}`);
@@ -1296,7 +1453,7 @@ function buildDetailLines(
1296
1453
  }
1297
1454
  lines.push("");
1298
1455
  if (options.bottomFooter === false) return lines.map((line) => safeTruncate(line, width, truncate));
1299
- const footerLines = [dim(` ← back · ${actions}`, fg), dim(rule("", width), fg)];
1456
+ const footerLines = [dim(` ${BACK_TO_MAIN} · ${actions}`, fg), dim(rule("", width), fg)];
1300
1457
  padBeforeFooter(lines, footerLines.length, options.minRows);
1301
1458
  lines.push(...footerLines);
1302
1459
  return lines.map((line) => safeTruncate(line, width, truncate));