glintline 0.0.0-stage → 0.3.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/CHANGELOG.md +266 -0
- package/INSTALL.md +214 -0
- package/LICENSE +21 -0
- package/README.md +405 -2
- package/dist/glintline.js +7 -0
- package/glintline.example.json +38 -0
- package/package.json +54 -4
- package/src/ansi.ts +35 -0
- package/src/cache.ts +42 -0
- package/src/config.ts +199 -0
- package/src/context.ts +64 -0
- package/src/credentials.ts +46 -0
- package/src/debug.ts +9 -0
- package/src/format.ts +109 -0
- package/src/git.ts +68 -0
- package/src/index.ts +100 -0
- package/src/model.ts +120 -0
- package/src/render.ts +176 -0
- package/src/transcript.ts +240 -0
- package/src/types.ts +115 -0
- package/src/usage-api.ts +189 -0
- package/src/usage.ts +63 -0
- package/src/widgets.ts +419 -0
package/src/config.ts
ADDED
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { homedir } from "node:os";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { debug } from "./debug";
|
|
5
|
+
|
|
6
|
+
export interface ModelOpts {
|
|
7
|
+
showFamily: boolean;
|
|
8
|
+
showVersion: boolean;
|
|
9
|
+
showContext: boolean;
|
|
10
|
+
showEffortIcon: boolean;
|
|
11
|
+
label: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface EffortOpts {
|
|
15
|
+
label: string;
|
|
16
|
+
icons: Record<string, string>;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface ContextOpts {
|
|
20
|
+
showBar: boolean;
|
|
21
|
+
showNumbers: boolean;
|
|
22
|
+
showPercent: boolean;
|
|
23
|
+
showCompactBuffer: boolean;
|
|
24
|
+
// In the buffer-aware view, the trailing `/buffer` of `used/threshold/buffer`.
|
|
25
|
+
showBufferTokens: boolean;
|
|
26
|
+
fillEmpty: boolean;
|
|
27
|
+
barWidth: number;
|
|
28
|
+
barStyle: string;
|
|
29
|
+
barChars?: { full: string; empty: string };
|
|
30
|
+
gap: string;
|
|
31
|
+
label: string;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface LimitOpts {
|
|
35
|
+
showCountdown: boolean;
|
|
36
|
+
separator: string;
|
|
37
|
+
label: string;
|
|
38
|
+
mono: boolean;
|
|
39
|
+
showBurnRate: boolean;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface Limit7dOpts extends LimitOpts {
|
|
43
|
+
// Swap the general weekly window for the active model's per-model weekly cap
|
|
44
|
+
// if one exists, labeling it `<label>-<family>` (e.g. "7d-fable") and showing
|
|
45
|
+
// the general cap dimmed in parens. Requires the OAuth API; no-op otherwise.
|
|
46
|
+
perModel: boolean;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface CostOpts {
|
|
50
|
+
precision: number;
|
|
51
|
+
onlyWhenFast: boolean;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export interface ExtraOpts {
|
|
55
|
+
onlyWhenActive: boolean;
|
|
56
|
+
label: string;
|
|
57
|
+
mono: boolean;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface CacheOpts {
|
|
61
|
+
label: string;
|
|
62
|
+
mono: boolean;
|
|
63
|
+
// "session" — cumulative hit-rate over the whole transcript (lifetime).
|
|
64
|
+
// "last" — hit-rate of the most recent assistant turn only, so it reflects
|
|
65
|
+
// whether the cache is warm right now (drops after a resume re-primes it).
|
|
66
|
+
// "both" — the last turn (primary, health-colored) followed by the session
|
|
67
|
+
// total marked with `Σ` in a dimmer color, e.g. `96% Σ91%`.
|
|
68
|
+
window: "session" | "last" | "both";
|
|
69
|
+
// The hit-rate percentages; off leaves only the expiry/cold state.
|
|
70
|
+
showPercent: boolean;
|
|
71
|
+
// Trail the prompt cache's state from stdin `prompt_cache` (Claude Code
|
|
72
|
+
// v2.1.251+): while warm, the local time it goes cold (`⟳ 14:49`); once cold,
|
|
73
|
+
// `coldLabel` and the tokens the next request writes back (`cold ↑28K`).
|
|
74
|
+
// Absent on a resumed session until its first response.
|
|
75
|
+
showExpiry: boolean;
|
|
76
|
+
expiryLabel: string;
|
|
77
|
+
coldLabel: string;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export interface GitOpts {
|
|
81
|
+
worktreeLabel: string;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export interface SessionOpts {
|
|
85
|
+
label: string;
|
|
86
|
+
mono: boolean;
|
|
87
|
+
idLength: number;
|
|
88
|
+
idLabel: string;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export type CwdFormat = "short" | "home" | "full" | "smart";
|
|
92
|
+
|
|
93
|
+
export interface CwdOpts {
|
|
94
|
+
format: CwdFormat;
|
|
95
|
+
maxLength: number;
|
|
96
|
+
label: string;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export interface Config {
|
|
100
|
+
widgets: string[][];
|
|
101
|
+
separator: string;
|
|
102
|
+
calmColors: boolean;
|
|
103
|
+
// Widgets to shed, least important first, while a rendered line is wider than
|
|
104
|
+
// the terminal — so the overflow eats these instead of truncating whatever
|
|
105
|
+
// happens to sit at the end of the line. Claude Code exports COLUMNS before
|
|
106
|
+
// running the status line (v2.1.153+); without it nothing is dropped.
|
|
107
|
+
dropWhenNarrow: string[];
|
|
108
|
+
// Mirror of `statusLine.padding` from Claude Code settings — it is not passed
|
|
109
|
+
// to the command, yet it narrows the room the line gets on both sides.
|
|
110
|
+
statusLinePadding: number;
|
|
111
|
+
model: ModelOpts;
|
|
112
|
+
effort: EffortOpts;
|
|
113
|
+
context: ContextOpts;
|
|
114
|
+
limit5h: LimitOpts;
|
|
115
|
+
limit7d: Limit7dOpts;
|
|
116
|
+
cost: CostOpts;
|
|
117
|
+
extra: ExtraOpts;
|
|
118
|
+
cache: CacheOpts;
|
|
119
|
+
git: GitOpts;
|
|
120
|
+
session: SessionOpts;
|
|
121
|
+
cwd: CwdOpts;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export const DEFAULT_CONFIG: Config = {
|
|
125
|
+
widgets: [
|
|
126
|
+
["model", "effort", "cost", "context", "limit5h", "limit7d", "extra", "git.worktree", "git.branch", "git.diff"],
|
|
127
|
+
["session.id", "cwd"],
|
|
128
|
+
],
|
|
129
|
+
separator: " │ ",
|
|
130
|
+
calmColors: false,
|
|
131
|
+
dropWhenNarrow: [],
|
|
132
|
+
statusLinePadding: 0,
|
|
133
|
+
model: { showFamily: true, showVersion: true, showContext: true, showEffortIcon: true, label: "✳" },
|
|
134
|
+
effort: { label: "∼", icons: { low: "·", medium: "⁚", high: "⁖", xhigh: "⁘", max: "⁙" } },
|
|
135
|
+
context: { showBar: true, showNumbers: true, showPercent: false, showCompactBuffer: false, showBufferTokens: true, fillEmpty: true, barWidth: 10, barStyle: "square", gap: " ", label: "⊠" },
|
|
136
|
+
limit5h: { showCountdown: true, separator: " ", label: "5h", mono: false, showBurnRate: true },
|
|
137
|
+
limit7d: { showCountdown: true, separator: " ", label: "7d", mono: false, showBurnRate: true, perModel: false },
|
|
138
|
+
cost: { precision: 2, onlyWhenFast: true },
|
|
139
|
+
extra: { onlyWhenActive: false, label: "extra", mono: false },
|
|
140
|
+
cache: { label: "cache", mono: false, window: "session", showPercent: true, showExpiry: false, expiryLabel: "⟳", coldLabel: "cold" },
|
|
141
|
+
git: { worktreeLabel: "⎘" },
|
|
142
|
+
session: { label: "⏱︎", mono: false, idLength: 8, idLabel: "#" },
|
|
143
|
+
cwd: { format: "smart", maxLength: 40, label: "⊕" },
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
type DeepPartial<T> = { [K in keyof T]?: T[K] extends object ? DeepPartial<T[K]> : T[K] };
|
|
147
|
+
|
|
148
|
+
function candidatePaths(): string[] {
|
|
149
|
+
const env = process.env.GLINTLINE_CONFIG;
|
|
150
|
+
if (env) return [env];
|
|
151
|
+
return [
|
|
152
|
+
join(homedir(), ".config", "glintline", "settings.json"),
|
|
153
|
+
join(homedir(), ".config", "glintline.json"),
|
|
154
|
+
join(homedir(), ".claude", "glintline.json"),
|
|
155
|
+
];
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function readJson(path: string): unknown {
|
|
159
|
+
try {
|
|
160
|
+
return JSON.parse(readFileSync(path, "utf8"));
|
|
161
|
+
} catch (e) {
|
|
162
|
+
if ((e as NodeJS.ErrnoException).code !== "ENOENT") debug("config.read", e);
|
|
163
|
+
return null;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function isPlainObject(v: unknown): v is Record<string, unknown> {
|
|
168
|
+
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const DANGEROUS_KEYS = new Set(["__proto__", "constructor", "prototype"]);
|
|
172
|
+
|
|
173
|
+
function merge<T>(base: T, over: DeepPartial<T> | undefined): T {
|
|
174
|
+
if (!over) return base;
|
|
175
|
+
if (!isPlainObject(base) || !isPlainObject(over)) return base;
|
|
176
|
+
const out = { ...base } as Record<string, unknown>;
|
|
177
|
+
for (const key of Object.keys(over)) {
|
|
178
|
+
if (DANGEROUS_KEYS.has(key)) continue;
|
|
179
|
+
const val = (over as Record<string, unknown>)[key];
|
|
180
|
+
const baseVal = out[key];
|
|
181
|
+
if (val === undefined) continue;
|
|
182
|
+
if (isPlainObject(val) && isPlainObject(baseVal)) {
|
|
183
|
+
out[key] = merge(baseVal, val as DeepPartial<typeof baseVal>);
|
|
184
|
+
} else {
|
|
185
|
+
out[key] = val;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
return out as T;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
export function loadConfig(): Config {
|
|
192
|
+
for (const path of candidatePaths()) {
|
|
193
|
+
const parsed = readJson(path);
|
|
194
|
+
if (isPlainObject(parsed)) {
|
|
195
|
+
return merge(DEFAULT_CONFIG, parsed as DeepPartial<Config>);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
return DEFAULT_CONFIG;
|
|
199
|
+
}
|
package/src/context.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { StatusInput } from "./types";
|
|
2
|
+
|
|
3
|
+
export interface ContextUsage {
|
|
4
|
+
used: number;
|
|
5
|
+
total: number;
|
|
6
|
+
compactThreshold?: number;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const DEFAULT_CONTEXT_SIZE = 200_000;
|
|
10
|
+
const MAX_WINDOW_TOKENS = 10_000_000;
|
|
11
|
+
|
|
12
|
+
function readPositive(name: string, max: number): number | null {
|
|
13
|
+
const raw = process.env[name];
|
|
14
|
+
if (!raw) return null;
|
|
15
|
+
const n = parseFloat(raw);
|
|
16
|
+
if (!Number.isFinite(n) || n <= 0) return null;
|
|
17
|
+
return Math.min(n, max);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
interface Totals {
|
|
21
|
+
total: number;
|
|
22
|
+
compactThreshold?: number;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function effectiveTotals(stdinTotal: number, showBuffer: boolean): Totals {
|
|
26
|
+
const window = readPositive("CLAUDE_CODE_AUTO_COMPACT_WINDOW", MAX_WINDOW_TOKENS) ?? stdinTotal;
|
|
27
|
+
const pct = readPositive("CLAUDE_AUTOCOMPACT_PCT_OVERRIDE", 100);
|
|
28
|
+
if (pct === null) return { total: window };
|
|
29
|
+
const threshold = Math.round(window * (pct / 100));
|
|
30
|
+
if (showBuffer) return { total: window, compactThreshold: threshold };
|
|
31
|
+
return { total: threshold };
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// Apply the same auto-compact window/threshold logic to a context reading taken
|
|
35
|
+
// from the transcript fallback, so its percentage base matches the stdin path.
|
|
36
|
+
export function withAutoCompact(
|
|
37
|
+
ctx: { used: number; total: number } | null,
|
|
38
|
+
showBuffer: boolean,
|
|
39
|
+
): ContextUsage | null {
|
|
40
|
+
if (!ctx) return null;
|
|
41
|
+
return { used: ctx.used, ...effectiveTotals(ctx.total, showBuffer) };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function getContextFromInput(input: StatusInput, showBuffer: boolean): ContextUsage | null {
|
|
45
|
+
const cw = input.context_window;
|
|
46
|
+
if (!cw) return null;
|
|
47
|
+
|
|
48
|
+
const totals = effectiveTotals(cw.context_window_size ?? DEFAULT_CONTEXT_SIZE, showBuffer);
|
|
49
|
+
const cur = cw.current_usage;
|
|
50
|
+
if (cur) {
|
|
51
|
+
const used =
|
|
52
|
+
(cur.input_tokens ?? 0) +
|
|
53
|
+
(cur.cache_creation_input_tokens ?? 0) +
|
|
54
|
+
(cur.cache_read_input_tokens ?? 0);
|
|
55
|
+
if (used > 0) return { used, ...totals };
|
|
56
|
+
}
|
|
57
|
+
if (cw.total_input_tokens && cw.total_input_tokens > 0) {
|
|
58
|
+
return { used: cw.total_input_tokens, ...totals };
|
|
59
|
+
}
|
|
60
|
+
if (cw.used_percentage !== undefined) {
|
|
61
|
+
return { used: Math.round((cw.used_percentage / 100) * totals.total), ...totals };
|
|
62
|
+
}
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { execFileSync } from "node:child_process";
|
|
2
|
+
import { readFileSync } from "node:fs";
|
|
3
|
+
import { homedir, platform } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { debug } from "./debug";
|
|
6
|
+
|
|
7
|
+
const KEYCHAIN_SERVICE = "Claude Code-credentials";
|
|
8
|
+
|
|
9
|
+
interface CredentialsFile {
|
|
10
|
+
claudeAiOauth?: { accessToken?: string };
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function fromKeychain(): string | null {
|
|
14
|
+
try {
|
|
15
|
+
const out = execFileSync(
|
|
16
|
+
"security",
|
|
17
|
+
["find-generic-password", "-s", KEYCHAIN_SERVICE, "-w"],
|
|
18
|
+
{ encoding: "utf8", stdio: ["ignore", "pipe", "ignore"] },
|
|
19
|
+
).trim();
|
|
20
|
+
if (!out) return null;
|
|
21
|
+
const parsed = JSON.parse(out) as CredentialsFile;
|
|
22
|
+
return parsed.claudeAiOauth?.accessToken ?? null;
|
|
23
|
+
} catch (e) {
|
|
24
|
+
debug("credentials.keychain", e);
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function fromFile(): string | null {
|
|
30
|
+
try {
|
|
31
|
+
const path = join(homedir(), ".claude", ".credentials.json");
|
|
32
|
+
const raw = readFileSync(path, "utf8");
|
|
33
|
+
const parsed = JSON.parse(raw) as CredentialsFile;
|
|
34
|
+
return parsed.claudeAiOauth?.accessToken ?? null;
|
|
35
|
+
} catch (e) {
|
|
36
|
+
debug("credentials.file", e);
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function getOAuthToken(): string | null {
|
|
42
|
+
if (platform() === "darwin") {
|
|
43
|
+
return fromKeychain() ?? fromFile();
|
|
44
|
+
}
|
|
45
|
+
return fromFile();
|
|
46
|
+
}
|
package/src/debug.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
const ENABLED =
|
|
2
|
+
process.env.GLINTLINE_DEBUG === "1" ||
|
|
3
|
+
process.env.GLINTLINE_DEBUG === "true";
|
|
4
|
+
|
|
5
|
+
export function debug(scope: string, err: unknown): void {
|
|
6
|
+
if (!ENABLED) return;
|
|
7
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
8
|
+
process.stderr.write(`[glintline:${scope}] ${msg}\n`);
|
|
9
|
+
}
|
package/src/format.ts
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { c, wrap } from "./ansi";
|
|
2
|
+
|
|
3
|
+
export function formatNumber(n: number): string {
|
|
4
|
+
if (n >= 1_000_000) {
|
|
5
|
+
const m = n / 1_000_000;
|
|
6
|
+
return `${m >= 10 ? m.toFixed(0) : trimZero(m.toFixed(1))}M`;
|
|
7
|
+
}
|
|
8
|
+
if (n >= 1_000) {
|
|
9
|
+
const k = n / 1_000;
|
|
10
|
+
return `${k >= 100 ? k.toFixed(0) : trimZero(k.toFixed(1))}K`;
|
|
11
|
+
}
|
|
12
|
+
return String(n);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function trimZero(s: string): string {
|
|
16
|
+
return s.replace(/\.0$/, "");
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function formatTokens(n: number): string {
|
|
20
|
+
if (n >= 1_000_000) {
|
|
21
|
+
const v = n / 1_000_000;
|
|
22
|
+
return `${v >= 10 ? v.toFixed(0) : trimZero(v.toFixed(1))}M`;
|
|
23
|
+
}
|
|
24
|
+
if (n >= 1_000) return `${Math.round(n / 1_000)}K`;
|
|
25
|
+
return String(n);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function formatDuration(seconds: number): string {
|
|
29
|
+
if (seconds <= 0) return "0m";
|
|
30
|
+
const minutes = seconds / 60;
|
|
31
|
+
if (minutes < 60) return `${Math.round(minutes)}m`;
|
|
32
|
+
const hours = minutes / 60;
|
|
33
|
+
if (hours < 24) {
|
|
34
|
+
return `${hours >= 10 ? Math.round(hours) : trimZero(hours.toFixed(1))}h`;
|
|
35
|
+
}
|
|
36
|
+
const days = hours / 24;
|
|
37
|
+
return `${days >= 10 ? Math.round(days) : trimZero(days.toFixed(1))}d`;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Local wall-clock time as HH:MM. Used where a relative countdown would go stale:
|
|
41
|
+
// Claude Code does not redraw the status line while the session sits idle.
|
|
42
|
+
export function formatClock(epochSec: number): string {
|
|
43
|
+
const d = new Date(epochSec * 1000);
|
|
44
|
+
return `${String(d.getHours()).padStart(2, "0")}:${String(d.getMinutes()).padStart(2, "0")}`;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function colorByPercent(pct: number, calm: boolean = false): string {
|
|
48
|
+
if (pct >= 90) return calm ? c.mutedRed : c.red;
|
|
49
|
+
if (pct >= 75) return calm ? c.mutedYellow : c.yellow;
|
|
50
|
+
return calm ? c.mutedGreen : c.green;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function cacheHitColor(pct: number): string {
|
|
54
|
+
if (pct >= 80) return c.green;
|
|
55
|
+
if (pct >= 50) return c.yellow;
|
|
56
|
+
return c.gray;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function cacheHitRate(cacheRead: number, cacheCreation: number, input: number): number | null {
|
|
60
|
+
const total = cacheRead + cacheCreation + input;
|
|
61
|
+
return total > 0 ? (cacheRead / total) * 100 : null;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export const BAR_PRESETS: Record<string, { full: string; empty: string }> = {
|
|
65
|
+
square: { full: "■", empty: "□" },
|
|
66
|
+
circle: { full: "●", empty: "○" },
|
|
67
|
+
bar: { full: "█", empty: "░" },
|
|
68
|
+
ascii: { full: "#", empty: "-" },
|
|
69
|
+
dots: { full: "⬢", empty: "⬡" },
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
export interface ProgressBarOpts {
|
|
73
|
+
full: string;
|
|
74
|
+
empty: string;
|
|
75
|
+
threshold?: number;
|
|
76
|
+
fillEmpty?: boolean;
|
|
77
|
+
calm?: boolean;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function progressBar(
|
|
81
|
+
used: number,
|
|
82
|
+
total: number,
|
|
83
|
+
width: number,
|
|
84
|
+
opts: ProgressBarOpts,
|
|
85
|
+
): string {
|
|
86
|
+
if (total <= 0) return "";
|
|
87
|
+
const { full, empty, threshold, fillEmpty = false, calm = false } = opts;
|
|
88
|
+
const usedRatio = Math.min(1, Math.max(0, used / total));
|
|
89
|
+
const usedCells = Math.round(usedRatio * width);
|
|
90
|
+
const emptyChar = fillEmpty ? full : empty;
|
|
91
|
+
const emptyColor = fillEmpty ? c.darkestGray : c.darkGray;
|
|
92
|
+
|
|
93
|
+
if (threshold !== undefined && threshold > 0 && threshold < total) {
|
|
94
|
+
const pctForColor = (used / threshold) * 100;
|
|
95
|
+
const color = colorByPercent(pctForColor, calm);
|
|
96
|
+
const thresholdCells = Math.round((threshold / total) * width);
|
|
97
|
+
const middleCells = Math.max(0, thresholdCells - usedCells);
|
|
98
|
+
const bufferCells = Math.max(0, width - usedCells - middleCells);
|
|
99
|
+
return (
|
|
100
|
+
wrap(color, full.repeat(usedCells)) +
|
|
101
|
+
wrap(emptyColor, emptyChar.repeat(middleCells)) +
|
|
102
|
+
wrap(c.gray, full.repeat(bufferCells))
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const emptyCount = Math.max(0, width - usedCells);
|
|
107
|
+
const color = colorByPercent(usedRatio * 100, calm);
|
|
108
|
+
return wrap(color, full.repeat(usedCells)) + wrap(emptyColor, emptyChar.repeat(emptyCount));
|
|
109
|
+
}
|
package/src/git.ts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { execFileSync } from "node:child_process";
|
|
2
|
+
import { debug } from "./debug";
|
|
3
|
+
|
|
4
|
+
const GIT_TIMEOUT_MS = 1500;
|
|
5
|
+
|
|
6
|
+
export interface GitInfo {
|
|
7
|
+
branch: string;
|
|
8
|
+
added: number;
|
|
9
|
+
removed: number;
|
|
10
|
+
dirty: boolean;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function run(args: string[], cwd: string): string {
|
|
14
|
+
return execFileSync("git", args, {
|
|
15
|
+
cwd,
|
|
16
|
+
encoding: "utf8",
|
|
17
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
18
|
+
timeout: GIT_TIMEOUT_MS,
|
|
19
|
+
}).trim();
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function gitInfo(cwd: string): GitInfo | null {
|
|
23
|
+
let branch = "";
|
|
24
|
+
try {
|
|
25
|
+
branch = run(["symbolic-ref", "--short", "HEAD"], cwd);
|
|
26
|
+
} catch {
|
|
27
|
+
try {
|
|
28
|
+
branch = run(["rev-parse", "--short", "HEAD"], cwd);
|
|
29
|
+
} catch {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
let added = 0;
|
|
35
|
+
let removed = 0;
|
|
36
|
+
let dirty = false;
|
|
37
|
+
const addNumstat = (out: string) => {
|
|
38
|
+
if (!out) return;
|
|
39
|
+
dirty = true;
|
|
40
|
+
for (const line of out.split("\n")) {
|
|
41
|
+
const [a, r] = line.split("\t");
|
|
42
|
+
const ai = parseInt(a ?? "", 10);
|
|
43
|
+
const ri = parseInt(r ?? "", 10);
|
|
44
|
+
if (Number.isFinite(ai)) added += ai;
|
|
45
|
+
if (Number.isFinite(ri)) removed += ri;
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
// Unstaged and staged diffs are run separately so one failing doesn't suppress
|
|
49
|
+
// the others, and so a repo with no commits (unborn HEAD, where `diff HEAD`
|
|
50
|
+
// errors) still reports staged changes via `--cached` against the empty tree.
|
|
51
|
+
try {
|
|
52
|
+
addNumstat(run(["diff", "--numstat"], cwd));
|
|
53
|
+
} catch (e) {
|
|
54
|
+
debug("git.diff", e);
|
|
55
|
+
}
|
|
56
|
+
try {
|
|
57
|
+
addNumstat(run(["diff", "--cached", "--numstat"], cwd));
|
|
58
|
+
} catch (e) {
|
|
59
|
+
debug("git.diffCached", e);
|
|
60
|
+
}
|
|
61
|
+
try {
|
|
62
|
+
if (run(["ls-files", "--others", "--exclude-standard"], cwd)) dirty = true;
|
|
63
|
+
} catch (e) {
|
|
64
|
+
debug("git.untracked", e);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return { branch, added, removed, dirty };
|
|
68
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { writeFileSync } from "node:fs";
|
|
3
|
+
import type { StatusInput } from "./types";
|
|
4
|
+
import { getModelInfo } from "./model";
|
|
5
|
+
import { getContextFromInput, withAutoCompact } from "./context";
|
|
6
|
+
import { scanTranscript } from "./transcript";
|
|
7
|
+
import { getUsage, widgetsNeedApi } from "./usage";
|
|
8
|
+
import { gitInfo } from "./git";
|
|
9
|
+
import { render, terminalColumns, visibleWidth } from "./render";
|
|
10
|
+
import { loadConfig } from "./config";
|
|
11
|
+
import { debug } from "./debug";
|
|
12
|
+
|
|
13
|
+
async function readStdin(): Promise<string> {
|
|
14
|
+
if (process.stdin.isTTY) return "";
|
|
15
|
+
const chunks: Buffer[] = [];
|
|
16
|
+
for await (const chunk of process.stdin) {
|
|
17
|
+
chunks.push(chunk as Buffer);
|
|
18
|
+
}
|
|
19
|
+
return Buffer.concat(chunks).toString("utf8");
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function parseInput(raw: string): StatusInput {
|
|
23
|
+
if (!raw.trim()) return {};
|
|
24
|
+
try {
|
|
25
|
+
return JSON.parse(raw) as StatusInput;
|
|
26
|
+
} catch {
|
|
27
|
+
return {};
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
async function main(): Promise<void> {
|
|
32
|
+
if (process.argv.includes("--refresh-usage")) {
|
|
33
|
+
const { refreshUsageOnce } = await import("./usage-api");
|
|
34
|
+
await refreshUsageOnce();
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const raw = await readStdin();
|
|
39
|
+
const dumpPath = process.env.GLINTLINE_DUMP_STDIN;
|
|
40
|
+
if (dumpPath) {
|
|
41
|
+
try {
|
|
42
|
+
writeFileSync(dumpPath, raw);
|
|
43
|
+
} catch (e) {
|
|
44
|
+
debug("stdin.dump", e);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
// Diagnostics for the narrow-terminal logic: records the width Claude Code
|
|
48
|
+
// actually hands us (COLUMNS/LINES) alongside the line it produced.
|
|
49
|
+
const envDump = process.env.GLINTLINE_DUMP_ENV;
|
|
50
|
+
|
|
51
|
+
const input = parseInput(raw);
|
|
52
|
+
|
|
53
|
+
const cwd = input.workspace?.current_dir ?? input.cwd ?? process.cwd();
|
|
54
|
+
const config = loadConfig();
|
|
55
|
+
const transcript = scanTranscript(input.transcript_path);
|
|
56
|
+
|
|
57
|
+
const usage = getUsage(input, widgetsNeedApi(config));
|
|
58
|
+
const context =
|
|
59
|
+
getContextFromInput(input, config.context.showCompactBuffer) ??
|
|
60
|
+
withAutoCompact(transcript.context, config.context.showCompactBuffer);
|
|
61
|
+
|
|
62
|
+
const line = render({
|
|
63
|
+
input,
|
|
64
|
+
model: getModelInfo({ input, sessionEffort: transcript.effort, fast: transcript.fast }),
|
|
65
|
+
context,
|
|
66
|
+
usage,
|
|
67
|
+
git: gitInfo(cwd),
|
|
68
|
+
totals: transcript.totals,
|
|
69
|
+
cacheLast: transcript.last,
|
|
70
|
+
config,
|
|
71
|
+
columns: terminalColumns(),
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
if (envDump) {
|
|
75
|
+
try {
|
|
76
|
+
writeFileSync(
|
|
77
|
+
envDump,
|
|
78
|
+
JSON.stringify(
|
|
79
|
+
{
|
|
80
|
+
COLUMNS: process.env.COLUMNS ?? null,
|
|
81
|
+
LINES: process.env.LINES ?? null,
|
|
82
|
+
parsedColumns: terminalColumns(),
|
|
83
|
+
lines: line.split("\n").map((l) => ({ width: visibleWidth(l), text: l.replace(/\x1b\[[0-9;]*m/g, "") })),
|
|
84
|
+
},
|
|
85
|
+
null,
|
|
86
|
+
2,
|
|
87
|
+
),
|
|
88
|
+
);
|
|
89
|
+
} catch (e) {
|
|
90
|
+
debug("env.dump", e);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
process.stdout.write(line);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
main().catch((err) => {
|
|
98
|
+
debug("main", err);
|
|
99
|
+
process.exit(0);
|
|
100
|
+
});
|