@praeviso/code-env-switch 0.1.3 → 0.1.4

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.
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.appendStatuslineDebug = appendStatuslineDebug;
4
+ const fs = require("fs");
5
+ const path = require("path");
6
+ const os = require("os");
7
+ const utils_1 = require("../shell/utils");
8
+ function isStatuslineDebugEnabled() {
9
+ const raw = process.env.CODE_ENV_STATUSLINE_DEBUG;
10
+ if (!raw)
11
+ return false;
12
+ const value = String(raw).trim().toLowerCase();
13
+ if (!value)
14
+ return false;
15
+ return !["0", "false", "no", "off"].includes(value);
16
+ }
17
+ function resolveDefaultConfigDir(configPath) {
18
+ if (configPath)
19
+ return path.dirname(configPath);
20
+ return path.join(os.homedir(), ".config", "code-env");
21
+ }
22
+ function getStatuslineDebugPath(configPath) {
23
+ const envPath = (0, utils_1.resolvePath)(process.env.CODE_ENV_STATUSLINE_DEBUG_PATH);
24
+ if (envPath)
25
+ return envPath;
26
+ return path.join(resolveDefaultConfigDir(configPath), "statusline-debug.jsonl");
27
+ }
28
+ function appendStatuslineDebug(configPath, payload) {
29
+ if (!isStatuslineDebugEnabled())
30
+ return;
31
+ try {
32
+ const debugPath = getStatuslineDebugPath(configPath);
33
+ const dir = path.dirname(debugPath);
34
+ if (!fs.existsSync(dir)) {
35
+ fs.mkdirSync(dir, { recursive: true });
36
+ }
37
+ fs.appendFileSync(debugPath, `${JSON.stringify(payload)}\n`, "utf8");
38
+ }
39
+ catch {
40
+ // ignore debug logging failures
41
+ }
42
+ }
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getCwdSegment = getCwdSegment;
4
+ exports.formatUsageSegment = formatUsageSegment;
5
+ exports.formatModelSegment = formatModelSegment;
6
+ exports.formatProfileSegment = formatProfileSegment;
7
+ exports.formatContextSegment = formatContextSegment;
8
+ exports.formatContextUsedSegment = formatContextUsedSegment;
9
+ exports.formatModeSegment = formatModeSegment;
10
+ const usage_1 = require("../usage");
11
+ const style_1 = require("./style");
12
+ const path = require("path");
13
+ function getCwdSegment(cwd) {
14
+ if (!cwd)
15
+ return null;
16
+ const base = path.basename(cwd) || cwd;
17
+ const segment = `${style_1.ICON_CWD} ${base}`;
18
+ return (0, style_1.dim)(segment);
19
+ }
20
+ function formatUsageSegment(usage) {
21
+ var _a;
22
+ if (!usage)
23
+ return null;
24
+ const today = (_a = usage.todayTokens) !== null && _a !== void 0 ? _a : (usage.inputTokens !== null || usage.outputTokens !== null
25
+ ? (usage.inputTokens || 0) + (usage.outputTokens || 0)
26
+ : usage.totalTokens);
27
+ if (today === null)
28
+ return null;
29
+ const text = `Today ${(0, usage_1.formatTokenCount)(today)}`;
30
+ return (0, style_1.colorize)(`${style_1.ICON_USAGE} ${text}`, "33");
31
+ }
32
+ function formatModelSegment(model, provider) {
33
+ if (!model)
34
+ return null;
35
+ const providerLabel = provider ? `${provider}:${model}` : model;
36
+ return (0, style_1.colorize)(`${style_1.ICON_MODEL} ${providerLabel}`, "35");
37
+ }
38
+ function formatProfileSegment(type, profileKey, profileName) {
39
+ const name = profileName || profileKey;
40
+ if (!name)
41
+ return null;
42
+ const label = type ? `${type}:${name}` : name;
43
+ return (0, style_1.colorize)(`${style_1.ICON_PROFILE} ${label}`, "37");
44
+ }
45
+ function formatContextSegment(contextLeft) {
46
+ if (contextLeft === null)
47
+ return null;
48
+ const left = Math.max(0, Math.min(100, Math.round(contextLeft)));
49
+ return (0, style_1.colorize)(`${style_1.ICON_CONTEXT} ${left}% left`, "36");
50
+ }
51
+ function formatContextUsedSegment(usedTokens) {
52
+ if (usedTokens === null)
53
+ return null;
54
+ return (0, style_1.colorize)(`${style_1.ICON_CONTEXT} ${(0, usage_1.formatTokenCount)(usedTokens)} used`, "36");
55
+ }
56
+ function formatModeSegment(reviewMode) {
57
+ if (!reviewMode)
58
+ return null;
59
+ return (0, style_1.colorize)(`${style_1.ICON_REVIEW} review`, "34");
60
+ }
@@ -0,0 +1,96 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getGitStatus = getGitStatus;
4
+ exports.formatGitSegment = formatGitSegment;
5
+ const child_process_1 = require("child_process");
6
+ const style_1 = require("./style");
7
+ const style_2 = require("./style");
8
+ function getGitStatus(cwd) {
9
+ if (!cwd)
10
+ return null;
11
+ const result = (0, child_process_1.spawnSync)("git", ["-C", cwd, "status", "--porcelain=v2", "-b"], {
12
+ encoding: "utf8",
13
+ stdio: ["ignore", "pipe", "ignore"],
14
+ });
15
+ if (result.status !== 0 || !result.stdout)
16
+ return null;
17
+ const status = {
18
+ branch: null,
19
+ ahead: 0,
20
+ behind: 0,
21
+ staged: 0,
22
+ unstaged: 0,
23
+ untracked: 0,
24
+ conflicted: 0,
25
+ };
26
+ const lines = result.stdout.split(/\r?\n/);
27
+ for (const line of lines) {
28
+ if (!line)
29
+ continue;
30
+ if (line.startsWith("# branch.head ")) {
31
+ status.branch = line.slice("# branch.head ".length).trim();
32
+ continue;
33
+ }
34
+ if (line.startsWith("# branch.ab ")) {
35
+ const parts = line
36
+ .slice("# branch.ab ".length)
37
+ .trim()
38
+ .split(/\s+/);
39
+ for (const part of parts) {
40
+ if (part.startsWith("+"))
41
+ status.ahead = Number(part.slice(1)) || 0;
42
+ if (part.startsWith("-"))
43
+ status.behind = Number(part.slice(1)) || 0;
44
+ }
45
+ continue;
46
+ }
47
+ if (line.startsWith("? ")) {
48
+ status.untracked += 1;
49
+ continue;
50
+ }
51
+ if (line.startsWith("u ")) {
52
+ status.conflicted += 1;
53
+ continue;
54
+ }
55
+ if (line.startsWith("1 ") || line.startsWith("2 ")) {
56
+ const parts = line.split(/\s+/);
57
+ const xy = parts[1] || "";
58
+ const staged = xy[0];
59
+ const unstaged = xy[1];
60
+ if (staged && staged !== ".")
61
+ status.staged += 1;
62
+ if (unstaged && unstaged !== ".")
63
+ status.unstaged += 1;
64
+ continue;
65
+ }
66
+ }
67
+ if (!status.branch) {
68
+ status.branch = "HEAD";
69
+ }
70
+ return status;
71
+ }
72
+ function formatGitSegment(status) {
73
+ if (!status || !status.branch)
74
+ return null;
75
+ const meta = [];
76
+ const dirtyCount = status.staged + status.unstaged + status.untracked;
77
+ if (status.ahead > 0)
78
+ meta.push(`↑${status.ahead}`);
79
+ if (status.behind > 0)
80
+ meta.push(`↓${status.behind}`);
81
+ if (status.conflicted > 0)
82
+ meta.push(`✖${status.conflicted}`);
83
+ if (dirtyCount > 0)
84
+ meta.push(`+${dirtyCount}`);
85
+ const suffix = meta.length > 0 ? ` [${meta.join("")}]` : "";
86
+ const text = `${style_2.ICON_GIT} ${status.branch}${suffix}`;
87
+ const hasConflicts = status.conflicted > 0;
88
+ const isDirty = dirtyCount > 0;
89
+ if (hasConflicts)
90
+ return (0, style_1.colorize)(text, "31");
91
+ if (isDirty)
92
+ return (0, style_1.colorize)(text, "33");
93
+ if (status.ahead > 0 || status.behind > 0)
94
+ return (0, style_1.colorize)(text, "36");
95
+ return (0, style_1.colorize)(text, "32");
96
+ }