@aoshizuku/pi-bettertui 0.1.1 → 0.1.2

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/tool-lines.ts CHANGED
@@ -1,173 +1,173 @@
1
- /**
2
- * Line builders shared by tool rows, read-only groups and fold segments. Each returns finished
3
- * lines (truncated to `width`); callers only decide where they go.
4
- */
5
- import type { Theme } from "@earendil-works/pi-coding-agent";
6
- import { truncateToWidth, visibleWidth, wrapTextWithAnsi } from "@earendil-works/pi-tui";
7
-
8
- import { formatDuration, sanitizeText, str } from "./format.js";
9
- import type { ToolRow } from "./timeline.js";
10
- import {
11
- TOOL_LABELS,
12
- callDetail,
13
- isFailed,
14
- isInterrupted,
15
- isShellTool,
16
- resultLineTotal,
17
- resultLines,
18
- resultText,
19
- shellCommandLines,
20
- summarizeResult,
21
- } from "./tool-summary.js";
22
-
23
- export const MAX_EXPANDED_COMMAND_LINES = 12;
24
-
25
- /** One child row under a group or segment: rail in column 2, the tool from column 4. */
26
- export function childLine(member: ToolRow, rail: "├" | "└", width: number, theme: Theme, cwd: string, status?: string): string {
27
- const railText = theme.fg("dim", ` ${rail} `);
28
- const label = TOOL_LABELS[member.name] ?? member.name;
29
- const detail = sanitizeText(callDetail(member.name, member.args, cwd));
30
- const labelText = theme.fg("toolTitle", label);
31
- const detailText = detail ? ` ${theme.fg("muted", `(${detail})`)}` : "";
32
- const interrupted = isInterrupted(member.result);
33
- const summary = status ?? (interrupted
34
- ? "interrupted"
35
- : member.result?.isError
36
- ? "failed"
37
- : member.endedAt === undefined
38
- ? "running"
39
- : member.result
40
- ? summarizeResult(member.name, member.result, member.args)
41
- : "Done");
42
- const color = interrupted ? "warning" : member.result?.isError ? "error" : "dim";
43
- const summaryText = summary ? ` ${theme.fg(color, `· ${summary}`)}` : "";
44
- return truncateToWidth(`${railText}${labelText}${detailText}${summaryText}`, width, "…");
45
- }
46
-
47
- /** Output or diff lines of a row, each behind `rail` (dim). */
48
- export function memberDetailLines(row: ToolRow, width: number, theme: Theme, rail: string, maxLines: number): string[] {
49
- const source = detailSource(row);
50
- if (!source.text) return [];
51
- let lines = source.lines;
52
- const total = source.total;
53
- if (lines.length > maxLines) {
54
- lines = source.keep === "head" ? lines.slice(0, maxLines) : lines.slice(-maxLines);
55
- }
56
- const railText = theme.fg("dim", rail);
57
- // An interrupted command keeps its output's own color; only a failure turns it red.
58
- const failed = isFailed(row.result);
59
- const output = lines.map((line) => {
60
- const color = failed
61
- ? "error"
62
- : row.name === "edit" && line.startsWith("+")
63
- ? "success"
64
- : row.name === "edit" && line.startsWith("-")
65
- ? "error"
66
- : "toolOutput";
67
- return `${railText}${theme.fg(color, line)}`;
68
- });
69
- const hidden = Math.max(0, total - lines.length);
70
- if (hidden > 0) {
71
- output.push(`${railText}${theme.fg("dim", `… ${hidden} more line${hidden === 1 ? "" : "s"}`)}`);
72
- }
73
- return output.map((line) => truncateToWidth(line, width, "…"));
74
- }
75
-
76
- export function detailSource(row: ToolRow): { text: string; lines: string[]; total: number; keep: "head" | "tail" } {
77
- if (row.name === "edit" && row.result) {
78
- const diff = (row.result.details as { diff?: unknown } | undefined)?.diff;
79
- if (typeof diff === "string") {
80
- const lines = sanitizeText(diff).split("\n").filter((line) => line.trim() !== "");
81
- return { text: diff, lines, total: lines.length, keep: "head" };
82
- }
83
- }
84
- const text = row.partialText || resultText(row.result);
85
- if (!text) return { text: "", lines: [], total: 0, keep: "tail" };
86
- const total = resultLineTotal(row.result ?? { content: [{ type: "text", text }], isError: false });
87
- const keep: "head" | "tail" = isShellTool(row.name) || row.result?.isError ? "tail" : "head";
88
- return { text, lines: resultLines(row.result ?? { content: [{ type: "text", text }], isError: false }, 1_000, keep), total, keep };
89
- }
90
-
91
- /** The full command, wrapped: `$ ` before the first line, continuation lines aligned under it. */
92
- export function commandLines(row: ToolRow, width: number, indent: string, theme: Theme, max = MAX_EXPANDED_COMMAND_LINES): string[] {
93
- const source = shellCommandLines(row.args);
94
- if (source.length === 0) return [];
95
- const contentWidth = Math.max(1, width - visibleWidth(indent) - 2);
96
- const wrapped: string[] = [];
97
- for (const line of source) {
98
- const pieces = line ? wrapTextWithAnsi(line, contentWidth) : [""];
99
- wrapped.push(...pieces);
100
- }
101
- const shown = wrapped.slice(0, max);
102
- const output = shown.map((line, index) => {
103
- const prompt = index === 0 ? theme.fg("accent", "$ ") : " ";
104
- return `${indent}${prompt}${theme.fg("text", line)}`;
105
- });
106
- const hidden = wrapped.length - shown.length;
107
- if (hidden > 0) {
108
- output.push(`${indent}${theme.fg("dim", ` … ${hidden} more command line${hidden === 1 ? "" : "s"}`)}`);
109
- }
110
- return output.map((line) => truncateToWidth(line, width, "…"));
111
- }
112
-
113
- /** `Running… 12s` for a command, `Running…` below one second (fold-more.md §5.8). */
114
- export function runningStatus(row: ToolRow, now = Date.now()): string {
115
- const elapsed = Math.max(0, now - row.startedAt);
116
- return elapsed >= 1_000 ? `Running… ${formatDuration(elapsed)}` : "Running…";
117
- }
118
-
119
- /**
120
- * How long a command runs before its preview block opens (fold-more.md §5.8). A quick command
121
- * stays one line for its whole life, so a run of quick commands does not make the screen
122
- * stretch and shrink.
123
- */
124
- export const RUNNING_BLOCK_DELAY_MS = 1_000;
125
-
126
- /** Whether a running command has run long enough to show its fixed-height preview block. */
127
- export function showsRunningBlock(row: ToolRow, now = Date.now()): boolean {
128
- if (row.endedAt !== undefined || row.execStartedAt === undefined) return false;
129
- return now - row.execStartedAt >= RUNNING_BLOCK_DELAY_MS;
130
- }
131
-
132
- /** Rows a running command block takes below its header: script lines only with a description. */
133
- export function runningBlockHeight(row: ToolRow, previewLines: number): number {
134
- return (hasDescription(row) ? 2 : 0) + Math.max(0, previewLines);
135
- }
136
-
137
- function hasDescription(row: ToolRow): boolean {
138
- return str(row.args.description).trim().length > 0;
139
- }
140
-
141
- /**
142
- * The fixed-height body of a running command (fold-more.md §5.8): the first two script lines
143
- * when the header shows a description instead of the command, then the last `previewLines`
144
- * output lines. Missing lines are padded with the bare rail so the block never changes height
145
- * while output arrives.
146
- */
147
- export function runningBlock(row: ToolRow, width: number, theme: Theme, rail: string, previewLines: number): string[] {
148
- const railText = theme.fg("dim", rail);
149
- const room = Math.max(1, width - visibleWidth(rail));
150
- const lines: string[] = [];
151
- if (hasDescription(row)) {
152
- const script = shellCommandLines(row.args);
153
- for (let index = 0; index < 2; index++) {
154
- const line = script[index];
155
- if (line === undefined) {
156
- lines.push(railText);
157
- continue;
158
- }
159
- const more = index === 1 && script.length > 2 ? " …" : "";
160
- const prompt = index === 0 ? theme.fg("accent", "$ ") : " ";
161
- lines.push(`${railText}${prompt}${theme.fg("text", truncateToWidth(line + more, Math.max(1, room - 2), "…"))}`);
162
- }
163
- }
164
- const count = Math.max(0, previewLines);
165
- if (count > 0) {
166
- const output = resultLines({ content: [{ type: "text", text: row.partialText }], isError: false }, count, "tail");
167
- for (let index = 0; index < count; index++) {
168
- const line = output[index];
169
- lines.push(line === undefined ? railText : `${railText}${theme.fg("toolOutput", line)}`);
170
- }
171
- }
172
- return lines.map((line) => truncateToWidth(line, width, "…"));
173
- }
1
+ /**
2
+ * Line builders shared by tool rows, read-only groups and fold segments. Each returns finished
3
+ * lines (truncated to `width`); callers only decide where they go.
4
+ */
5
+ import type { Theme } from "@earendil-works/pi-coding-agent";
6
+ import { truncateToWidth, visibleWidth, wrapTextWithAnsi } from "@earendil-works/pi-tui";
7
+
8
+ import { formatDuration, sanitizeText, str } from "./format.js";
9
+ import type { ToolRow } from "./timeline.js";
10
+ import {
11
+ TOOL_LABELS,
12
+ callDetail,
13
+ isFailed,
14
+ isInterrupted,
15
+ isShellTool,
16
+ resultLineTotal,
17
+ resultLines,
18
+ resultText,
19
+ shellCommandLines,
20
+ summarizeResult,
21
+ } from "./tool-summary.js";
22
+
23
+ export const MAX_EXPANDED_COMMAND_LINES = 12;
24
+
25
+ /** One child row under a group or segment: rail in column 2, the tool from column 4. */
26
+ export function childLine(member: ToolRow, rail: "├" | "└", width: number, theme: Theme, cwd: string, status?: string): string {
27
+ const railText = theme.fg("dim", ` ${rail} `);
28
+ const label = TOOL_LABELS[member.name] ?? member.name;
29
+ const detail = sanitizeText(callDetail(member.name, member.args, cwd));
30
+ const labelText = theme.fg("toolTitle", label);
31
+ const detailText = detail ? ` ${theme.fg("muted", `(${detail})`)}` : "";
32
+ const interrupted = isInterrupted(member.result);
33
+ const summary = status ?? (interrupted
34
+ ? "interrupted"
35
+ : member.result?.isError
36
+ ? "failed"
37
+ : member.endedAt === undefined
38
+ ? "running"
39
+ : member.result
40
+ ? summarizeResult(member.name, member.result, member.args)
41
+ : "Done");
42
+ const color = interrupted ? "warning" : member.result?.isError ? "error" : "dim";
43
+ const summaryText = summary ? ` ${theme.fg(color, `· ${summary}`)}` : "";
44
+ return truncateToWidth(`${railText}${labelText}${detailText}${summaryText}`, width, "…");
45
+ }
46
+
47
+ /** Output or diff lines of a row, each behind `rail` (dim). */
48
+ export function memberDetailLines(row: ToolRow, width: number, theme: Theme, rail: string, maxLines: number): string[] {
49
+ const source = detailSource(row);
50
+ if (!source.text) return [];
51
+ let lines = source.lines;
52
+ const total = source.total;
53
+ if (lines.length > maxLines) {
54
+ lines = source.keep === "head" ? lines.slice(0, maxLines) : lines.slice(-maxLines);
55
+ }
56
+ const railText = theme.fg("dim", rail);
57
+ // An interrupted command keeps its output's own color; only a failure turns it red.
58
+ const failed = isFailed(row.result);
59
+ const output = lines.map((line) => {
60
+ const color = failed
61
+ ? "error"
62
+ : row.name === "edit" && line.startsWith("+")
63
+ ? "success"
64
+ : row.name === "edit" && line.startsWith("-")
65
+ ? "error"
66
+ : "toolOutput";
67
+ return `${railText}${theme.fg(color, line)}`;
68
+ });
69
+ const hidden = Math.max(0, total - lines.length);
70
+ if (hidden > 0) {
71
+ output.push(`${railText}${theme.fg("dim", `… ${hidden} more line${hidden === 1 ? "" : "s"}`)}`);
72
+ }
73
+ return output.map((line) => truncateToWidth(line, width, "…"));
74
+ }
75
+
76
+ export function detailSource(row: ToolRow): { text: string; lines: string[]; total: number; keep: "head" | "tail" } {
77
+ if (row.name === "edit" && row.result) {
78
+ const diff = (row.result.details as { diff?: unknown } | undefined)?.diff;
79
+ if (typeof diff === "string") {
80
+ const lines = sanitizeText(diff).split("\n").filter((line) => line.trim() !== "");
81
+ return { text: diff, lines, total: lines.length, keep: "head" };
82
+ }
83
+ }
84
+ const text = row.partialText || resultText(row.result);
85
+ if (!text) return { text: "", lines: [], total: 0, keep: "tail" };
86
+ const total = resultLineTotal(row.result ?? { content: [{ type: "text", text }], isError: false });
87
+ const keep: "head" | "tail" = isShellTool(row.name) || row.result?.isError ? "tail" : "head";
88
+ return { text, lines: resultLines(row.result ?? { content: [{ type: "text", text }], isError: false }, 1_000, keep), total, keep };
89
+ }
90
+
91
+ /** The full command, wrapped: `$ ` before the first line, continuation lines aligned under it. */
92
+ export function commandLines(row: ToolRow, width: number, indent: string, theme: Theme, max = MAX_EXPANDED_COMMAND_LINES): string[] {
93
+ const source = shellCommandLines(row.args);
94
+ if (source.length === 0) return [];
95
+ const contentWidth = Math.max(1, width - visibleWidth(indent) - 2);
96
+ const wrapped: string[] = [];
97
+ for (const line of source) {
98
+ const pieces = line ? wrapTextWithAnsi(line, contentWidth) : [""];
99
+ wrapped.push(...pieces);
100
+ }
101
+ const shown = wrapped.slice(0, max);
102
+ const output = shown.map((line, index) => {
103
+ const prompt = index === 0 ? theme.fg("accent", "$ ") : " ";
104
+ return `${indent}${prompt}${theme.fg("text", line)}`;
105
+ });
106
+ const hidden = wrapped.length - shown.length;
107
+ if (hidden > 0) {
108
+ output.push(`${indent}${theme.fg("dim", ` … ${hidden} more command line${hidden === 1 ? "" : "s"}`)}`);
109
+ }
110
+ return output.map((line) => truncateToWidth(line, width, "…"));
111
+ }
112
+
113
+ /** `Running… 12s` for a command, `Running…` below one second (fold-more.md §5.8). */
114
+ export function runningStatus(row: ToolRow, now = Date.now()): string {
115
+ const elapsed = Math.max(0, now - row.startedAt);
116
+ return elapsed >= 1_000 ? `Running… ${formatDuration(elapsed)}` : "Running…";
117
+ }
118
+
119
+ /**
120
+ * How long a command runs before its preview block opens (fold-more.md §5.8). A quick command
121
+ * stays one line for its whole life, so a run of quick commands does not make the screen
122
+ * stretch and shrink.
123
+ */
124
+ export const RUNNING_BLOCK_DELAY_MS = 1_000;
125
+
126
+ /** Whether a running command has run long enough to show its fixed-height preview block. */
127
+ export function showsRunningBlock(row: ToolRow, now = Date.now()): boolean {
128
+ if (row.endedAt !== undefined || row.execStartedAt === undefined) return false;
129
+ return now - row.execStartedAt >= RUNNING_BLOCK_DELAY_MS;
130
+ }
131
+
132
+ /** Rows a running command block takes below its header: script lines only with a description. */
133
+ export function runningBlockHeight(row: ToolRow, previewLines: number): number {
134
+ return (hasDescription(row) ? 2 : 0) + Math.max(0, previewLines);
135
+ }
136
+
137
+ function hasDescription(row: ToolRow): boolean {
138
+ return str(row.args.description).trim().length > 0;
139
+ }
140
+
141
+ /**
142
+ * The fixed-height body of a running command (fold-more.md §5.8): the first two script lines
143
+ * when the header shows a description instead of the command, then the last `previewLines`
144
+ * output lines. Missing lines are padded with the bare rail so the block never changes height
145
+ * while output arrives.
146
+ */
147
+ export function runningBlock(row: ToolRow, width: number, theme: Theme, rail: string, previewLines: number): string[] {
148
+ const railText = theme.fg("dim", rail);
149
+ const room = Math.max(1, width - visibleWidth(rail));
150
+ const lines: string[] = [];
151
+ if (hasDescription(row)) {
152
+ const script = shellCommandLines(row.args);
153
+ for (let index = 0; index < 2; index++) {
154
+ const line = script[index];
155
+ if (line === undefined) {
156
+ lines.push(railText);
157
+ continue;
158
+ }
159
+ const more = index === 1 && script.length > 2 ? " …" : "";
160
+ const prompt = index === 0 ? theme.fg("accent", "$ ") : " ";
161
+ lines.push(`${railText}${prompt}${theme.fg("text", truncateToWidth(line + more, Math.max(1, room - 2), "…"))}`);
162
+ }
163
+ }
164
+ const count = Math.max(0, previewLines);
165
+ if (count > 0) {
166
+ const output = resultLines({ content: [{ type: "text", text: row.partialText }], isError: false }, count, "tail");
167
+ for (let index = 0; index < count; index++) {
168
+ const line = output[index];
169
+ lines.push(line === undefined ? railText : `${railText}${theme.fg("toolOutput", line)}`);
170
+ }
171
+ }
172
+ return lines.map((line) => truncateToWidth(line, width, "…"));
173
+ }