pi-open-tui 0.1.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 +72 -0
- package/extensions/open-tui/config.ts +109 -0
- package/extensions/open-tui/editor.ts +101 -0
- package/extensions/open-tui/footer.ts +267 -0
- package/extensions/open-tui/git.ts +154 -0
- package/extensions/open-tui/header.ts +177 -0
- package/extensions/open-tui/icons.ts +194 -0
- package/extensions/open-tui/index.ts +241 -0
- package/extensions/open-tui/runtime.ts +174 -0
- package/extensions/open-tui/session-lifecycle.ts +43 -0
- package/extensions/open-tui/settings-command.ts +258 -0
- package/extensions/open-tui/state.ts +90 -0
- package/extensions/open-tui/utils.ts +157 -0
- package/package.json +53 -0
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
import type { ExtensionAPI, ExtensionContext, Theme } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import {
|
|
3
|
+
Box,
|
|
4
|
+
Key,
|
|
5
|
+
matchesKey,
|
|
6
|
+
SelectList,
|
|
7
|
+
type SelectItem,
|
|
8
|
+
type TUI,
|
|
9
|
+
Text,
|
|
10
|
+
} from "@earendil-works/pi-tui";
|
|
11
|
+
import type { IconMode, OpenTuiConfig } from "./config.ts";
|
|
12
|
+
|
|
13
|
+
interface SettingItem {
|
|
14
|
+
id: string;
|
|
15
|
+
label: string;
|
|
16
|
+
currentValue: string;
|
|
17
|
+
values: string[];
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
type Tab = "features" | "icons" | "segments";
|
|
21
|
+
|
|
22
|
+
const TABS: { id: Tab; label: string }[] = [
|
|
23
|
+
{ id: "features", label: "Features" },
|
|
24
|
+
{ id: "icons", label: "Icons" },
|
|
25
|
+
{ id: "segments", label: "Segments" },
|
|
26
|
+
];
|
|
27
|
+
|
|
28
|
+
function toggleSetting(config: OpenTuiConfig, key: keyof OpenTuiConfig["footerSegments"]): OpenTuiConfig {
|
|
29
|
+
return {
|
|
30
|
+
...config,
|
|
31
|
+
footerSegments: {
|
|
32
|
+
...config.footerSegments,
|
|
33
|
+
[key]: !config.footerSegments[key],
|
|
34
|
+
},
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function cycleIconMode(config: OpenTuiConfig): OpenTuiConfig {
|
|
39
|
+
const order: IconMode[] = ["auto", "nerd", "ascii"];
|
|
40
|
+
const currentIdx = order.indexOf(config.icons.mode);
|
|
41
|
+
const next = order[(currentIdx + 1) % order.length]!;
|
|
42
|
+
return { ...config, icons: { mode: next } };
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function toggleEnabled(config: OpenTuiConfig): OpenTuiConfig {
|
|
46
|
+
return { ...config, enabled: !config.enabled };
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function buildFeaturesItems(config: OpenTuiConfig): SettingItem[] {
|
|
50
|
+
return [
|
|
51
|
+
{
|
|
52
|
+
id: "enabled",
|
|
53
|
+
label: "Enabled",
|
|
54
|
+
currentValue: config.enabled ? "on" : "off",
|
|
55
|
+
values: ["on", "off"],
|
|
56
|
+
},
|
|
57
|
+
];
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function buildIconsItems(config: OpenTuiConfig): SettingItem[] {
|
|
61
|
+
return [
|
|
62
|
+
{
|
|
63
|
+
id: "mode",
|
|
64
|
+
label: "Icon mode",
|
|
65
|
+
currentValue: config.icons.mode,
|
|
66
|
+
values: ["auto", "nerd", "ascii"],
|
|
67
|
+
},
|
|
68
|
+
];
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function buildSegmentsItems(config: OpenTuiConfig): SettingItem[] {
|
|
72
|
+
const segs = config.footerSegments;
|
|
73
|
+
return [
|
|
74
|
+
{ id: "cwd", label: "cwd", currentValue: segs.cwd ? "on" : "off", values: ["on", "off"] },
|
|
75
|
+
{ id: "gitBranch", label: "git branch", currentValue: segs.gitBranch ? "on" : "off", values: ["on", "off"] },
|
|
76
|
+
{ id: "gitStatus", label: "git status", currentValue: segs.gitStatus ? "on" : "off", values: ["on", "off"] },
|
|
77
|
+
{ id: "gitCommit", label: "git commit (detached)", currentValue: segs.gitCommit ? "on" : "off", values: ["on", "off"] },
|
|
78
|
+
{ id: "runtime", label: "runtime", currentValue: segs.runtime ? "on" : "off", values: ["on", "off"] },
|
|
79
|
+
{ id: "context", label: "context bar", currentValue: segs.context ? "on" : "off", values: ["on", "off"] },
|
|
80
|
+
{ id: "tokens", label: "tokens", currentValue: segs.tokens ? "on" : "off", values: ["on", "off"] },
|
|
81
|
+
{ id: "cost", label: "cost", currentValue: segs.cost ? "on" : "off", values: ["on", "off"] },
|
|
82
|
+
];
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function buildItems(tab: Tab, config: OpenTuiConfig): SettingItem[] {
|
|
86
|
+
switch (tab) {
|
|
87
|
+
case "features": return buildFeaturesItems(config);
|
|
88
|
+
case "icons": return buildIconsItems(config);
|
|
89
|
+
case "segments": return buildSegmentsItems(config);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function handleSettingChange(
|
|
94
|
+
tab: Tab,
|
|
95
|
+
itemId: string,
|
|
96
|
+
config: OpenTuiConfig,
|
|
97
|
+
): OpenTuiConfig {
|
|
98
|
+
if (tab === "features" && itemId === "enabled") {
|
|
99
|
+
return toggleEnabled(config);
|
|
100
|
+
}
|
|
101
|
+
if (tab === "icons" && itemId === "mode") {
|
|
102
|
+
return cycleIconMode(config);
|
|
103
|
+
}
|
|
104
|
+
if (tab === "segments") {
|
|
105
|
+
return toggleSetting(config, itemId as keyof OpenTuiConfig["footerSegments"]);
|
|
106
|
+
}
|
|
107
|
+
return config;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
interface SettingsUiHandle {
|
|
111
|
+
render: (width: number) => string[];
|
|
112
|
+
invalidate: () => void;
|
|
113
|
+
handleInput: (data: string) => void;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
class SettingsUi implements SettingsUiHandle {
|
|
117
|
+
private tab: Tab = "features";
|
|
118
|
+
private config: OpenTuiConfig;
|
|
119
|
+
private selectList: SelectList;
|
|
120
|
+
private readonly selectedItemByTab: Partial<Record<Tab, string>> = {};
|
|
121
|
+
private readonly container: Box;
|
|
122
|
+
private cachedWidth: number | undefined;
|
|
123
|
+
private cachedLines: string[] | undefined;
|
|
124
|
+
|
|
125
|
+
constructor(
|
|
126
|
+
private readonly theme: Theme,
|
|
127
|
+
config: OpenTuiConfig,
|
|
128
|
+
private readonly onChange: (config: OpenTuiConfig) => void,
|
|
129
|
+
private readonly onClose: () => void,
|
|
130
|
+
) {
|
|
131
|
+
this.config = config;
|
|
132
|
+
this.container = new Box(1, 1, (s: string) => theme.bg("customMessageBg", s));
|
|
133
|
+
this.selectList = new SelectList([], 12, {
|
|
134
|
+
selectedPrefix: (t) => theme.fg("accent", t),
|
|
135
|
+
selectedText: (t) => theme.fg("accent", t),
|
|
136
|
+
description: (t) => theme.fg("muted", t),
|
|
137
|
+
scrollInfo: (t) => theme.fg("dim", t),
|
|
138
|
+
noMatch: (t) => theme.fg("warning", t),
|
|
139
|
+
});
|
|
140
|
+
this.rebuild();
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
private rebuild(preferredItemId = this.selectedItemByTab[this.tab]): void {
|
|
144
|
+
this.container.clear();
|
|
145
|
+
this.container.addChild(new Text(this.theme.bold(this.theme.fg("accent", "Open TUI Settings")), 1, 0));
|
|
146
|
+
|
|
147
|
+
const tabBar = TABS.map((t) => {
|
|
148
|
+
const active = t.id === this.tab;
|
|
149
|
+
const label = active ? `[${t.label}]` : ` ${t.label} `;
|
|
150
|
+
return active ? this.theme.fg("accent", label) : this.theme.fg("dim", label);
|
|
151
|
+
}).join(" ");
|
|
152
|
+
this.container.addChild(new Text(tabBar, 1, 0));
|
|
153
|
+
this.container.addChild(new Text(this.theme.fg("dim", "Tab/Shift+Tab to switch · Enter to toggle · Esc to close"), 1, 0));
|
|
154
|
+
|
|
155
|
+
const items = buildItems(this.tab, this.config).map((item) => ({
|
|
156
|
+
value: item.id,
|
|
157
|
+
label: item.label,
|
|
158
|
+
description: item.currentValue,
|
|
159
|
+
} as SelectItem));
|
|
160
|
+
this.selectList = new SelectList(items, Math.min(items.length, 10), {
|
|
161
|
+
selectedPrefix: (t) => this.theme.fg("accent", t),
|
|
162
|
+
selectedText: (t) => this.theme.fg("accent", t),
|
|
163
|
+
description: (t) => this.theme.fg("muted", t),
|
|
164
|
+
scrollInfo: (t) => this.theme.fg("dim", t),
|
|
165
|
+
noMatch: (t) => this.theme.fg("warning", t),
|
|
166
|
+
});
|
|
167
|
+
const selectedIndex = items.findIndex((item) => item.value === preferredItemId);
|
|
168
|
+
if (selectedIndex >= 0) {
|
|
169
|
+
this.selectList.setSelectedIndex(selectedIndex);
|
|
170
|
+
}
|
|
171
|
+
this.selectedItemByTab[this.tab] = this.selectList.getSelectedItem()?.value;
|
|
172
|
+
this.selectList.onSelectionChange = (item) => {
|
|
173
|
+
this.selectedItemByTab[this.tab] = item.value;
|
|
174
|
+
};
|
|
175
|
+
this.selectList.onSelect = (item) => {
|
|
176
|
+
this.selectedItemByTab[this.tab] = item.value;
|
|
177
|
+
this.config = handleSettingChange(this.tab, item.value, this.config);
|
|
178
|
+
this.onChange(this.config);
|
|
179
|
+
this.rebuild(item.value);
|
|
180
|
+
this.invalidate();
|
|
181
|
+
};
|
|
182
|
+
this.selectList.onCancel = () => {
|
|
183
|
+
this.onClose();
|
|
184
|
+
};
|
|
185
|
+
this.container.addChild(this.selectList);
|
|
186
|
+
|
|
187
|
+
this.cachedWidth = undefined;
|
|
188
|
+
this.cachedLines = undefined;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
handleInput(data: string): void {
|
|
192
|
+
if (matchesKey(data, Key.tab)) {
|
|
193
|
+
const idx = TABS.findIndex((t) => t.id === this.tab);
|
|
194
|
+
this.tab = TABS[(idx + 1) % TABS.length]!.id;
|
|
195
|
+
this.rebuild();
|
|
196
|
+
this.invalidate();
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
if (matchesKey(data, Key.shift("tab"))) {
|
|
200
|
+
const idx = TABS.findIndex((t) => t.id === this.tab);
|
|
201
|
+
this.tab = TABS[(idx - 1 + TABS.length) % TABS.length]!.id;
|
|
202
|
+
this.rebuild();
|
|
203
|
+
this.invalidate();
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
if (matchesKey(data, Key.escape)) {
|
|
207
|
+
this.onClose();
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
this.selectList.handleInput?.(data);
|
|
211
|
+
this.invalidate();
|
|
212
|
+
this.invalidate();
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
render(width: number): string[] {
|
|
216
|
+
if (this.cachedLines && this.cachedWidth === width) return this.cachedLines;
|
|
217
|
+
this.cachedWidth = width;
|
|
218
|
+
this.cachedLines = this.container.render(width);
|
|
219
|
+
return this.cachedLines;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
invalidate(): void {
|
|
223
|
+
this.cachedWidth = undefined;
|
|
224
|
+
this.cachedLines = undefined;
|
|
225
|
+
this.container.invalidate();
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
export function registerSettingsCommand(
|
|
230
|
+
pi: ExtensionAPI,
|
|
231
|
+
hooks: {
|
|
232
|
+
getConfig: () => OpenTuiConfig;
|
|
233
|
+
onConfigChanged: (config: OpenTuiConfig) => void;
|
|
234
|
+
},
|
|
235
|
+
): void {
|
|
236
|
+
pi.registerCommand("open-tui", {
|
|
237
|
+
description: "Open the open-tui settings UI",
|
|
238
|
+
handler: async (_args, ctx: ExtensionContext) => {
|
|
239
|
+
if (!ctx.hasUI) return;
|
|
240
|
+
await ctx.ui.custom<void>((tui: TUI, theme, _kb, done) => {
|
|
241
|
+
const ui = new SettingsUi(
|
|
242
|
+
theme,
|
|
243
|
+
hooks.getConfig(),
|
|
244
|
+
(config) => hooks.onConfigChanged(config),
|
|
245
|
+
() => done(undefined),
|
|
246
|
+
);
|
|
247
|
+
return {
|
|
248
|
+
render: (w: number) => ui.render(w),
|
|
249
|
+
invalidate: () => ui.invalidate(),
|
|
250
|
+
handleInput: (data: string) => {
|
|
251
|
+
ui.handleInput(data);
|
|
252
|
+
tui.requestRender();
|
|
253
|
+
},
|
|
254
|
+
};
|
|
255
|
+
}, { overlay: true });
|
|
256
|
+
},
|
|
257
|
+
});
|
|
258
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import type { AssistantMessage } from "@earendil-works/pi-ai";
|
|
3
|
+
import type { GitStatus } from "./git.ts";
|
|
4
|
+
import { emptyGitStatus } from "./git.ts";
|
|
5
|
+
import type { RuntimeInfo } from "./runtime.ts";
|
|
6
|
+
import { fmtTokens, formatProviderLabel } from "./utils.ts";
|
|
7
|
+
|
|
8
|
+
export interface FooterState {
|
|
9
|
+
git: GitStatus;
|
|
10
|
+
runtime: RuntimeInfo | null;
|
|
11
|
+
sessionStartEpoch: number;
|
|
12
|
+
workingSince: number | undefined;
|
|
13
|
+
lastDoneIn: number | undefined;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface UsageTotals {
|
|
17
|
+
input: number;
|
|
18
|
+
output: number;
|
|
19
|
+
cacheRead: number;
|
|
20
|
+
cacheWrite: number;
|
|
21
|
+
cost: number;
|
|
22
|
+
latestCacheHitRate: number | undefined;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
let usageCache: { key: string; totals: UsageTotals } | undefined;
|
|
26
|
+
|
|
27
|
+
function entriesKey(ctx: ExtensionContext): string {
|
|
28
|
+
const entries = ctx.sessionManager.getEntries();
|
|
29
|
+
const last = entries.at(-1);
|
|
30
|
+
return `${entries.length}:${last?.id ?? ""}:${last?.timestamp ?? ""}`;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function getUsageTotals(ctx: ExtensionContext): UsageTotals {
|
|
34
|
+
const key = entriesKey(ctx);
|
|
35
|
+
if (usageCache && usageCache.key === key) return usageCache.totals;
|
|
36
|
+
|
|
37
|
+
const totals: UsageTotals = {
|
|
38
|
+
input: 0, output: 0, cacheRead: 0, cacheWrite: 0, cost: 0,
|
|
39
|
+
latestCacheHitRate: undefined,
|
|
40
|
+
};
|
|
41
|
+
for (const entry of ctx.sessionManager.getEntries()) {
|
|
42
|
+
if (entry.type === "message" && entry.message?.role === "assistant") {
|
|
43
|
+
const m = entry.message as AssistantMessage;
|
|
44
|
+
const u = m.usage;
|
|
45
|
+
if (!u) continue;
|
|
46
|
+
totals.input += u.input ?? 0;
|
|
47
|
+
totals.output += u.output ?? 0;
|
|
48
|
+
totals.cacheRead += u.cacheRead ?? 0;
|
|
49
|
+
totals.cacheWrite += u.cacheWrite ?? 0;
|
|
50
|
+
totals.cost += u.cost?.total ?? 0;
|
|
51
|
+
const promptTokens = (u.input ?? 0) + (u.cacheRead ?? 0) + (u.cacheWrite ?? 0);
|
|
52
|
+
if (promptTokens > 0) {
|
|
53
|
+
totals.latestCacheHitRate = ((u.cacheRead ?? 0) / promptTokens) * 100;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
usageCache = { key, totals };
|
|
58
|
+
return totals;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function invalidateUsageCache(): void {
|
|
62
|
+
usageCache = undefined;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function createInitialState(): FooterState {
|
|
66
|
+
return {
|
|
67
|
+
git: emptyGitStatus(),
|
|
68
|
+
runtime: null,
|
|
69
|
+
sessionStartEpoch: Date.now(),
|
|
70
|
+
workingSince: undefined,
|
|
71
|
+
lastDoneIn: undefined,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export interface ModelMeta {
|
|
76
|
+
provider: string;
|
|
77
|
+
model: string;
|
|
78
|
+
effort: string | undefined;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function getModelMeta(
|
|
82
|
+
ctx: ExtensionContext,
|
|
83
|
+
getThinkingLevel: () => string,
|
|
84
|
+
): ModelMeta {
|
|
85
|
+
const provider = formatProviderLabel(ctx.model?.provider);
|
|
86
|
+
const model = ctx.model?.id ?? "no-model";
|
|
87
|
+
const reasoning = ctx.model?.reasoning ?? false;
|
|
88
|
+
const effort = reasoning ? getThinkingLevel() : undefined;
|
|
89
|
+
return { provider, model, effort };
|
|
90
|
+
}
|
|
@@ -0,0 +1,157 @@
|
|
|
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
|
+
|
|
6
|
+
export { truncateToWidth, visibleWidth };
|
|
7
|
+
|
|
8
|
+
export function stripAnsi(text: string): string {
|
|
9
|
+
return text
|
|
10
|
+
.replace(/\x1b\[[0-9;]*m/g, "")
|
|
11
|
+
.replace(/\x1b\][^\x07]*(?:\x07|\x1b\\)/g, "")
|
|
12
|
+
.replace(/\x1b_[^\x07]*\x07/g, "");
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function formatCwd(cwd: string): string {
|
|
16
|
+
const home = process.env.HOME || process.env.USERPROFILE;
|
|
17
|
+
if (!home) return cwd;
|
|
18
|
+
const resolvedCwd = resolve(cwd);
|
|
19
|
+
const resolvedHome = resolve(home);
|
|
20
|
+
const rel = relative(resolvedHome, resolvedCwd);
|
|
21
|
+
const insideHome =
|
|
22
|
+
rel === "" || (rel !== ".." && !rel.startsWith(`..${sep}`) && !isAbsolute(rel));
|
|
23
|
+
if (!insideHome) return cwd;
|
|
24
|
+
return rel === "" ? "~" : `~${sep}${rel}`;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function fmtTokens(n: number): string {
|
|
28
|
+
if (n < 1000) return n.toString();
|
|
29
|
+
if (n < 10_000) return `${(n / 1000).toFixed(1)}k`;
|
|
30
|
+
if (n < 1_000_000) return `${Math.round(n / 1000)}k`;
|
|
31
|
+
if (n < 10_000_000) return `${(n / 1_000_000).toFixed(1)}M`;
|
|
32
|
+
return `${Math.round(n / 1_000_000)}M`;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function formatDuration(ms: number): string {
|
|
36
|
+
const totalSeconds = Math.max(0, Math.floor(ms / 1000));
|
|
37
|
+
if (totalSeconds < 60) return `${totalSeconds}s`;
|
|
38
|
+
const s = totalSeconds % 60;
|
|
39
|
+
const totalMinutes = Math.floor(totalSeconds / 60);
|
|
40
|
+
if (totalMinutes < 60) return `${totalMinutes}m ${s}s`;
|
|
41
|
+
const m = totalMinutes % 60;
|
|
42
|
+
const h = Math.floor(totalMinutes / 60);
|
|
43
|
+
return `${h}h ${m}m ${s}s`;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function formatModelLabel(model: { provider?: string; id?: string } | null | undefined): string {
|
|
47
|
+
if (!model?.id) return "no-model";
|
|
48
|
+
return model.provider ? `${model.provider}/${model.id}` : model.id;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function formatProviderLabel(provider: string | undefined): string {
|
|
52
|
+
if (!provider) return "Unknown";
|
|
53
|
+
return provider.charAt(0).toUpperCase() + provider.slice(1);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function alignRight(left: string, right: string, width: number, theme: Theme): string {
|
|
57
|
+
const rightW = visibleWidth(right);
|
|
58
|
+
if (rightW > width) {
|
|
59
|
+
right = truncateToWidth(right, width, theme.fg("dim", "..."));
|
|
60
|
+
}
|
|
61
|
+
const leftW = visibleWidth(left);
|
|
62
|
+
const rightW2 = visibleWidth(right);
|
|
63
|
+
const pad = width - leftW - rightW2;
|
|
64
|
+
if (pad >= 1) {
|
|
65
|
+
return left + " ".repeat(pad) + right;
|
|
66
|
+
}
|
|
67
|
+
const availableForLeft = Math.max(0, width - rightW2 - 1);
|
|
68
|
+
const truncatedLeft =
|
|
69
|
+
availableForLeft > 0 ? truncateToWidth(left, availableForLeft, theme.fg("dim", "...")) : "";
|
|
70
|
+
return truncatedLeft ? truncatedLeft + " " + right : right;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function stressColor(value: number, warn = 70, danger = 90): ThemeColor {
|
|
74
|
+
if (value >= danger) return "error";
|
|
75
|
+
if (value >= warn) return "warning";
|
|
76
|
+
return "accent";
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function cacheHitColor(value: number): ThemeColor {
|
|
80
|
+
if (value < 30) return "error";
|
|
81
|
+
if (value < 70) return "warning";
|
|
82
|
+
return "success";
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export function providerColor(provider: string): ThemeColor {
|
|
86
|
+
switch (provider.toLowerCase()) {
|
|
87
|
+
case "anthropic":
|
|
88
|
+
return "accent";
|
|
89
|
+
case "openai":
|
|
90
|
+
case "openai-codex":
|
|
91
|
+
return "success";
|
|
92
|
+
case "google":
|
|
93
|
+
case "google-vertex":
|
|
94
|
+
return "warning";
|
|
95
|
+
case "amazon-bedrock":
|
|
96
|
+
return "thinkingHigh";
|
|
97
|
+
case "github-copilot":
|
|
98
|
+
return "mdLink";
|
|
99
|
+
case "deepseek":
|
|
100
|
+
return "thinkingLow";
|
|
101
|
+
case "xai":
|
|
102
|
+
case "groq":
|
|
103
|
+
return "error";
|
|
104
|
+
default:
|
|
105
|
+
return "muted";
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export function effortColor(level: ThinkingLevel | string | undefined): ThemeColor {
|
|
110
|
+
switch (level) {
|
|
111
|
+
case "minimal":
|
|
112
|
+
return "thinkingMinimal";
|
|
113
|
+
case "low":
|
|
114
|
+
return "thinkingLow";
|
|
115
|
+
case "medium":
|
|
116
|
+
return "thinkingMedium";
|
|
117
|
+
case "high":
|
|
118
|
+
return "thinkingHigh";
|
|
119
|
+
case "xhigh":
|
|
120
|
+
return "thinkingXhigh";
|
|
121
|
+
default:
|
|
122
|
+
return "thinkingMedium";
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export function isEditorBorderLine(line: string): boolean {
|
|
127
|
+
const plain = stripAnsi(line);
|
|
128
|
+
if (/^─+$/.test(plain)) return true;
|
|
129
|
+
if (/^─*\s*[↑↓]\s+\d+\s+more\s*─*$/.test(plain)) return true;
|
|
130
|
+
return false;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export function findBottomBorderIndex(lines: string[]): number {
|
|
134
|
+
for (let i = lines.length - 1; i >= 1; i--) {
|
|
135
|
+
if (isEditorBorderLine(lines[i]!)) return i;
|
|
136
|
+
}
|
|
137
|
+
return Math.max(0, lines.length - 1);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export function padRight(text: string, width: number, ellipsis = ""): string {
|
|
141
|
+
const clipped = truncateToWidth(text, width, ellipsis);
|
|
142
|
+
return clipped + " ".repeat(Math.max(0, width - visibleWidth(clipped)));
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export function center(text: string, width: number): string {
|
|
146
|
+
if (width <= 0) return "";
|
|
147
|
+
const w = visibleWidth(text);
|
|
148
|
+
if (w >= width) return truncateToWidth(text, width, "...");
|
|
149
|
+
return `${" ".repeat(Math.floor((width - w) / 2))}${text}`;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export function sanitizeStatus(text: string): string {
|
|
153
|
+
return stripAnsi(text)
|
|
154
|
+
.replace(/[\u0000-\u001f\u007f-\u009f]/g, " ")
|
|
155
|
+
.replace(/ +/g, " ")
|
|
156
|
+
.trim();
|
|
157
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "pi-open-tui",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "A polished TUI for Pi coding agent: animated logo header, Starship-style footer, rounded editor with model metadata, and prompt-box user messages. Combines the best of pi-haiku, pi-claude-code-tui, and pi-zentui.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"pi-package",
|
|
8
|
+
"pi",
|
|
9
|
+
"pi-coding-agent",
|
|
10
|
+
"pi-extension",
|
|
11
|
+
"tui",
|
|
12
|
+
"starship",
|
|
13
|
+
"theme"
|
|
14
|
+
],
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"files": [
|
|
17
|
+
"extensions/",
|
|
18
|
+
"README.md",
|
|
19
|
+
"LICENSE"
|
|
20
|
+
],
|
|
21
|
+
"scripts": {
|
|
22
|
+
"test": "node --test --experimental-transform-types tests/settings-command.test.ts tests/footer.test.ts tests/editor.test.ts",
|
|
23
|
+
"typecheck": "tsc --noEmit"
|
|
24
|
+
},
|
|
25
|
+
"pi": {
|
|
26
|
+
"extensions": [
|
|
27
|
+
"./extensions/open-tui/index.ts"
|
|
28
|
+
]
|
|
29
|
+
},
|
|
30
|
+
"peerDependencies": {
|
|
31
|
+
"@earendil-works/pi-coding-agent": ">=0.80",
|
|
32
|
+
"@earendil-works/pi-ai": ">=0.80",
|
|
33
|
+
"@earendil-works/pi-tui": ">=0.80"
|
|
34
|
+
},
|
|
35
|
+
"peerDependenciesMeta": {
|
|
36
|
+
"@earendil-works/pi-coding-agent": {
|
|
37
|
+
"optional": true
|
|
38
|
+
},
|
|
39
|
+
"@earendil-works/pi-ai": {
|
|
40
|
+
"optional": true
|
|
41
|
+
},
|
|
42
|
+
"@earendil-works/pi-tui": {
|
|
43
|
+
"optional": true
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"@earendil-works/pi-coding-agent": "*",
|
|
48
|
+
"@earendil-works/pi-ai": "*",
|
|
49
|
+
"@earendil-works/pi-tui": "*",
|
|
50
|
+
"@types/node": "^22.0.0",
|
|
51
|
+
"typescript": "^5.5.0"
|
|
52
|
+
}
|
|
53
|
+
}
|