pi-stereo-view 0.1.22 → 0.1.24

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/src/native.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { Theme, ToolInfo } from "@earendil-works/pi-coding-agent";
2
- import { Container, Spacer, Text, type Component, type TuiMouseEvent } from "@earendil-works/pi-tui";
2
+ import { Container, Spacer, stripTerminalSequences, Text, type Color, type Component, type TuiMouseEvent } from "@earendil-works/pi-tui";
3
3
  import { searchText, type SearchEntry, type SearchHit } from "./search.ts";
4
4
  import { highlightSearchRows, searchHitRow, type SearchClock } from "./search-highlight.ts";
5
5
  import { CompactMessage, type TranscriptTimestamps } from "./timestamps.ts";
@@ -11,8 +11,32 @@ import { toolGroups, groupSummary, type ToolGroup } from "./tool-groups.ts";
11
11
  import { ToolIndent } from "./tool-indent.ts";
12
12
  import { NestedTree, type NativeTreeBody } from "./nested-tree.ts";
13
13
  import { pushShellInputs, sameValues } from "./render-cache.ts";
14
+ import { decorateLine, decorateLines, type TurnColor, type TurnLayers, type TurnMix } from "./turns.ts";
15
+ import type { TurnStripe, GoTo } from "./preferences.ts";
14
16
  export type { Pane } from "./content.ts";
15
17
 
18
+ /** Turn marks, hover and Go to, shared by both panels through the split layout. */
19
+ export interface DocumentTurns {
20
+ /** Whether each block's clock gets its turn's background. */
21
+ clock(): boolean;
22
+ stripe(): TurnStripe;
23
+ /** Whether default cells of every block get their turn's background. */
24
+ background(): boolean;
25
+ /** Whether a rule marks the first block of each turn. */
26
+ separator(): boolean;
27
+ /** How strongly each mark mixes its turn's hue into the background. */
28
+ mix(): TurnMix;
29
+ /** The colors of a turn, mixed into the terminal background; none before the first turn. */
30
+ color(turn: number, layers?: TurnLayers): TurnColor | undefined;
31
+ /** The turn under the pointer, while hover highlighting is on and available. */
32
+ hovered(): number | undefined;
33
+ /** Report the turn under the pointer during a pointer move; 0 for a block before the first turn. */
34
+ hover(turn: number): void;
35
+ goTo(): GoTo;
36
+ /** Open the same point of history in the other panel. */
37
+ jump(from: Pane, source: Component): void;
38
+ }
39
+
16
40
  /** Decorate one selected block without changing native text or mouse coordinates. */
17
41
  class SearchHighlight implements Component {
18
42
  private cache?: { lines: string[]; result: string[]; width: number; query: string; hit: SearchHit; revision: number };
@@ -59,11 +83,26 @@ const FULL_TOOLS: ToolDisplay = { compact: false, group: false };
59
83
 
60
84
  /** Time per frame for measuring offscreen entries that are new, changed or stale. */
61
85
  const LAZY_BUDGET_MS = 4;
86
+
87
+ /** A rule in a turn's color in place of the blank row between the previous turn and the turn's first block in a panel. */
88
+ class TurnRule implements Component {
89
+ constructor(private readonly color: () => string | undefined) {}
90
+ render(width: number): string[] {
91
+ const color = this.color();
92
+ const rule = "─".repeat(Math.max(0, width));
93
+ return [color ? color + rule + "\x1b[39m" : rule];
94
+ }
95
+ invalidate(): void {}
96
+ }
97
+
62
98
  /** Offscreen entries checked for native changes per frame, in rotation. */
63
99
  const SWEEP_ENTRIES = 64;
64
100
 
65
101
  /** One projected entry's rows, the components that produced them, and the inputs they depend on. */
66
102
  interface Segment {
103
+ source: Component;
104
+ /** The entry's turn when the segment was last placed, so pointer moves need not read the index. */
105
+ turn: number;
67
106
  components: Component[];
68
107
  heights: number[];
69
108
  lines: string[];
@@ -73,6 +112,8 @@ interface Segment {
73
112
  estimate: boolean;
74
113
  visibleIn: boolean;
75
114
  toolRunIn: boolean;
115
+ /** The turn above the separator rule this segment starts with; undefined without one. */
116
+ ruleIn?: number;
76
117
  visibleOut: boolean;
77
118
  toolRunOut: boolean;
78
119
  /** Cheap fingerprint of native state, compared while the entry stays offscreen. */
@@ -130,7 +171,17 @@ export class NativeDocument extends Container {
130
171
  private searchQuery = "";
131
172
  private highlightRevision = 0;
132
173
  private highlights = new WeakMap<Component, SearchHighlight>();
174
+ /** One separator rule per turn; it reads the turn's current color when it renders. */
175
+ private readonly rules = new Map<number, TurnRule>();
133
176
  private lastWidth = 1;
177
+ /** Width of the last render, including the turn stripe. */
178
+ private lastOuter = 1;
179
+ /** Columns the turn stripe takes before the content. */
180
+ private inset = 0;
181
+ /** Measure every entry on the next render, to locate a block for Go to. */
182
+ private measureAll = false;
183
+ /** Every block's turn, read once at the start of the current render. */
184
+ private renderActs?: ReadonlyMap<Component, number>;
134
185
  private frameActive = false;
135
186
  private frameWidth?: number;
136
187
  private frameLines?: string[];
@@ -171,6 +222,7 @@ export class NativeDocument extends Container {
171
222
  private readonly beforeExpand: () => void = () => {},
172
223
  private readonly buttonStyle: () => "icons" | "text" = () => "text",
173
224
  private readonly streaming?: { enabled(): boolean; delay(): number; changed(): void },
225
+ private readonly turns?: DocumentTurns,
174
226
  ) {
175
227
  super();
176
228
  if (streaming) this.streamer = new ToolStreaming(() => streaming.delay(), () => streaming.changed());
@@ -304,7 +356,7 @@ export class NativeDocument extends Container {
304
356
  searchRow(hit: SearchHit): number | undefined {
305
357
  // Async regex can complete between paints. Measure this document at its last
306
358
  // allocated width instead of assuming requestRender already emitted a frame.
307
- if (this.searchAnchor === hit && this.frameWidth === undefined) this.render(this.lastWidth);
359
+ if (this.searchAnchor === hit && this.frameWidth === undefined) this.render(this.lastOuter);
308
360
  const block = this.positions.get(hit.entry.source);
309
361
  if (!block) return undefined;
310
362
  if (block.excerptRow !== undefined) return block.excerptRow;
@@ -327,7 +379,14 @@ export class NativeDocument extends Container {
327
379
  override handleMouse(event: TuiMouseEvent): ReturnType<Container["handleMouse"]> {
328
380
  if (event.type === "wheel" || (event.type === "press" && event.button === "left")) this.interact();
329
381
  const built = this.built;
330
- if (!built || event.y < 0) return undefined;
382
+ if (!built || event.y < 0) {
383
+ if (event.type === "move") this.turns?.hover(0);
384
+ return undefined;
385
+ }
386
+ if (this.inset) {
387
+ if (event.x < this.inset && event.type !== "move") return undefined;
388
+ event = { ...event, x: event.x - this.inset, width: Math.max(0, event.width - this.inset) };
389
+ }
331
390
  // Find the entry under the pointer, then route like a container of that entry's components.
332
391
  let low = 0;
333
392
  let high = built.order.length - 1;
@@ -337,6 +396,12 @@ export class NativeDocument extends Container {
337
396
  }
338
397
  const segment = built.order[low];
339
398
  const row = event.y - (built.rows[low] ?? 0);
399
+ if (event.type === "move") {
400
+ // A separator stands between turns, so pointing at it highlights neither.
401
+ const separator = segment?.components[0] instanceof TurnRule && row < segment.heights[0];
402
+ const turn = !segment || row >= segment.lines.length || separator ? 0 : segment.turn;
403
+ this.turns?.hover(turn);
404
+ }
340
405
  if (!segment || segment.estimate || row >= segment.lines.length) return undefined;
341
406
  const entry = {
342
407
  children: segment.components,
@@ -344,6 +409,55 @@ export class NativeDocument extends Container {
344
409
  };
345
410
  return Container.prototype.handleMouse.call(entry, { ...event, y: row, height: segment.lines.length });
346
411
  }
412
+ /**
413
+ * The turn color behind a block's clock, when clock marks are on. Blocks render inside this document's render, so the turns it
414
+ * read are current; reading the index per block would verify the whole history per block outside a frame.
415
+ */
416
+ private clockBackground(source: Component): Color | undefined {
417
+ if (!this.turns?.clock()) return undefined;
418
+ const turn = this.renderActs?.get(source) ?? 0;
419
+ return this.turns.color(turn, this.layers(turn))?.clock;
420
+ }
421
+ /** The layers under a turn's clocks, stripes and rules in this frame: its background, and hover while pointed at. */
422
+ private layers(turn: number): TurnLayers {
423
+ return { background: this.turns?.background() === true, hover: turn > 0 && this.turns?.hovered() === turn };
424
+ }
425
+ /** Decorate a separator's row: it stands between turns, so it takes no background or hover layer of either. */
426
+ private separatorRows(rows: readonly string[], turn: number,
427
+ mark: (marks: TurnColor | undefined, fill: string | undefined) => string): string[] {
428
+ const marks = this.turns?.color(turn);
429
+ return rows.map(row => decorateLine(row, undefined, mark(marks, undefined)));
430
+ }
431
+ private goToClock(): boolean {
432
+ const goTo = this.turns?.goTo();
433
+ return goTo === "clock" || goTo === "both";
434
+ }
435
+ /** Clicking a compact row's clock opens its point of history in the other panel. */
436
+ private clockJump(source: Component): (() => void) | undefined {
437
+ return this.goToClock() ? () => this.turns?.jump(this.pane, source) : undefined;
438
+ }
439
+
440
+ /**
441
+ * The first row of a block in this panel, measured from every entry. A block hidden in a closed group or shown
442
+ * inside its owner's tree resolves to the nearest shown row above it.
443
+ */
444
+ locate(source: Component): number | undefined {
445
+ if (this.disposed) return undefined;
446
+ this.measureAll = true;
447
+ this.frameWidth = undefined;
448
+ try { this.render(this.lastOuter); } finally { this.measureAll = false; }
449
+ const entries = this.index.entries(this.pane);
450
+ for (let index = entries.findIndex(entry => entry.source === source); index >= 0; index--) {
451
+ const segment = this.segments.get(entries[index].source);
452
+ if (segment?.build !== this.buildId || !segment.lines.length) continue;
453
+ // Start at the block's first visible row, after its separator.
454
+ let row = 0;
455
+ while (row < segment.lines.length - 1 && !stripTerminalSequences(segment.lines[row]).trim()) row++;
456
+ return segment.start + row;
457
+ }
458
+ return undefined;
459
+ }
460
+
347
461
  private toolOpen(source: Component): boolean {
348
462
  return this.openTools.has(source) || this.searchTool === source;
349
463
  }
@@ -359,6 +473,8 @@ export class NativeDocument extends Container {
359
473
  }, this.theme, {
360
474
  clock: () => this.showTimestamps() ? this.timestamps.label(entry.source) : "",
361
475
  gutter: this.timestampGutter,
476
+ clockBackground: () => this.clockBackground(entry.source),
477
+ clockAction: () => this.clockJump(entry.source),
362
478
  expanded: () => this.toolOpen(entry.source),
363
479
  toggle: () => {
364
480
  if (!this.index.contains(entry.source, this.pane)) return;
@@ -387,6 +503,8 @@ export class NativeDocument extends Container {
387
503
  count: () => this.groups.get(source)!.calls.length,
388
504
  clock: () => this.showTimestamps() ? this.timestamps.label(source) : "",
389
505
  gutter: this.timestampGutter,
506
+ clockBackground: () => this.clockBackground(source),
507
+ clockAction: () => this.clockJump(source),
390
508
  expanded: () => this.groupOpen(this.groups.get(source)!),
391
509
  toggle: () => {
392
510
  const current = this.groups.get(source);
@@ -412,6 +530,10 @@ export class NativeDocument extends Container {
412
530
  this.copy ? () => this.copyBlock(component) : undefined, this.showCopy, this.buttonStyle, () => {
413
531
  // Nested panels render inside their owner's segment.
414
532
  this.dirty.add(component).add(entry.owner);
533
+ }, () => this.clockBackground(component), {
534
+ clock: () => this.goToClock(),
535
+ button: () => { const goTo = this.turns?.goTo(); return goTo === "button" || goTo === "both"; },
536
+ go: () => this.turns?.jump(this.pane, component),
415
537
  });
416
538
  this.stamped.set(routed, stamped);
417
539
  }
@@ -504,8 +626,20 @@ export class NativeDocument extends Container {
504
626
  return trees;
505
627
  }
506
628
 
507
- override render(width: number): string[] {
508
- if (this.frameActive && this.frameWidth === width && this.frameLines) return this.frameLines;
629
+ override render(outer: number): string[] {
630
+ if (this.frameActive && this.frameWidth === outer && this.frameLines) return this.frameLines;
631
+ const stripe = this.turns?.stripe() ?? "off";
632
+ const tint = this.turns?.background() === true;
633
+ const separator = this.turns?.separator() === true;
634
+ // The turn stripe takes the first column; native blocks render beside it.
635
+ const inset = stripe !== "off" && outer > 1 ? 1 : 0;
636
+ const width = outer - inset;
637
+ this.inset = inset;
638
+ this.lastOuter = outer;
639
+ const theme = this.theme();
640
+ const hovered = this.turns?.hovered();
641
+ const turns = this.turns ? this.index.turnMap() : undefined;
642
+ this.renderActs = turns;
509
643
  const chat = this.source.children[2] as Container;
510
644
  this.timestamps.prepare(chat.children);
511
645
  this.lastWidth = width;
@@ -528,11 +662,14 @@ export class NativeDocument extends Container {
528
662
  this.searchTool = searched?.owner;
529
663
  const entries = this.index.entries(this.pane);
530
664
  const trees = this.treesFor(entries);
531
- const view = locateMatch ? undefined : this.viewport?.();
665
+ const view = locateMatch || this.measureAll ? undefined : this.viewport?.();
532
666
  // The first frame and search measure every entry; later frames reuse offscreen rows.
533
667
  const virtual = view !== undefined && view.height > 0 && this.built !== undefined;
534
- const presentation = [this.theme(), display.compact, display.group, this.index.nestedTrees(), this.streamActive, this.showTimestamps(),
535
- this.showCopy(), this.buttonStyle(), this.timestampGutter(), this.invalidations];
668
+ const mix = this.turns?.mix();
669
+ const hoverColor = mix?.hoverColor && `${mix.hoverColor.r},${mix.hoverColor.g},${mix.hoverColor.b}`;
670
+ const presentation = [theme, stripe, tint, separator, this.turns?.clock(), mix?.clock, mix?.stripe, mix?.background, mix?.hover, hoverColor,
671
+ display.compact, display.group, this.index.nestedTrees(), this.streamActive, this.showTimestamps(),
672
+ this.showCopy(), this.buttonStyle(), this.turns?.goTo(), this.timestampGutter(), this.invalidations];
536
673
  if (!sameValues(presentation, this.presentation)) {
537
674
  this.presentation = presentation;
538
675
  this.epoch++;
@@ -553,13 +690,24 @@ export class NativeDocument extends Container {
553
690
  const top = virtual && !view.following ? view.top : 0;
554
691
  let shift = 0;
555
692
  let pending = false;
693
+ let lastAct = 0;
556
694
  const order: Segment[] = [];
557
- const renderEntry = (entry: ContentEntry, rowed: boolean, tree: NestedTree | undefined): Segment => {
695
+ // `rule` is the turn above the separator this entry starts with, when it starts one.
696
+ const renderEntry = (entry: ContentEntry, rowed: boolean, tree: NestedTree | undefined, turn: number, rule: number | undefined): Segment => {
558
697
  const component = entry.source;
559
698
  const routed = entry.component;
560
699
  const result: Component[] = [];
561
700
  const visibleIn = visibleBlock;
562
701
  const toolRunIn = toolRun;
702
+ if (rule !== undefined) {
703
+ let turnRule = this.rules.get(turn);
704
+ // The rule stands between turns, outside the layers of either: hover and backgrounds leave it unchanged.
705
+ if (!turnRule) this.rules.set(turn, turnRule = new TurnRule(() => this.turns?.color(turn)?.line));
706
+ result.push(turnRule);
707
+ // The rule takes the place of the blank row that separates blocks, so the block below adds none.
708
+ toolRun = false;
709
+ visibleBlock = false;
710
+ }
563
711
  let clock: SearchClock | undefined;
564
712
  let nativeBody = true;
565
713
  let bodyDepth = 0;
@@ -596,7 +744,8 @@ export class NativeDocument extends Container {
596
744
  visibleBlock = true;
597
745
  } else if (nativeBody && !entry.compact) {
598
746
  toolRun = false;
599
- result.push(...entry.spacing, routed);
747
+ // Pi's own spacing separates this block from the one above; a rule already does.
748
+ result.push(...(rule === undefined ? entry.spacing : []), routed);
600
749
  } else if (nativeBody) {
601
750
  if (!rowed) toolRun = false;
602
751
  const stamped = this.nativeBlock(entry);
@@ -637,8 +786,16 @@ export class NativeDocument extends Container {
637
786
  const heights: number[] = [];
638
787
  // Copy rows: native renderers may reuse and later mutate their row arrays.
639
788
  const lines: string[] = [];
789
+ const color = turn && (inset || tint || hovered === turn) ? this.turns?.color(turn, { background: tint, hover: hovered === turn }) : undefined;
790
+ const background = color?.fill;
791
+ // A line stripe draws on the block's layers; a block stripe is a layer of its own.
792
+ const mark = (marks: TurnColor | undefined, fill: string | undefined): string =>
793
+ !inset ? "" : !marks ? " " : stripe === "line" ? (fill ?? "") + marks.line + "│\x1b[39m" : marks.stripe + " \x1b[49m";
794
+ const prefix = mark(color, background);
640
795
  for (const child of result) {
641
- const childLines = child.render(width);
796
+ const childLines = child instanceof TurnRule && rule !== undefined
797
+ ? this.separatorRows(child.render(width), turn, mark)
798
+ : decorateLines(child.render(width), background, prefix);
642
799
  heights.push(childLines.length);
643
800
  for (const line of childLines) lines.push(line);
644
801
  }
@@ -646,15 +803,15 @@ export class NativeDocument extends Container {
646
803
  this.fingerprint(entry, rowed, tree, print);
647
804
  let segment = this.segments.get(component);
648
805
  if (!segment) {
649
- segment = { components: result, heights, lines, width, epoch: this.epoch, estimate: false, visibleIn, toolRunIn,
650
- visibleOut: visibleBlock, toolRunOut: toolRun, print, start: row, build };
806
+ segment = { source: component, turn, components: result, heights, lines, width, epoch: this.epoch, estimate: false, visibleIn, toolRunIn,
807
+ ruleIn: rule, visibleOut: visibleBlock, toolRunOut: toolRun, print, start: row, build };
651
808
  this.segments.set(component, segment);
652
809
  return segment;
653
810
  }
654
811
  // Keep unchanged arrays, so the document can reuse the rows before the first changed entry.
655
812
  if (segment.estimate || !sameValues(segment.lines, lines)) segment.lines = lines;
656
813
  if (!sameValues(segment.components, result)) segment.components = result;
657
- Object.assign(segment, { heights, width, epoch: this.epoch, estimate: false, visibleIn, toolRunIn,
814
+ Object.assign(segment, { heights, width, epoch: this.epoch, estimate: false, visibleIn, toolRunIn, ruleIn: rule,
658
815
  visibleOut: visibleBlock, toolRunOut: toolRun, print });
659
816
  return segment;
660
817
  };
@@ -664,6 +821,9 @@ export class NativeDocument extends Container {
664
821
  const component = entry.source;
665
822
  const rowed = display.compact && entry.kind === "tool" && !this.live(entry.owner);
666
823
  const tree = trees.get(component);
824
+ const turn = turns?.get(component) ?? 0;
825
+ // A rule marks where a new turn starts in this panel, below other content.
826
+ const rule = separator && turn > 0 && turn !== lastAct && visibleBlock ? lastAct : undefined;
667
827
  let segment = this.segments.get(component);
668
828
  const previous = segment?.build === build - 1 ? { start: segment.start, height: segment.lines.length } : undefined;
669
829
  let render = !virtual;
@@ -675,7 +835,7 @@ export class NativeDocument extends Container {
675
835
  if (hot) render = true;
676
836
  else {
677
837
  const measured = segment !== undefined && !segment.estimate && segment.width === width && segment.epoch === this.epoch &&
678
- segment.visibleIn === visibleBlock && segment.toolRunIn === toolRun;
838
+ segment.visibleIn === visibleBlock && segment.toolRunIn === toolRun && segment.ruleIn === rule;
679
839
  // Offscreen changes are invisible until the entry nears the viewport, where it renders anyway.
680
840
  // Check a rotating slice per frame so heights converge without reading every entry each frame.
681
841
  const swept = this.dirty.delete(component) ||
@@ -692,12 +852,12 @@ export class NativeDocument extends Container {
692
852
  }
693
853
  if (render) {
694
854
  this.dirty.delete(component);
695
- segment = renderEntry(entry, rowed, tree);
855
+ segment = renderEntry(entry, rowed, tree, turn, rule);
696
856
  }
697
857
  else {
698
858
  if (!segment) {
699
- segment = { components: [], heights: [], lines: blankLines(1), width, epoch: -1, estimate: true, visibleIn: visibleBlock,
700
- toolRunIn: toolRun, visibleOut: visibleBlock, toolRunOut: toolRun, print: [], start: row, build };
859
+ segment = { source: component, turn: 0, components: [], heights: [], lines: blankLines(1), width, epoch: -1, estimate: true, visibleIn: visibleBlock,
860
+ toolRunIn: toolRun, ruleIn: undefined, visibleOut: visibleBlock, toolRunOut: toolRun, print: [], start: row, build };
701
861
  this.segments.set(component, segment);
702
862
  } else if (segment.width !== width && !segment.estimate) {
703
863
  // Rows at another width would overflow the panel; keep only the height until measured.
@@ -713,6 +873,8 @@ export class NativeDocument extends Container {
713
873
  if (virtual && !view.following && previous && previous.start + previous.height <= top) shift += segment.lines.length - previous.height;
714
874
  segment.start = row;
715
875
  segment.build = build;
876
+ segment.turn = turn;
877
+ if (segment.lines.length) lastAct = turn;
716
878
  order.push(segment);
717
879
  row += segment.lines.length;
718
880
  }
@@ -721,6 +883,9 @@ export class NativeDocument extends Container {
721
883
  this.segmentsFor = entries;
722
884
  for (const [source, segment] of this.segments) if (segment.build !== build) this.segments.delete(source);
723
885
  for (const source of this.dirty) if (!this.segments.has(source)) this.dirty.delete(source);
886
+ // Separators of turns that left the history (a new session, compaction) are not needed any more.
887
+ const present = new Set(order.map(segment => segment.turn));
888
+ for (const turn of this.rules.keys()) if (!present.has(turn)) this.rules.delete(turn);
724
889
  }
725
890
  if (virtual && !view.following) this.scrollShift += shift;
726
891
  let lines = built.document;
@@ -731,7 +896,7 @@ export class NativeDocument extends Container {
731
896
  children = [...children, new Spacer(1)];
732
897
  }
733
898
  this.children = children;
734
- this.frameWidth = width;
899
+ this.frameWidth = outer;
735
900
  this.frameLines = lines;
736
901
  if (pending) this.scheduleFollowUp();
737
902
  return lines;
@@ -26,12 +26,56 @@ export interface Preferences {
26
26
  /** Seconds a finished streamed call stays in full before it collapses. */
27
27
  streamDelay: number;
28
28
  buttonStyle: "icons" | "text";
29
+ /*
30
+ * Turn marks: every block of a turn, a user or custom message and the work that follows it, gets the same color in
31
+ * both panels. Each mark is chosen independently and applies only while `turns` is on.
32
+ */
33
+ /** Turn colors, hover highlighting and Go to as a whole; the settings below keep their values while it is off. */
34
+ turns: boolean;
35
+ /** Color the background of each block's clock. */
36
+ turnClock: boolean;
37
+ /** A one-column colored cell or line on the left of every block. */
38
+ turnStripe: TurnStripe;
39
+ /** Tint the default background of every block. */
40
+ turnBackground: boolean;
41
+ /** A colored rule above the first block of each turn. */
42
+ turnSeparator: boolean;
43
+ /** Percent of the turn hue mixed into the terminal background behind clocks, in stripes and lines, in blocks and under the pointer. */
44
+ turnClockMix: number;
45
+ turnStripeMix: number;
46
+ turnBackgroundMix: number;
47
+ turnHoverMix: number;
48
+ /** The hover layer's color: `turn` for each turn's own hue, or one `#rrggbb` color for every turn. */
49
+ turnHoverColor: string;
50
+ /** Highlight the turn under the pointer in both panels, where the terminal reports pointer motion. */
51
+ turnHover: boolean;
52
+ /** How a block opens the same point of history in the other panel. */
53
+ goTo: GoTo;
29
54
  }
30
55
 
56
+ export type TurnStripe = "off" | "block" | "line";
57
+ /** Settings that change how turns are marked; each applies at once without other effects. */
58
+ export type TurnMarkSetting = "turns" | "turnClock" | "turnStripe" | "turnBackground" | "turnSeparator" | "turnClockMix" | "turnStripeMix" |
59
+ "turnBackgroundMix" | "turnHoverMix" | "turnHoverColor";
60
+ export type GoTo = "clock" | "button" | "both" | "off";
61
+ export const TURN_STRIPES: readonly TurnStripe[] = ["off", "block", "line"];
62
+ export const GO_TO: readonly GoTo[] = ["clock", "button", "both", "off"];
63
+
31
64
  export const defaults: Preferences = {
32
65
  mode: "split", side: "left", chatWidth: null, layout: "columns", chatHeight: null, zoom: null, showTimestamps: true, showCopyButtons: true, compactTools: true, groupTools: true, nestedTree: true, streamTools: true, streamDelay: 5, buttonStyle: "text",
66
+ // Strengths chosen on dark and light terminals; one gray hover reads as "this turn" without competing with turn hues.
67
+ turns: true, turnClock: true, turnStripe: "block", turnBackground: false, turnSeparator: true,
68
+ turnClockMix: 10, turnStripeMix: 20, turnBackgroundMix: 8, turnHoverMix: 10, turnHoverColor: "#808080", turnHover: true, goTo: "both",
33
69
  };
34
70
 
71
+ /** One-color hover choices offered in settings besides each turn's own hue; any `#rrggbb` can be typed in. */
72
+ export const HOVER_COLORS: readonly string[] = ["#808080", "#3b82f6", "#14b8a6", "#22c55e", "#eab308", "#f97316", "#ef4444", "#a855f7"];
73
+ const validHoverColor = (value: unknown): value is string => value === "turn" || (typeof value === "string" && /^#[0-9a-f]{6}$/i.test(value));
74
+
75
+ /** Mix choices offered in settings, in percent; saved files may hold any whole percent from 1 to 100. */
76
+ export const TURN_MIXES: readonly number[] = [2, 5, 8, 10, 15, 20, 30, 50, 75, 100];
77
+ const validMix = (value: unknown): value is number => typeof value === "number" && Number.isInteger(value) && value >= 1 && value <= 100;
78
+
35
79
  /** Read display preferences, retaining the first version's saved arrangement. */
36
80
  export async function loadPreferences(file: string): Promise<Preferences> {
37
81
  let value: unknown;
@@ -55,6 +99,18 @@ export async function loadPreferences(file: string): Promise<Preferences> {
55
99
  const streamTools = record.streamTools === undefined ? defaults.streamTools : record.streamTools;
56
100
  const streamDelay = record.streamDelay === undefined ? defaults.streamDelay : record.streamDelay;
57
101
  const buttonStyle = record.buttonStyle === undefined ? defaults.buttonStyle : record.buttonStyle;
102
+ const turns = record.turns === undefined ? defaults.turns : record.turns;
103
+ const turnClock = record.turnClock === undefined ? defaults.turnClock : record.turnClock;
104
+ const turnStripe = record.turnStripe === undefined ? defaults.turnStripe : record.turnStripe;
105
+ const turnBackground = record.turnBackground === undefined ? defaults.turnBackground : record.turnBackground;
106
+ const turnSeparator = record.turnSeparator === undefined ? defaults.turnSeparator : record.turnSeparator;
107
+ const turnClockMix = record.turnClockMix === undefined ? defaults.turnClockMix : record.turnClockMix;
108
+ const turnStripeMix = record.turnStripeMix === undefined ? defaults.turnStripeMix : record.turnStripeMix;
109
+ const turnBackgroundMix = record.turnBackgroundMix === undefined ? defaults.turnBackgroundMix : record.turnBackgroundMix;
110
+ const turnHoverMix = record.turnHoverMix === undefined ? defaults.turnHoverMix : record.turnHoverMix;
111
+ const turnHoverColor = record.turnHoverColor === undefined ? defaults.turnHoverColor : record.turnHoverColor;
112
+ const turnHover = record.turnHover === undefined ? defaults.turnHover : record.turnHover;
113
+ const goTo = record.goTo === undefined ? defaults.goTo : record.goTo;
58
114
  if ((record.side !== "left" && record.side !== "right") ||
59
115
  (record.chatWidth !== null && (typeof record.chatWidth !== "number" || !Number.isInteger(record.chatWidth) ||
60
116
  record.chatWidth < 24 || record.chatWidth > 240)) ||
@@ -63,11 +119,16 @@ export async function loadPreferences(file: string): Promise<Preferences> {
63
119
  typeof showTimestamps !== "boolean" || typeof showCopyButtons !== "boolean" || typeof compactTools !== "boolean" ||
64
120
  typeof groupTools !== "boolean" || typeof nestedTree !== "boolean" ||
65
121
  typeof streamTools !== "boolean" || typeof streamDelay !== "number" || !Number.isInteger(streamDelay) || streamDelay < 1 || streamDelay > 300 ||
66
- (buttonStyle !== "icons" && buttonStyle !== "text")) {
122
+ (buttonStyle !== "icons" && buttonStyle !== "text") || typeof turns !== "boolean" || typeof turnClock !== "boolean" ||
123
+ !TURN_STRIPES.includes(turnStripe as TurnStripe) || typeof turnBackground !== "boolean" || typeof turnSeparator !== "boolean" ||
124
+ !validMix(turnClockMix) || !validMix(turnStripeMix) || !validMix(turnBackgroundMix) || !validMix(turnHoverMix) || !validHoverColor(turnHoverColor) ||
125
+ typeof turnHover !== "boolean" || !GO_TO.includes(goTo as GoTo)) {
67
126
  throw new Error("Invalid pi-stereo-view preferences");
68
127
  }
69
128
  // Legacy files may contain mode/zoom. Every session starts with both panels visible.
70
- return { mode: "split", side: record.side, chatWidth: record.chatWidth, layout, chatHeight, zoom: null, showTimestamps, showCopyButtons, compactTools, groupTools, nestedTree, streamTools, streamDelay, buttonStyle };
129
+ return { mode: "split", side: record.side, chatWidth: record.chatWidth, layout, chatHeight, zoom: null, showTimestamps, showCopyButtons, compactTools, groupTools, nestedTree, streamTools, streamDelay, buttonStyle,
130
+ turns, turnClock, turnStripe: turnStripe as TurnStripe, turnBackground, turnSeparator, turnClockMix, turnStripeMix, turnBackgroundMix, turnHoverMix,
131
+ turnHoverColor: turnHoverColor.toLowerCase(), turnHover, goTo: goTo as GoTo };
71
132
  }
72
133
 
73
134
  /** Atomically save only the display settings, never session messages. */
@@ -75,8 +136,10 @@ export async function savePreferences(file: string, preferences: Preferences): P
75
136
  await mkdir(path.dirname(file), { recursive: true });
76
137
  const temporary = file + "." + randomUUID() + ".tmp";
77
138
  try {
78
- const { side, chatWidth, layout, chatHeight, showTimestamps, showCopyButtons, compactTools, groupTools, nestedTree, streamTools, streamDelay, buttonStyle } = preferences;
79
- const saved = { side, chatWidth, layout, chatHeight, showTimestamps, showCopyButtons, compactTools, groupTools, nestedTree, streamTools, streamDelay, buttonStyle };
139
+ const { side, chatWidth, layout, chatHeight, showTimestamps, showCopyButtons, compactTools, groupTools, nestedTree, streamTools, streamDelay, buttonStyle,
140
+ turns, turnClock, turnStripe, turnBackground, turnSeparator, turnClockMix, turnStripeMix, turnBackgroundMix, turnHoverMix, turnHoverColor, turnHover, goTo } = preferences;
141
+ const saved = { side, chatWidth, layout, chatHeight, showTimestamps, showCopyButtons, compactTools, groupTools, nestedTree, streamTools, streamDelay, buttonStyle,
142
+ turns, turnClock, turnStripe, turnBackground, turnSeparator, turnClockMix, turnStripeMix, turnBackgroundMix, turnHoverMix, turnHoverColor, turnHover, goTo };
80
143
  await writeFile(temporary, JSON.stringify(saved, null, 2) + "\n", { mode: 0o600 });
81
144
  await rename(temporary, file);
82
145
  } finally {