pi-stereo-view 0.1.23 → 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/layout.ts CHANGED
@@ -2,12 +2,13 @@ import { setImmediate } from "node:timers/promises";
2
2
  import type { Theme, ToolInfo } from "@earendil-works/pi-coding-agent";
3
3
  import {
4
4
  Container, HStack, isViewportTUI, mixColors, MouseRegion, rgbColor, ScrollView, Text, truncateToWidth, visibleWidth, VStack,
5
- type Color, type Component, type ScrollViewOptions, type StackChild, type TUI, type TuiMouseEvent,
5
+ type Color, type Component, type RgbColor, type ScrollViewOptions, type StackChild, type TUI, type TuiMouseEvent,
6
6
  } from "@earendil-works/pi-tui";
7
- import { NativeDocument, type Pane } from "./native.ts";
7
+ import { NativeDocument, type DocumentTurns, type Pane } from "./native.ts";
8
+ import { parseHex, turnColor, pointerMotionAvailable, type TurnMix } from "./turns.ts";
8
9
  import { NativeContentIndex } from "./content.ts";
9
10
  import { NestedCalls } from "./nested-calls.ts";
10
- import type { Preferences } from "./preferences.ts";
11
+ import type { TurnMarkSetting, Preferences } from "./preferences.ts";
11
12
  import type { TranscriptTimestamps } from "./timestamps.ts";
12
13
  import { PANEL_SEARCH_ROWS, PanelSearch, PanelSearchShortcut, type SearchHit } from "./search.ts";
13
14
  import { SplitSurface } from "./surface.ts";
@@ -239,6 +240,14 @@ export class TranscriptLayout {
239
240
  private automaticHeight = MIN_ROWS;
240
241
  private readonly rejectedRoots = new WeakSet<Component>();
241
242
  private rebuild = (): void => {};
243
+ private index?: NativeContentIndex;
244
+ /** The turn under the pointer; a pointer move collects the turn its document reports. */
245
+ private hoverAct?: number;
246
+ private moving?: { turn: number };
247
+ /** The terminal's reported background, which turn colors are mixed into. */
248
+ private terminalBackground?: RgbColor;
249
+ /** Set by `markTerminalStarted()`; terminal queries wait for it. */
250
+ private terminalStarted = false;
242
251
 
243
252
  constructor(
244
253
  readonly preferences: Preferences,
@@ -369,15 +378,33 @@ export class TranscriptLayout {
369
378
  const showTimestamps = (): boolean => this.preferences.showTimestamps;
370
379
  const showCopyButtons = (): boolean => this.preferences.showCopyButtons;
371
380
  const index = new NativeContentIndex(document, this.nested, () => this.preferences.nestedTree);
381
+ this.index = index;
382
+ // With turns off, every turn setting keeps its value but nothing marks, highlights or jumps.
383
+ const on = (): boolean => this.preferences.turns;
384
+ const turns: DocumentTurns = {
385
+ clock: () => on() && this.preferences.turnClock,
386
+ stripe: () => on() ? this.preferences.turnStripe : "off",
387
+ background: () => on() && this.preferences.turnBackground,
388
+ separator: () => on() && this.preferences.turnSeparator,
389
+ mix: () => this.turnMix(),
390
+ color: (turn, layers) => turnColor(this.theme(), turn, this.terminalBackground, this.turnMix(), layers),
391
+ hovered: () => this.hoverEnabled() ? this.hoverAct : undefined,
392
+ hover: turn => { if (this.moving) this.moving.turn = turn; },
393
+ goTo: () => on() ? this.preferences.goTo : "off",
394
+ jump: (from, source) => {
395
+ void this.goTo(from, source).catch(error => this.warn("Could not open the other panel: " + String(error)));
396
+ },
397
+ };
372
398
  const activityDocument = new NativeDocument(document, "activity", this.timestamps, this.theme, timestampGutter, showTimestamps,
373
399
  () => this.activate("activity"), false, index, this.copy, showCopyButtons,
374
400
  () => ({ compact: this.preferences.compactTools, group: this.preferences.groupTools }), this.getTools,
375
401
  () => { if (this.activity) this.activity.scrollTo(this.activity.scrollTop, { disableFollow: true }); }, () => this.preferences.buttonStyle, {
376
402
  enabled: () => this.preferences.streamTools, delay: () => this.preferences.streamDelay * 1000,
377
403
  changed: () => this.host?.requestRender(),
378
- });
404
+ }, turns);
379
405
  const chatDocument = new NativeDocument(document, "chat", this.timestamps, this.theme, timestampGutter, showTimestamps,
380
- () => this.activate("chat"), false, index, this.copy, showCopyButtons, undefined, undefined, undefined, () => this.preferences.buttonStyle);
406
+ () => this.activate("chat"), false, index, this.copy, showCopyButtons, undefined, undefined, undefined, () => this.preferences.buttonStyle,
407
+ undefined, turns);
381
408
  this.documents = { activity: activityDocument, chat: chatDocument };
382
409
  this.disposeDocuments = () => {
383
410
  activityDocument.dispose();
@@ -628,6 +655,15 @@ export class TranscriptLayout {
628
655
  return pane === "chat" ? this.chat : pane === "activity" ? this.activity : undefined;
629
656
  },
630
657
  failed: error => this.fail(error),
658
+ pointer: {
659
+ start: () => { this.moving = { turn: 0 }; },
660
+ end: () => {
661
+ const turn = this.moving?.turn ?? 0;
662
+ this.moving = undefined;
663
+ this.setHover(turn);
664
+ },
665
+ leave: () => this.setHover(0),
666
+ },
631
667
  });
632
668
  surface.setModels([columns, rows, activityPane, chatPane]);
633
669
  this.surface = surface;
@@ -665,6 +701,66 @@ export class TranscriptLayout {
665
701
  this.mounted = mounted;
666
702
  host.setLayoutRoot(mounted);
667
703
  host.requestRender();
704
+ if (this.terminalStarted) this.queryBackground(host);
705
+ }
706
+
707
+ /**
708
+ * Pi's terminal has started, so terminal queries can be sent. An early layout mounts before that start, and a
709
+ * query sent then answers ahead of Pi's own start queries: Pi takes that answer for its own and misses the
710
+ * terminal's support for program status (OSC 7501) and keyboard protocols.
711
+ */
712
+ markTerminalStarted(): void {
713
+ this.terminalStarted = true;
714
+ if (this.host) this.queryBackground(this.host);
715
+ }
716
+
717
+ /** Ask the terminal for its background once; turn colors use a guess from the theme until it answers. */
718
+ private queryBackground(host: NativeTui): void {
719
+ if (this.terminalBackground || typeof host.queryTerminalColors !== "function") return;
720
+ void host.queryTerminalColors({ timeoutMs: 1000 }).then(colors => {
721
+ if (!colors.background || this.host !== host) return;
722
+ this.terminalBackground = colors.background;
723
+ host.requestRender();
724
+ }, () => {});
725
+ }
726
+
727
+ /** Pi reports pointer motion for hover only outside terminal multiplexers and with its mouse support on. */
728
+ hoverAvailable(): boolean {
729
+ return pointerMotionAvailable(this.host);
730
+ }
731
+
732
+ /** Hover highlighting is on, with turns, and the terminal reports pointer motion. */
733
+ private hoverEnabled(): boolean {
734
+ return this.preferences.turns && this.preferences.turnHover && this.hoverAvailable();
735
+ }
736
+
737
+ /** Highlight another turn; pointer moves within one turn render nothing. */
738
+ private setHover(turn: number): void {
739
+ const next = turn || undefined;
740
+ if (next === this.hoverAct) return;
741
+ this.hoverAct = next;
742
+ if (this.hoverEnabled()) this.host?.requestRender();
743
+ }
744
+
745
+ /** Scroll the other panel to the same point of history as a block, returning from a zoomed panel first. */
746
+ async goTo(from: Pane, source: Component): Promise<void> {
747
+ const target = this.index?.counterpart(source, from);
748
+ if (!target || !this.searchPane()) return;
749
+ const pane: Pane = from === "chat" ? "activity" : "chat";
750
+ if (this.preferences.zoom) {
751
+ this.preferences.zoom = null;
752
+ this.changed(false);
753
+ // The restored panel needs a layout pass for its viewport.
754
+ await setImmediate();
755
+ }
756
+ const document = this.documents?.[pane];
757
+ const view = pane === "chat" ? this.chat : this.activity;
758
+ if (!document || !view || !this.searchPane()) return;
759
+ this.activate(pane);
760
+ const row = document.locate(target);
761
+ if (row === undefined) return;
762
+ view.scrollTo(Math.max(0, row - 1), { disableFollow: true });
763
+ this.host?.requestRender();
668
764
  }
669
765
 
670
766
  private activate(pane: Pane): void {
@@ -798,6 +894,31 @@ export class TranscriptLayout {
798
894
  if (persist) this.save();
799
895
  }
800
896
 
897
+ /** The turn marks' mix strengths, as the palette reads them. */
898
+ private turnMix(): TurnMix {
899
+ const { turnClockMix: clock, turnStripeMix: stripe, turnBackgroundMix: background, turnHoverMix: hover, turnHoverColor } = this.preferences;
900
+ // One hover color for every turn, or each turn's own hue.
901
+ return { clock, stripe, background, hover, hoverColor: parseHex(turnHoverColor) };
902
+ }
903
+
904
+ /** Turn the whole turn feature on or off, or change one of its marks or strengths. */
905
+ setTurnMark<Key extends TurnMarkSetting>(key: Key, value: Preferences[Key]): void {
906
+ this.preferences[key] = value;
907
+ this.changed();
908
+ }
909
+
910
+ /** Highlight the turn under the pointer in both panels. */
911
+ setTurnHover(hover: boolean): void {
912
+ this.preferences.turnHover = hover;
913
+ this.changed();
914
+ }
915
+
916
+ /** Open the other panel from a block's clock, its button, both or neither. */
917
+ setGoTo(goTo: Preferences["goTo"]): void {
918
+ this.preferences.goTo = goTo;
919
+ this.changed();
920
+ }
921
+
801
922
  /** Show or hide clock rows without changing their recorded source times. */
802
923
  setTimestamps(show: boolean): void {
803
924
  this.preferences.showTimestamps = show;
@@ -944,6 +1065,9 @@ export class TranscriptLayout {
944
1065
  this.activity = undefined;
945
1066
  this.chat = undefined;
946
1067
  this.documents = undefined;
1068
+ this.index = undefined;
1069
+ this.hoverAct = undefined;
1070
+ this.moving = undefined;
947
1071
  this.drag = undefined;
948
1072
  this.rebuild = () => {};
949
1073
  }
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;