@leolastforce/chalice-status-line 0.0.0-stage → 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/README.md +104 -2
- package/footer.ts +37 -0
- package/git.ts +76 -0
- package/index.ts +191 -0
- package/package.json +26 -5
- package/status-line.ts +193 -0
package/README.md
CHANGED
|
@@ -1,3 +1,105 @@
|
|
|
1
|
-
|
|
1
|
+
> This is a chalice package, check out github.com/leolastforce/Chalice
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
> note: stuff below here is generated doc.
|
|
4
|
+
|
|
5
|
+
# Status line
|
|
6
|
+
|
|
7
|
+
One flat line below the editor showing the mode, directory, git branch, model, session cost and
|
|
8
|
+
context usage. It also replaces Pi's built-in footer with a thin one that shows only extension
|
|
9
|
+
|
|
10
|
+
## What it displays
|
|
11
|
+
|
|
12
|
+
Segments are joined with `·` and only appear when their data is available.
|
|
13
|
+
|
|
14
|
+
| Segment | Content |
|
|
15
|
+
|---|---|
|
|
16
|
+
| `mode` | Active Chalice mode, e.g. `Build`, `Plan`, `Ask`, `YOLO`. Only when `PermissionModes` is installed. |
|
|
17
|
+
| `cwd` | Working directory, with the home directory shortened to `~`. |
|
|
18
|
+
| `branch` | Git branch, read straight from `.git/HEAD` (including worktree `.git` files). No `git` binary needed; cached for one second. Detached HEAD shows `detached`. |
|
|
19
|
+
| `model` | Model id, plus the thinking level when the model supports reasoning. |
|
|
20
|
+
| `cost` | Session cost from usage entries, e.g. `$0.012`. Hidden at zero. |
|
|
21
|
+
| `context` | Context used as a percentage. Turns red above 90%. |
|
|
22
|
+
| `context` | Context used as a percentage. Uses the error color above 90%. |
|
|
23
|
+
| `index` | Status reported by the code index extension. `not indexed`, `stale`, `error` and `changes pending` use the error color; `off` is dimmed. |
|
|
24
|
+
|
|
25
|
+
## Layout
|
|
26
|
+
|
|
27
|
+
`left` (default) and `right` keep all segments together. `center` pins the model segment in the
|
|
28
|
+
middle and alternates the rest around it; if there is no model segment, the middle segment is
|
|
29
|
+
pinned instead. Overflow is truncated with an ellipsis.
|
|
30
|
+
|
|
31
|
+
## Footer
|
|
32
|
+
|
|
33
|
+
By default Pi's built-in footer (which repeats cwd, context and model) is replaced with a footer
|
|
34
|
+
that renders only statuses other extensions set through `ctx.ui.setStatus()`, sorted by key and
|
|
35
|
+
collapsed to one line. The `code-index` status is consumed into the `index` segment so it is not
|
|
36
|
+
printed twice; turning that segment off puts it back in the footer.
|
|
37
|
+
|
|
38
|
+
Set `defaultFooter: true` to restore Pi's footer. Extension statuses then render there, and the
|
|
39
|
+
`index` segment drops out of the status line.
|
|
40
|
+
|
|
41
|
+
## Settings
|
|
42
|
+
|
|
43
|
+
All keys live under `chalice.statusLine` in `~/.pi/agent/settings.json`, or in a trusted project's
|
|
44
|
+
`.pi/settings.json`. All of them are also editable in the **Status line** section of
|
|
45
|
+
`/chalicesettings` when the `chalice-settings` module is installed. Without it, edit the file.
|
|
46
|
+
|
|
47
|
+
| Key | Type | Default | Effect |
|
|
48
|
+
|---|---|---|---|
|
|
49
|
+
| `enabled` | boolean | `true` | Show the line. `false` removes both the widget and the custom footer. |
|
|
50
|
+
| `layout` | `left` \| `center` \| `right` | `left` | Segment placement. `middle` is accepted as a legacy alias for `center`. |
|
|
51
|
+
| `defaultFooter` | boolean | `false` | Keep Pi's built-in footer instead of the replacement. |
|
|
52
|
+
| `segments.<key>` | boolean | `true` | Toggle an individual segment; see the table above for keys. |
|
|
53
|
+
|
|
54
|
+
```json
|
|
55
|
+
{
|
|
56
|
+
"chalice": {
|
|
57
|
+
"statusLine": {
|
|
58
|
+
"layout": "center",
|
|
59
|
+
"defaultFooter": false,
|
|
60
|
+
"segments": { "cost": false, "contextWindow": false }
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Legacy configuration is still read as a fallback, in this order: the fork's top-level `ribbon`
|
|
67
|
+
key, then `~/.pi/agent/chalice-settings.json` with a `statusLine` object, then `chalice.statusLine`
|
|
68
|
+
(the current value always wins). Writing a setting through `/chalicesettings` moves it into the
|
|
69
|
+
`chalice` namespace.
|
|
70
|
+
|
|
71
|
+
## Standalone behavior
|
|
72
|
+
|
|
73
|
+
Modules communicate over Pi's event bus, so nothing here hard-depends on the rest of the distro:
|
|
74
|
+
|
|
75
|
+
- No `PermissionModes` → no mode badge (listens on `chalice:mode-state`).
|
|
76
|
+
- No `CodeIndex` → no index segment.
|
|
77
|
+
- No `chalice-settings` → no settings UI; the section registration is a no-op.
|
|
78
|
+
- Settings changes (`chalice:settings-changed`) reapply the widget and footer immediately.
|
|
79
|
+
|
|
80
|
+
Only the interactive TUI is affected; RPC and print modes are untouched. On session shutdown the
|
|
81
|
+
widget and footer are removed.
|
|
82
|
+
|
|
83
|
+
## Source layout
|
|
84
|
+
|
|
85
|
+
| File | Role |
|
|
86
|
+
|---|---|
|
|
87
|
+
| `index.ts` | Pi wiring, settings section, session cost cache, footer/widget setup. |
|
|
88
|
+
| `status-line.ts` | Segment building, layouts, line composition, TUI component. |
|
|
89
|
+
| `footer.ts` | Minimal footer that renders extension statuses only. |
|
|
90
|
+
| `git.ts` | Branch lookup without a subprocess, cwd formatting. |
|
|
91
|
+
|
|
92
|
+
The component reads a fresh snapshot on every render, so the host does not have to push updates.
|
|
93
|
+
|
|
94
|
+
## Development
|
|
95
|
+
|
|
96
|
+
From the repo root:
|
|
97
|
+
|
|
98
|
+
```bash
|
|
99
|
+
./distro/run-tui.sh # launch the distro with this module
|
|
100
|
+
node distro/harness.mjs # load every package alone and together
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
## License
|
|
104
|
+
|
|
105
|
+
MIT.
|
package/footer.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { ReadonlyFooterDataProvider, Theme } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { type Component, truncateToWidth } from "@earendil-works/pi-tui";
|
|
3
|
+
|
|
4
|
+
/** Collapse newlines/tabs so a multi-line status cannot break the footer layout. */
|
|
5
|
+
function sanitize(text: string): string {
|
|
6
|
+
return text.replace(/[\r\n\t]+/g, " ").trim();
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Footer replacement that keeps only extension statuses. Pi's built-in footer duplicates the
|
|
11
|
+
* status line (cwd, context, model) and is hidden; statuses from setStatus() still need a home.
|
|
12
|
+
*/
|
|
13
|
+
export class StatusFooterComponent implements Component {
|
|
14
|
+
private readonly theme: Theme;
|
|
15
|
+
private readonly footerData: ReadonlyFooterDataProvider;
|
|
16
|
+
/** Status keys rendered as status line segments instead of here. */
|
|
17
|
+
private readonly consumedKeys: ReadonlySet<string>;
|
|
18
|
+
|
|
19
|
+
constructor(theme: Theme, footerData: ReadonlyFooterDataProvider, consumedKeys: readonly string[] = []) {
|
|
20
|
+
this.theme = theme;
|
|
21
|
+
this.footerData = footerData;
|
|
22
|
+
this.consumedKeys = new Set(consumedKeys);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
invalidate(): void {}
|
|
26
|
+
|
|
27
|
+
render(width: number): string[] {
|
|
28
|
+
if (width <= 0) return [];
|
|
29
|
+
const statuses = [...this.footerData.getExtensionStatuses().entries()]
|
|
30
|
+
.filter(([key]) => !this.consumedKeys.has(key))
|
|
31
|
+
.sort(([left], [right]) => left.localeCompare(right))
|
|
32
|
+
.map(([, text]) => sanitize(text))
|
|
33
|
+
.filter((text) => text.length > 0);
|
|
34
|
+
if (statuses.length === 0) return [];
|
|
35
|
+
return [truncateToWidth(statuses.join(" "), width, this.theme.fg("dim", "..."))];
|
|
36
|
+
}
|
|
37
|
+
}
|
package/git.ts
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { readFileSync, statSync } from "node:fs";
|
|
2
|
+
import { dirname, isAbsolute, resolve } from "node:path";
|
|
3
|
+
|
|
4
|
+
/** How long a resolved branch is reused before the git metadata is read again. */
|
|
5
|
+
const CACHE_MS = 1000;
|
|
6
|
+
|
|
7
|
+
interface CacheEntry {
|
|
8
|
+
branch: string | undefined;
|
|
9
|
+
readAt: number;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const cache = new Map<string, CacheEntry>();
|
|
13
|
+
|
|
14
|
+
function readHead(headPath: string): string | undefined {
|
|
15
|
+
let head: string;
|
|
16
|
+
try {
|
|
17
|
+
head = readFileSync(headPath, "utf8").trim();
|
|
18
|
+
} catch {
|
|
19
|
+
return undefined;
|
|
20
|
+
}
|
|
21
|
+
if (head.startsWith("ref:")) {
|
|
22
|
+
const ref = head.slice(4).trim();
|
|
23
|
+
const prefix = "refs/heads/";
|
|
24
|
+
return ref.startsWith(prefix) ? ref.slice(prefix.length) : ref.slice(ref.lastIndexOf("/") + 1) || undefined;
|
|
25
|
+
}
|
|
26
|
+
// A branch name can also be written verbatim by reftable backends.
|
|
27
|
+
if (head.length > 0 && !/^[0-9a-f]{40,64}$/i.test(head)) return head;
|
|
28
|
+
return "detached";
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Resolve the git directory for a working tree, following `.git` files used by worktrees. */
|
|
32
|
+
function resolveGitHead(cwd: string): string | undefined {
|
|
33
|
+
let current = resolve(cwd);
|
|
34
|
+
for (;;) {
|
|
35
|
+
const candidate = resolve(current, ".git");
|
|
36
|
+
const stat = statSync(candidate, { throwIfNoEntry: false });
|
|
37
|
+
if (stat?.isDirectory()) return resolve(candidate, "HEAD");
|
|
38
|
+
if (stat?.isFile()) {
|
|
39
|
+
let pointer: string;
|
|
40
|
+
try {
|
|
41
|
+
pointer = readFileSync(candidate, "utf8").trim();
|
|
42
|
+
} catch {
|
|
43
|
+
return undefined;
|
|
44
|
+
}
|
|
45
|
+
if (pointer.startsWith("gitdir:")) {
|
|
46
|
+
const target = pointer.slice("gitdir:".length).trim();
|
|
47
|
+
return resolve(current, isAbsolute(target) ? target : resolve(dirname(candidate), target), "HEAD");
|
|
48
|
+
}
|
|
49
|
+
return undefined;
|
|
50
|
+
}
|
|
51
|
+
const parent = dirname(current);
|
|
52
|
+
if (parent === current) return undefined;
|
|
53
|
+
current = parent;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Current git branch for a working directory, or undefined outside a repository.
|
|
59
|
+
* Read without shelling out to `git`, so the status line stays usable when git is not installed.
|
|
60
|
+
*/
|
|
61
|
+
export function readGitBranch(cwd: string): string | undefined {
|
|
62
|
+
const now = Date.now();
|
|
63
|
+
const cached = cache.get(cwd);
|
|
64
|
+
if (cached && now - cached.readAt < CACHE_MS) return cached.branch;
|
|
65
|
+
const headPath = resolveGitHead(cwd);
|
|
66
|
+
const branch = headPath ? readHead(headPath) : undefined;
|
|
67
|
+
cache.set(cwd, { branch, readAt: now });
|
|
68
|
+
return branch;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Shorten an absolute path for display, replacing the home directory with `~`. */
|
|
72
|
+
export function formatCwd(cwd: string, home: string | undefined): string {
|
|
73
|
+
if (cwd === home) return "~";
|
|
74
|
+
if (home && cwd.startsWith(`${home}/`)) return `~${cwd.slice(home.length)}`;
|
|
75
|
+
return cwd;
|
|
76
|
+
}
|
package/index.ts
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import type { ExtensionAPI, ExtensionContext, ReadonlyFooterDataProvider } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import {
|
|
3
|
+
CHALICE_SETTINGS_EVENT,
|
|
4
|
+
type ChaliceSettingsSection,
|
|
5
|
+
readChaliceSettings,
|
|
6
|
+
registerChaliceSettingsSection,
|
|
7
|
+
STATUS_LINE_SEGMENTS,
|
|
8
|
+
type StatusLineSegmentKey,
|
|
9
|
+
} from "@leolastforce/chalice-shared";
|
|
10
|
+
import { StatusFooterComponent } from "./footer.ts";
|
|
11
|
+
import { formatCwd, readGitBranch } from "./git.ts";
|
|
12
|
+
import { StatusLineComponent, type StatusLineSnapshot } from "./status-line.ts";
|
|
13
|
+
|
|
14
|
+
const WIDGET_KEY = "chalice-status-line";
|
|
15
|
+
/** Emitted by the permission-modes extension whenever the active chalice mode changes. */
|
|
16
|
+
const MODE_EVENT = "chalice:mode-state";
|
|
17
|
+
|
|
18
|
+
const SEGMENT_LABELS: Record<StatusLineSegmentKey, string> = {
|
|
19
|
+
mode: "Mode badge",
|
|
20
|
+
cwd: "Directory",
|
|
21
|
+
branch: "Git branch",
|
|
22
|
+
model: "Model",
|
|
23
|
+
cost: "Cost",
|
|
24
|
+
context: "Context percent",
|
|
25
|
+
contextWindow: "Context window",
|
|
26
|
+
index: "Code index",
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const STATUS_LINE_SECTION: ChaliceSettingsSection = {
|
|
30
|
+
id: "status-line",
|
|
31
|
+
title: "Status line",
|
|
32
|
+
description: "The line below the editor, and the footer statuses below it.",
|
|
33
|
+
order: 30,
|
|
34
|
+
items: [
|
|
35
|
+
{
|
|
36
|
+
id: "enabled",
|
|
37
|
+
type: "boolean",
|
|
38
|
+
key: "statusLine.enabled",
|
|
39
|
+
label: "Show status line",
|
|
40
|
+
defaultValue: true,
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
id: "layout",
|
|
44
|
+
type: "enum",
|
|
45
|
+
key: "statusLine.layout",
|
|
46
|
+
label: "Layout",
|
|
47
|
+
options: [
|
|
48
|
+
{ value: "left", label: "Left" },
|
|
49
|
+
{ value: "center", label: "Center" },
|
|
50
|
+
{ value: "right", label: "Right" },
|
|
51
|
+
],
|
|
52
|
+
defaultValue: "left",
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
id: "default-footer",
|
|
56
|
+
type: "boolean",
|
|
57
|
+
key: "statusLine.defaultFooter",
|
|
58
|
+
label: "Use Pi footer",
|
|
59
|
+
description: "Use Pi's built-in footer, which repeats cwd, context and model. Extension statuses (including the code index) render there instead, so the index segment drops out of the status line.",
|
|
60
|
+
defaultValue: false,
|
|
61
|
+
},
|
|
62
|
+
...STATUS_LINE_SEGMENTS.map((key) => ({
|
|
63
|
+
id: `segment-${key}`,
|
|
64
|
+
type: "boolean" as const,
|
|
65
|
+
key: `statusLine.segments.${key}`,
|
|
66
|
+
label: SEGMENT_LABELS[key],
|
|
67
|
+
defaultValue: true,
|
|
68
|
+
})),
|
|
69
|
+
],
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
interface CostCache {
|
|
73
|
+
entryCount: number;
|
|
74
|
+
total: number;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Session cost, summed from usage entries. Entries are append-only, so the result only has to be
|
|
79
|
+
* recomputed when the entry count changes.
|
|
80
|
+
*/
|
|
81
|
+
function sessionCost(ctx: ExtensionContext, cache: CostCache): number {
|
|
82
|
+
const entries = ctx.sessionManager.getEntries();
|
|
83
|
+
if (entries.length === cache.entryCount) return cache.total;
|
|
84
|
+
let total = 0;
|
|
85
|
+
for (const entry of entries) {
|
|
86
|
+
if (entry === null || typeof entry !== "object") continue;
|
|
87
|
+
const record = entry as { type?: unknown; usage?: unknown; message?: unknown };
|
|
88
|
+
const usage = record.type === "message" ? (record.message as { usage?: unknown } | undefined)?.usage : record.usage;
|
|
89
|
+
const cost = (usage as { cost?: { total?: unknown } } | undefined)?.cost?.total;
|
|
90
|
+
if (typeof cost === "number") total += cost;
|
|
91
|
+
}
|
|
92
|
+
cache.entryCount = entries.length;
|
|
93
|
+
cache.total = total;
|
|
94
|
+
return total;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function createSnapshot(
|
|
98
|
+
ctx: ExtensionContext,
|
|
99
|
+
mode: () => string | undefined,
|
|
100
|
+
cost: () => number,
|
|
101
|
+
statuses: () => ReadonlyMap<string, string> | undefined,
|
|
102
|
+
): () => StatusLineSnapshot {
|
|
103
|
+
return () => {
|
|
104
|
+
const usage = ctx.getContextUsage();
|
|
105
|
+
const model = ctx.model;
|
|
106
|
+
return {
|
|
107
|
+
mode: mode(),
|
|
108
|
+
cwd: formatCwd(ctx.cwd, process.env.HOME),
|
|
109
|
+
branch: readGitBranch(ctx.cwd),
|
|
110
|
+
modelId: model?.id,
|
|
111
|
+
thinkingLevel: model?.reasoning ? ctx.thinkingLevel : undefined,
|
|
112
|
+
cost: cost(),
|
|
113
|
+
contextPercent: usage?.percent ?? undefined,
|
|
114
|
+
contextWindow: usage?.contextWindow ?? model?.contextWindow,
|
|
115
|
+
indexStatus: statuses()?.get("code-index"),
|
|
116
|
+
};
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export default function statusLineExtension(pi: ExtensionAPI): void {
|
|
121
|
+
let modeName: string | undefined;
|
|
122
|
+
let requestRender: (() => void) | undefined;
|
|
123
|
+
let latestCtx: ExtensionContext | undefined;
|
|
124
|
+
/** Footer provider, captured from our own footer factory: it owns setStatus() texts. */
|
|
125
|
+
let latestFooterData: ReadonlyFooterDataProvider | undefined;
|
|
126
|
+
const costCache: CostCache = { entryCount: -1, total: 0 };
|
|
127
|
+
|
|
128
|
+
const apply = (ctx: ExtensionContext): void => {
|
|
129
|
+
if (ctx.mode !== "tui") return;
|
|
130
|
+
const { statusLine } = readChaliceSettings(ctx);
|
|
131
|
+
if (!statusLine.enabled) {
|
|
132
|
+
requestRender = undefined;
|
|
133
|
+
ctx.ui.setWidget(WIDGET_KEY, undefined);
|
|
134
|
+
ctx.ui.setFooter(undefined);
|
|
135
|
+
latestFooterData = undefined;
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
const snapshot = createSnapshot(
|
|
139
|
+
ctx,
|
|
140
|
+
() => modeName,
|
|
141
|
+
() => sessionCost(ctx, costCache),
|
|
142
|
+
() => latestFooterData?.getExtensionStatuses(),
|
|
143
|
+
);
|
|
144
|
+
ctx.ui.setWidget(
|
|
145
|
+
WIDGET_KEY,
|
|
146
|
+
(tui, theme) => {
|
|
147
|
+
requestRender = () => tui.requestRender();
|
|
148
|
+
return new StatusLineComponent(theme, snapshot, {
|
|
149
|
+
layout: statusLine.layout,
|
|
150
|
+
segments: statusLine.segments,
|
|
151
|
+
});
|
|
152
|
+
},
|
|
153
|
+
{ placement: "belowEditor" },
|
|
154
|
+
);
|
|
155
|
+
if (statusLine.defaultFooter) {
|
|
156
|
+
ctx.ui.setFooter(undefined);
|
|
157
|
+
latestFooterData = undefined;
|
|
158
|
+
} else {
|
|
159
|
+
// Pi's built-in footer repeats cwd/context/model. Replace it, but keep extension statuses.
|
|
160
|
+
const consumed = statusLine.segments.index === false ? [] : ["code-index"];
|
|
161
|
+
ctx.ui.setFooter((_tui, theme, footerData) => {
|
|
162
|
+
latestFooterData = footerData;
|
|
163
|
+
return new StatusFooterComponent(theme, footerData, consumed);
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
registerChaliceSettingsSection(pi, STATUS_LINE_SECTION);
|
|
169
|
+
|
|
170
|
+
pi.events.on(MODE_EVENT, (value) => {
|
|
171
|
+
modeName = typeof value === "string" ? value : undefined;
|
|
172
|
+
requestRender?.();
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
pi.on("session_start", (_event, ctx) => {
|
|
176
|
+
latestCtx = ctx;
|
|
177
|
+
apply(ctx);
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
pi.events.on(CHALICE_SETTINGS_EVENT, () => {
|
|
181
|
+
if (latestCtx) apply(latestCtx);
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
pi.on("session_shutdown", (_event, ctx) => {
|
|
185
|
+
latestCtx = undefined;
|
|
186
|
+
latestFooterData = undefined;
|
|
187
|
+
requestRender = undefined;
|
|
188
|
+
ctx.ui.setWidget(WIDGET_KEY, undefined);
|
|
189
|
+
ctx.ui.setFooter(undefined);
|
|
190
|
+
});
|
|
191
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,27 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
"name": "@leolastforce/chalice-status-line",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Chalice module for the status line under the input bar",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"keywords": ["chalice", "pi-package"],
|
|
8
|
+
"pi": {
|
|
9
|
+
"extensions": ["./index.ts"]
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"index.ts",
|
|
13
|
+
"footer.ts",
|
|
14
|
+
"git.ts",
|
|
15
|
+
"status-line.ts"
|
|
16
|
+
],
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"@leolastforce/chalice-shared": "0.1.0"
|
|
19
|
+
},
|
|
20
|
+
"publishConfig": {
|
|
21
|
+
"access": "public"
|
|
22
|
+
},
|
|
23
|
+
"peerDependencies": {
|
|
24
|
+
"@earendil-works/pi-coding-agent": "*",
|
|
25
|
+
"@earendil-works/pi-tui": "*"
|
|
26
|
+
}
|
|
27
|
+
}
|
package/status-line.ts
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import type { Theme, ThemeColor } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { type Component, truncateToWidth, visibleWidth } from "@earendil-works/pi-tui";
|
|
3
|
+
import type { StatusLineLayout, StatusLineSegmentKey } from "@leolastforce/chalice-shared";
|
|
4
|
+
|
|
5
|
+
export type { StatusLineSegmentKey };
|
|
6
|
+
|
|
7
|
+
export interface StatusLineSegment {
|
|
8
|
+
key: StatusLineSegmentKey;
|
|
9
|
+
/** Plain text, used by callers that need a width without styling. */
|
|
10
|
+
text: string;
|
|
11
|
+
styled: string;
|
|
12
|
+
color: ThemeColor;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface StatusLineSnapshot {
|
|
16
|
+
mode?: string;
|
|
17
|
+
cwd: string;
|
|
18
|
+
branch?: string;
|
|
19
|
+
modelId?: string;
|
|
20
|
+
thinkingLevel?: string;
|
|
21
|
+
cost: number;
|
|
22
|
+
contextPercent?: number;
|
|
23
|
+
contextWindow?: number;
|
|
24
|
+
/** Code index status reported by the index extension through ctx.ui.setStatus(). */
|
|
25
|
+
indexStatus?: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface StatusLineOptions {
|
|
29
|
+
layout?: StatusLineLayout;
|
|
30
|
+
segments?: Record<string, boolean>;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Display labels for chalice modes whose config name is not simply title-cased. */
|
|
34
|
+
const MODE_LABELS: Record<string, string> = { yolo: "YOLO" };
|
|
35
|
+
|
|
36
|
+
function modeLabel(value: string): string {
|
|
37
|
+
const known = MODE_LABELS[value.toLowerCase()];
|
|
38
|
+
if (known) return known;
|
|
39
|
+
return value.charAt(0).toUpperCase() + value.slice(1);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Compact token counts, matching Pi's own footer formatting. */
|
|
43
|
+
export function formatTokens(count: number): string {
|
|
44
|
+
if (count < 1000) return count.toString();
|
|
45
|
+
if (count < 10000) return `${(count / 1000).toFixed(1)}k`;
|
|
46
|
+
if (count < 1000000) return `${Math.round(count / 1000)}k`;
|
|
47
|
+
if (count < 10000000) return `${(count / 1000000).toFixed(1)}M`;
|
|
48
|
+
return `${Math.round(count / 1000000)}M`;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Strip SGR sequences so status text can be re-themed and measured as plain text. */
|
|
52
|
+
function stripSgr(text: string): string {
|
|
53
|
+
return text.replace(/\x1b\[[0-9;]*m/g, "");
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Collapse newlines and runs of spaces so a status cannot break the single-line layout. */
|
|
57
|
+
function sanitizeStatus(text: string): string {
|
|
58
|
+
return stripSgr(text).replace(/[\r\n\t]+/g, " ").replace(/ +/g, " ").trim();
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Index states that ask the user to reindex. */
|
|
62
|
+
const INDEX_NEEDS_ACTION = /^(not indexed|stale|error|changes pending)\b/i;
|
|
63
|
+
export function buildSegments(
|
|
64
|
+
snapshot: StatusLineSnapshot,
|
|
65
|
+
theme: Theme,
|
|
66
|
+
visible: (key: StatusLineSegmentKey) => boolean,
|
|
67
|
+
): StatusLineSegment[] {
|
|
68
|
+
const segments: StatusLineSegment[] = [];
|
|
69
|
+
if (visible("mode") && snapshot.mode) {
|
|
70
|
+
const label = modeLabel(snapshot.mode);
|
|
71
|
+
segments.push({ key: "mode", text: label, styled: theme.bold(theme.fg("accent", label)), color: "accent" });
|
|
72
|
+
}
|
|
73
|
+
if (visible("cwd") && snapshot.cwd) {
|
|
74
|
+
segments.push({ key: "cwd", text: snapshot.cwd, styled: theme.fg("muted", snapshot.cwd), color: "muted" });
|
|
75
|
+
}
|
|
76
|
+
if (visible("branch") && snapshot.branch) {
|
|
77
|
+
segments.push({
|
|
78
|
+
key: "branch",
|
|
79
|
+
text: snapshot.branch,
|
|
80
|
+
styled: theme.fg("success", snapshot.branch),
|
|
81
|
+
color: "success",
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
if (visible("model") && snapshot.modelId) {
|
|
85
|
+
const { modelId, thinkingLevel } = snapshot;
|
|
86
|
+
const text = thinkingLevel ? `${modelId} · ${thinkingLevel}` : modelId;
|
|
87
|
+
const styled = thinkingLevel
|
|
88
|
+
? `${theme.fg("text", modelId)}${theme.fg("muted", " · ")}${theme.fg("warning", thinkingLevel)}`
|
|
89
|
+
: theme.fg("text", modelId);
|
|
90
|
+
segments.push({ key: "model", text, styled, color: "text" });
|
|
91
|
+
}
|
|
92
|
+
if (visible("cost") && snapshot.cost > 0) {
|
|
93
|
+
const cost = `$${snapshot.cost.toFixed(3)}`;
|
|
94
|
+
segments.push({ key: "cost", text: cost, styled: theme.fg("warning", cost), color: "warning" });
|
|
95
|
+
}
|
|
96
|
+
if (visible("context") && snapshot.contextPercent !== undefined) {
|
|
97
|
+
const percent = `${snapshot.contextPercent.toFixed(1)}%`;
|
|
98
|
+
const high = snapshot.contextPercent > 90;
|
|
99
|
+
const color: ThemeColor = high ? "error" : "warning";
|
|
100
|
+
segments.push({ key: "context", text: percent, styled: theme.fg(color, percent), color });
|
|
101
|
+
}
|
|
102
|
+
if (visible("contextWindow") && snapshot.contextWindow) {
|
|
103
|
+
const window = formatTokens(snapshot.contextWindow);
|
|
104
|
+
segments.push({ key: "contextWindow", text: window, styled: theme.fg("dim", window), color: "dim" });
|
|
105
|
+
}
|
|
106
|
+
if (visible("index") && snapshot.indexStatus) {
|
|
107
|
+
const status = sanitizeStatus(snapshot.indexStatus);
|
|
108
|
+
if (status) {
|
|
109
|
+
const color: ThemeColor = INDEX_NEEDS_ACTION.test(status)
|
|
110
|
+
? "error"
|
|
111
|
+
: status === "off"
|
|
112
|
+
? "dim"
|
|
113
|
+
: "accent";
|
|
114
|
+
segments.push({ key: "index", text: status, styled: theme.fg(color, status), color });
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return segments;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function join(segments: readonly StatusLineSegment[], theme: Theme): string {
|
|
121
|
+
return segments.map((segment) => segment.styled).join(theme.fg("dim", " · "));
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export interface StatusLineParts {
|
|
125
|
+
left: string;
|
|
126
|
+
center?: string;
|
|
127
|
+
right: string;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Split stats across the line. "left" and "right" keep them together; "center" pins the model
|
|
132
|
+
* segment in the middle and alternates the remaining stats around it.
|
|
133
|
+
*/
|
|
134
|
+
export function placeSegments(segments: readonly StatusLineSegment[], layout: StatusLineLayout, theme: Theme): StatusLineParts {
|
|
135
|
+
if (layout === "left") return { left: join(segments, theme), right: "" };
|
|
136
|
+
if (layout === "right") return { left: "", right: join(segments, theme) };
|
|
137
|
+
const pinned = segments.find((segment) => segment.key === "model") ?? segments[Math.floor(segments.length / 2)];
|
|
138
|
+
const others = segments.filter((segment) => segment !== pinned);
|
|
139
|
+
return {
|
|
140
|
+
left: join(
|
|
141
|
+
others.filter((_segment, index) => index % 2 === 0),
|
|
142
|
+
theme,
|
|
143
|
+
),
|
|
144
|
+
center: pinned ? join([pinned], theme) : undefined,
|
|
145
|
+
right: join(
|
|
146
|
+
others.filter((_segment, index) => index % 2 === 1),
|
|
147
|
+
theme,
|
|
148
|
+
),
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** Lay out one line of `width` columns, padding between the groups and truncating overflow. */
|
|
153
|
+
export function composeLine(parts: StatusLineParts, width: number): string {
|
|
154
|
+
const leftWidth = visibleWidth(parts.left);
|
|
155
|
+
const rightWidth = visibleWidth(parts.right);
|
|
156
|
+
if (parts.center === undefined) {
|
|
157
|
+
const gap = Math.max(0, width - leftWidth - rightWidth);
|
|
158
|
+
return truncateToWidth(`${parts.left}${" ".repeat(gap)}${parts.right}`, width, "…");
|
|
159
|
+
}
|
|
160
|
+
const centerWidth = visibleWidth(parts.center);
|
|
161
|
+
const centerStart = Math.max(leftWidth, Math.floor((width - centerWidth) / 2));
|
|
162
|
+
const rightStart = Math.max(centerStart + centerWidth, width - rightWidth);
|
|
163
|
+
const beforeCenter = " ".repeat(Math.max(0, centerStart - leftWidth));
|
|
164
|
+
const beforeRight = " ".repeat(Math.max(0, rightStart - centerStart - centerWidth));
|
|
165
|
+
return truncateToWidth(`${parts.left}${beforeCenter}${parts.center}${beforeRight}${parts.right}`, width, "…");
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Status line component. Every value is read from the snapshot on render, so the host does not have
|
|
170
|
+
* to push updates: the TUI repaints the widget whenever the frame changes.
|
|
171
|
+
*/
|
|
172
|
+
export class StatusLineComponent implements Component {
|
|
173
|
+
private readonly theme: Theme;
|
|
174
|
+
private readonly snapshot: () => StatusLineSnapshot;
|
|
175
|
+
private readonly layout: StatusLineLayout;
|
|
176
|
+
private readonly visible: (key: StatusLineSegmentKey) => boolean;
|
|
177
|
+
|
|
178
|
+
constructor(theme: Theme, snapshot: () => StatusLineSnapshot, options: StatusLineOptions = {}) {
|
|
179
|
+
this.theme = theme;
|
|
180
|
+
this.snapshot = snapshot;
|
|
181
|
+
this.layout = options.layout ?? "left";
|
|
182
|
+
this.visible = (key) => options.segments?.[key] !== false;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
invalidate(): void {}
|
|
186
|
+
|
|
187
|
+
render(width: number): readonly string[] {
|
|
188
|
+
if (width <= 0) return [];
|
|
189
|
+
const segments = buildSegments(this.snapshot(), this.theme, this.visible);
|
|
190
|
+
if (segments.length === 0) return [];
|
|
191
|
+
return [composeLine(placeSegments(segments, this.layout, this.theme), width)];
|
|
192
|
+
}
|
|
193
|
+
}
|