@praeviso/code-env-switch 0.1.0 → 0.1.2
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/.eslintrc.cjs +18 -0
- package/.github/workflows/npm-publish.yml +25 -0
- package/.vscode/settings.json +4 -0
- package/AGENTS.md +32 -0
- package/LICENSE +21 -0
- package/PLAN.md +33 -0
- package/README.md +208 -32
- package/README_zh.md +265 -0
- package/bin/cli/args.js +303 -0
- package/bin/cli/help.js +77 -0
- package/bin/cli/index.js +13 -0
- package/bin/commands/add.js +81 -0
- package/bin/commands/index.js +21 -0
- package/bin/commands/launch.js +330 -0
- package/bin/commands/list.js +57 -0
- package/bin/commands/show.js +10 -0
- package/bin/commands/statusline.js +12 -0
- package/bin/commands/unset.js +20 -0
- package/bin/commands/use.js +92 -0
- package/bin/config/defaults.js +85 -0
- package/bin/config/index.js +20 -0
- package/bin/config/io.js +72 -0
- package/bin/constants.js +27 -0
- package/bin/index.js +279 -0
- package/bin/profile/display.js +78 -0
- package/bin/profile/index.js +26 -0
- package/bin/profile/match.js +40 -0
- package/bin/profile/resolve.js +79 -0
- package/bin/profile/type.js +90 -0
- package/bin/shell/detect.js +40 -0
- package/bin/shell/index.js +18 -0
- package/bin/shell/snippet.js +92 -0
- package/bin/shell/utils.js +35 -0
- package/bin/statusline/claude.js +153 -0
- package/bin/statusline/codex.js +356 -0
- package/bin/statusline/index.js +469 -0
- package/bin/types.js +5 -0
- package/bin/ui/index.js +16 -0
- package/bin/ui/interactive.js +189 -0
- package/bin/ui/readline.js +76 -0
- package/bin/usage/index.js +709 -0
- package/code-env.example.json +36 -23
- package/package.json +14 -2
- package/src/cli/args.ts +318 -0
- package/src/cli/help.ts +75 -0
- package/src/cli/index.ts +5 -0
- package/src/commands/add.ts +91 -0
- package/src/commands/index.ts +10 -0
- package/src/commands/launch.ts +395 -0
- package/src/commands/list.ts +91 -0
- package/src/commands/show.ts +12 -0
- package/src/commands/statusline.ts +18 -0
- package/src/commands/unset.ts +19 -0
- package/src/commands/use.ts +121 -0
- package/src/config/defaults.ts +88 -0
- package/src/config/index.ts +19 -0
- package/src/config/io.ts +69 -0
- package/src/constants.ts +28 -0
- package/src/index.ts +359 -0
- package/src/profile/display.ts +77 -0
- package/src/profile/index.ts +12 -0
- package/src/profile/match.ts +41 -0
- package/src/profile/resolve.ts +84 -0
- package/src/profile/type.ts +83 -0
- package/src/shell/detect.ts +30 -0
- package/src/shell/index.ts +6 -0
- package/src/shell/snippet.ts +92 -0
- package/src/shell/utils.ts +30 -0
- package/src/statusline/claude.ts +172 -0
- package/src/statusline/codex.ts +393 -0
- package/src/statusline/index.ts +626 -0
- package/src/types.ts +95 -0
- package/src/ui/index.ts +5 -0
- package/src/ui/interactive.ts +220 -0
- package/src/ui/readline.ts +85 -0
- package/src/usage/index.ts +833 -0
- package/tsconfig.json +12 -0
- package/bin/codenv.js +0 -377
|
@@ -0,0 +1,626 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Statusline builder
|
|
3
|
+
*/
|
|
4
|
+
import * as fs from "fs";
|
|
5
|
+
import * as path from "path";
|
|
6
|
+
import { spawnSync } from "child_process";
|
|
7
|
+
import type { Config, StatuslineArgs } from "../types";
|
|
8
|
+
import { DEFAULT_PROFILE_TYPES } from "../constants";
|
|
9
|
+
import { normalizeType, inferProfileType, getProfileDisplayName } from "../profile/type";
|
|
10
|
+
import {
|
|
11
|
+
formatTokenCount,
|
|
12
|
+
readUsageTotalsIndex,
|
|
13
|
+
resolveUsageTotalsForProfile,
|
|
14
|
+
} from "../usage";
|
|
15
|
+
|
|
16
|
+
interface StatuslineInputProfile {
|
|
17
|
+
key?: string;
|
|
18
|
+
name?: string;
|
|
19
|
+
type?: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
interface StatuslineInputUsage {
|
|
23
|
+
todayTokens?: number;
|
|
24
|
+
totalTokens?: number;
|
|
25
|
+
inputTokens?: number;
|
|
26
|
+
outputTokens?: number;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
interface StatuslineInputModel {
|
|
30
|
+
id?: string;
|
|
31
|
+
displayName?: string;
|
|
32
|
+
display_name?: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
interface StatuslineInput {
|
|
36
|
+
cwd?: string;
|
|
37
|
+
type?: string;
|
|
38
|
+
profile?: StatuslineInputProfile;
|
|
39
|
+
model?: string | StatuslineInputModel;
|
|
40
|
+
model_provider?: string;
|
|
41
|
+
usage?: StatuslineInputUsage;
|
|
42
|
+
token_usage?: StatuslineInputUsage | number | Record<string, unknown>;
|
|
43
|
+
git_branch?: string;
|
|
44
|
+
task_running?: boolean;
|
|
45
|
+
review_mode?: boolean;
|
|
46
|
+
context_window_percent?: number;
|
|
47
|
+
context_window_used_tokens?: number;
|
|
48
|
+
workspace?: {
|
|
49
|
+
current_dir?: string;
|
|
50
|
+
project_dir?: string;
|
|
51
|
+
};
|
|
52
|
+
cost?: Record<string, unknown>;
|
|
53
|
+
version?: string;
|
|
54
|
+
output_style?: { name?: string };
|
|
55
|
+
session_id?: string;
|
|
56
|
+
transcript_path?: string;
|
|
57
|
+
hook_event_name?: string;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
interface StatuslineUsage {
|
|
61
|
+
todayTokens: number | null;
|
|
62
|
+
totalTokens: number | null;
|
|
63
|
+
inputTokens: number | null;
|
|
64
|
+
outputTokens: number | null;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
interface GitStatus {
|
|
68
|
+
branch: string | null;
|
|
69
|
+
ahead: number;
|
|
70
|
+
behind: number;
|
|
71
|
+
staged: number;
|
|
72
|
+
unstaged: number;
|
|
73
|
+
untracked: number;
|
|
74
|
+
conflicted: number;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const COLOR_ENABLED = !process.env.NO_COLOR && process.env.TERM !== "dumb";
|
|
78
|
+
const ANSI_RESET = "\x1b[0m";
|
|
79
|
+
const ICON_GIT = "⎇";
|
|
80
|
+
const ICON_PROFILE = "👤";
|
|
81
|
+
const ICON_MODEL = "⚙";
|
|
82
|
+
const ICON_USAGE = "⚡";
|
|
83
|
+
const ICON_CONTEXT = "🧠";
|
|
84
|
+
const ICON_REVIEW = "📝";
|
|
85
|
+
const ICON_CWD = "📁";
|
|
86
|
+
|
|
87
|
+
function colorize(text: string, colorCode: string): string {
|
|
88
|
+
if (!COLOR_ENABLED) return text;
|
|
89
|
+
return `\x1b[${colorCode}m${text}${ANSI_RESET}`;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function dim(text: string): string {
|
|
93
|
+
return colorize(text, "2");
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function getCwdSegment(cwd: string): string | null {
|
|
97
|
+
if (!cwd) return null;
|
|
98
|
+
const base = path.basename(cwd) || cwd;
|
|
99
|
+
const segment = `${ICON_CWD} ${base}`;
|
|
100
|
+
return dim(segment);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export interface StatuslineJson {
|
|
104
|
+
cwd: string;
|
|
105
|
+
type: string | null;
|
|
106
|
+
profile: { key: string | null; name: string | null };
|
|
107
|
+
model: string | null;
|
|
108
|
+
usage: StatuslineUsage | null;
|
|
109
|
+
git: GitStatus | null;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export interface StatuslineResult {
|
|
113
|
+
text: string;
|
|
114
|
+
json: StatuslineJson;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
118
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function readStdinJson(): StatuslineInput | null {
|
|
122
|
+
if (process.stdin.isTTY) return null;
|
|
123
|
+
try {
|
|
124
|
+
const raw = fs.readFileSync(0, "utf8");
|
|
125
|
+
const trimmed = raw.trim();
|
|
126
|
+
if (!trimmed) return null;
|
|
127
|
+
const parsed = JSON.parse(trimmed);
|
|
128
|
+
if (!isRecord(parsed)) return null;
|
|
129
|
+
return parsed as StatuslineInput;
|
|
130
|
+
} catch {
|
|
131
|
+
return null;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function firstNonEmpty(...values: Array<string | null | undefined>): string | null {
|
|
136
|
+
for (const value of values) {
|
|
137
|
+
if (value === null || value === undefined) continue;
|
|
138
|
+
const text = String(value).trim();
|
|
139
|
+
if (text) return text;
|
|
140
|
+
}
|
|
141
|
+
return null;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function coerceNumber(value: unknown): number | null {
|
|
145
|
+
if (value === null || value === undefined || value === "") return null;
|
|
146
|
+
const num = Number(value);
|
|
147
|
+
if (!Number.isFinite(num)) return null;
|
|
148
|
+
return num;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function firstNumber(...values: Array<unknown>): number | null {
|
|
152
|
+
for (const value of values) {
|
|
153
|
+
const num = coerceNumber(value);
|
|
154
|
+
if (num !== null) return num;
|
|
155
|
+
}
|
|
156
|
+
return null;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function normalizeTypeValue(value: string | null): string | null {
|
|
160
|
+
if (!value) return null;
|
|
161
|
+
const normalized = normalizeType(value);
|
|
162
|
+
if (normalized) return normalized;
|
|
163
|
+
const trimmed = String(value).trim();
|
|
164
|
+
return trimmed ? trimmed : null;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function detectTypeFromEnv(): string | null {
|
|
168
|
+
const matches = DEFAULT_PROFILE_TYPES.filter((type) => {
|
|
169
|
+
const suffix = type.toUpperCase();
|
|
170
|
+
return (
|
|
171
|
+
process.env[`CODE_ENV_PROFILE_KEY_${suffix}`] ||
|
|
172
|
+
process.env[`CODE_ENV_PROFILE_NAME_${suffix}`]
|
|
173
|
+
);
|
|
174
|
+
});
|
|
175
|
+
if (matches.length === 1) return matches[0];
|
|
176
|
+
return null;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function resolveEnvProfile(type: string | null): { key: string | null; name: string | null } {
|
|
180
|
+
const genericKey = process.env.CODE_ENV_PROFILE_KEY || null;
|
|
181
|
+
const genericName = process.env.CODE_ENV_PROFILE_NAME || null;
|
|
182
|
+
if (!type) {
|
|
183
|
+
return { key: genericKey, name: genericName };
|
|
184
|
+
}
|
|
185
|
+
const suffix = type.toUpperCase();
|
|
186
|
+
const key = process.env[`CODE_ENV_PROFILE_KEY_${suffix}`] || genericKey;
|
|
187
|
+
const name = process.env[`CODE_ENV_PROFILE_NAME_${suffix}`] || genericName;
|
|
188
|
+
return { key: key || null, name: name || null };
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function getModelFromInput(input: StatuslineInput | null): string | null {
|
|
192
|
+
if (!input) return null;
|
|
193
|
+
const raw = input.model;
|
|
194
|
+
if (!raw) return null;
|
|
195
|
+
if (typeof raw === "string") return raw;
|
|
196
|
+
if (isRecord(raw)) {
|
|
197
|
+
const displayName = raw.displayName || raw.display_name;
|
|
198
|
+
if (displayName) return String(displayName);
|
|
199
|
+
if (raw.id) return String(raw.id);
|
|
200
|
+
}
|
|
201
|
+
return null;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function getModelProviderFromInput(input: StatuslineInput | null): string | null {
|
|
205
|
+
if (!input || !input.model_provider) return null;
|
|
206
|
+
const provider = String(input.model_provider).trim();
|
|
207
|
+
return provider ? provider : null;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function getInputProfile(input: StatuslineInput | null): StatuslineInputProfile | null {
|
|
211
|
+
if (!input || !isRecord(input.profile)) return null;
|
|
212
|
+
return input.profile as StatuslineInputProfile;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function getInputUsage(input: StatuslineInput | null): StatuslineInputUsage | null {
|
|
216
|
+
if (!input) return null;
|
|
217
|
+
if (isRecord(input.usage)) {
|
|
218
|
+
return input.usage as StatuslineInputUsage;
|
|
219
|
+
}
|
|
220
|
+
const tokenUsage = input.token_usage;
|
|
221
|
+
if (tokenUsage === null || tokenUsage === undefined) return null;
|
|
222
|
+
if (typeof tokenUsage === "number") {
|
|
223
|
+
return {
|
|
224
|
+
todayTokens: null,
|
|
225
|
+
totalTokens: coerceNumber(tokenUsage),
|
|
226
|
+
inputTokens: null,
|
|
227
|
+
outputTokens: null,
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
if (isRecord(tokenUsage)) {
|
|
231
|
+
const record = tokenUsage as Record<string, unknown>;
|
|
232
|
+
return {
|
|
233
|
+
todayTokens:
|
|
234
|
+
firstNumber(
|
|
235
|
+
record.todayTokens,
|
|
236
|
+
record.today,
|
|
237
|
+
record.today_tokens,
|
|
238
|
+
record.daily,
|
|
239
|
+
record.daily_tokens
|
|
240
|
+
) ?? null,
|
|
241
|
+
totalTokens:
|
|
242
|
+
firstNumber(
|
|
243
|
+
record.totalTokens,
|
|
244
|
+
record.total,
|
|
245
|
+
record.total_tokens
|
|
246
|
+
) ?? null,
|
|
247
|
+
inputTokens:
|
|
248
|
+
firstNumber(
|
|
249
|
+
record.inputTokens,
|
|
250
|
+
record.input,
|
|
251
|
+
record.input_tokens
|
|
252
|
+
) ?? null,
|
|
253
|
+
outputTokens:
|
|
254
|
+
firstNumber(
|
|
255
|
+
record.outputTokens,
|
|
256
|
+
record.output,
|
|
257
|
+
record.output_tokens
|
|
258
|
+
) ?? null,
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
return null;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
function getContextUsedTokens(input: StatuslineInput | null): number | null {
|
|
265
|
+
if (!input) return null;
|
|
266
|
+
return coerceNumber(input.context_window_used_tokens);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
function getContextLeftPercent(
|
|
270
|
+
input: StatuslineInput | null,
|
|
271
|
+
type: string | null
|
|
272
|
+
): number | null {
|
|
273
|
+
if (!input) return null;
|
|
274
|
+
const raw = coerceNumber(input.context_window_percent);
|
|
275
|
+
if (raw === null || raw < 0) return null;
|
|
276
|
+
const percent = raw <= 1 ? raw * 100 : raw;
|
|
277
|
+
if (percent > 100) return null;
|
|
278
|
+
const usedTokens = getContextUsedTokens(input);
|
|
279
|
+
const normalizedType = normalizeTypeValue(type);
|
|
280
|
+
// Prefer treating the percent as "remaining" for codex/claude and when usage is absent.
|
|
281
|
+
const preferRemaining =
|
|
282
|
+
normalizedType === "codex" ||
|
|
283
|
+
normalizedType === "claude" ||
|
|
284
|
+
usedTokens === null ||
|
|
285
|
+
(usedTokens <= 0 && percent >= 99);
|
|
286
|
+
const left = preferRemaining ? percent : 100 - percent;
|
|
287
|
+
return Math.max(0, Math.min(100, left));
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
function getWorkspaceDir(input: StatuslineInput | null): string | null {
|
|
291
|
+
if (!input || !isRecord(input.workspace)) return null;
|
|
292
|
+
const currentDir = input.workspace.current_dir;
|
|
293
|
+
if (currentDir) {
|
|
294
|
+
const trimmed = String(currentDir).trim();
|
|
295
|
+
if (trimmed) return trimmed;
|
|
296
|
+
}
|
|
297
|
+
const projectDir = input.workspace.project_dir;
|
|
298
|
+
if (!projectDir) return null;
|
|
299
|
+
const trimmed = String(projectDir).trim();
|
|
300
|
+
return trimmed ? trimmed : null;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
function getGitStatusFromInput(
|
|
304
|
+
input: StatuslineInput | null
|
|
305
|
+
): GitStatus | null {
|
|
306
|
+
if (!input || !input.git_branch) return null;
|
|
307
|
+
const branch = String(input.git_branch).trim();
|
|
308
|
+
if (!branch) return null;
|
|
309
|
+
return {
|
|
310
|
+
branch,
|
|
311
|
+
ahead: 0,
|
|
312
|
+
behind: 0,
|
|
313
|
+
staged: 0,
|
|
314
|
+
unstaged: 0,
|
|
315
|
+
untracked: 0,
|
|
316
|
+
conflicted: 0,
|
|
317
|
+
};
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
function getGitStatus(cwd: string): GitStatus | null {
|
|
321
|
+
if (!cwd) return null;
|
|
322
|
+
const result = spawnSync("git", ["-C", cwd, "status", "--porcelain=v2", "-b"], {
|
|
323
|
+
encoding: "utf8",
|
|
324
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
325
|
+
});
|
|
326
|
+
if (result.status !== 0 || !result.stdout) return null;
|
|
327
|
+
const status: GitStatus = {
|
|
328
|
+
branch: null,
|
|
329
|
+
ahead: 0,
|
|
330
|
+
behind: 0,
|
|
331
|
+
staged: 0,
|
|
332
|
+
unstaged: 0,
|
|
333
|
+
untracked: 0,
|
|
334
|
+
conflicted: 0,
|
|
335
|
+
};
|
|
336
|
+
|
|
337
|
+
const lines = result.stdout.split(/\r?\n/);
|
|
338
|
+
for (const line of lines) {
|
|
339
|
+
if (!line) continue;
|
|
340
|
+
if (line.startsWith("# branch.head ")) {
|
|
341
|
+
status.branch = line.slice("# branch.head ".length).trim();
|
|
342
|
+
continue;
|
|
343
|
+
}
|
|
344
|
+
if (line.startsWith("# branch.ab ")) {
|
|
345
|
+
const parts = line
|
|
346
|
+
.slice("# branch.ab ".length)
|
|
347
|
+
.trim()
|
|
348
|
+
.split(/\s+/);
|
|
349
|
+
for (const part of parts) {
|
|
350
|
+
if (part.startsWith("+")) status.ahead = Number(part.slice(1)) || 0;
|
|
351
|
+
if (part.startsWith("-")) status.behind = Number(part.slice(1)) || 0;
|
|
352
|
+
}
|
|
353
|
+
continue;
|
|
354
|
+
}
|
|
355
|
+
if (line.startsWith("? ")) {
|
|
356
|
+
status.untracked += 1;
|
|
357
|
+
continue;
|
|
358
|
+
}
|
|
359
|
+
if (line.startsWith("u ")) {
|
|
360
|
+
status.conflicted += 1;
|
|
361
|
+
continue;
|
|
362
|
+
}
|
|
363
|
+
if (line.startsWith("1 ") || line.startsWith("2 ")) {
|
|
364
|
+
const parts = line.split(/\s+/);
|
|
365
|
+
const xy = parts[1] || "";
|
|
366
|
+
const staged = xy[0];
|
|
367
|
+
const unstaged = xy[1];
|
|
368
|
+
if (staged && staged !== ".") status.staged += 1;
|
|
369
|
+
if (unstaged && unstaged !== ".") status.unstaged += 1;
|
|
370
|
+
continue;
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
if (!status.branch) {
|
|
375
|
+
status.branch = "HEAD";
|
|
376
|
+
}
|
|
377
|
+
return status;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
function formatGitSegment(status: GitStatus | null): string | null {
|
|
381
|
+
if (!status || !status.branch) return null;
|
|
382
|
+
const meta: string[] = [];
|
|
383
|
+
const dirtyCount = status.staged + status.unstaged + status.untracked;
|
|
384
|
+
if (status.ahead > 0) meta.push(`↑${status.ahead}`);
|
|
385
|
+
if (status.behind > 0) meta.push(`↓${status.behind}`);
|
|
386
|
+
if (status.conflicted > 0) meta.push(`✖${status.conflicted}`);
|
|
387
|
+
if (dirtyCount > 0) meta.push(`+${dirtyCount}`);
|
|
388
|
+
const suffix = meta.length > 0 ? ` [${meta.join("")}]` : "";
|
|
389
|
+
const text = `${ICON_GIT} ${status.branch}${suffix}`;
|
|
390
|
+
const hasConflicts = status.conflicted > 0;
|
|
391
|
+
const isDirty = dirtyCount > 0;
|
|
392
|
+
if (hasConflicts) return colorize(text, "31");
|
|
393
|
+
if (isDirty) return colorize(text, "33");
|
|
394
|
+
if (status.ahead > 0 || status.behind > 0) return colorize(text, "36");
|
|
395
|
+
return colorize(text, "32");
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
function resolveUsageFromRecords(
|
|
399
|
+
config: Config,
|
|
400
|
+
configPath: string | null,
|
|
401
|
+
type: string | null,
|
|
402
|
+
profileKey: string | null,
|
|
403
|
+
profileName: string | null,
|
|
404
|
+
syncUsage: boolean
|
|
405
|
+
): StatuslineUsage | null {
|
|
406
|
+
try {
|
|
407
|
+
const normalized = normalizeType(type || "");
|
|
408
|
+
if (!normalized || (!profileKey && !profileName)) return null;
|
|
409
|
+
const totals = readUsageTotalsIndex(config, configPath, syncUsage);
|
|
410
|
+
if (!totals) return null;
|
|
411
|
+
const usage = resolveUsageTotalsForProfile(
|
|
412
|
+
totals,
|
|
413
|
+
normalized,
|
|
414
|
+
profileKey,
|
|
415
|
+
profileName
|
|
416
|
+
);
|
|
417
|
+
if (!usage) return null;
|
|
418
|
+
return {
|
|
419
|
+
todayTokens: usage.today,
|
|
420
|
+
totalTokens: usage.total,
|
|
421
|
+
inputTokens: null,
|
|
422
|
+
outputTokens: null,
|
|
423
|
+
};
|
|
424
|
+
} catch {
|
|
425
|
+
return null;
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
function formatUsageSegment(usage: StatuslineUsage | null): string | null {
|
|
430
|
+
if (!usage) return null;
|
|
431
|
+
const today =
|
|
432
|
+
usage.todayTokens ??
|
|
433
|
+
(usage.inputTokens !== null || usage.outputTokens !== null
|
|
434
|
+
? (usage.inputTokens || 0) + (usage.outputTokens || 0)
|
|
435
|
+
: usage.totalTokens);
|
|
436
|
+
if (today === null) return null;
|
|
437
|
+
const text = `Today ${formatTokenCount(today)}`;
|
|
438
|
+
return colorize(`${ICON_USAGE} ${text}`, "33");
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
function formatModelSegment(
|
|
442
|
+
model: string | null,
|
|
443
|
+
provider: string | null
|
|
444
|
+
): string | null {
|
|
445
|
+
if (!model) return null;
|
|
446
|
+
const providerLabel = provider ? `${provider}:${model}` : model;
|
|
447
|
+
return colorize(`${ICON_MODEL} ${providerLabel}`, "35");
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
function formatProfileSegment(
|
|
451
|
+
type: string | null,
|
|
452
|
+
profileKey: string | null,
|
|
453
|
+
profileName: string | null
|
|
454
|
+
): string | null {
|
|
455
|
+
const name = profileName || profileKey;
|
|
456
|
+
if (!name) return null;
|
|
457
|
+
const label = type ? `${type}:${name}` : name;
|
|
458
|
+
return colorize(`${ICON_PROFILE} ${label}`, "37");
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
function formatContextSegment(contextLeft: number | null): string | null {
|
|
462
|
+
if (contextLeft === null) return null;
|
|
463
|
+
const left = Math.max(0, Math.min(100, Math.round(contextLeft)));
|
|
464
|
+
return colorize(`${ICON_CONTEXT} ${left}% left`, "36");
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
function formatContextUsedSegment(usedTokens: number | null): string | null {
|
|
468
|
+
if (usedTokens === null) return null;
|
|
469
|
+
return colorize(`${ICON_CONTEXT} ${formatTokenCount(usedTokens)} used`, "36");
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
function formatModeSegment(reviewMode: boolean): string | null {
|
|
473
|
+
if (!reviewMode) return null;
|
|
474
|
+
return colorize(`${ICON_REVIEW} review`, "34");
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
export function buildStatuslineResult(
|
|
478
|
+
args: StatuslineArgs,
|
|
479
|
+
config: Config,
|
|
480
|
+
configPath: string | null
|
|
481
|
+
): StatuslineResult {
|
|
482
|
+
const stdinInput = readStdinJson();
|
|
483
|
+
const inputProfile = getInputProfile(stdinInput);
|
|
484
|
+
|
|
485
|
+
let typeCandidate = firstNonEmpty(
|
|
486
|
+
args.type,
|
|
487
|
+
process.env.CODE_ENV_TYPE,
|
|
488
|
+
inputProfile ? inputProfile.type : null,
|
|
489
|
+
stdinInput ? stdinInput.type : null
|
|
490
|
+
);
|
|
491
|
+
|
|
492
|
+
if (!typeCandidate) {
|
|
493
|
+
typeCandidate = detectTypeFromEnv();
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
let type = normalizeTypeValue(typeCandidate);
|
|
497
|
+
const envProfile = resolveEnvProfile(type);
|
|
498
|
+
|
|
499
|
+
let profileKey = firstNonEmpty(
|
|
500
|
+
args.profileKey,
|
|
501
|
+
envProfile.key,
|
|
502
|
+
inputProfile ? inputProfile.key : null
|
|
503
|
+
);
|
|
504
|
+
let profileName = firstNonEmpty(
|
|
505
|
+
args.profileName,
|
|
506
|
+
envProfile.name,
|
|
507
|
+
inputProfile ? inputProfile.name : null
|
|
508
|
+
);
|
|
509
|
+
|
|
510
|
+
if (profileKey && !profileName && config.profiles && config.profiles[profileKey]) {
|
|
511
|
+
const profile = config.profiles[profileKey];
|
|
512
|
+
profileName = getProfileDisplayName(profileKey, profile, type || undefined);
|
|
513
|
+
if (!type) {
|
|
514
|
+
const inferred = inferProfileType(profileKey, profile, null);
|
|
515
|
+
if (inferred) type = inferred;
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
if (!type && profileKey && config.profiles && config.profiles[profileKey]) {
|
|
520
|
+
const profile = config.profiles[profileKey];
|
|
521
|
+
const inferred = inferProfileType(profileKey, profile, null);
|
|
522
|
+
if (inferred) type = inferred;
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
const cwd = firstNonEmpty(
|
|
526
|
+
args.cwd,
|
|
527
|
+
process.env.CODE_ENV_CWD,
|
|
528
|
+
getWorkspaceDir(stdinInput),
|
|
529
|
+
stdinInput ? stdinInput.cwd : null,
|
|
530
|
+
process.cwd()
|
|
531
|
+
)!;
|
|
532
|
+
|
|
533
|
+
const model = firstNonEmpty(
|
|
534
|
+
args.model,
|
|
535
|
+
process.env.CODE_ENV_MODEL,
|
|
536
|
+
getModelFromInput(stdinInput)
|
|
537
|
+
);
|
|
538
|
+
const modelProvider = firstNonEmpty(
|
|
539
|
+
process.env.CODE_ENV_MODEL_PROVIDER,
|
|
540
|
+
getModelProviderFromInput(stdinInput)
|
|
541
|
+
);
|
|
542
|
+
|
|
543
|
+
const usage: StatuslineUsage = {
|
|
544
|
+
todayTokens: firstNumber(
|
|
545
|
+
args.usageToday,
|
|
546
|
+
process.env.CODE_ENV_USAGE_TODAY
|
|
547
|
+
),
|
|
548
|
+
totalTokens: firstNumber(
|
|
549
|
+
args.usageTotal,
|
|
550
|
+
process.env.CODE_ENV_USAGE_TOTAL
|
|
551
|
+
),
|
|
552
|
+
inputTokens: firstNumber(
|
|
553
|
+
args.usageInput,
|
|
554
|
+
process.env.CODE_ENV_USAGE_INPUT
|
|
555
|
+
),
|
|
556
|
+
outputTokens: firstNumber(
|
|
557
|
+
args.usageOutput,
|
|
558
|
+
process.env.CODE_ENV_USAGE_OUTPUT
|
|
559
|
+
),
|
|
560
|
+
};
|
|
561
|
+
|
|
562
|
+
const hasExplicitUsage =
|
|
563
|
+
usage.todayTokens !== null ||
|
|
564
|
+
usage.totalTokens !== null ||
|
|
565
|
+
usage.inputTokens !== null ||
|
|
566
|
+
usage.outputTokens !== null;
|
|
567
|
+
|
|
568
|
+
let finalUsage: StatuslineUsage | null = hasExplicitUsage ? usage : null;
|
|
569
|
+
if (!finalUsage) {
|
|
570
|
+
finalUsage = resolveUsageFromRecords(
|
|
571
|
+
config,
|
|
572
|
+
configPath,
|
|
573
|
+
type,
|
|
574
|
+
profileKey,
|
|
575
|
+
profileName,
|
|
576
|
+
args.syncUsage
|
|
577
|
+
);
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
let gitStatus = getGitStatus(cwd);
|
|
581
|
+
if (!gitStatus) {
|
|
582
|
+
gitStatus = getGitStatusFromInput(stdinInput);
|
|
583
|
+
} else {
|
|
584
|
+
const inputGit = getGitStatusFromInput(stdinInput);
|
|
585
|
+
if (inputGit && (!gitStatus.branch || gitStatus.branch === "HEAD")) {
|
|
586
|
+
gitStatus.branch = inputGit.branch;
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
const gitSegment = formatGitSegment(gitStatus);
|
|
590
|
+
const profileSegment = formatProfileSegment(type, profileKey, profileName);
|
|
591
|
+
const modelSegment = formatModelSegment(model, modelProvider);
|
|
592
|
+
const usageSegment = formatUsageSegment(finalUsage);
|
|
593
|
+
const contextLeft = getContextLeftPercent(stdinInput, type);
|
|
594
|
+
const contextSegment = formatContextSegment(contextLeft);
|
|
595
|
+
const contextUsedTokens = getContextUsedTokens(stdinInput);
|
|
596
|
+
const contextUsedSegment =
|
|
597
|
+
contextSegment === null ? formatContextUsedSegment(contextUsedTokens) : null;
|
|
598
|
+
const modeSegment = formatModeSegment(
|
|
599
|
+
stdinInput?.review_mode === true
|
|
600
|
+
);
|
|
601
|
+
const cwdSegment = getCwdSegment(cwd);
|
|
602
|
+
|
|
603
|
+
const segments: string[] = [];
|
|
604
|
+
if (gitSegment) segments.push(gitSegment);
|
|
605
|
+
if (profileSegment) segments.push(profileSegment);
|
|
606
|
+
if (modeSegment) segments.push(modeSegment);
|
|
607
|
+
if (modelSegment) segments.push(modelSegment);
|
|
608
|
+
if (usageSegment) segments.push(usageSegment);
|
|
609
|
+
if (contextSegment) segments.push(contextSegment);
|
|
610
|
+
if (contextUsedSegment) segments.push(contextUsedSegment);
|
|
611
|
+
if (cwdSegment) segments.push(cwdSegment);
|
|
612
|
+
|
|
613
|
+
const text = segments.join(" ");
|
|
614
|
+
|
|
615
|
+
return {
|
|
616
|
+
text,
|
|
617
|
+
json: {
|
|
618
|
+
cwd,
|
|
619
|
+
type,
|
|
620
|
+
profile: { key: profileKey, name: profileName },
|
|
621
|
+
model,
|
|
622
|
+
usage: finalUsage,
|
|
623
|
+
git: gitStatus,
|
|
624
|
+
},
|
|
625
|
+
};
|
|
626
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type definitions for codenv
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export type EnvValue = string | null | undefined;
|
|
6
|
+
|
|
7
|
+
export type ProfileType = "codex" | "claude";
|
|
8
|
+
|
|
9
|
+
export type DefaultProfiles = Partial<Record<ProfileType, string>>;
|
|
10
|
+
|
|
11
|
+
export interface Profile {
|
|
12
|
+
name?: string;
|
|
13
|
+
type?: string;
|
|
14
|
+
note?: string;
|
|
15
|
+
env?: Record<string, EnvValue>;
|
|
16
|
+
removeFiles?: string[];
|
|
17
|
+
commands?: string[];
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface CodexStatuslineConfig {
|
|
21
|
+
command?: string | string[];
|
|
22
|
+
showHints?: boolean;
|
|
23
|
+
updateIntervalMs?: number;
|
|
24
|
+
timeoutMs?: number;
|
|
25
|
+
configPath?: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface ClaudeStatuslineConfig {
|
|
29
|
+
command?: string | string[];
|
|
30
|
+
type?: string;
|
|
31
|
+
padding?: number;
|
|
32
|
+
settingsPath?: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface Config {
|
|
36
|
+
unset?: string[];
|
|
37
|
+
profiles?: Record<string, Profile>;
|
|
38
|
+
defaultProfiles?: DefaultProfiles;
|
|
39
|
+
usagePath?: string;
|
|
40
|
+
usageStatePath?: string;
|
|
41
|
+
profileLogPath?: string;
|
|
42
|
+
codexSessionsPath?: string;
|
|
43
|
+
claudeSessionsPath?: string;
|
|
44
|
+
codexStatusline?: CodexStatuslineConfig;
|
|
45
|
+
claudeStatusline?: ClaudeStatuslineConfig;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface ListRow {
|
|
49
|
+
key: string;
|
|
50
|
+
name: string;
|
|
51
|
+
type: string;
|
|
52
|
+
note: string;
|
|
53
|
+
active: boolean;
|
|
54
|
+
usageType?: ProfileType | null;
|
|
55
|
+
todayTokens?: number;
|
|
56
|
+
totalTokens?: number;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export interface ParsedArgs {
|
|
60
|
+
args: string[];
|
|
61
|
+
configPath: string | null;
|
|
62
|
+
help: boolean;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export interface InitArgs {
|
|
66
|
+
apply: boolean;
|
|
67
|
+
print: boolean;
|
|
68
|
+
shell: string | null;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export interface AddArgs {
|
|
72
|
+
profile: string | null;
|
|
73
|
+
pairs: string[];
|
|
74
|
+
note: string | null;
|
|
75
|
+
removeFiles: string[];
|
|
76
|
+
commands: string[];
|
|
77
|
+
unset: string[];
|
|
78
|
+
type: ProfileType | null;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export type StatuslineFormat = "text" | "json";
|
|
82
|
+
|
|
83
|
+
export interface StatuslineArgs {
|
|
84
|
+
format: StatuslineFormat;
|
|
85
|
+
cwd: string | null;
|
|
86
|
+
type: string | null;
|
|
87
|
+
profileKey: string | null;
|
|
88
|
+
profileName: string | null;
|
|
89
|
+
model: string | null;
|
|
90
|
+
usageToday: number | null;
|
|
91
|
+
usageTotal: number | null;
|
|
92
|
+
usageInput: number | null;
|
|
93
|
+
usageOutput: number | null;
|
|
94
|
+
syncUsage: boolean;
|
|
95
|
+
}
|