tokenusage-cli 0.1.0 → 0.2.1
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 +46 -19
- package/LICENSE +22 -22
- package/README.md +123 -94
- package/THIRD_PARTY_NOTICES.md +140 -140
- package/bin/tokenusage.js +69 -69
- package/package.json +59 -59
- package/src/cli.js +146 -134
- package/src/commands/device-login.js +205 -205
- package/src/commands/diagnostics.js +38 -38
- package/src/commands/doctor.js +110 -110
- package/src/commands/init.js +2484 -2436
- package/src/commands/pause.js +77 -0
- package/src/commands/sessions.js +29 -29
- package/src/commands/status.js +1758 -1747
- package/src/commands/sync.js +8048 -7934
- package/src/commands/uninstall.js +322 -312
- package/src/lib/account-usage-dedup.js +267 -267
- package/src/lib/antigravity-paths.js +48 -48
- package/src/lib/ark-agent-plan-limits.js +438 -438
- package/src/lib/ark-coding-plan-limits.js +537 -537
- package/src/lib/async-queue-reader.js +138 -138
- package/src/lib/atomic-json.js +27 -27
- package/src/lib/bearer-token.js +11 -11
- package/src/lib/categorizer-utils.js +232 -232
- package/src/lib/claude-categorizer.js +1343 -1343
- package/src/lib/claude-config.js +279 -279
- package/src/lib/claude-user-identity.js +56 -56
- package/src/lib/cli-ui.js +179 -179
- package/src/lib/cloud-account.js +541 -541
- package/src/lib/cloud-device-token.js +141 -141
- package/src/lib/cloud-sync-prefs.js +14 -14
- package/src/lib/cloud-usage-reset.js +115 -115
- package/src/lib/codex-config.js +595 -595
- package/src/lib/codex-context-breakdown.js +806 -806
- package/src/lib/codex-model-attribution.js +128 -128
- package/src/lib/codex-rollout-parser.js +1471 -1471
- package/src/lib/codex-service-tier.js +49 -49
- package/src/lib/codex-token-refresh.js +140 -140
- package/src/lib/codex-token-usage.js +195 -195
- package/src/lib/codex-usage-record.js +195 -195
- package/src/lib/collection-pause.js +77 -0
- package/src/lib/command-runner.js +313 -313
- package/src/lib/commandcode-limits.js +317 -317
- package/src/lib/context-health.js +124 -124
- package/src/lib/cursor-config.js +605 -605
- package/src/lib/cursor-store.js +1918 -1918
- package/src/lib/debug-flags.js +7 -7
- package/src/lib/devin-limits.js +249 -249
- package/src/lib/devin-usage.js +212 -212
- package/src/lib/diagnostics.js +383 -383
- package/src/lib/doctor.js +385 -385
- package/src/lib/dumate.js +95 -95
- package/src/lib/fs.js +455 -455
- package/src/lib/function-url.js +14 -14
- package/src/lib/gemini-config.js +291 -291
- package/src/lib/git-outcomes.js +243 -243
- package/src/lib/grok-context-breakdown.js +890 -890
- package/src/lib/grok-hook.js +360 -360
- package/src/lib/grok-limits.js +865 -865
- package/src/lib/grok-usage.js +98 -98
- package/src/lib/heatmap-compact.js +114 -114
- package/src/lib/ingest-sanitize.js +41 -41
- package/src/lib/init-flow.js +42 -42
- package/src/lib/install-resolver.js +170 -170
- package/src/lib/jsonl-lines.js +134 -134
- package/src/lib/kimi-model-repair.js +173 -173
- package/src/lib/kimi-profile.js +61 -61
- package/src/lib/kimi-session.js +216 -216
- package/src/lib/local-api.js +3588 -3588
- package/src/lib/machine-id.js +238 -238
- package/src/lib/multi-install-parser.js +113 -113
- package/src/lib/notify-command.js +38 -38
- package/src/lib/omp-hook.js +589 -589
- package/src/lib/openclaw-cli.js +104 -104
- package/src/lib/openclaw-hook.js +375 -375
- package/src/lib/openclaw-session-plugin.js +560 -560
- package/src/lib/opencode-config.js +121 -121
- package/src/lib/opencode-go-limits.js +807 -807
- package/src/lib/outcomes-engine.js +253 -253
- package/src/lib/passive-mode.js +219 -219
- package/src/lib/pet-packages.js +722 -722
- package/src/lib/pricing/curated-overrides.json +822 -822
- package/src/lib/pricing/index.js +379 -379
- package/src/lib/pricing/litellm-fetcher.js +172 -172
- package/src/lib/pricing/matcher.js +456 -456
- package/src/lib/pricing/seed-snapshot.json +1 -1
- package/src/lib/progress.js +76 -76
- package/src/lib/project-usage-purge.js +117 -117
- package/src/lib/prompt.js +24 -24
- package/src/lib/provider-status.js +104 -104
- package/src/lib/proxy-env.js +489 -489
- package/src/lib/proxy-settings.js +163 -163
- package/src/lib/qoder-cn-cli-limits.js +119 -119
- package/src/lib/qoder-limits.js +880 -880
- package/src/lib/quiet-warnings.js +26 -26
- package/src/lib/rollout.js +26005 -25966
- package/src/lib/runtime-config.js +9 -4
- package/src/lib/scan-roots.js +277 -277
- package/src/lib/scheduled-task.js +145 -0
- package/src/lib/sentry.js +231 -231
- package/src/lib/session-analytics.js +2972 -2972
- package/src/lib/session-performance.js +237 -237
- package/src/lib/session-upload.js +379 -379
- package/src/lib/skill-usage.js +267 -267
- package/src/lib/skills-manager.js +1591 -1591
- package/src/lib/source-metadata.js +48 -48
- package/src/lib/sqlite-reader.js +215 -215
- package/src/lib/star-cta.js +73 -73
- package/src/lib/subscription-manager.js +293 -293
- package/src/lib/subscriptions.js +508 -508
- package/src/lib/sync-report.js +138 -0
- package/src/lib/telemetry.js +193 -193
- package/src/lib/tracker-paths.js +16 -16
- package/src/lib/trae-cn-config.js +548 -548
- package/src/lib/trae-db.js +127 -127
- package/src/lib/trae-snapshot.js +230 -230
- package/src/lib/trae-sqlite.js +143 -143
- package/src/lib/trae-usage.js +121 -121
- package/src/lib/upload-queue.js +134 -134
- package/src/lib/upload-throttle.js +171 -171
- package/src/lib/usage-limits.js +4552 -4552
- package/src/lib/wrapped-aggregator.js +236 -236
- package/src/lib/wsl-probe.js +281 -281
- package/src/lib/zcode-limits.js +1172 -1172
- package/src/lib/zcode-reset-credits.js +66 -66
- package/test/audit-fixes.test.js +200 -0
- package/test/cli-smoke.test.js +53 -53
- package/test/collection-controls.test.js +210 -0
- package/test/ingest-sanitize.test.js +162 -162
- package/test/quiet-warnings.test.js +53 -53
- package/test/session-upload.test.js +427 -427
|
@@ -1,56 +1,56 @@
|
|
|
1
|
-
const fs = require("node:fs");
|
|
2
|
-
const fsp = require("node:fs/promises");
|
|
3
|
-
const readline = require("node:readline");
|
|
4
|
-
|
|
5
|
-
function noteClaudeFork(aliases, obj) {
|
|
6
|
-
const uuid = obj?.uuid;
|
|
7
|
-
const parent = obj?.forkedFrom?.messageUuid;
|
|
8
|
-
if (typeof uuid === "string" && uuid && typeof parent === "string" && parent && uuid !== parent) {
|
|
9
|
-
aliases[uuid] = parent;
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
function claudeUserIdentity(obj, aliases) {
|
|
14
|
-
let uuid = typeof obj?.uuid === "string" && obj.uuid ? obj.uuid : null;
|
|
15
|
-
if (!uuid) return null;
|
|
16
|
-
const chain = new Set();
|
|
17
|
-
while (!chain.has(uuid)) {
|
|
18
|
-
chain.add(uuid);
|
|
19
|
-
const parent = aliases[uuid];
|
|
20
|
-
if (typeof parent !== "string" || !parent) return `u:${uuid}`;
|
|
21
|
-
uuid = parent;
|
|
22
|
-
}
|
|
23
|
-
// Malformed cycles still collapse deterministically regardless of file order.
|
|
24
|
-
return `u:${Array.from(chain).slice(Array.from(chain).indexOf(uuid)).sort()[0]}`;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
async function collectClaudeForkAliases(files, cursors = {}, onFork = () => {}) {
|
|
28
|
-
const aliases = Object.assign(Object.create(null), cursors.claudeForkAliases || {});
|
|
29
|
-
for (const entry of files) {
|
|
30
|
-
const filePath = typeof entry === "string" ? entry : entry?.path;
|
|
31
|
-
if (!filePath || /[\\/]subagents[\\/]/.test(filePath)) continue;
|
|
32
|
-
const stat = await fsp.stat(filePath).catch(() => null);
|
|
33
|
-
if (!stat?.isFile()) continue;
|
|
34
|
-
const prev = cursors.files?.[filePath];
|
|
35
|
-
const start = prev?.claudeForkIndexed && prev.inode === (stat.ino || 0) && prev.offset <= stat.size ? prev.offset || 0 : 0;
|
|
36
|
-
if (start >= stat.size) continue;
|
|
37
|
-
const stream = fs.createReadStream(filePath, { encoding: "utf8", start });
|
|
38
|
-
const lines = readline.createInterface({ input: stream, crlfDelay: Infinity });
|
|
39
|
-
try {
|
|
40
|
-
for await (const line of lines) {
|
|
41
|
-
if (!line.includes('"forkedFrom"')) continue;
|
|
42
|
-
try {
|
|
43
|
-
const obj = JSON.parse(line);
|
|
44
|
-
if (obj?.type === "user") { noteClaudeFork(aliases, obj); onFork(obj); }
|
|
45
|
-
} catch { /* Ignore incomplete or malformed JSONL records. */ }
|
|
46
|
-
}
|
|
47
|
-
} finally {
|
|
48
|
-
lines.close();
|
|
49
|
-
stream.destroy();
|
|
50
|
-
}
|
|
51
|
-
if (prev) prev.claudeForkIndexed = true;
|
|
52
|
-
}
|
|
53
|
-
return aliases;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
module.exports = { noteClaudeFork, claudeUserIdentity, collectClaudeForkAliases };
|
|
1
|
+
const fs = require("node:fs");
|
|
2
|
+
const fsp = require("node:fs/promises");
|
|
3
|
+
const readline = require("node:readline");
|
|
4
|
+
|
|
5
|
+
function noteClaudeFork(aliases, obj) {
|
|
6
|
+
const uuid = obj?.uuid;
|
|
7
|
+
const parent = obj?.forkedFrom?.messageUuid;
|
|
8
|
+
if (typeof uuid === "string" && uuid && typeof parent === "string" && parent && uuid !== parent) {
|
|
9
|
+
aliases[uuid] = parent;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function claudeUserIdentity(obj, aliases) {
|
|
14
|
+
let uuid = typeof obj?.uuid === "string" && obj.uuid ? obj.uuid : null;
|
|
15
|
+
if (!uuid) return null;
|
|
16
|
+
const chain = new Set();
|
|
17
|
+
while (!chain.has(uuid)) {
|
|
18
|
+
chain.add(uuid);
|
|
19
|
+
const parent = aliases[uuid];
|
|
20
|
+
if (typeof parent !== "string" || !parent) return `u:${uuid}`;
|
|
21
|
+
uuid = parent;
|
|
22
|
+
}
|
|
23
|
+
// Malformed cycles still collapse deterministically regardless of file order.
|
|
24
|
+
return `u:${Array.from(chain).slice(Array.from(chain).indexOf(uuid)).sort()[0]}`;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
async function collectClaudeForkAliases(files, cursors = {}, onFork = () => {}) {
|
|
28
|
+
const aliases = Object.assign(Object.create(null), cursors.claudeForkAliases || {});
|
|
29
|
+
for (const entry of files) {
|
|
30
|
+
const filePath = typeof entry === "string" ? entry : entry?.path;
|
|
31
|
+
if (!filePath || /[\\/]subagents[\\/]/.test(filePath)) continue;
|
|
32
|
+
const stat = await fsp.stat(filePath).catch(() => null);
|
|
33
|
+
if (!stat?.isFile()) continue;
|
|
34
|
+
const prev = cursors.files?.[filePath];
|
|
35
|
+
const start = prev?.claudeForkIndexed && prev.inode === (stat.ino || 0) && prev.offset <= stat.size ? prev.offset || 0 : 0;
|
|
36
|
+
if (start >= stat.size) continue;
|
|
37
|
+
const stream = fs.createReadStream(filePath, { encoding: "utf8", start });
|
|
38
|
+
const lines = readline.createInterface({ input: stream, crlfDelay: Infinity });
|
|
39
|
+
try {
|
|
40
|
+
for await (const line of lines) {
|
|
41
|
+
if (!line.includes('"forkedFrom"')) continue;
|
|
42
|
+
try {
|
|
43
|
+
const obj = JSON.parse(line);
|
|
44
|
+
if (obj?.type === "user") { noteClaudeFork(aliases, obj); onFork(obj); }
|
|
45
|
+
} catch { /* Ignore incomplete or malformed JSONL records. */ }
|
|
46
|
+
}
|
|
47
|
+
} finally {
|
|
48
|
+
lines.close();
|
|
49
|
+
stream.destroy();
|
|
50
|
+
}
|
|
51
|
+
if (prev) prev.claudeForkIndexed = true;
|
|
52
|
+
}
|
|
53
|
+
return aliases;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
module.exports = { noteClaudeFork, claudeUserIdentity, collectClaudeForkAliases };
|
package/src/lib/cli-ui.js
CHANGED
|
@@ -1,179 +1,179 @@
|
|
|
1
|
-
const readline = require("node:readline");
|
|
2
|
-
|
|
3
|
-
const RESET = "\x1b[0m";
|
|
4
|
-
const BOLD = "\x1b[1m";
|
|
5
|
-
const DIM = "\x1b[2m";
|
|
6
|
-
const CYAN = "\x1b[36m";
|
|
7
|
-
const GREEN = "\x1b[32m";
|
|
8
|
-
const YELLOW = "\x1b[33m";
|
|
9
|
-
const BLUE = "\x1b[34m";
|
|
10
|
-
const UNDERLINE = "\x1b[4m";
|
|
11
|
-
|
|
12
|
-
const SPINNER_FRAMES = ["|", "/", "-", "\\"];
|
|
13
|
-
|
|
14
|
-
function isInteractive() {
|
|
15
|
-
return Boolean(process.stdin.isTTY && process.stdout.isTTY);
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
function formatLine(line, width) {
|
|
19
|
-
if (!width) return line;
|
|
20
|
-
const raw = String(line || "");
|
|
21
|
-
const pad = Math.max(0, width - raw.length);
|
|
22
|
-
return raw + " ".repeat(pad);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
function renderBox(lines, { padding = 1 } = {}) {
|
|
26
|
-
const content = lines.map((line) => String(line || ""));
|
|
27
|
-
const maxLen = content.reduce((max, line) => Math.max(max, line.length), 0);
|
|
28
|
-
const innerWidth = maxLen + padding * 2;
|
|
29
|
-
const top = `+${"-".repeat(innerWidth)}+`;
|
|
30
|
-
const bottom = `+${"-".repeat(innerWidth)}+`;
|
|
31
|
-
const body = content.map((line) => {
|
|
32
|
-
const padded = " ".repeat(padding) + formatLine(line, maxLen) + " ".repeat(padding);
|
|
33
|
-
return `|${padded}|`;
|
|
34
|
-
});
|
|
35
|
-
return [top, ...body, bottom].join("\n");
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
function color(text, token) {
|
|
39
|
-
return `${token}${text}${RESET}`;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
function underline(text) {
|
|
43
|
-
return `${UNDERLINE}${text}${RESET}`;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
async function promptEnter(message) {
|
|
47
|
-
if (!isInteractive()) return;
|
|
48
|
-
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
|
|
49
|
-
await new Promise((resolve) => rl.question(message, () => resolve()));
|
|
50
|
-
rl.close();
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
async function promptMenu({ message, options, defaultIndex = 0 }) {
|
|
54
|
-
if (!isInteractive()) return options[defaultIndex] || options[0];
|
|
55
|
-
|
|
56
|
-
const safeOptions = Array.isArray(options) ? options : [];
|
|
57
|
-
if (safeOptions.length === 0) return "";
|
|
58
|
-
|
|
59
|
-
const maxIndex = safeOptions.length - 1;
|
|
60
|
-
let currentIndex = Math.min(Math.max(defaultIndex, 0), maxIndex);
|
|
61
|
-
const promptMessage = `${message} (Use Up/Down arrows, Enter)`;
|
|
62
|
-
const linesCount = safeOptions.length + 1;
|
|
63
|
-
|
|
64
|
-
const renderLines = () => {
|
|
65
|
-
const lines = [promptMessage];
|
|
66
|
-
safeOptions.forEach((opt, idx) => {
|
|
67
|
-
const prefix = idx === currentIndex ? ">" : " ";
|
|
68
|
-
lines.push(`${prefix} ${opt}`);
|
|
69
|
-
});
|
|
70
|
-
process.stdout.write(lines.join("\n"));
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
const rerender = () => {
|
|
74
|
-
for (let i = 0; i < linesCount; i += 1) {
|
|
75
|
-
process.stdout.write("\x1b[2K");
|
|
76
|
-
if (i < linesCount - 1) process.stdout.write("\x1b[1A");
|
|
77
|
-
}
|
|
78
|
-
process.stdout.write("\r");
|
|
79
|
-
renderLines();
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
renderLines();
|
|
83
|
-
|
|
84
|
-
return await new Promise((resolve) => {
|
|
85
|
-
const cleanup = () => {
|
|
86
|
-
process.stdin.off("keypress", onKeypress);
|
|
87
|
-
if (process.stdin.isTTY) process.stdin.setRawMode(false);
|
|
88
|
-
process.stdin.pause();
|
|
89
|
-
process.stdout.write("\n");
|
|
90
|
-
};
|
|
91
|
-
|
|
92
|
-
const onKeypress = (str, key = {}) => {
|
|
93
|
-
if (key.ctrl && key.name === "c") {
|
|
94
|
-
cleanup();
|
|
95
|
-
return resolve(safeOptions[currentIndex]);
|
|
96
|
-
}
|
|
97
|
-
if (key.name === "up" || str === "k") {
|
|
98
|
-
currentIndex = currentIndex === 0 ? maxIndex : currentIndex - 1;
|
|
99
|
-
rerender();
|
|
100
|
-
return;
|
|
101
|
-
}
|
|
102
|
-
if (key.name === "down" || str === "j") {
|
|
103
|
-
currentIndex = currentIndex === maxIndex ? 0 : currentIndex + 1;
|
|
104
|
-
rerender();
|
|
105
|
-
return;
|
|
106
|
-
}
|
|
107
|
-
if (key.name === "return") {
|
|
108
|
-
cleanup();
|
|
109
|
-
return resolve(safeOptions[currentIndex]);
|
|
110
|
-
}
|
|
111
|
-
if (str && /^[1-9]$/.test(str)) {
|
|
112
|
-
const idx = Number.parseInt(str, 10) - 1;
|
|
113
|
-
if (idx >= 0 && idx <= maxIndex) {
|
|
114
|
-
currentIndex = idx;
|
|
115
|
-
cleanup();
|
|
116
|
-
return resolve(safeOptions[currentIndex]);
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
};
|
|
120
|
-
|
|
121
|
-
readline.emitKeypressEvents(process.stdin);
|
|
122
|
-
if (process.stdin.isTTY) process.stdin.setRawMode(true);
|
|
123
|
-
process.stdin.resume();
|
|
124
|
-
process.stdin.on("keypress", onKeypress);
|
|
125
|
-
});
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
function createSpinner({ text, intervalMs = 80 }) {
|
|
129
|
-
let frame = 0;
|
|
130
|
-
let timer = null;
|
|
131
|
-
|
|
132
|
-
function start() {
|
|
133
|
-
if (!isInteractive()) {
|
|
134
|
-
process.stdout.write(`${text}\n`);
|
|
135
|
-
return;
|
|
136
|
-
}
|
|
137
|
-
timer = setInterval(() => {
|
|
138
|
-
const glyph = SPINNER_FRAMES[frame % SPINNER_FRAMES.length];
|
|
139
|
-
frame += 1;
|
|
140
|
-
process.stdout.write(`\r${glyph} ${text}`);
|
|
141
|
-
}, intervalMs);
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
function stop(successText) {
|
|
145
|
-
if (timer) clearInterval(timer);
|
|
146
|
-
if (isInteractive()) {
|
|
147
|
-
process.stdout.write(`\r${" ".repeat(text.length + 4)}\r`);
|
|
148
|
-
}
|
|
149
|
-
if (successText) process.stdout.write(`${successText}\n`);
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
return { start, stop };
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
function formatSummaryLine({ label, status, detail }) {
|
|
156
|
-
const isSuccess = status === "updated" || status === "set" || status === "installed";
|
|
157
|
-
const bullet = isSuccess ? color("*", GREEN) : "o";
|
|
158
|
-
const statusLabel = isSuccess ? detail || status : detail ? `Skipped - ${detail}` : "Skipped";
|
|
159
|
-
const line = ` ${bullet} ${label.padEnd(22)} [${statusLabel}]`;
|
|
160
|
-
return isSuccess ? line : color(line, DIM);
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
module.exports = {
|
|
164
|
-
BOLD,
|
|
165
|
-
DIM,
|
|
166
|
-
CYAN,
|
|
167
|
-
GREEN,
|
|
168
|
-
YELLOW,
|
|
169
|
-
BLUE,
|
|
170
|
-
RESET,
|
|
171
|
-
color,
|
|
172
|
-
underline,
|
|
173
|
-
renderBox,
|
|
174
|
-
isInteractive,
|
|
175
|
-
promptMenu,
|
|
176
|
-
promptEnter,
|
|
177
|
-
createSpinner,
|
|
178
|
-
formatSummaryLine,
|
|
179
|
-
};
|
|
1
|
+
const readline = require("node:readline");
|
|
2
|
+
|
|
3
|
+
const RESET = "\x1b[0m";
|
|
4
|
+
const BOLD = "\x1b[1m";
|
|
5
|
+
const DIM = "\x1b[2m";
|
|
6
|
+
const CYAN = "\x1b[36m";
|
|
7
|
+
const GREEN = "\x1b[32m";
|
|
8
|
+
const YELLOW = "\x1b[33m";
|
|
9
|
+
const BLUE = "\x1b[34m";
|
|
10
|
+
const UNDERLINE = "\x1b[4m";
|
|
11
|
+
|
|
12
|
+
const SPINNER_FRAMES = ["|", "/", "-", "\\"];
|
|
13
|
+
|
|
14
|
+
function isInteractive() {
|
|
15
|
+
return Boolean(process.stdin.isTTY && process.stdout.isTTY);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function formatLine(line, width) {
|
|
19
|
+
if (!width) return line;
|
|
20
|
+
const raw = String(line || "");
|
|
21
|
+
const pad = Math.max(0, width - raw.length);
|
|
22
|
+
return raw + " ".repeat(pad);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function renderBox(lines, { padding = 1 } = {}) {
|
|
26
|
+
const content = lines.map((line) => String(line || ""));
|
|
27
|
+
const maxLen = content.reduce((max, line) => Math.max(max, line.length), 0);
|
|
28
|
+
const innerWidth = maxLen + padding * 2;
|
|
29
|
+
const top = `+${"-".repeat(innerWidth)}+`;
|
|
30
|
+
const bottom = `+${"-".repeat(innerWidth)}+`;
|
|
31
|
+
const body = content.map((line) => {
|
|
32
|
+
const padded = " ".repeat(padding) + formatLine(line, maxLen) + " ".repeat(padding);
|
|
33
|
+
return `|${padded}|`;
|
|
34
|
+
});
|
|
35
|
+
return [top, ...body, bottom].join("\n");
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function color(text, token) {
|
|
39
|
+
return `${token}${text}${RESET}`;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function underline(text) {
|
|
43
|
+
return `${UNDERLINE}${text}${RESET}`;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async function promptEnter(message) {
|
|
47
|
+
if (!isInteractive()) return;
|
|
48
|
+
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
|
|
49
|
+
await new Promise((resolve) => rl.question(message, () => resolve()));
|
|
50
|
+
rl.close();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async function promptMenu({ message, options, defaultIndex = 0 }) {
|
|
54
|
+
if (!isInteractive()) return options[defaultIndex] || options[0];
|
|
55
|
+
|
|
56
|
+
const safeOptions = Array.isArray(options) ? options : [];
|
|
57
|
+
if (safeOptions.length === 0) return "";
|
|
58
|
+
|
|
59
|
+
const maxIndex = safeOptions.length - 1;
|
|
60
|
+
let currentIndex = Math.min(Math.max(defaultIndex, 0), maxIndex);
|
|
61
|
+
const promptMessage = `${message} (Use Up/Down arrows, Enter)`;
|
|
62
|
+
const linesCount = safeOptions.length + 1;
|
|
63
|
+
|
|
64
|
+
const renderLines = () => {
|
|
65
|
+
const lines = [promptMessage];
|
|
66
|
+
safeOptions.forEach((opt, idx) => {
|
|
67
|
+
const prefix = idx === currentIndex ? ">" : " ";
|
|
68
|
+
lines.push(`${prefix} ${opt}`);
|
|
69
|
+
});
|
|
70
|
+
process.stdout.write(lines.join("\n"));
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
const rerender = () => {
|
|
74
|
+
for (let i = 0; i < linesCount; i += 1) {
|
|
75
|
+
process.stdout.write("\x1b[2K");
|
|
76
|
+
if (i < linesCount - 1) process.stdout.write("\x1b[1A");
|
|
77
|
+
}
|
|
78
|
+
process.stdout.write("\r");
|
|
79
|
+
renderLines();
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
renderLines();
|
|
83
|
+
|
|
84
|
+
return await new Promise((resolve) => {
|
|
85
|
+
const cleanup = () => {
|
|
86
|
+
process.stdin.off("keypress", onKeypress);
|
|
87
|
+
if (process.stdin.isTTY) process.stdin.setRawMode(false);
|
|
88
|
+
process.stdin.pause();
|
|
89
|
+
process.stdout.write("\n");
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
const onKeypress = (str, key = {}) => {
|
|
93
|
+
if (key.ctrl && key.name === "c") {
|
|
94
|
+
cleanup();
|
|
95
|
+
return resolve(safeOptions[currentIndex]);
|
|
96
|
+
}
|
|
97
|
+
if (key.name === "up" || str === "k") {
|
|
98
|
+
currentIndex = currentIndex === 0 ? maxIndex : currentIndex - 1;
|
|
99
|
+
rerender();
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
if (key.name === "down" || str === "j") {
|
|
103
|
+
currentIndex = currentIndex === maxIndex ? 0 : currentIndex + 1;
|
|
104
|
+
rerender();
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
if (key.name === "return") {
|
|
108
|
+
cleanup();
|
|
109
|
+
return resolve(safeOptions[currentIndex]);
|
|
110
|
+
}
|
|
111
|
+
if (str && /^[1-9]$/.test(str)) {
|
|
112
|
+
const idx = Number.parseInt(str, 10) - 1;
|
|
113
|
+
if (idx >= 0 && idx <= maxIndex) {
|
|
114
|
+
currentIndex = idx;
|
|
115
|
+
cleanup();
|
|
116
|
+
return resolve(safeOptions[currentIndex]);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
readline.emitKeypressEvents(process.stdin);
|
|
122
|
+
if (process.stdin.isTTY) process.stdin.setRawMode(true);
|
|
123
|
+
process.stdin.resume();
|
|
124
|
+
process.stdin.on("keypress", onKeypress);
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function createSpinner({ text, intervalMs = 80 }) {
|
|
129
|
+
let frame = 0;
|
|
130
|
+
let timer = null;
|
|
131
|
+
|
|
132
|
+
function start() {
|
|
133
|
+
if (!isInteractive()) {
|
|
134
|
+
process.stdout.write(`${text}\n`);
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
timer = setInterval(() => {
|
|
138
|
+
const glyph = SPINNER_FRAMES[frame % SPINNER_FRAMES.length];
|
|
139
|
+
frame += 1;
|
|
140
|
+
process.stdout.write(`\r${glyph} ${text}`);
|
|
141
|
+
}, intervalMs);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function stop(successText) {
|
|
145
|
+
if (timer) clearInterval(timer);
|
|
146
|
+
if (isInteractive()) {
|
|
147
|
+
process.stdout.write(`\r${" ".repeat(text.length + 4)}\r`);
|
|
148
|
+
}
|
|
149
|
+
if (successText) process.stdout.write(`${successText}\n`);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
return { start, stop };
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function formatSummaryLine({ label, status, detail }) {
|
|
156
|
+
const isSuccess = status === "updated" || status === "set" || status === "installed";
|
|
157
|
+
const bullet = isSuccess ? color("*", GREEN) : "o";
|
|
158
|
+
const statusLabel = isSuccess ? detail || status : detail ? `Skipped - ${detail}` : "Skipped";
|
|
159
|
+
const line = ` ${bullet} ${label.padEnd(22)} [${statusLabel}]`;
|
|
160
|
+
return isSuccess ? line : color(line, DIM);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
module.exports = {
|
|
164
|
+
BOLD,
|
|
165
|
+
DIM,
|
|
166
|
+
CYAN,
|
|
167
|
+
GREEN,
|
|
168
|
+
YELLOW,
|
|
169
|
+
BLUE,
|
|
170
|
+
RESET,
|
|
171
|
+
color,
|
|
172
|
+
underline,
|
|
173
|
+
renderBox,
|
|
174
|
+
isInteractive,
|
|
175
|
+
promptMenu,
|
|
176
|
+
promptEnter,
|
|
177
|
+
createSpinner,
|
|
178
|
+
formatSummaryLine,
|
|
179
|
+
};
|