@ftrdotdev/pi-tui 0.2.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.
- package/LICENSE +21 -0
- package/README.md +63 -0
- package/package.json +80 -0
- package/src/autocomplete-contract.test.ts +126 -0
- package/src/autocomplete-contract.ts +91 -0
- package/src/commands/git-execution.test.ts +69 -0
- package/src/commands/git-execution.ts +198 -0
- package/src/commands/git-status.test.ts +379 -0
- package/src/commands/git-status.ts +320 -0
- package/src/components/bordered-box.test.ts +136 -0
- package/src/components/bordered-box.ts +106 -0
- package/src/components/define-component.ts +33 -0
- package/src/components/index.ts +35 -0
- package/src/config.test.ts +469 -0
- package/src/config.ts +307 -0
- package/src/frame/box-frame.test.ts +44 -0
- package/src/frame/box-frame.ts +160 -0
- package/src/icons.ts +203 -0
- package/src/index.ts +359 -0
- package/src/runtime.ts +256 -0
- package/src/session-lifecycle.ts +41 -0
- package/src/state.test.ts +7 -0
- package/src/state.ts +90 -0
- package/src/telemetry.ts +301 -0
- package/src/ui/editor-tint.test.ts +70 -0
- package/src/ui/editor-tint.ts +78 -0
- package/src/ui/editor.ts +403 -0
- package/src/ui/footer.test.ts +350 -0
- package/src/ui/footer.ts +225 -0
- package/src/ui/header.test.ts +260 -0
- package/src/ui/header.ts +165 -0
- package/src/ui/preview.test.ts +639 -0
- package/src/ui/preview.ts +374 -0
- package/src/ui/render-helpers.ts +275 -0
- package/src/ui/settings-command.test.ts +218 -0
- package/src/ui/settings-command.ts +390 -0
- package/src/utils.ts +448 -0
- package/src/vim/editor-close-autocomplete.ts +26 -0
- package/src/vim/editor-host.test.ts +340 -0
- package/src/vim/editor-state.test.ts +385 -0
- package/src/vim/editor-state.ts +314 -0
- package/src/vim/mode-indicator.test.ts +27 -0
- package/src/vim/mode-indicator.ts +44 -0
- package/src/vim/selection-render.test.ts +77 -0
- package/src/vim/selection-render.ts +85 -0
- package/src/vim/text-model.test.ts +85 -0
- package/src/vim/text-model.ts +75 -0
- package/src/vim/vim-router.test.ts +50 -0
- package/src/vim/vim-router.ts +55 -0
- package/src/vim/word-motion.ts +96 -0
package/src/utils.ts
ADDED
|
@@ -0,0 +1,448 @@
|
|
|
1
|
+
import { isAbsolute, relative, resolve, sep } from "node:path";
|
|
2
|
+
import type { ThinkingLevel } from "@earendil-works/pi-ai";
|
|
3
|
+
import type { Theme, ThemeColor } from "@earendil-works/pi-coding-agent";
|
|
4
|
+
import { truncateToWidth, visibleWidth } from "@earendil-works/pi-tui";
|
|
5
|
+
import { Effect, Random } from "effect";
|
|
6
|
+
|
|
7
|
+
export { truncateToWidth, visibleWidth };
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Strip ANSI escape sequences (SGR color/style, OSC, and underscore-OSC
|
|
11
|
+
* variants) from a themed string. Used for plain-text comparisons and for
|
|
12
|
+
* measuring content without styling noise.
|
|
13
|
+
*/
|
|
14
|
+
export function stripAnsi(text: string): string {
|
|
15
|
+
return text
|
|
16
|
+
.replace(/\x1b\[[0-9;]*m/g, "")
|
|
17
|
+
.replace(/\x1b\][^\x07]*(?:\x07|\x1b\\)/g, "")
|
|
18
|
+
.replace(/\x1b_[^\x07]*\x07/g, "");
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Collapse a working directory inside the user's home to a `~`-relative
|
|
23
|
+
* path (`/Users/x/dev` -> `~/dev`, the home itself -> `~`). Paths outside
|
|
24
|
+
* the home (or with no home resolvable) render literally.
|
|
25
|
+
*/
|
|
26
|
+
export function formatCwd(cwd: string): string {
|
|
27
|
+
const home = process.env.HOME || process.env.USERPROFILE;
|
|
28
|
+
if (!home) return cwd;
|
|
29
|
+
const resolvedCwd = resolve(cwd);
|
|
30
|
+
const resolvedHome = resolve(home);
|
|
31
|
+
const rel = relative(resolvedHome, resolvedCwd);
|
|
32
|
+
const insideHome =
|
|
33
|
+
rel === "" || (rel !== ".." && !rel.startsWith(`..${sep}`) && !isAbsolute(rel));
|
|
34
|
+
if (!insideHome) return cwd;
|
|
35
|
+
return rel === "" ? "~" : `~${sep}${rel}`;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Truncate a path to `maxLen` visible columns while keeping the first
|
|
40
|
+
* segment (e.g. `~`) and as many trailing segments as fit, joined by
|
|
41
|
+
* `...` — e.g. `~/dev/.../packages/tui`.
|
|
42
|
+
*/
|
|
43
|
+
export function truncatePath(path: string, maxLen: number): string {
|
|
44
|
+
if (path.length <= maxLen) return path;
|
|
45
|
+
if (maxLen <= 3) return "...".slice(0, maxLen);
|
|
46
|
+
const sepChar = path.includes("/") ? "/" : "\\";
|
|
47
|
+
const parts = path.split(/[\\/]/);
|
|
48
|
+
if (parts.length <= 2) return path.slice(0, maxLen - 3) + "...";
|
|
49
|
+
// Keep first segment (e.g. ~) and as many trailing segments as fit.
|
|
50
|
+
const tail: string[] = [];
|
|
51
|
+
let tailLen = 0;
|
|
52
|
+
for (let i = parts.length - 1; i >= 1; i--) {
|
|
53
|
+
const seg = parts[i]!;
|
|
54
|
+
if (tailLen + seg.length + 4 > maxLen) break;
|
|
55
|
+
tail.unshift(seg);
|
|
56
|
+
tailLen += seg.length + 1;
|
|
57
|
+
}
|
|
58
|
+
const head = parts[0]!;
|
|
59
|
+
const result = `${head}${sepChar}...${sepChar}${tail.join(sepChar)}`;
|
|
60
|
+
return result.length > maxLen ? result.slice(0, maxLen - 3) + "..." : result;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Compact token-count formatting: plain digits below 1k, one decimal in
|
|
65
|
+
* the 1k–10k band, whole `k` below 1M, then `M` (one decimal below 10M).
|
|
66
|
+
*/
|
|
67
|
+
export function fmtTokens(n: number): string {
|
|
68
|
+
if (n < 1000) return n.toString();
|
|
69
|
+
if (n < 10_000) return `${(n / 1000).toFixed(1)}k`;
|
|
70
|
+
if (n < 1_000_000) return `${Math.round(n / 1000)}k`;
|
|
71
|
+
if (n < 10_000_000) return `${(n / 1_000_000).toFixed(1)}M`;
|
|
72
|
+
return `${Math.round(n / 1_000_000)}M`;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Cost display: the ascii cost glyph already is a `$`, so the currency
|
|
77
|
+
* sign is appended only when the glyph does not carry it (nerd glyphs).
|
|
78
|
+
*/
|
|
79
|
+
export function formatCost(glyph: string, value: number, digits = 3): string {
|
|
80
|
+
const currency = glyph.includes("$") ? "" : "$";
|
|
81
|
+
return `${glyph} ${currency}${value.toFixed(digits)}`;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Human duration formatting from milliseconds: `42s`, `5m 12s`, `2h 3m 4s`.
|
|
86
|
+
* Negative inputs clamp to zero.
|
|
87
|
+
*/
|
|
88
|
+
export function formatDuration(ms: number): string {
|
|
89
|
+
const totalSeconds = Math.max(0, Math.floor(ms / 1000));
|
|
90
|
+
if (totalSeconds < 60) return `${totalSeconds}s`;
|
|
91
|
+
const s = totalSeconds % 60;
|
|
92
|
+
const totalMinutes = Math.floor(totalSeconds / 60);
|
|
93
|
+
if (totalMinutes < 60) return `${totalMinutes}m ${s}s`;
|
|
94
|
+
const m = totalMinutes % 60;
|
|
95
|
+
const h = Math.floor(totalMinutes / 60);
|
|
96
|
+
return `${h}h ${m}m ${s}s`;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* The `provider/model` label shown in the header and footer, or
|
|
101
|
+
* `no-model` when no model is selected.
|
|
102
|
+
*/
|
|
103
|
+
export function formatModelLabel(
|
|
104
|
+
model: { provider?: string; id?: string } | null | undefined,
|
|
105
|
+
): string {
|
|
106
|
+
if (!model?.id) return "no-model";
|
|
107
|
+
return model.provider ? `${model.provider}/${model.id}` : model.id;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Capitalized provider display label; `Unknown` when absent.
|
|
112
|
+
*/
|
|
113
|
+
export function formatProviderLabel(provider: string | undefined): string {
|
|
114
|
+
if (!provider) return "Unknown";
|
|
115
|
+
return provider.charAt(0).toUpperCase() + provider.slice(1);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Compose a single line from a left block and a right block: the right
|
|
120
|
+
* block is right-aligned (truncated with ellipsis when it alone exceeds
|
|
121
|
+
* `width`), and the left block fills the remainder — truncated if it
|
|
122
|
+
* does not fit — separated by one space.
|
|
123
|
+
*/
|
|
124
|
+
export function alignRight(left: string, right: string, width: number, theme: Theme): string {
|
|
125
|
+
const rightW = visibleWidth(right);
|
|
126
|
+
if (rightW > width) {
|
|
127
|
+
right = truncateToWidth(right, width, theme.fg("dim", "..."));
|
|
128
|
+
}
|
|
129
|
+
const leftW = visibleWidth(left);
|
|
130
|
+
const rightW2 = visibleWidth(right);
|
|
131
|
+
const pad = width - leftW - rightW2;
|
|
132
|
+
if (pad >= 1) {
|
|
133
|
+
return left + " ".repeat(pad) + right;
|
|
134
|
+
}
|
|
135
|
+
const availableForLeft = Math.max(0, width - rightW2 - 1);
|
|
136
|
+
const truncatedLeft =
|
|
137
|
+
availableForLeft > 0 ? truncateToWidth(left, availableForLeft, theme.fg("dim", "...")) : "";
|
|
138
|
+
return truncatedLeft ? truncatedLeft + " " + right : right;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** A text segment with a survival priority; higher survives longer. */
|
|
142
|
+
export type PrioritizedSegment = {
|
|
143
|
+
text: string;
|
|
144
|
+
priority: number;
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Pack segments into maxWidth, shrinking/dropping lowest-priority segments first.
|
|
149
|
+
* Higher priority = survives longer. Returns the surviving segment texts in
|
|
150
|
+
* original order, space-joined. Each segment is either kept whole, truncated
|
|
151
|
+
* with ellipsis, or dropped entirely.
|
|
152
|
+
*/
|
|
153
|
+
export function fitSegmentsByPriority(
|
|
154
|
+
segs: readonly PrioritizedSegment[],
|
|
155
|
+
maxW: number,
|
|
156
|
+
ellipsis = "...",
|
|
157
|
+
): string[] {
|
|
158
|
+
const items = segs.map((s) => ({ text: s.text, priority: s.priority, w: visibleWidth(s.text) }));
|
|
159
|
+
const totalW = () => {
|
|
160
|
+
const active = items.filter((it) => it.text !== "");
|
|
161
|
+
return active.reduce((a, it) => a + it.w, 0) + Math.max(0, active.length - 1);
|
|
162
|
+
};
|
|
163
|
+
while (totalW() > maxW) {
|
|
164
|
+
let target = -1;
|
|
165
|
+
for (let i = 0; i < items.length; i++) {
|
|
166
|
+
if (
|
|
167
|
+
items[i]?.text !== "" &&
|
|
168
|
+
(target === -1 || (items[i]?.priority ?? -1) < (items[target]?.priority ?? -1))
|
|
169
|
+
) {
|
|
170
|
+
target = i;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
if (target === -1) break;
|
|
174
|
+
const item = items[target];
|
|
175
|
+
if (!item) break;
|
|
176
|
+
|
|
177
|
+
const others = items.filter((_, i) => i !== target && items[i]?.text !== "");
|
|
178
|
+
const otherW = others.reduce((a, it) => a + it.w, 0) + Math.max(0, others.length - 1);
|
|
179
|
+
const avail = maxW - otherW - (others.length > 0 ? 1 : 0);
|
|
180
|
+
if (avail <= visibleWidth(ellipsis)) {
|
|
181
|
+
item.text = "";
|
|
182
|
+
item.w = 0;
|
|
183
|
+
} else if (avail < item.w) {
|
|
184
|
+
item.text = truncateToWidth(item.text, avail, ellipsis);
|
|
185
|
+
item.w = visibleWidth(item.text);
|
|
186
|
+
} else {
|
|
187
|
+
break;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
return items.filter((it) => it.text !== "").map((it) => it.text);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Color token for a utilization percentage (accent under `warn`, warning
|
|
195
|
+
* up to `danger`, error from there).
|
|
196
|
+
*/
|
|
197
|
+
export function stressColor(value: number, warn = 70, danger = 90): ThemeColor {
|
|
198
|
+
if (value >= danger) return "error";
|
|
199
|
+
if (value >= warn) return "warning";
|
|
200
|
+
return "accent";
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Color token for a cache-hit rate: error below 30%, warning below 70%,
|
|
205
|
+
* success from there.
|
|
206
|
+
*/
|
|
207
|
+
export function cacheHitColor(value: number): ThemeColor {
|
|
208
|
+
if (value < 30) return "error";
|
|
209
|
+
if (value < 70) return "warning";
|
|
210
|
+
return "success";
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Color token for a provider name (identity styling); `muted` for
|
|
215
|
+
* unlisted providers.
|
|
216
|
+
*/
|
|
217
|
+
export function providerColor(provider: string): ThemeColor {
|
|
218
|
+
switch (provider.toLowerCase()) {
|
|
219
|
+
case "anthropic":
|
|
220
|
+
return "accent";
|
|
221
|
+
case "openai":
|
|
222
|
+
case "openai-codex":
|
|
223
|
+
return "success";
|
|
224
|
+
case "google":
|
|
225
|
+
case "google-vertex":
|
|
226
|
+
return "warning";
|
|
227
|
+
case "opencode":
|
|
228
|
+
case "opencode-go":
|
|
229
|
+
return "success";
|
|
230
|
+
case "amazon-bedrock":
|
|
231
|
+
return "thinkingHigh";
|
|
232
|
+
case "github-copilot":
|
|
233
|
+
return "mdLink";
|
|
234
|
+
case "deepseek":
|
|
235
|
+
return "thinkingLow";
|
|
236
|
+
case "xai":
|
|
237
|
+
case "groq":
|
|
238
|
+
return "error";
|
|
239
|
+
default:
|
|
240
|
+
return "muted";
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Color token for a thinking-effort label; unknown levels fall back to
|
|
246
|
+
* `thinkingMedium`.
|
|
247
|
+
*/
|
|
248
|
+
export function effortColor(level: ThinkingLevel | string | undefined): ThemeColor {
|
|
249
|
+
switch (level) {
|
|
250
|
+
case "minimal":
|
|
251
|
+
return "thinkingMinimal";
|
|
252
|
+
case "low":
|
|
253
|
+
return "thinkingLow";
|
|
254
|
+
case "medium":
|
|
255
|
+
return "thinkingMedium";
|
|
256
|
+
case "high":
|
|
257
|
+
return "thinkingHigh";
|
|
258
|
+
case "xhigh":
|
|
259
|
+
return "thinkingXhigh";
|
|
260
|
+
default:
|
|
261
|
+
return "thinkingMedium";
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* True when a line is one of the editor's border rows: a pure dash run, or
|
|
267
|
+
* a dash-framed `↑ n more`/`↓ n more` collapsed-messages hint.
|
|
268
|
+
*/
|
|
269
|
+
export function isEditorBorderLine(line: string): boolean {
|
|
270
|
+
const plain = stripAnsi(line);
|
|
271
|
+
if (/^─+$/.test(plain)) return true;
|
|
272
|
+
if (/^─*\s*[↑↓]\s+\d+\s+more\s*─*$/.test(plain)) return true;
|
|
273
|
+
return false;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Index of the editor's bottom border (last border row); falls back to
|
|
278
|
+
* the last line when none is found.
|
|
279
|
+
*/
|
|
280
|
+
export function findBottomBorderIndex(lines: string[]): number {
|
|
281
|
+
for (let i = lines.length - 1; i >= 1; i--) {
|
|
282
|
+
if (isEditorBorderLine(lines[i]!)) return i;
|
|
283
|
+
}
|
|
284
|
+
return Math.max(0, lines.length - 1);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* Pad `text` to `width` columns, truncating with `ellipsis` when it
|
|
289
|
+
* overflows. The returned string is exactly `width` terminal columns wide.
|
|
290
|
+
*/
|
|
291
|
+
export function padRight(text: string, width: number, ellipsis = ""): string {
|
|
292
|
+
const clipped = truncateToWidth(text, width, ellipsis);
|
|
293
|
+
return clipped + " ".repeat(Math.max(0, width - visibleWidth(clipped)));
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Center `text` within `width` columns (left-biased on odd padding),
|
|
298
|
+
* truncating with `...` when the text is wider than the width.
|
|
299
|
+
*/
|
|
300
|
+
export function center(text: string, width: number): string {
|
|
301
|
+
if (width <= 0) return "";
|
|
302
|
+
const w = visibleWidth(text);
|
|
303
|
+
if (w >= width) return truncateToWidth(text, width, "...");
|
|
304
|
+
return `${" ".repeat(Math.floor((width - w) / 2))}${text}`;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* Normalize an extension status text for footer display: strip ANSI and
|
|
309
|
+
* control characters, collapse whitespace runs, trim.
|
|
310
|
+
*/
|
|
311
|
+
export function sanitizeStatus(text: string): string {
|
|
312
|
+
return stripAnsi(text)
|
|
313
|
+
.replace(/[\u0000-\u001f\u007f-\u009f]/g, " ")
|
|
314
|
+
.replace(/ +/g, " ")
|
|
315
|
+
.trim();
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* Human label for a thinking level: `thinking off` for "off", otherwise
|
|
320
|
+
* `<level> effort`.
|
|
321
|
+
*/
|
|
322
|
+
export function formatThinkingLabel(level: string): string {
|
|
323
|
+
if (level === "off") return "thinking off";
|
|
324
|
+
return `${level} effort`;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* Slash-command names pi ships built-in (without a leading `/`). Merged
|
|
329
|
+
* with the session-command names to form the header tips pool.
|
|
330
|
+
*/
|
|
331
|
+
export const PI_BUILTIN_SLASH_COMMAND_NAMES = [
|
|
332
|
+
"settings",
|
|
333
|
+
"model",
|
|
334
|
+
"scoped-models",
|
|
335
|
+
"export",
|
|
336
|
+
"import",
|
|
337
|
+
"share",
|
|
338
|
+
"copy",
|
|
339
|
+
"name",
|
|
340
|
+
"session",
|
|
341
|
+
"changelog",
|
|
342
|
+
"hotkeys",
|
|
343
|
+
"fork",
|
|
344
|
+
"clone",
|
|
345
|
+
"tree",
|
|
346
|
+
"trust",
|
|
347
|
+
"login",
|
|
348
|
+
"logout",
|
|
349
|
+
"new",
|
|
350
|
+
"compact",
|
|
351
|
+
"resume",
|
|
352
|
+
"reload",
|
|
353
|
+
"quit",
|
|
354
|
+
] as const;
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* The full slash-command name pool: pi's builtin names plus any session
|
|
358
|
+
* commands (deduplicated, no leading `/`).
|
|
359
|
+
*/
|
|
360
|
+
export function collectPiCommandNames(sessionCommands: readonly { name: string }[]): string[] {
|
|
361
|
+
const names = new Set<string>(PI_BUILTIN_SLASH_COMMAND_NAMES);
|
|
362
|
+
for (const command of sessionCommands) {
|
|
363
|
+
if (command.name) names.add(command.name);
|
|
364
|
+
}
|
|
365
|
+
return [...names];
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/**
|
|
369
|
+
* Select the header's slash-command tips: the fixed commands (always
|
|
370
|
+
* first) followed by `count` commands shuffled from the remaining pool
|
|
371
|
+
* (Fisher–Yates) using Effect's Random service. The shuffle draws from
|
|
372
|
+
* the ambient seed, so runners must wrap the program in
|
|
373
|
+
* `Random.withSeed(seed)` for deterministic output.
|
|
374
|
+
*/
|
|
375
|
+
export const pickSlashCommandTips = Effect.fn("tui/utils/pickSlashCommandTips")(function* (
|
|
376
|
+
availableNames: readonly string[],
|
|
377
|
+
options: {
|
|
378
|
+
fixed?: readonly string[];
|
|
379
|
+
count?: number;
|
|
380
|
+
exclude?: readonly string[];
|
|
381
|
+
} = {},
|
|
382
|
+
) {
|
|
383
|
+
const fixed = [...(options.fixed ?? [])];
|
|
384
|
+
const count = options.count ?? 3;
|
|
385
|
+
const exclude = new Set<string>([...(options.exclude ?? []), ...fixed]);
|
|
386
|
+
|
|
387
|
+
const pool = [...new Set(availableNames.map((n) => n.trim()).filter(Boolean))].filter(
|
|
388
|
+
(name) => !exclude.has(name),
|
|
389
|
+
);
|
|
390
|
+
|
|
391
|
+
for (let i = pool.length - 1; i > 0; i--) {
|
|
392
|
+
const j = yield* Random.nextIntBetween(0, i);
|
|
393
|
+
const [pi, pj] = [String(pool[i]), String(pool[j])];
|
|
394
|
+
pool[j] = pi;
|
|
395
|
+
pool[i] = pj;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
const picked = pool.slice(0, Math.max(0, count));
|
|
399
|
+
return [...fixed, ...picked].map((name) => (name.startsWith("/") ? name : `/${name}`));
|
|
400
|
+
});
|
|
401
|
+
|
|
402
|
+
/** Minimum left-column width for the two-column header body. */
|
|
403
|
+
export const MIN_LEFT_WIDTH = 28;
|
|
404
|
+
/** Minimum tips-column width before the tips are dropped. */
|
|
405
|
+
export const MIN_TIPS_WIDTH = 16;
|
|
406
|
+
/** Maximum tips-column width; excess goes to the left column. */
|
|
407
|
+
export const MAX_TIPS_WIDTH = 28;
|
|
408
|
+
const COLUMN_GAP = 3;
|
|
409
|
+
|
|
410
|
+
/**
|
|
411
|
+
* Split an inner (rail-to-rail) width into left/tips columns for the
|
|
412
|
+
* header body. Returns `useTips: false` with a single full-width left
|
|
413
|
+
* column when the width cannot hold both columns at their minimums.
|
|
414
|
+
*/
|
|
415
|
+
export function headerColumnWidths(
|
|
416
|
+
innerWidth: number,
|
|
417
|
+
minTipsWidth = MIN_TIPS_WIDTH,
|
|
418
|
+
maxTipsWidth = MAX_TIPS_WIDTH,
|
|
419
|
+
minLeftWidth = MIN_LEFT_WIDTH,
|
|
420
|
+
): { leftWidth: number; rightWidth: number; useTips: boolean } {
|
|
421
|
+
if (innerWidth <= 0) {
|
|
422
|
+
return { leftWidth: 0, rightWidth: 0, useTips: false };
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
const gap = COLUMN_GAP;
|
|
426
|
+
if (innerWidth < minLeftWidth + gap + minTipsWidth) {
|
|
427
|
+
return { leftWidth: innerWidth, rightWidth: 0, useTips: false };
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
let rightWidth = Math.min(maxTipsWidth, Math.max(minTipsWidth, Math.round(innerWidth * 0.28)));
|
|
431
|
+
let leftWidth = innerWidth - gap - rightWidth;
|
|
432
|
+
|
|
433
|
+
if (leftWidth < minLeftWidth) {
|
|
434
|
+
leftWidth = minLeftWidth;
|
|
435
|
+
rightWidth = innerWidth - gap - leftWidth;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
if (leftWidth <= rightWidth) {
|
|
439
|
+
leftWidth = Math.ceil((innerWidth - gap) * 0.65);
|
|
440
|
+
rightWidth = innerWidth - gap - leftWidth;
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
if (rightWidth < minTipsWidth || leftWidth < minLeftWidth) {
|
|
444
|
+
return { leftWidth: innerWidth, rightWidth: 0, useTips: false };
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
return { leftWidth, rightWidth, useTips: true };
|
|
448
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Close the built-in editor's autocomplete popup on vim Normal entry
|
|
3
|
+
* (binding decision 2).
|
|
4
|
+
*
|
|
5
|
+
* pi-tui's `Editor` keeps its autocomplete state in private fields and its
|
|
6
|
+
* close methods (`cancelAutocomplete`, `clearAutocompleteUi`) are private.
|
|
7
|
+
* Mirroring `autocomplete-contract.ts`, we guard the private surface and
|
|
8
|
+
* degrade softly (no-op) if pi's internals change shape, instead of crashing
|
|
9
|
+
* with a TypeError.
|
|
10
|
+
*/
|
|
11
|
+
export function closeAutocomplete(editor: {
|
|
12
|
+
readonly isShowingAutocomplete: () => boolean;
|
|
13
|
+
}): void {
|
|
14
|
+
if (!editor.isShowingAutocomplete()) return;
|
|
15
|
+
const internal = editor as unknown as {
|
|
16
|
+
readonly cancelAutocomplete?: () => void;
|
|
17
|
+
readonly clearAutocompleteUi?: () => void;
|
|
18
|
+
};
|
|
19
|
+
// cancelAutocomplete also aborts an in-flight request; clearAutocompleteUi
|
|
20
|
+
// is the minimal fallback if the former is renamed/reshaped.
|
|
21
|
+
if (typeof internal.cancelAutocomplete === "function") {
|
|
22
|
+
internal.cancelAutocomplete();
|
|
23
|
+
} else {
|
|
24
|
+
internal.clearAutocompleteUi?.();
|
|
25
|
+
}
|
|
26
|
+
}
|