@oh-my-pi/pi-tui 18.10.4 → 18.11.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.
Files changed (37) hide show
  1. package/CHANGELOG.md +22 -100
  2. package/dist/types/chat/figure-markdown.d.ts +7 -2
  3. package/dist/types/components/markdown.d.ts +8 -0
  4. package/dist/types/components/text.d.ts +2 -0
  5. package/dist/types/keybinding-matchers.d.ts +2 -0
  6. package/dist/types/multiplexers/tmux.d.ts +1 -0
  7. package/dist/types/multiplexers/types.d.ts +6 -0
  8. package/dist/types/overlays/approval-dialog.d.ts +24 -6
  9. package/dist/types/prompt/custom-editor.d.ts +18 -10
  10. package/dist/types/render/hanging-wrap.d.ts +5 -0
  11. package/dist/types/terminal-capabilities.d.ts +8 -0
  12. package/dist/types/terminal-multiplexer.d.ts +1 -0
  13. package/dist/types/tmux.d.ts +13 -4
  14. package/package.json +9 -9
  15. package/src/chat/assistant-message.ts +19 -4
  16. package/src/chat/figure-markdown.ts +10 -3
  17. package/src/chat/read-tool-group.ts +5 -1
  18. package/src/chat/svg-figure.ts +3 -1
  19. package/src/chat/svg-source.ts +6 -3
  20. package/src/components/markdown.ts +12 -0
  21. package/src/components/text.ts +11 -1
  22. package/src/keybinding-matchers.ts +5 -0
  23. package/src/multiplexers/tmux.ts +3 -0
  24. package/src/multiplexers/types.ts +6 -0
  25. package/src/overlays/approval-dialog.ts +115 -24
  26. package/src/prompt/custom-editor.ts +149 -64
  27. package/src/render/code-cell.ts +10 -4
  28. package/src/render/hanging-wrap.ts +31 -0
  29. package/src/terminal-capabilities.ts +25 -7
  30. package/src/terminal.ts +14 -3
  31. package/src/tmux.ts +42 -16
  32. package/src/tools/ask.ts +20 -9
  33. package/src/tools/edit.ts +4 -1
  34. package/src/tools/lsp.ts +9 -2
  35. package/src/tools/todo.ts +3 -2
  36. package/src/tools/write.ts +14 -4
  37. package/src/tui.ts +11 -2
@@ -30,6 +30,7 @@ export class Text implements Component {
30
30
  #customBgFn?: (text: string) => string;
31
31
  #styleFn?: (text: string) => string;
32
32
  #ignoreTight = false;
33
+ #wrapLine?: (line: string, width: number) => string[];
33
34
 
34
35
  setIgnoreTight(ignore: boolean): this {
35
36
  if (this.#ignoreTight === ignore) return this;
@@ -37,6 +38,12 @@ export class Text implements Component {
37
38
  this.invalidate();
38
39
  return this;
39
40
  }
41
+ /** Override row wrapping when the source carries structural prefixes. */
42
+ setWrapLine(wrapLine: (line: string, width: number) => string[]): this {
43
+ this.#wrapLine = wrapLine;
44
+ this.invalidate();
45
+ return this;
46
+ }
40
47
 
41
48
  // Cache for rendered output
42
49
  #cachedText?: string;
@@ -162,7 +169,10 @@ export class Text implements Component {
162
169
  const paddingX = this.#ignoreTight ? this.#paddingX : getPaddingX(this.#paddingX);
163
170
  const contentWidth = Math.max(1, width - paddingX * 2);
164
171
  // Wrap text (this preserves ANSI codes but does NOT pad)
165
- const wrappedLines = wrapTextWithAnsi(normalizedText, contentWidth);
172
+ const wrapLine = this.#wrapLine;
173
+ const wrappedLines = wrapLine
174
+ ? normalizedText.split("\n").flatMap(line => wrapLine(line, contentWidth))
175
+ : wrapTextWithAnsi(normalizedText, contentWidth);
166
176
 
167
177
  // Add margins and background to each line
168
178
  const leftMargin = padding(paddingX);
@@ -28,6 +28,11 @@ export function matchesAppToolsExpand(data: string): boolean {
28
28
  return matchesKey(data, "ctrl+o");
29
29
  }
30
30
 
31
+ /** Effective tools-expand key: the first bound key, or Ctrl+O when unbound. */
32
+ export function appToolsExpandKey(): KeyId {
33
+ return getKeybindings().getKeys("app.tools.expand")[0] ?? "ctrl+o";
34
+ }
35
+
31
36
  /** Match the generic selector cancel keybinding. */
32
37
  export function matchesSelectCancel(data: string): boolean {
33
38
  return getKeybindings().matches(data, "tui.select.cancel");
@@ -32,6 +32,9 @@ export const tmuxMultiplexer = {
32
32
  // An explicit appearance refresh passes OSC 11 to the outer terminal, then
33
33
  // re-reads tmux's cache once it has consumed the reply.
34
34
  backgroundColorCache: { passthrough: wrapTmuxPassthrough, settleMs: 100 },
35
+ // tmux drops paste markers for a pane whose own DEC 2004 flag another
36
+ // writer cleared, and only answers DECRQM 2004 since 3.6.
37
+ gatesBracketedPaste: true,
35
38
  expandsRepPadding: true,
36
39
  // tmux expires synchronized output after one second.
37
40
  expiresSynchronizedOutput: true,
@@ -83,6 +83,12 @@ export interface TerminalMultiplexerModule<Id extends string = string> {
83
83
  * session, and an unrecognized report does not turn it off.
84
84
  */
85
85
  readonly honorsSynchronizedOutput?: boolean;
86
+ /**
87
+ * Tracks DEC 2004 per pane and forwards paste markers only while it is set,
88
+ * yet may leave DECRQM 2004 unanswered, so bracketed paste is reasserted
89
+ * without waiting for a DECRPM confirmation.
90
+ */
91
+ readonly gatesBracketedPaste?: boolean;
86
92
  /**
87
93
  * The session does not reveal whether its client renders graphics while
88
94
  * outer-terminal identity variables leak in, so images and Kitty
@@ -5,7 +5,11 @@
5
5
  * so an Enter typed for the composer as the prompt appears never approves a
6
6
  * call, and the chord itself only arms {@link APPROVE_ARM_MS} after mount: it
7
7
  * is also the follow-up key, pressed mid-run exactly when prompts appear.
8
- * Esc denies.
8
+ * Esc denies. With an {@link ApprovalDialogDraft}, every other key edits the
9
+ * composer mounted beneath the dialog, so a message can steer the turn while
10
+ * the call waits; an empty composer ignores Enter and arrows scroll the details.
11
+ * The prompt stays a few rows so the composer keeps its room; with a full
12
+ * prompt, the expand key (`app.tools.expand`) swaps it in, wrapped.
9
13
  */
10
14
  import { type Component, Ellipsis, ScrollView, type TUI, truncateToWidth, wrapTextWithAnsi } from "../index";
11
15
  import { formatKeyHints, formatTooltipKey } from "../app-keybindings";
@@ -14,7 +18,9 @@ import { editorKey, editorKeys } from "../chrome/keybinding-hints";
14
18
  import { getKeybindings } from "../keybindings";
15
19
  import {
16
20
  appApproveKeys,
21
+ appToolsExpandKey,
17
22
  matchesAppApprove,
23
+ matchesAppToolsExpand,
18
24
  matchesSelectCancel,
19
25
  matchesSelectDown,
20
26
  matchesSelectPageDown,
@@ -28,7 +34,7 @@ import { PREVIEW_LIMITS, sanitizeDisplayLines } from "../render/render-utils";
28
34
  import { theme } from "../theme/theme";
29
35
  import { CountdownTimer } from "../chrome/countdown-timer";
30
36
 
31
- /** Detail rows shown before the rest scrolls: the prompt stays compact, the transcript has the call in full. */
37
+ /** Detail rows shown before the rest scrolls, so the composer beneath keeps its room. */
32
38
  const MAX_DETAIL_ROWS: number = PREVIEW_LIMITS.COLLAPSED_ITEMS;
33
39
  /** The approve key is ignored this long after mount, so a follow-up sent as the prompt appears cannot approve it. */
34
40
  export const APPROVE_ARM_MS = 500;
@@ -38,10 +44,17 @@ interface ApprovalDialogCallbacks {
38
44
  onDeny(): void;
39
45
  }
40
46
 
47
+ /** The composer mounted beneath the dialog: it receives every key the dialog does not own. */
48
+ export interface ApprovalDialogDraft {
49
+ /** True while the composer holds a draft — text, attachments, or a paste still landing: keys then edit and send it. */
50
+ hasDraft(): boolean;
51
+ handleInput(keyData: string): void;
52
+ }
53
+
41
54
  interface ApprovalDialogOptions {
42
55
  tui?: TUI;
43
- /** Wrap long details instead of clipping them to a row: set when the dialog is the only place they show. */
44
- wrapDetails?: boolean;
56
+ /** The prompt with every detail in full, which the expand key swaps in (wrapped) for the summarized one. */
57
+ fullPrompt?: string;
45
58
  /** Deny once this many ms pass without input (any key restarts the countdown). */
46
59
  timeout?: number;
47
60
  /** Called when {@link timeout} expires, before the dialog denies. */
@@ -50,11 +63,22 @@ interface ApprovalDialogOptions {
50
63
  onTimeoutStart?: () => void;
51
64
  /** Called each time a key restarts the countdown. */
52
65
  onTimeoutReset?: () => void;
66
+ /** Route typing to the composer instead of dropping it. */
67
+ draft?: ApprovalDialogDraft;
68
+ }
69
+
70
+ /** A prompt's detail rows: every line after the title, trailing blanks dropped. */
71
+ function promptDetails(lines: string[]): string[] {
72
+ const details = lines.slice(1);
73
+ while (details.length > 0 && details[details.length - 1]!.trim() === "") details.pop();
74
+ return details;
53
75
  }
54
76
 
55
77
  export class ApprovalDialogComponent implements Component {
56
78
  readonly #title: string;
57
- readonly #details: readonly string[];
79
+ readonly #summary: readonly string[];
80
+ readonly #full: readonly string[] | undefined;
81
+ #expanded = false;
58
82
  readonly #panel: OverlayPanel;
59
83
  readonly #body = new PanelRows();
60
84
  readonly #footer = new PanelRows();
@@ -71,10 +95,12 @@ export class ApprovalDialogComponent implements Component {
71
95
  private readonly callbacks: ApprovalDialogCallbacks,
72
96
  private readonly options: ApprovalDialogOptions = {},
73
97
  ) {
74
- const [title = "", ...details] = sanitizeDisplayLines(prompt);
75
- while (details.length > 0 && details[details.length - 1]!.trim() === "") details.pop();
98
+ const lines = sanitizeDisplayLines(prompt);
99
+ const title = lines[0] ?? "";
76
100
  this.#title = title;
77
- this.#details = details;
101
+ this.#summary = promptDetails(lines);
102
+ this.#full =
103
+ options.fullPrompt === undefined ? undefined : promptDetails(sanitizeDisplayLines(options.fullPrompt));
78
104
  this.#panel = new OverlayPanel(title, "omp.overlay.approval");
79
105
  this.#footer.setHeight(1);
80
106
  this.#panel.addChild(this.#body);
@@ -109,25 +135,73 @@ export class ApprovalDialogComponent implements Component {
109
135
 
110
136
  handleInput(keyData: string): void {
111
137
  if (this.#closed) return;
112
- if (this.#countdown) {
113
- this.#countdown.reset();
114
- this.options.onTimeoutReset?.();
115
- // The described countdown restarts with it.
116
- this.#native = undefined;
117
- }
138
+ this.#restartCountdown();
118
139
  if (matchesAppApprove(keyData)) {
119
140
  if (performance.now() >= this.#armedAt) this.#finish(true);
120
141
  return;
121
142
  }
143
+ if (this.#full && matchesAppToolsExpand(keyData)) {
144
+ this.#toggleExpanded();
145
+ return;
146
+ }
147
+ const draft = this.options.draft;
148
+ // The composer's clear key (Ctrl+C, also a cancel key) clears a draft before it denies.
149
+ if (draft?.hasDraft() && getKeybindings().matches(keyData, "app.clear")) {
150
+ this.#editDraft(draft, keyData);
151
+ return;
152
+ }
122
153
  if (matchesSelectCancel(keyData)) {
123
154
  this.#finish(false);
124
155
  return;
125
156
  }
157
+ if (draft?.hasDraft()) {
158
+ this.#editDraft(draft, keyData);
159
+ return;
160
+ }
126
161
  const page = Math.max(1, this.#bodyRows - 1);
127
162
  if (matchesSelectUp(keyData)) this.#scroll(-1);
128
163
  else if (matchesSelectDown(keyData)) this.#scroll(1);
129
164
  else if (matchesSelectPageUp(keyData)) this.#scroll(-page);
130
165
  else if (matchesSelectPageDown(keyData)) this.#scroll(page);
166
+ // An empty composer swallows Enter: it would otherwise submit nothing, or interrupt the run.
167
+ else if (draft && !getKeybindings().matches(keyData, "tui.input.submit")) this.#editDraft(draft, keyData);
168
+ }
169
+
170
+ #editDraft(draft: ApprovalDialogDraft, keyData: string): void {
171
+ draft.handleInput(keyData);
172
+ this.options.tui?.requestRender();
173
+ }
174
+
175
+ /**
176
+ * Swap the summarized prompt for every detail, wrapped, or back, as the expand key does here;
177
+ * for hosts that intercept that key first. Restarts the countdown like any key. False (nothing
178
+ * changes) without a full prompt, so the host's own expand handling (tool previews) can run.
179
+ */
180
+ toggleDetails(): boolean {
181
+ if (this.#closed || !this.#full) return false;
182
+ this.#restartCountdown();
183
+ this.#toggleExpanded();
184
+ return true;
185
+ }
186
+
187
+ /** Any user input restarts the countdown (and its described form). */
188
+ #restartCountdown(): void {
189
+ if (!this.#countdown) return;
190
+ this.#countdown.reset();
191
+ this.options.onTimeoutReset?.();
192
+ this.#native = undefined;
193
+ }
194
+
195
+ #toggleExpanded(): void {
196
+ this.#expanded = !this.#expanded;
197
+ this.#scrollOffset = 0;
198
+ this.#native = undefined;
199
+ this.options.tui?.requestRender();
200
+ }
201
+
202
+ /** The rows on show: the summary, or every detail once expanded. */
203
+ get #details(): readonly string[] {
204
+ return this.#expanded && this.#full ? this.#full : this.#summary;
131
205
  }
132
206
 
133
207
  render(width: number): readonly string[] {
@@ -158,8 +232,8 @@ export class ApprovalDialogComponent implements Component {
158
232
  /**
159
233
  * In the dock, in the composer's place and framed as the composer is (an
160
234
  * `omp.editor.*` root role), like the ask dialog, rather than a card over
161
- * the transcript: the title, the details, then Deny and the primary
162
- * Approve, whose keys show in their tooltips.
235
+ * the transcript: the title, the details, then Details (with a full prompt),
236
+ * Deny, and the primary Approve, whose keys show in their tooltips.
163
237
  */
164
238
  describe(): NativeNode {
165
239
  if (this.#native) return this.#native;
@@ -175,35 +249,46 @@ export class ApprovalDialogComponent implements Component {
175
249
  tone: "accent",
176
250
  title: `Approve this call${approveKey ? ` ${formatTooltipKey(approveKey)}` : ""}`,
177
251
  });
252
+ const expandKey = appToolsExpandKey();
253
+ const expand = this.#full
254
+ ? [
255
+ actionButton(this.#expanded ? "Less" : "Details", "expand", {
256
+ keys: expandKey,
257
+ title: `${this.#expanded ? "Summarize" : "Show every detail of"} this call ${formatTooltipKey(expandKey)}`,
258
+ }),
259
+ ]
260
+ : [];
178
261
  const children: NativeNode[] = [
179
262
  node("text", { text: this.#title, role: "omp.approval.title", tone: "accent" }, undefined, "title"),
180
263
  ];
181
264
  if (this.#details.length > 0) {
182
- // One clipped row per detail like the text view, unless wrapping was asked for.
183
- const wrap = this.options.wrapDetails === true;
265
+ // One clipped row per detail like the text view; expanded details wrap.
184
266
  const rows = this.#details.map((line, index) =>
185
- node("text", wrap ? { text: line } : { text: line, truncate: "end" }, undefined, String(index)),
267
+ node("text", this.#expanded ? { text: line } : { text: line, truncate: "end" }, undefined, String(index)),
186
268
  );
187
269
  children.push(node("col", { role: "omp.approval.details" }, rows, "details"));
188
270
  }
189
271
  // The countdown, when one runs, leads the bar like the ask dialog's.
190
- children.push(actionBar([...(this.#countdown ? [this.#countdown.describe()] : []), null, deny, approve]));
272
+ children.push(
273
+ actionBar([...(this.#countdown ? [this.#countdown.describe()] : []), null, ...expand, deny, approve]),
274
+ );
191
275
  // Under the `omp.editor.ask` prefix: Tern's console layout sizes an ask's rows to the
192
276
  // composer column by that prefix, where a bare `omp.editor.*` role spans the whole dock.
193
277
  this.#native = col(children, { role: "omp.editor.ask.approval", gap: "md" });
194
278
  return this.#native;
195
279
  }
196
280
 
197
- /** Deny and Approve run exactly what Esc and the approve key run. */
281
+ /** Deny, Approve, and Details run exactly what Esc, the approve key, and the expand key run. */
198
282
  handleNativeEvent(event: NativeUiEvent): void {
199
283
  if (this.#closed || event.type !== "action") return;
200
284
  if (event.act === "approve") this.#finish(true);
201
285
  else if (event.act === "deny") this.#finish(false);
286
+ else if (event.act === "expand") this.toggleDetails();
202
287
  }
203
288
 
204
- /** Detail rows at `width`: one clipped row per detail, or wrapped rows with {@link ApprovalDialogOptions.wrapDetails}. */
289
+ /** Detail rows at `width`: one clipped row per detail, or wrapped rows once expanded. */
205
290
  #detailLines(width: number): string[] {
206
- if (!this.options.wrapDetails) {
291
+ if (!this.#expanded) {
207
292
  return this.#details.map(line => truncateToWidth(theme.fg("accent", line), width, Ellipsis.Unicode));
208
293
  }
209
294
  return this.#details.flatMap(line => (line === "" ? [""] : wrapTextWithAnsi(theme.fg("accent", line), width)));
@@ -211,8 +296,14 @@ export class ApprovalDialogComponent implements Component {
211
296
 
212
297
  #footerHint(overflows: boolean): string {
213
298
  const approve = formatKeyHints(appApproveKeys());
299
+ const expand = this.#full
300
+ ? ` · ${formatKeyHints([appToolsExpandKey()])} ${this.#expanded ? "less" : "details"}`
301
+ : "";
302
+ const hint = `${approve} approve · ${editorKey("tui.select.cancel")} deny${expand}`;
303
+ const draft = this.options.draft;
304
+ if (draft?.hasDraft()) return `${hint} · ${editorKey("tui.input.submit")} deny and send`;
214
305
  const scroll = overflows ? ` · ${editorKeys("tui.select.up", "tui.select.down")} scroll` : "";
215
- return `${approve} approve · ${editorKey("tui.select.cancel")} deny${scroll}`;
306
+ return `${hint}${scroll}${draft ? " · type to steer instead" : ""}`;
216
307
  }
217
308
 
218
309
  #scroll(delta: number): void {