pi-one-ui 0.4.0 → 0.5.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.
@@ -1,183 +0,0 @@
1
- import type { Theme } from "@earendil-works/pi-coding-agent";
2
- import { truncateToWidth, visibleWidth } from "@earendil-works/pi-tui";
3
-
4
- export type CompletionRowsOptions = {
5
- lines: string[];
6
- width: number;
7
- theme: Theme;
8
- selectedPrefix: string;
9
- ownedBackground: boolean;
10
- };
11
-
12
- export type CompletionPaletteOptions = {
13
- lines: string[];
14
- width: number;
15
- theme: Theme;
16
- renderSeparator: (text: string) => string;
17
- ownedBackground: boolean;
18
- };
19
-
20
- const SGR_SEQUENCE = /(?:\x1b\[|\u009b)([0-9:;]*)m/g;
21
-
22
- function normalizeC1Csi(content: string): string {
23
- return content.replace(/\u009b([0-?]*[ -/]*[@-~])/g, "\x1b[$1");
24
- }
25
-
26
- function sgrBackgroundAction(parameters: string): "reset" | "set" | undefined {
27
- let action: "reset" | "set" | undefined;
28
- const values = (parameters || "0").split(";");
29
- for (let index = 0; index < values.length; index++) {
30
- const parameter = values[index] ?? "";
31
- const primary =
32
- parameter === ""
33
- ? 0
34
- : Number.parseInt(parameter.split(":", 1)[0] ?? "", 10);
35
- if (primary === 38 || primary === 48 || primary === 58) {
36
- if (primary === 48) action = "set";
37
- if (!parameter.includes(":")) {
38
- const mode = Number.parseInt(values[index + 1] ?? "", 10);
39
- if (mode === 5) index += 2;
40
- else if (mode === 2) index += 4;
41
- }
42
- continue;
43
- }
44
- if (primary === 0 || primary === 49) action = "reset";
45
- else if (
46
- (primary >= 40 && primary <= 47) ||
47
- (primary >= 100 && primary <= 107)
48
- )
49
- action = "set";
50
- }
51
- return action;
52
- }
53
-
54
- function restoresOwnedBackground(sequence: string): boolean {
55
- const parameters = sequence.slice(sequence.startsWith("\u009b") ? 1 : 2, -1);
56
- return sgrBackgroundAction(parameters) === "reset";
57
- }
58
-
59
- function truncateTerminalLine(content: string, width: number): string {
60
- const safeWidth = Math.max(0, width);
61
- const normalized = normalizeC1Csi(content);
62
- return visibleWidth(normalized) <= safeWidth
63
- ? content
64
- : truncateToWidth(normalized, safeWidth, "");
65
- }
66
-
67
- export function fillTerminalLine(content: string, width: number): string {
68
- const truncated = truncateTerminalLine(content, width);
69
- const renderedWidth = visibleWidth(normalizeC1Csi(truncated));
70
- return `${truncated}${" ".repeat(Math.max(0, width - renderedWidth))}`;
71
- }
72
-
73
- export function applyOwnedLayoutBackground(theme: Theme, text: string): string {
74
- try {
75
- const background = theme.getBgAnsi("userMessageBg");
76
- const restored = text.replace(SGR_SEQUENCE, (sequence) =>
77
- restoresOwnedBackground(sequence) ? `${sequence}${background}` : sequence,
78
- );
79
- return `${background}${restored}\x1b[49m`;
80
- } catch {
81
- try {
82
- let cursor = 0;
83
- let rendered = "";
84
- for (const match of text.matchAll(SGR_SEQUENCE)) {
85
- if (!restoresOwnedBackground(match[0])) continue;
86
- const before = text.slice(cursor, match.index);
87
- if (before) rendered += theme.bg("userMessageBg", before);
88
- rendered += match[0];
89
- cursor = (match.index ?? 0) + match[0].length;
90
- }
91
- const trailing = text.slice(cursor);
92
- if (trailing) rendered += theme.bg("userMessageBg", trailing);
93
- return rendered || theme.bg("userMessageBg", text);
94
- } catch {
95
- return text;
96
- }
97
- }
98
- }
99
-
100
- export function replaceNativeSelectedPrefix(
101
- line: string,
102
- replacement: string,
103
- ): string {
104
- const selectedPrefix = /^((?:(?:\x1b\[|\u009b)[0-9:;]*m)*)→ /;
105
- const match = line.match(selectedPrefix);
106
- if (!match) return line;
107
- return `${replacement}${match[1]}${line.slice(match[0].length)}`;
108
- }
109
-
110
- export function isNativeCompletionCountRow(line: string): boolean {
111
- const withoutSgr = line.replace(/(?:\x1b\[|\u009b)[0-?]*[ -/]*m/g, "");
112
- return /^\s*\(\d+\/\d+\)\s*$/.test(withoutSgr);
113
- }
114
-
115
- export function omitTrailingNativeCompletionCountRow(
116
- lines: string[],
117
- ): string[] {
118
- return lines.length > 0 && isNativeCompletionCountRow(lines.at(-1) ?? "")
119
- ? lines.slice(0, -1)
120
- : lines;
121
- }
122
-
123
- export function renderCompletionRows({
124
- lines,
125
- width,
126
- theme,
127
- selectedPrefix,
128
- ownedBackground,
129
- }: CompletionRowsOptions): string[] {
130
- return lines.map((line) => {
131
- const content = fillTerminalLine(
132
- replaceNativeSelectedPrefix(line, selectedPrefix),
133
- width,
134
- );
135
- const surfaced = ownedBackground
136
- ? applyOwnedLayoutBackground(theme, content)
137
- : content;
138
- return truncateTerminalLine(surfaced, width);
139
- });
140
- }
141
-
142
- function helpLabel(width: number): string {
143
- const labels = [
144
- " ↑↓ Navigate Enter Use Esc Close",
145
- " ↑↓ Nav Enter Use Esc",
146
- " ↑↓ Enter Esc",
147
- ];
148
- return (
149
- labels.find((label) => visibleWidth(label) <= width) ?? labels.at(-1) ?? ""
150
- );
151
- }
152
-
153
- /** Full-width, provenance-preserving completion shell for Opencode editor styles. */
154
- export function renderCompletionPalette({
155
- lines,
156
- width,
157
- theme,
158
- renderSeparator,
159
- ownedBackground,
160
- }: CompletionPaletteOptions): string[] {
161
- const resultLines = omitTrailingNativeCompletionCountRow(lines);
162
- if (resultLines.length === 0) return [];
163
- const safeWidth = Math.max(0, width);
164
- const rows = renderCompletionRows({
165
- lines: resultLines,
166
- width: safeWidth,
167
- theme,
168
- selectedPrefix: " ",
169
- ownedBackground,
170
- });
171
- const separator = renderSeparator("─".repeat(safeWidth));
172
- if (safeWidth < 3)
173
- return [...rows, truncateToWidth(separator, safeWidth, "")];
174
- const helpText = fillTerminalLine(helpLabel(safeWidth), safeWidth);
175
- const help = ownedBackground
176
- ? applyOwnedLayoutBackground(theme, helpText)
177
- : helpText;
178
- return [
179
- ...rows,
180
- truncateToWidth(separator, safeWidth, ""),
181
- truncateToWidth(help, safeWidth, ""),
182
- ];
183
- }