@aoshizuku/pi-bettertui 0.1.0 → 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/README.md +1 -1
- package/package.json +1 -1
- package/src/format.ts +165 -165
- package/src/gutter.ts +3 -5
- package/src/index.ts +660 -660
- package/src/segment.ts +311 -311
- package/src/shell-description.ts +75 -75
- package/src/thinking.ts +524 -524
- package/src/timeline.ts +881 -881
- package/src/tool-lines.ts +173 -173
- package/src/tool-row.ts +387 -387
- package/src/tool-summary.ts +237 -234
- package/src/types.ts +13 -13
package/README.md
CHANGED
|
@@ -42,7 +42,7 @@
|
|
|
42
42
|
|
|
43
43
|
Pi 原生给每条工具套 `Box` + 背景 + `Spacer(1)`,bash 还默认展示 5 行输出。`pi-bettertui` 用 `renderShell: "self"` 重画:
|
|
44
44
|
|
|
45
|
-
- `read/grep/find/ls`:一行 `Read(path:range) · N lines` / `Grep(pattern in path) · N matches
|
|
45
|
+
- `read/grep/find/ls`:一行 `Read(path:range) · N lines` / `Grep(pattern in path) · N matches`;读图片写 `image`,读取时的模型不支持图片输入则写 `image · model has no image input`(pi 1.6.0 起)。
|
|
46
46
|
- `edit`:`Edit(path) · +N −M`。
|
|
47
47
|
- `write`:`Write(path) · N lines`。
|
|
48
48
|
- `bash/powershell`:`Bash(描述) · Done · N lines · 2.0s`;失败时显示最后 2 行错误。展开后先列出完整命令(`$ cmd`,自动换行,最多 12 行),再列输出。
|
package/package.json
CHANGED
package/src/format.ts
CHANGED
|
@@ -1,165 +1,165 @@
|
|
|
1
|
-
import { homedir } from "node:os";
|
|
2
|
-
import { isAbsolute, relative, sep } from "node:path";
|
|
3
|
-
import { stripTerminalSequences, visibleWidth, wrapTextWithAnsi } from "@earendil-works/pi-tui";
|
|
4
|
-
|
|
5
|
-
const graphemeSegmenter = new Intl.Segmenter(undefined, { granularity: "grapheme" });
|
|
6
|
-
|
|
7
|
-
export const str = (value: unknown): string => (typeof value === "string" ? value : "");
|
|
8
|
-
|
|
9
|
-
export const argsOf = (value: unknown): Record<string, unknown> =>
|
|
10
|
-
typeof value === "object" && value !== null && !Array.isArray(value)
|
|
11
|
-
? (value as Record<string, unknown>)
|
|
12
|
-
: {};
|
|
13
|
-
|
|
14
|
-
export function plural(count: number, one: string, many = `${one}s`): string {
|
|
15
|
-
return `${count} ${count === 1 ? one : many}`;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export function countLines(text: string): number {
|
|
19
|
-
const trimmed = text.trim();
|
|
20
|
-
if (!trimmed || trimmed === "(no output)") return 0;
|
|
21
|
-
return trimmed.split("\n").length;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export function firstLine(text: string): string {
|
|
25
|
-
const lines = sanitizeText(text).replace(/\r\n?/g, "\n").trim().split("\n");
|
|
26
|
-
const head = lines[0] ?? "";
|
|
27
|
-
return lines.length > 1 ? `${head} … (+${lines.length - 1} lines)` : head;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export function sanitizeText(text: string): string {
|
|
31
|
-
return stripTerminalSequences(text)
|
|
32
|
-
.replace(/\r\n/g, "\n")
|
|
33
|
-
.split("\n")
|
|
34
|
-
.map((line) => {
|
|
35
|
-
// Progress bars use carriage returns; keep the final frame only.
|
|
36
|
-
const frames = line.replace(/\r+$/, "");
|
|
37
|
-
return frames.slice(frames.lastIndexOf("\r") + 1);
|
|
38
|
-
})
|
|
39
|
-
.join("\n")
|
|
40
|
-
.replace(/[\x00-\x08\x0B\x0C\x0E-\x1F\uFFF9-\uFFFB]/g, "");
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export function displayPath(path: string, cwd: string, home = homedir()): string {
|
|
44
|
-
if (!path || !isAbsolute(path)) return path;
|
|
45
|
-
const inCwd = inside(cwd, path);
|
|
46
|
-
if (inCwd !== undefined) return inCwd;
|
|
47
|
-
const inHome = inside(home, path);
|
|
48
|
-
return inHome === undefined ? path : inHome ? `~/${inHome}` : "~";
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
function inside(base: string, path: string): string | undefined {
|
|
52
|
-
const result = relative(base, path);
|
|
53
|
-
return result !== ".." && !result.startsWith(`..${sep}`) && !isAbsolute(result)
|
|
54
|
-
? result.split(sep).join("/")
|
|
55
|
-
: undefined;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
export function lineRange(args: Record<string, unknown>): string {
|
|
59
|
-
const offset = args.offset;
|
|
60
|
-
const limit = args.limit;
|
|
61
|
-
if (typeof offset !== "number" && typeof limit !== "number") return "";
|
|
62
|
-
const start = typeof offset === "number" ? offset : 1;
|
|
63
|
-
return typeof limit === "number" ? `:${start}-${start + limit - 1}` : `:${start}`;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
export function formatDuration(ms: number): string {
|
|
67
|
-
if (!Number.isFinite(ms) || ms < 0) return "0s";
|
|
68
|
-
if (ms < 1_000) return "<1s";
|
|
69
|
-
const seconds = Math.floor(ms / 1_000);
|
|
70
|
-
if (seconds < 60) return `${seconds}s`;
|
|
71
|
-
const minutes = Math.floor(seconds / 60);
|
|
72
|
-
const rest = seconds % 60;
|
|
73
|
-
if (minutes < 60) return rest ? `${minutes}m ${rest}s` : `${minutes}m`;
|
|
74
|
-
const hours = Math.floor(minutes / 60);
|
|
75
|
-
const mins = minutes % 60;
|
|
76
|
-
return mins ? `${hours}h ${mins}m` : `${hours}h`;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* The header of a finished thought: `Thought for 18s`, or plain `Thought` under a second or
|
|
81
|
-
* without a known duration. The standalone thinking block and the fold list share it.
|
|
82
|
-
*/
|
|
83
|
-
/** `Thought for 0.8s` / `Thought for 12.4s` under a minute, `Thought for 2m 5s` beyond; `Thought` when unknown. */
|
|
84
|
-
export function thoughtLabel(ms: number | undefined): string {
|
|
85
|
-
if (ms === undefined || !Number.isFinite(ms) || ms < 100) return "Thought";
|
|
86
|
-
if (ms < 60_000) return `Thought for ${(Math.floor(ms / 100) / 10).toFixed(1)}s`;
|
|
87
|
-
return `Thought for ${formatDuration(ms)}`;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
export function formatElapsed(ms: number): string {
|
|
91
|
-
if (!Number.isFinite(ms) || ms < 0) return "0.0s";
|
|
92
|
-
if (ms < 1_000) return `${Math.max(0, Math.round(ms))}ms`;
|
|
93
|
-
return `${(ms / 1_000).toFixed(1)}s`;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* Clip `text` to `budget` visible columns by removing its middle, so both the
|
|
98
|
-
* program and the end of a long command stay readable. Grapheme-safe.
|
|
99
|
-
*/
|
|
100
|
-
export function clipMiddle(text: string, budget: number): string {
|
|
101
|
-
if (budget <= 0) return "";
|
|
102
|
-
if (visibleWidth(text) <= budget) return text;
|
|
103
|
-
if (budget === 1) return "…";
|
|
104
|
-
const parts = Array.from(graphemeSegmenter.segment(text), (part) => part.segment);
|
|
105
|
-
const room = budget - 1;
|
|
106
|
-
const headBudget = Math.ceil(room * 0.6);
|
|
107
|
-
const tailBudget = room - headBudget;
|
|
108
|
-
let head = "";
|
|
109
|
-
let headWidth = 0;
|
|
110
|
-
let start = 0;
|
|
111
|
-
for (; start < parts.length; start++) {
|
|
112
|
-
const width = visibleWidth(parts[start]!);
|
|
113
|
-
if (headWidth + width > headBudget) break;
|
|
114
|
-
head += parts[start];
|
|
115
|
-
headWidth += width;
|
|
116
|
-
}
|
|
117
|
-
let tail = "";
|
|
118
|
-
let tailWidth = 0;
|
|
119
|
-
for (let end = parts.length - 1; end >= start; end--) {
|
|
120
|
-
const width = visibleWidth(parts[end]!);
|
|
121
|
-
if (tailWidth + width > tailBudget + (headBudget - headWidth)) break;
|
|
122
|
-
tail = parts[end] + tail;
|
|
123
|
-
tailWidth += width;
|
|
124
|
-
}
|
|
125
|
-
return `${head.trimEnd()}…${tail.trimStart()}`;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
/** Inside a paragraph longer than the tail window, the wrap start moves in steps of this many characters. */
|
|
129
|
-
const THINKING_TAIL_STEP = 4_096;
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* The newest `count` screen rows of a thought, word-wrapped at `width` (DESIGN.md §2.1).
|
|
133
|
-
*
|
|
134
|
-
* Rows that are full do not change as text streams in: only the last one grows, then the block
|
|
135
|
-
* moves up a row. Only the tail is wrapped, and it starts at a paragraph start (inside a very long
|
|
136
|
-
* paragraph, at a fixed step from that start), never at "the last N characters": a start that moved
|
|
137
|
-
* with every token would move every line break after it, and the rows would slide again.
|
|
138
|
-
*/
|
|
139
|
-
export function tailThinkingRows(
|
|
140
|
-
text: string,
|
|
141
|
-
count: number,
|
|
142
|
-
width: number,
|
|
143
|
-
): { rows: string[]; hidden: boolean } {
|
|
144
|
-
const start = thinkingTailStart(text, Math.max(2_000, count * width * 2));
|
|
145
|
-
const rows = sanitizeText(text.slice(start))
|
|
146
|
-
.split("\n")
|
|
147
|
-
.map((line) => line.replace(/\s+/g, " ").trim())
|
|
148
|
-
.filter(Boolean)
|
|
149
|
-
.flatMap((paragraph) => wrapTextWithAnsi(paragraph, Math.max(1, width)));
|
|
150
|
-
return { rows: rows.slice(-count), hidden: start > 0 || rows.length > count };
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
/** Where the wrapped tail starts: at most `window` characters from the end, on a stable boundary. */
|
|
154
|
-
function thinkingTailStart(text: string, window: number): number {
|
|
155
|
-
const cut = text.length - window;
|
|
156
|
-
if (cut <= 0) return 0;
|
|
157
|
-
const paragraph = text.lastIndexOf("\n", cut) + 1;
|
|
158
|
-
const start = paragraph + Math.floor((cut - paragraph) / THINKING_TAIL_STEP) * THINKING_TAIL_STEP;
|
|
159
|
-
// Never begin on the second half of a surrogate pair.
|
|
160
|
-
return /[\uDC00-\uDFFF]/.test(text[start] ?? "") ? start + 1 : start;
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
export function stripNotice(text: string): string {
|
|
164
|
-
return text.replace(/\n+$/, "");
|
|
165
|
-
}
|
|
1
|
+
import { homedir } from "node:os";
|
|
2
|
+
import { isAbsolute, relative, sep } from "node:path";
|
|
3
|
+
import { stripTerminalSequences, visibleWidth, wrapTextWithAnsi } from "@earendil-works/pi-tui";
|
|
4
|
+
|
|
5
|
+
const graphemeSegmenter = new Intl.Segmenter(undefined, { granularity: "grapheme" });
|
|
6
|
+
|
|
7
|
+
export const str = (value: unknown): string => (typeof value === "string" ? value : "");
|
|
8
|
+
|
|
9
|
+
export const argsOf = (value: unknown): Record<string, unknown> =>
|
|
10
|
+
typeof value === "object" && value !== null && !Array.isArray(value)
|
|
11
|
+
? (value as Record<string, unknown>)
|
|
12
|
+
: {};
|
|
13
|
+
|
|
14
|
+
export function plural(count: number, one: string, many = `${one}s`): string {
|
|
15
|
+
return `${count} ${count === 1 ? one : many}`;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function countLines(text: string): number {
|
|
19
|
+
const trimmed = text.trim();
|
|
20
|
+
if (!trimmed || trimmed === "(no output)") return 0;
|
|
21
|
+
return trimmed.split("\n").length;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function firstLine(text: string): string {
|
|
25
|
+
const lines = sanitizeText(text).replace(/\r\n?/g, "\n").trim().split("\n");
|
|
26
|
+
const head = lines[0] ?? "";
|
|
27
|
+
return lines.length > 1 ? `${head} … (+${lines.length - 1} lines)` : head;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function sanitizeText(text: string): string {
|
|
31
|
+
return stripTerminalSequences(text)
|
|
32
|
+
.replace(/\r\n/g, "\n")
|
|
33
|
+
.split("\n")
|
|
34
|
+
.map((line) => {
|
|
35
|
+
// Progress bars use carriage returns; keep the final frame only.
|
|
36
|
+
const frames = line.replace(/\r+$/, "");
|
|
37
|
+
return frames.slice(frames.lastIndexOf("\r") + 1);
|
|
38
|
+
})
|
|
39
|
+
.join("\n")
|
|
40
|
+
.replace(/[\x00-\x08\x0B\x0C\x0E-\x1F\uFFF9-\uFFFB]/g, "");
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function displayPath(path: string, cwd: string, home = homedir()): string {
|
|
44
|
+
if (!path || !isAbsolute(path)) return path;
|
|
45
|
+
const inCwd = inside(cwd, path);
|
|
46
|
+
if (inCwd !== undefined) return inCwd;
|
|
47
|
+
const inHome = inside(home, path);
|
|
48
|
+
return inHome === undefined ? path : inHome ? `~/${inHome}` : "~";
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function inside(base: string, path: string): string | undefined {
|
|
52
|
+
const result = relative(base, path);
|
|
53
|
+
return result !== ".." && !result.startsWith(`..${sep}`) && !isAbsolute(result)
|
|
54
|
+
? result.split(sep).join("/")
|
|
55
|
+
: undefined;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function lineRange(args: Record<string, unknown>): string {
|
|
59
|
+
const offset = args.offset;
|
|
60
|
+
const limit = args.limit;
|
|
61
|
+
if (typeof offset !== "number" && typeof limit !== "number") return "";
|
|
62
|
+
const start = typeof offset === "number" ? offset : 1;
|
|
63
|
+
return typeof limit === "number" ? `:${start}-${start + limit - 1}` : `:${start}`;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function formatDuration(ms: number): string {
|
|
67
|
+
if (!Number.isFinite(ms) || ms < 0) return "0s";
|
|
68
|
+
if (ms < 1_000) return "<1s";
|
|
69
|
+
const seconds = Math.floor(ms / 1_000);
|
|
70
|
+
if (seconds < 60) return `${seconds}s`;
|
|
71
|
+
const minutes = Math.floor(seconds / 60);
|
|
72
|
+
const rest = seconds % 60;
|
|
73
|
+
if (minutes < 60) return rest ? `${minutes}m ${rest}s` : `${minutes}m`;
|
|
74
|
+
const hours = Math.floor(minutes / 60);
|
|
75
|
+
const mins = minutes % 60;
|
|
76
|
+
return mins ? `${hours}h ${mins}m` : `${hours}h`;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* The header of a finished thought: `Thought for 18s`, or plain `Thought` under a second or
|
|
81
|
+
* without a known duration. The standalone thinking block and the fold list share it.
|
|
82
|
+
*/
|
|
83
|
+
/** `Thought for 0.8s` / `Thought for 12.4s` under a minute, `Thought for 2m 5s` beyond; `Thought` when unknown. */
|
|
84
|
+
export function thoughtLabel(ms: number | undefined): string {
|
|
85
|
+
if (ms === undefined || !Number.isFinite(ms) || ms < 100) return "Thought";
|
|
86
|
+
if (ms < 60_000) return `Thought for ${(Math.floor(ms / 100) / 10).toFixed(1)}s`;
|
|
87
|
+
return `Thought for ${formatDuration(ms)}`;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function formatElapsed(ms: number): string {
|
|
91
|
+
if (!Number.isFinite(ms) || ms < 0) return "0.0s";
|
|
92
|
+
if (ms < 1_000) return `${Math.max(0, Math.round(ms))}ms`;
|
|
93
|
+
return `${(ms / 1_000).toFixed(1)}s`;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Clip `text` to `budget` visible columns by removing its middle, so both the
|
|
98
|
+
* program and the end of a long command stay readable. Grapheme-safe.
|
|
99
|
+
*/
|
|
100
|
+
export function clipMiddle(text: string, budget: number): string {
|
|
101
|
+
if (budget <= 0) return "";
|
|
102
|
+
if (visibleWidth(text) <= budget) return text;
|
|
103
|
+
if (budget === 1) return "…";
|
|
104
|
+
const parts = Array.from(graphemeSegmenter.segment(text), (part) => part.segment);
|
|
105
|
+
const room = budget - 1;
|
|
106
|
+
const headBudget = Math.ceil(room * 0.6);
|
|
107
|
+
const tailBudget = room - headBudget;
|
|
108
|
+
let head = "";
|
|
109
|
+
let headWidth = 0;
|
|
110
|
+
let start = 0;
|
|
111
|
+
for (; start < parts.length; start++) {
|
|
112
|
+
const width = visibleWidth(parts[start]!);
|
|
113
|
+
if (headWidth + width > headBudget) break;
|
|
114
|
+
head += parts[start];
|
|
115
|
+
headWidth += width;
|
|
116
|
+
}
|
|
117
|
+
let tail = "";
|
|
118
|
+
let tailWidth = 0;
|
|
119
|
+
for (let end = parts.length - 1; end >= start; end--) {
|
|
120
|
+
const width = visibleWidth(parts[end]!);
|
|
121
|
+
if (tailWidth + width > tailBudget + (headBudget - headWidth)) break;
|
|
122
|
+
tail = parts[end] + tail;
|
|
123
|
+
tailWidth += width;
|
|
124
|
+
}
|
|
125
|
+
return `${head.trimEnd()}…${tail.trimStart()}`;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** Inside a paragraph longer than the tail window, the wrap start moves in steps of this many characters. */
|
|
129
|
+
const THINKING_TAIL_STEP = 4_096;
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* The newest `count` screen rows of a thought, word-wrapped at `width` (DESIGN.md §2.1).
|
|
133
|
+
*
|
|
134
|
+
* Rows that are full do not change as text streams in: only the last one grows, then the block
|
|
135
|
+
* moves up a row. Only the tail is wrapped, and it starts at a paragraph start (inside a very long
|
|
136
|
+
* paragraph, at a fixed step from that start), never at "the last N characters": a start that moved
|
|
137
|
+
* with every token would move every line break after it, and the rows would slide again.
|
|
138
|
+
*/
|
|
139
|
+
export function tailThinkingRows(
|
|
140
|
+
text: string,
|
|
141
|
+
count: number,
|
|
142
|
+
width: number,
|
|
143
|
+
): { rows: string[]; hidden: boolean } {
|
|
144
|
+
const start = thinkingTailStart(text, Math.max(2_000, count * width * 2));
|
|
145
|
+
const rows = sanitizeText(text.slice(start))
|
|
146
|
+
.split("\n")
|
|
147
|
+
.map((line) => line.replace(/\s+/g, " ").trim())
|
|
148
|
+
.filter(Boolean)
|
|
149
|
+
.flatMap((paragraph) => wrapTextWithAnsi(paragraph, Math.max(1, width)));
|
|
150
|
+
return { rows: rows.slice(-count), hidden: start > 0 || rows.length > count };
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** Where the wrapped tail starts: at most `window` characters from the end, on a stable boundary. */
|
|
154
|
+
function thinkingTailStart(text: string, window: number): number {
|
|
155
|
+
const cut = text.length - window;
|
|
156
|
+
if (cut <= 0) return 0;
|
|
157
|
+
const paragraph = text.lastIndexOf("\n", cut) + 1;
|
|
158
|
+
const start = paragraph + Math.floor((cut - paragraph) / THINKING_TAIL_STEP) * THINKING_TAIL_STEP;
|
|
159
|
+
// Never begin on the second half of a surrogate pair.
|
|
160
|
+
return /[\uDC00-\uDFFF]/.test(text[start] ?? "") ? start + 1 : start;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export function stripNotice(text: string): string {
|
|
164
|
+
return text.replace(/\n+$/, "");
|
|
165
|
+
}
|
package/src/gutter.ts
CHANGED
|
@@ -35,11 +35,9 @@ export class MarkedBlock implements Component {
|
|
|
35
35
|
) {}
|
|
36
36
|
|
|
37
37
|
render(width: number): string[] {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
const out = [...lines];
|
|
42
|
-
out[this.line] = placeMarker(out[this.line]!, this.marker());
|
|
38
|
+
// Components hand out cached arrays; never write into one.
|
|
39
|
+
const out = [...this.block.render(width)];
|
|
40
|
+
if (out.length > this.line) out[this.line] = placeMarker(out[this.line]!, this.marker());
|
|
43
41
|
return out;
|
|
44
42
|
}
|
|
45
43
|
|