@illuminis/comprism 0.1.8 → 2.4.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/out/{graph → build/machine/codemap}/read-python.js +2 -2
- package/out/{executor → build/machine/executor}/diagnostics.js +1 -1
- package/out/{executor → build/machine/executor}/hooks.js +1 -1
- package/out/{executor → build/machine/executor}/sandbox.js +1 -1
- package/out/build/machine/line/index.js +339 -0
- package/out/{commands/privacy.js → build/machine/runtime/home.js} +10 -19
- package/out/build/machine/runtime/needs.js +89 -0
- package/out/{lib → build/machine/runtime}/self.js +25 -3
- package/out/build/machine/terminal/jobs.js +67 -0
- package/out/build/machine/terminal/sign-in.js +212 -0
- package/out/build/machine/terminal/steps.js +460 -0
- package/out/build/machine/terminal/voice.js +136 -0
- package/out/build/machine/voice/record.js +286 -0
- package/out/cli-start.js +128 -0
- package/out/machine/line/index.js +339 -0
- package/out/machine/runtime/self.js +22 -0
- package/out/machine/terminal/clipboard.js +182 -0
- package/out/machine/terminal/jobs.js +67 -0
- package/out/machine/terminal/sign-in.js +212 -0
- package/out/machine/terminal/steps.js +460 -0
- package/out/machine/terminal/voice.js +136 -0
- package/out/machine/voice/record.js +79 -1
- package/out/native/dictate +0 -0
- package/out/server.generated.js +6 -0
- package/package.json +8 -40
- package/README.md +0 -343
- package/out/agent/command.d.ts +0 -157
- package/out/agent/command.js +0 -454
- package/out/agent/render.d.ts +0 -169
- package/out/agent/render.js +0 -689
- package/out/agent/session.d.ts +0 -334
- package/out/agent/session.js +0 -1232
- package/out/commands/agents.d.ts +0 -2
- package/out/commands/agents.js +0 -79
- package/out/commands/ask.d.ts +0 -1
- package/out/commands/ask.js +0 -222
- package/out/commands/codemap.d.ts +0 -2
- package/out/commands/codemap.js +0 -151
- package/out/commands/commands-thin.d.ts +0 -39
- package/out/commands/commands-thin.js +0 -244
- package/out/commands/config.d.ts +0 -1
- package/out/commands/config.js +0 -138
- package/out/commands/cost.d.ts +0 -1
- package/out/commands/cost.js +0 -167
- package/out/commands/decisionEngine.d.ts +0 -1
- package/out/commands/decisionEngine.js +0 -66
- package/out/commands/hooks.d.ts +0 -1
- package/out/commands/hooks.js +0 -83
- package/out/commands/install.d.ts +0 -206
- package/out/commands/install.js +0 -738
- package/out/commands/instructions.d.ts +0 -1
- package/out/commands/instructions.js +0 -113
- package/out/commands/integrations.d.ts +0 -3
- package/out/commands/integrations.js +0 -215
- package/out/commands/jobs.d.ts +0 -5
- package/out/commands/jobs.js +0 -157
- package/out/commands/keys.d.ts +0 -55
- package/out/commands/keys.js +0 -344
- package/out/commands/login.d.ts +0 -9
- package/out/commands/login.js +0 -606
- package/out/commands/memory.d.ts +0 -3
- package/out/commands/memory.js +0 -113
- package/out/commands/permissions.d.ts +0 -1
- package/out/commands/permissions.js +0 -94
- package/out/commands/plugins.d.ts +0 -4
- package/out/commands/plugins.js +0 -192
- package/out/commands/privacy.d.ts +0 -1
- package/out/commands/providerKey.d.ts +0 -32
- package/out/commands/providerKey.js +0 -108
- package/out/commands/repl.d.ts +0 -12
- package/out/commands/repl.js +0 -2303
- package/out/commands/report.d.ts +0 -39
- package/out/commands/report.js +0 -115
- package/out/commands/review.d.ts +0 -5
- package/out/commands/review.js +0 -223
- package/out/commands/sessions.d.ts +0 -23
- package/out/commands/sessions.js +0 -115
- package/out/commands/settings.d.ts +0 -23
- package/out/commands/settings.js +0 -153
- package/out/commands/skills.d.ts +0 -21
- package/out/commands/skills.js +0 -207
- package/out/commands/unattended.d.ts +0 -7
- package/out/commands/unattended.js +0 -351
- package/out/commands/update.d.ts +0 -1
- package/out/commands/update.js +0 -123
- package/out/commands/welcome.d.ts +0 -25
- package/out/commands/welcome.js +0 -223
- package/out/commands/worktrees.d.ts +0 -5
- package/out/commands/worktrees.js +0 -186
- package/out/executor/browser.d.ts +0 -14
- package/out/executor/diagnostics.d.ts +0 -2
- package/out/executor/documents.d.ts +0 -40
- package/out/executor/files.d.ts +0 -2
- package/out/executor/git.d.ts +0 -48
- package/out/executor/hooks.d.ts +0 -51
- package/out/executor/index.d.ts +0 -45
- package/out/executor/notebook.d.ts +0 -2
- package/out/executor/paths.d.ts +0 -20
- package/out/executor/sandbox.d.ts +0 -40
- package/out/executor/shell.d.ts +0 -86
- package/out/executor/toolservers.d.ts +0 -20
- package/out/executor/worktree.d.ts +0 -9
- package/out/graph/build.d.ts +0 -45
- package/out/graph/facts.d.ts +0 -47
- package/out/graph/files.d.ts +0 -45
- package/out/graph/read-locales.d.ts +0 -29
- package/out/graph/read-python.d.ts +0 -11
- package/out/graph/read-typescript.d.ts +0 -16
- package/out/graph/sync.d.ts +0 -66
- package/out/graph/sync.js +0 -242
- package/out/lib/attach.d.ts +0 -106
- package/out/lib/attach.js +0 -396
- package/out/lib/clipboard.d.ts +0 -23
- package/out/lib/commandlist.d.ts +0 -20
- package/out/lib/commandlist.js +0 -58
- package/out/lib/config.d.ts +0 -94
- package/out/lib/config.js +0 -197
- package/out/lib/connection.d.ts +0 -73
- package/out/lib/connection.js +0 -188
- package/out/lib/decision.d.ts +0 -22
- package/out/lib/decision.js +0 -50
- package/out/lib/fingerprint.d.ts +0 -25
- package/out/lib/fingerprint.js +0 -58
- package/out/lib/gateway.d.ts +0 -427
- package/out/lib/gateway.js +0 -226
- package/out/lib/history.d.ts +0 -24
- package/out/lib/history.js +0 -137
- package/out/lib/ide.d.ts +0 -19
- package/out/lib/ide.js +0 -131
- package/out/lib/keyboard.d.ts +0 -98
- package/out/lib/keyboard.js +0 -390
- package/out/lib/lastlogin.d.ts +0 -14
- package/out/lib/lastlogin.js +0 -105
- package/out/lib/machine.d.ts +0 -21
- package/out/lib/machine.js +0 -91
- package/out/lib/notify.d.ts +0 -4
- package/out/lib/notify.js +0 -52
- package/out/lib/output.d.ts +0 -48
- package/out/lib/output.js +0 -108
- package/out/lib/picker.d.ts +0 -1
- package/out/lib/picker.js +0 -158
- package/out/lib/project-ops.d.ts +0 -19
- package/out/lib/project-ops.js +0 -146
- package/out/lib/project.d.ts +0 -28
- package/out/lib/prompt.d.ts +0 -37
- package/out/lib/prompt.js +0 -142
- package/out/lib/queue.d.ts +0 -13
- package/out/lib/queue.js +0 -116
- package/out/lib/readiness.d.ts +0 -22
- package/out/lib/readiness.js +0 -207
- package/out/lib/self.d.ts +0 -23
- package/out/lib/servicecommand.d.ts +0 -14
- package/out/lib/servicecommand.js +0 -74
- package/out/lib/sessions.d.ts +0 -19
- package/out/lib/sessions.js +0 -221
- package/out/lib/startcache.d.ts +0 -13
- package/out/lib/startcache.js +0 -78
- package/out/lib/stdin.d.ts +0 -32
- package/out/lib/stdin.js +0 -117
- package/out/lib/store.d.ts +0 -40
- package/out/lib/store.js +0 -138
- package/out/lib/sync.d.ts +0 -18
- package/out/lib/sync.js +0 -81
- package/out/lib/types.d.ts +0 -417
- package/out/lib/types.js +0 -21
- package/out/lib/ui.d.ts +0 -123
- package/out/lib/ui.js +0 -299
- package/out/lib/version.d.ts +0 -23
- package/out/lib/version.js +0 -46
- package/out/lib/voice.d.ts +0 -49
- package/out/lib/voice.js +0 -236
- package/out/lib/words.d.ts +0 -20
- package/out/lib/words.js +0 -27
- package/out/machine/codemap/build.d.ts +0 -45
- package/out/machine/codemap/facts.d.ts +0 -47
- package/out/machine/codemap/files.d.ts +0 -45
- package/out/machine/codemap/read-locales.d.ts +0 -29
- package/out/machine/codemap/read-python.d.ts +0 -11
- package/out/machine/codemap/read-typescript.d.ts +0 -16
- package/out/machine/executor/browser.d.ts +0 -14
- package/out/machine/executor/diagnostics.d.ts +0 -2
- package/out/machine/executor/documents.d.ts +0 -40
- package/out/machine/executor/files.d.ts +0 -2
- package/out/machine/executor/git.d.ts +0 -48
- package/out/machine/executor/hooks.d.ts +0 -51
- package/out/machine/executor/index.d.ts +0 -45
- package/out/machine/executor/notebook.d.ts +0 -2
- package/out/machine/executor/paths.d.ts +0 -20
- package/out/machine/executor/sandbox.d.ts +0 -40
- package/out/machine/executor/shell.d.ts +0 -86
- package/out/machine/executor/toolservers.d.ts +0 -20
- package/out/machine/executor/worktree.d.ts +0 -9
- package/out/machine/folder/project.d.ts +0 -28
- package/out/machine/runtime/home.d.ts +0 -2
- package/out/machine/runtime/needs.d.ts +0 -30
- package/out/machine/runtime/self.d.ts +0 -23
- package/out/machine/voice/record.d.ts +0 -38
- package/out/postinstall.d.ts +0 -2
- package/out/postinstall.js +0 -120
- package/out/providers/anthropic.d.ts +0 -22
- package/out/providers/anthropic.js +0 -80
- package/out/providers/index.d.ts +0 -8
- package/out/providers/index.js +0 -84
- package/out/providers/openai.d.ts +0 -11
- package/out/providers/openai.js +0 -57
- package/out/thin.d.ts +0 -2
- package/out/thin.js +0 -855
- package/scripts/read_python.py +0 -270
- /package/out/{graph → build/machine/codemap}/build.js +0 -0
- /package/out/{graph → build/machine/codemap}/facts.js +0 -0
- /package/out/{graph → build/machine/codemap}/files.js +0 -0
- /package/out/{graph → build/machine/codemap}/read-locales.js +0 -0
- /package/out/{graph → build/machine/codemap}/read-typescript.js +0 -0
- /package/out/{executor → build/machine/executor}/browser.js +0 -0
- /package/out/{executor → build/machine/executor}/documents.js +0 -0
- /package/out/{executor → build/machine/executor}/files.js +0 -0
- /package/out/{executor → build/machine/executor}/git.js +0 -0
- /package/out/{executor → build/machine/executor}/index.js +0 -0
- /package/out/{executor → build/machine/executor}/notebook.js +0 -0
- /package/out/{executor → build/machine/executor}/paths.js +0 -0
- /package/out/{executor → build/machine/executor}/shell.js +0 -0
- /package/out/{executor → build/machine/executor}/toolservers.js +0 -0
- /package/out/{executor → build/machine/executor}/worktree.js +0 -0
- /package/out/{lib → build/machine/folder}/project.js +0 -0
- /package/out/{lib → build/machine/terminal}/clipboard.js +0 -0
package/out/agent/render.js
DELETED
|
@@ -1,689 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* What a coding agent looks like in a terminal.
|
|
4
|
-
*
|
|
5
|
-
* Specification: docs/modules/CODING_AGENT_BUILD_SPECIFICATION.md, register
|
|
6
|
-
* items A6 (a real coding agent in the terminal), I8 (a terminal panel, real
|
|
7
|
-
* output, watchable) and I3 (a live action timeline).
|
|
8
|
-
*
|
|
9
|
-
* A terminal has no panels, so the whole interface is a stream of lines and the
|
|
10
|
-
* scrollback IS the record. That constrains everything here:
|
|
11
|
-
*
|
|
12
|
-
* **Every line has to make sense on its own, later.** Somebody scrolls back
|
|
13
|
-
* through a twenty minute run looking for the moment it went wrong. A line that
|
|
14
|
-
* only means something next to the line above it has failed them.
|
|
15
|
-
*
|
|
16
|
-
* **Nothing is redrawn or erased.** Spinners that rewrite their own line leave
|
|
17
|
-
* nothing behind, and a terminal being piped to a file fills with escape codes.
|
|
18
|
-
* What is printed stays printed.
|
|
19
|
-
*
|
|
20
|
-
* **Color is never the only signal.** Terminals get piped, logged and read by
|
|
21
|
-
* people who cannot separate red from green, so every state carries a word or a
|
|
22
|
-
* symbol as well as a color.
|
|
23
|
-
*/
|
|
24
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
25
|
-
exports.violet = exports.gray = exports.yellow = exports.green = exports.red = exports.bold = exports.dim = void 0;
|
|
26
|
-
exports.modelName = modelName;
|
|
27
|
-
exports.money = money;
|
|
28
|
-
exports.duration = duration;
|
|
29
|
-
exports.action = action;
|
|
30
|
-
exports.actionName = actionName;
|
|
31
|
-
exports.reusePct = reusePct;
|
|
32
|
-
exports.step = step;
|
|
33
|
-
exports.todos = todos;
|
|
34
|
-
exports.question = question;
|
|
35
|
-
exports.diff = diff;
|
|
36
|
-
exports.stoppedLines = stoppedLines;
|
|
37
|
-
exports.ending = ending;
|
|
38
|
-
exports.outcome = outcome;
|
|
39
|
-
exports.link = link;
|
|
40
|
-
exports.savedFile = savedFile;
|
|
41
|
-
exports.receipt = receipt;
|
|
42
|
-
exports.plain = plain;
|
|
43
|
-
/** Built from a character code rather than written as a literal escape, so this
|
|
44
|
-
* file contains no control characters and stays safe to grep, diff and paste. */
|
|
45
|
-
const url_1 = require("url");
|
|
46
|
-
const CSI = String.fromCharCode(27) + "[";
|
|
47
|
-
/** Decided when drawn, so plain mode set at start up applies (manual 6.12). */
|
|
48
|
-
const supportsColour = () => process.stdout.isTTY && !process.env.NO_COLOR;
|
|
49
|
-
function paint(code, text) {
|
|
50
|
-
return supportsColour() ? CSI + code + "m" + text + CSI + "0m" : text;
|
|
51
|
-
}
|
|
52
|
-
const dim = (t) => paint("2", t);
|
|
53
|
-
exports.dim = dim;
|
|
54
|
-
const bold = (t) => paint("1", t);
|
|
55
|
-
exports.bold = bold;
|
|
56
|
-
const red = (t) => paint("31", t);
|
|
57
|
-
exports.red = red;
|
|
58
|
-
const green = (t) => paint("32", t);
|
|
59
|
-
exports.green = green;
|
|
60
|
-
const yellow = (t) => paint("33", t);
|
|
61
|
-
exports.yellow = yellow;
|
|
62
|
-
const gray = (t) => paint("90", t);
|
|
63
|
-
exports.gray = gray;
|
|
64
|
-
/** The brand's violet, for model names and the receipt's label. */
|
|
65
|
-
const violet = (t) => paint("38;5;141", t);
|
|
66
|
-
exports.violet = violet;
|
|
67
|
-
/** The rail down the left of a job's steps, as the website draws it. */
|
|
68
|
-
const rail = () => (0, exports.dim)("\u2502");
|
|
69
|
-
const TICK = "\u2713";
|
|
70
|
-
const CROSS = "\u2717";
|
|
71
|
-
/** What a person sees, with color and link escapes taken out, so lines can be
|
|
72
|
-
* measured and padded. */
|
|
73
|
-
function visible(text) {
|
|
74
|
-
const esc = String.fromCharCode(27);
|
|
75
|
-
return text
|
|
76
|
-
.replace(new RegExp(`${esc}\\][^${String.fromCharCode(7)}${esc}]*(?:${String.fromCharCode(7)}|${esc}\\\\)`, "g"), "")
|
|
77
|
-
.replace(new RegExp(`${esc}\\[[0-9;]*m`, "g"), "");
|
|
78
|
-
}
|
|
79
|
-
function width() {
|
|
80
|
-
return Math.max(60, Math.min(process.stdout.columns || 100, 110));
|
|
81
|
-
}
|
|
82
|
-
/** `claude-haiku-4-5` as a person says it, `Claude Haiku 4.5`. Other vendors'
|
|
83
|
-
* ids are shown as they arrive: a wrong guess at a name is worse than the id. */
|
|
84
|
-
function modelName(id) {
|
|
85
|
-
const m = /^claude-([a-z]+)-(\d+)(?:-(\d+))?$/.exec(id);
|
|
86
|
-
if (!m)
|
|
87
|
-
return id;
|
|
88
|
-
const word = m[1] ?? "";
|
|
89
|
-
const family = word.charAt(0).toUpperCase() + word.slice(1);
|
|
90
|
-
return `Claude ${family} ${m[2]}${m[3] ? `.${m[3]}` : ""}`;
|
|
91
|
-
}
|
|
92
|
-
/** Money, at the precision a person can act on.
|
|
93
|
-
*
|
|
94
|
-
* Four decimal places below a cent, because a coding step often costs less
|
|
95
|
-
* than one and rounding to $0.00 would show the cheapest work, which is what
|
|
96
|
-
* this product is best at, as free. */
|
|
97
|
-
function money(usd) {
|
|
98
|
-
if (usd === null || usd === undefined)
|
|
99
|
-
return "unpriced";
|
|
100
|
-
return usd < 0.01 ? `$${usd.toFixed(4)}` : `$${usd.toFixed(2)}`;
|
|
101
|
-
}
|
|
102
|
-
function duration(ms) {
|
|
103
|
-
if (!ms)
|
|
104
|
-
return "";
|
|
105
|
-
return ms < 1000 ? `${ms}ms` : `${(ms / 1000).toFixed(1)}s`;
|
|
106
|
-
}
|
|
107
|
-
/** One action, as it happens. A word as well as a color, so this still reads
|
|
108
|
-
* when piped to a file. */
|
|
109
|
-
function action(name, target, ok, ms) {
|
|
110
|
-
// A tick or a cross on the rail, as the website draws a job. A failure also
|
|
111
|
-
// says so in words, so it still reads in a log with no color.
|
|
112
|
-
const mark = ok ? (0, exports.green)(TICK) : (0, exports.red)(`${CROSS} failed`);
|
|
113
|
-
const tail = ms ? (0, exports.dim)(` ${duration(ms)}`) : "";
|
|
114
|
-
return ` ${rail()} ${mark} ${(0, exports.bold)(actionName(name))} ${(0, exports.dim)(target)}${tail}`;
|
|
115
|
-
}
|
|
116
|
-
/** A connected tool as people write it, `server.tool` (manual 9.10). */
|
|
117
|
-
function actionName(name) {
|
|
118
|
-
const m = /^connected__(.+?)__(.+)$/.exec(name);
|
|
119
|
-
return m ? `${m[1]}.${m[2]}` : name;
|
|
120
|
-
}
|
|
121
|
-
/** The share of a step's context the provider served from its cache, or null
|
|
122
|
-
* when it reported no counts. Anthropic counts cached tokens OUTSIDE the input
|
|
123
|
-
* figure, so the whole context is the three counts added together. */
|
|
124
|
-
function reusePct(tokensIn, cacheRead, cacheWrite) {
|
|
125
|
-
if (cacheRead === null || cacheRead === undefined)
|
|
126
|
-
return null;
|
|
127
|
-
const whole = (tokensIn ?? 0) + (cacheRead ?? 0) + (cacheWrite ?? 0);
|
|
128
|
-
if (!whole)
|
|
129
|
-
return null;
|
|
130
|
-
return Math.round((cacheRead / whole) * 100);
|
|
131
|
-
}
|
|
132
|
-
/** One model call: which model, what it cost, and how much of its context the
|
|
133
|
-
* provider had already seen. The last figure is the one study 005 found
|
|
134
|
-
* missing from our receipt: without it nobody could tell a warm step from a
|
|
135
|
-
* cold one, and every step was cold. */
|
|
136
|
-
function step(index, model, usd, ms, reused = null, reason = "") {
|
|
137
|
-
const warm = reused === null ? "" : ` cached ${reused}%`;
|
|
138
|
-
// The model that ran and why, in the service's words (manual 4.16).
|
|
139
|
-
const why = reason ? ` chosen: ${reason}` : "";
|
|
140
|
-
// The step and why its model was chosen on the left, the model on the right,
|
|
141
|
-
// as the website draws a job; the cost and timing follow, quieter.
|
|
142
|
-
const left = ` ${rail()} ${(0, exports.dim)(`step ${index}${why}`)}`;
|
|
143
|
-
const right = `${(0, exports.violet)(modelName(model || "?"))}${(0, exports.dim)(` ${money(usd)} ${duration(ms)}${warm}`)}`;
|
|
144
|
-
const gap = Math.max(2, width() - visible(left).length - visible(right).length - 2);
|
|
145
|
-
return `${left}${" ".repeat(gap)}${right}`;
|
|
146
|
-
}
|
|
147
|
-
/** The task list, reprinted whenever it changes.
|
|
148
|
-
*
|
|
149
|
-
* Reprinted rather than redrawn in place: what the agent intended at each point
|
|
150
|
-
* is worth keeping, and it is often the fastest way to see where a job went off
|
|
151
|
-
* course. */
|
|
152
|
-
function todos(items) {
|
|
153
|
-
// Drawn as manual 4.4 shows it: [x] done, [>] in progress, [ ] not yet.
|
|
154
|
-
if (!items.length)
|
|
155
|
-
return "";
|
|
156
|
-
const rows = items.map((t) => {
|
|
157
|
-
const mark = t.status === "completed" ? (0, exports.green)("[x]")
|
|
158
|
-
: t.status === "in_progress" ? (0, exports.yellow)("[>]") : (0, exports.dim)("[ ]");
|
|
159
|
-
const text = t.status === "completed" ? (0, exports.dim)(t.content) : t.content;
|
|
160
|
-
return ` ${mark} ${text}`;
|
|
161
|
-
});
|
|
162
|
-
return `${rows.join("\n")}\n`;
|
|
163
|
-
}
|
|
164
|
-
/** A question from the agent, or the plan question (manual 4.2, 4.3). The
|
|
165
|
-
* wording is the service's; this only lays it out. */
|
|
166
|
-
function question(q, options) {
|
|
167
|
-
const lines = [` ${(0, exports.bold)("Question:")} ${q}`];
|
|
168
|
-
options.forEach((o, i) => lines.push(` ${i + 1} ${o}`));
|
|
169
|
-
return `\n${lines.join("\n")}\n`;
|
|
170
|
-
}
|
|
171
|
-
/** A change, before it is made.
|
|
172
|
-
*
|
|
173
|
-
* Full text for a small edit and a summary for a large one. Pasting four
|
|
174
|
-
* hundred lines into a terminal is not review, it is a wall somebody presses
|
|
175
|
-
* through, and an approval people press through protects nobody. */
|
|
176
|
-
function diff(name, input, withHead = true) {
|
|
177
|
-
const target = String(input.path ?? input.to ?? "");
|
|
178
|
-
// Without the heading under an action line that already names the file.
|
|
179
|
-
const head = withHead ? `\n ${(0, exports.bold)(name)} ${target}\n` : "";
|
|
180
|
-
const show = (from, to) => {
|
|
181
|
-
const before = from.split("\n"), after = to.split("\n");
|
|
182
|
-
if (before.length + after.length > 60) {
|
|
183
|
-
return ` ${(0, exports.dim)(`${before.length} lines replaced by ${after.length} lines`)}\n`;
|
|
184
|
-
}
|
|
185
|
-
return [
|
|
186
|
-
...before.map((l) => ` ${(0, exports.red)("-")} ${l}`),
|
|
187
|
-
...after.map((l) => ` ${(0, exports.green)("+")} ${l}`),
|
|
188
|
-
].join("\n") + "\n";
|
|
189
|
-
};
|
|
190
|
-
if (name === "edit_file") {
|
|
191
|
-
return head + show(String(input.old_text ?? ""), String(input.new_text ?? ""));
|
|
192
|
-
}
|
|
193
|
-
if (name === "multi_edit") {
|
|
194
|
-
const edits = input.edits ?? [];
|
|
195
|
-
return head + edits.map((e) => show(e.old_text, e.new_text)).join(` ${(0, exports.dim)("--")}\n`);
|
|
196
|
-
}
|
|
197
|
-
if (name === "write_file") {
|
|
198
|
-
const bodyText = String(input.content ?? "");
|
|
199
|
-
const lines = bodyText.split("\n");
|
|
200
|
-
if (lines.length <= 40) {
|
|
201
|
-
return head + lines.map((l) => ` ${(0, exports.green)("+")} ${l}`).join("\n") + "\n";
|
|
202
|
-
}
|
|
203
|
-
return head + ` ${(0, exports.dim)(`${lines.length} lines, ${bodyText.length.toLocaleString()} characters`)}\n`;
|
|
204
|
-
}
|
|
205
|
-
if (name === "git_commit") {
|
|
206
|
-
// What will be committed, shown before it is (manual 5.17).
|
|
207
|
-
const files = input.paths ?? [];
|
|
208
|
-
return `\n ${(0, exports.bold)("git_commit")}\n files ${files.join(", ") || (0, exports.dim)("none")}\n`
|
|
209
|
-
+ ` message ${String(input.message ?? "").split("\n")[0]}\n`;
|
|
210
|
-
}
|
|
211
|
-
if (name === "run_command" || name === "run_background") {
|
|
212
|
-
return `\n ${(0, exports.bold)("run")} ${String(input.command ?? "")}\n`;
|
|
213
|
-
}
|
|
214
|
-
if (name === "install_dependency") {
|
|
215
|
-
return `\n ${(0, exports.bold)("install")} ${String(input.package ?? "")}`
|
|
216
|
-
+ ` ${(0, exports.dim)("(this changes the lockfile for everyone who works here)")}\n`;
|
|
217
|
-
}
|
|
218
|
-
if (name === "delete_file") {
|
|
219
|
-
return head + ` ${(0, exports.dim)("A copy is kept, so this can be undone.")}\n`;
|
|
220
|
-
}
|
|
221
|
-
return head;
|
|
222
|
-
}
|
|
223
|
-
/** What was stopped when a session ended, one line each (manual 5.13). */
|
|
224
|
-
function stoppedLines(stopped) {
|
|
225
|
-
if (!stopped.length)
|
|
226
|
-
return "";
|
|
227
|
-
return (0, exports.dim)(` stopped the background process${stopped.length === 1 ? "" : "es"} this session started:\n`)
|
|
228
|
-
+ stopped.map((l) => (0, exports.dim)(` ${l}\n`)).join("");
|
|
229
|
-
}
|
|
230
|
-
/** The first line of a finished job, as the service wrote it (manual 5.15).
|
|
231
|
-
* Colored by its first word only; the words are the service's. */
|
|
232
|
-
function ending(line, detail) {
|
|
233
|
-
const paint = /^Done$/.test(line) ? exports.green : /^Done,/.test(line) ? exports.yellow
|
|
234
|
-
: /^(Stopped|Incomplete)/.test(line) ? exports.red : (t) => t;
|
|
235
|
-
// The service may put the same words in both; they are said once.
|
|
236
|
-
const extra = detail && detail !== line ? (0, exports.dim)(` ${detail}`) : "";
|
|
237
|
-
return `\n${paint(line)}${extra}\n`;
|
|
238
|
-
}
|
|
239
|
-
/** How the job ended, in the words a person reads. */
|
|
240
|
-
function outcome(o, detail) {
|
|
241
|
-
// A clean finish prints no word: the answer above says what was done.
|
|
242
|
-
if (o === "finished")
|
|
243
|
-
return detail ? `\n${(0, exports.dim)(detail)}\n` : "";
|
|
244
|
-
const word = {
|
|
245
|
-
cancelled: (0, exports.yellow)("Stopped"),
|
|
246
|
-
step_limit: (0, exports.yellow)("Stopped after too many steps"),
|
|
247
|
-
spend_limit: (0, exports.yellow)("Stopped at the spending limit"),
|
|
248
|
-
failed: (0, exports.red)("Could not finish"),
|
|
249
|
-
refused: (0, exports.red)("Refused"),
|
|
250
|
-
busy: (0, exports.yellow)("Busy"),
|
|
251
|
-
unknown: (0, exports.yellow)("Outcome unknown"),
|
|
252
|
-
};
|
|
253
|
-
return `\n${word[o] ?? o}${detail ? (0, exports.dim)(` ${detail}`) : ""}\n`;
|
|
254
|
-
}
|
|
255
|
-
/**
|
|
256
|
-
* A link a person can click, where the terminal supports one.
|
|
257
|
-
*
|
|
258
|
-
* Modern terminals understand an escape that carries an address behind a piece
|
|
259
|
-
* of text; older ones print it as ordinary characters, so the address is shown
|
|
260
|
-
* in words there instead. Never a bare escape with no fallback: a receipt whose
|
|
261
|
-
* proof is unreachable is not proof.
|
|
262
|
-
*/
|
|
263
|
-
function link(text, url) {
|
|
264
|
-
if (!process.stdout.isTTY)
|
|
265
|
-
return `${text} (${url})`;
|
|
266
|
-
return `]8;;${url}${text}]8;;`;
|
|
267
|
-
}
|
|
268
|
-
/**
|
|
269
|
-
* A file the agent made, as a link that opens it, with where it lives beneath.
|
|
270
|
-
*
|
|
271
|
-
* The name alone is not enough: somebody who asked from their home folder for a
|
|
272
|
-
* diagram cannot tell which folder "saved diagram.png" means. So the full path
|
|
273
|
-
* is written out underneath in gray, where it can be copied into Finder or a
|
|
274
|
-
* terminal, and the name above it opens the file on a click.
|
|
275
|
-
*/
|
|
276
|
-
function savedFile(name, fullPath, bytes) {
|
|
277
|
-
const href = (0, url_1.pathToFileURL)(fullPath).href;
|
|
278
|
-
return `${(0, exports.green)(" saved ")}${link(name, href)}${(0, exports.dim)(` ${bytes} bytes`)}\n`
|
|
279
|
-
+ `${(0, exports.gray)(` ${fullPath}`)}\n`;
|
|
280
|
-
}
|
|
281
|
-
/**
|
|
282
|
-
* The savings table as aligned columns: names left, money right, the saving
|
|
283
|
-
* column bright and the rest quiet. Every word and figure is the server's.
|
|
284
|
-
* Too narrow for the headings, it keeps the rows and drops the heading line
|
|
285
|
-
* rather than cutting a word.
|
|
286
|
-
*/
|
|
287
|
-
function savingsRows(savings, room) {
|
|
288
|
-
if (!savings || !savings.rows.length)
|
|
289
|
-
return [];
|
|
290
|
-
const body = savings.rows.map((r) => [r.label, r.user, r.chosen, r.saving]);
|
|
291
|
-
const [c0 = "", c1 = "", c2 = "", c3 = ""] = savings.columns;
|
|
292
|
-
const heading = [c0, c1, c2, c3];
|
|
293
|
-
const fit = (rows) => [0, 1, 2, 3].map((i) => " ".repeat(Math.max(...rows.map((r) => r[i].length))));
|
|
294
|
-
let w = fit([...body, heading]).map((x) => x.length);
|
|
295
|
-
const head = w.reduce((a, b) => a + b, 0) + 6 <= room;
|
|
296
|
-
if (!head)
|
|
297
|
-
w = fit(body).map((x) => x.length);
|
|
298
|
-
const [w0 = 0, w1 = 0, w2 = 0, w3 = 0] = w;
|
|
299
|
-
const left = (r) => `${r[0].padEnd(w0)} ${r[1].padStart(w1)} ${r[2].padStart(w2)}`;
|
|
300
|
-
const out = [""];
|
|
301
|
-
if (head)
|
|
302
|
-
out.push((0, exports.dim)(`${left(heading)} ${heading[3].padStart(w3)}`));
|
|
303
|
-
for (const r of body)
|
|
304
|
-
out.push(`${(0, exports.dim)(left(r))} ${(0, exports.bold)(r[3].padStart(w3))}`);
|
|
305
|
-
out.push((0, exports.bold)(`${savings.total.label.padEnd(w0 + w1 + w2 + 4)} ${savings.total.value.padStart(w3)}`));
|
|
306
|
-
return out;
|
|
307
|
-
}
|
|
308
|
-
/** The card's two lines, as the approved look draws them. */
|
|
309
|
-
function cardRows(card) {
|
|
310
|
-
const rows = [];
|
|
311
|
-
const head = [card.title ? (0, exports.bold)(card.title) : "", card.badge ? (0, exports.violet)(card.badge) : "",
|
|
312
|
-
card.vs ? (0, exports.dim)(card.vs) : ""].filter(Boolean).join(" ");
|
|
313
|
-
if (head)
|
|
314
|
-
rows.push(head);
|
|
315
|
-
if (card.amount)
|
|
316
|
-
rows.push(card.saved ? (0, exports.bold)((0, exports.green)(card.amount)) : (0, exports.dim)(card.amount));
|
|
317
|
-
if (card.engine)
|
|
318
|
-
rows.push((0, exports.dim)(card.engine.line));
|
|
319
|
-
if (card.warning)
|
|
320
|
-
rows.push((0, exports.dim)(card.warning));
|
|
321
|
-
return rows;
|
|
322
|
-
}
|
|
323
|
-
/**
|
|
324
|
-
* The receipt under a finished job.
|
|
325
|
-
*
|
|
326
|
-
* ── The terminal does not compute this, and must not ──────────────────────
|
|
327
|
-
*
|
|
328
|
-
* This used to assemble its own sentence from four loose numbers, so the
|
|
329
|
-
* terminal quoted a different receipt from the browser and the desktop app for
|
|
330
|
-
* the same job: no models, no comparison model and no saving, which is the
|
|
331
|
-
* entire claim the product is sold on.
|
|
332
|
-
*
|
|
333
|
-
* Our server owns the receipt. It holds the price list, the comparison model
|
|
334
|
-
* and the arithmetic, it writes the wording once, and every client shows that
|
|
335
|
-
* wording. All this function does is make it read well in a terminal: indent
|
|
336
|
-
* it, quieten it, and turn the address in it into something clickable. That is
|
|
337
|
-
* formatting, which belongs to the client, and it is the only part that does.
|
|
338
|
-
*/
|
|
339
|
-
function receipt(text, models = [], testsPassed, card) {
|
|
340
|
-
const lines = card
|
|
341
|
-
? [...cardRows(card), ...savingsRows(card.savings, width() - 4)].map((r) => ` ${r}`) : [];
|
|
342
|
-
for (const line of card ? [] : String(text || "").split("\n")) {
|
|
343
|
-
if (!line.trim())
|
|
344
|
-
continue;
|
|
345
|
-
// The address our server put in the sentence becomes a real link, and the
|
|
346
|
-
// words around it are left exactly as they were written.
|
|
347
|
-
const shown = line.replace(/\[([^\]]+)\]\(([^)]+)\)/g, (_m, label, url) => link(String(label), String(url)));
|
|
348
|
-
lines.push((0, exports.dim)(` ${shown}`));
|
|
349
|
-
}
|
|
350
|
-
// Which models did the work, cheapest first, because the cheap ones doing
|
|
351
|
-
// most of the steps IS the mechanism and a reader should see it at a glance.
|
|
352
|
-
const priced = models.filter((m) => m.model);
|
|
353
|
-
if (priced.length > 1) {
|
|
354
|
-
const order = [...priced].sort((a, b) => (a.usd ?? 0) - (b.usd ?? 0));
|
|
355
|
-
const width = Math.max(...order.map((m) => String(m.model).length));
|
|
356
|
-
for (const m of order) {
|
|
357
|
-
const steps = `${m.steps} step${m.steps === 1 ? "" : "s"}`;
|
|
358
|
-
const cost = m.usd === null || m.usd === undefined
|
|
359
|
-
// Never a zero standing in for a price nobody has. A step we could not
|
|
360
|
-
// price is said so, because a total that hides one is a smaller total.
|
|
361
|
-
? "not priced"
|
|
362
|
-
: money(m.usd);
|
|
363
|
-
lines.push((0, exports.dim)(` ${String(m.model).padEnd(width)} ${steps.padEnd(9)} ${cost}`));
|
|
364
|
-
}
|
|
365
|
-
}
|
|
366
|
-
if (testsPassed !== undefined) {
|
|
367
|
-
lines.push(testsPassed ? (0, exports.green)(" tests passed") : (0, exports.red)(" tests failed"));
|
|
368
|
-
}
|
|
369
|
-
if (!lines.length)
|
|
370
|
-
return "";
|
|
371
|
-
// On a screen, the receipt is a highlighted box, as on the website. Piped to
|
|
372
|
-
// a file or another program it stays plain lines, which is what a log wants.
|
|
373
|
-
if (card) {
|
|
374
|
-
// The link the server put in its sentence still goes last.
|
|
375
|
-
const found = /\[([^\]]+)\]\(([^)]+)\)/.exec(String(text || ""));
|
|
376
|
-
if (found)
|
|
377
|
-
lines.push((0, exports.dim)(` ${link(String(card.details || found[1]), String(found[2]))}`));
|
|
378
|
-
}
|
|
379
|
-
return process.stdout.isTTY ? boxed(text, models, testsPassed, card) : `${lines.join("\n")}\n`;
|
|
380
|
-
}
|
|
381
|
-
/** Words wrapped to a width, never breaking a word. */
|
|
382
|
-
function wrap(text, room) {
|
|
383
|
-
const out = [];
|
|
384
|
-
let line = "";
|
|
385
|
-
for (const word of text.split(/\s+/).filter(Boolean)) {
|
|
386
|
-
if (line && line.length + 1 + word.length > room) {
|
|
387
|
-
out.push(line);
|
|
388
|
-
line = word;
|
|
389
|
-
}
|
|
390
|
-
else
|
|
391
|
-
line = line ? `${line} ${word}` : word;
|
|
392
|
-
}
|
|
393
|
-
if (line)
|
|
394
|
-
out.push(line);
|
|
395
|
-
return out;
|
|
396
|
-
}
|
|
397
|
-
/**
|
|
398
|
-
* The receipt in a rounded box: `receipt` as its label, the saving in green,
|
|
399
|
-
* everything else quiet. The server's words are unchanged; each clause it
|
|
400
|
-
* separated with `|` gets its own line, and its link goes last, clickable.
|
|
401
|
-
*/
|
|
402
|
-
function boxed(text, models, testsPassed, card) {
|
|
403
|
-
// Room for the border, the padding and the `receipt` label on the first line.
|
|
404
|
-
const inner = width() - 16;
|
|
405
|
-
const rows = card ? [...cardRows(card), ...savingsRows(card.savings, inner)] : [];
|
|
406
|
-
const links = [];
|
|
407
|
-
for (const raw of String(text || "").split("\n")) {
|
|
408
|
-
if (card) {
|
|
409
|
-
// The card carries the words; only the sentence's link is kept.
|
|
410
|
-
raw.replace(/\[([^\]]+)\]\(([^)]+)\)/g, (_m, label, url) => {
|
|
411
|
-
links.push({ label: String(card.details || label), url: String(url) });
|
|
412
|
-
return "";
|
|
413
|
-
});
|
|
414
|
-
continue;
|
|
415
|
-
}
|
|
416
|
-
const withoutLinks = raw.replace(/\[([^\]]+)\]\(([^)]+)\)/g, (_m, label, url) => {
|
|
417
|
-
links.push({ label: String(label), url: String(url) });
|
|
418
|
-
return "";
|
|
419
|
-
});
|
|
420
|
-
for (const clause of withoutLinks.split("|").map((c) => c.trim()).filter(Boolean)) {
|
|
421
|
-
const paintIt = /\bsaved\b/i.test(clause) ? exports.green : exports.dim;
|
|
422
|
-
for (const piece of wrap(clause, inner))
|
|
423
|
-
rows.push(paintIt(piece));
|
|
424
|
-
}
|
|
425
|
-
}
|
|
426
|
-
const priced = models.filter((m) => m.model);
|
|
427
|
-
if (priced.length > 1) {
|
|
428
|
-
const order = [...priced].sort((a, b) => (a.usd ?? 0) - (b.usd ?? 0));
|
|
429
|
-
const w = Math.max(...order.map((m) => modelName(String(m.model)).length));
|
|
430
|
-
for (const m of order) {
|
|
431
|
-
const steps = `${m.steps} step${m.steps === 1 ? "" : "s"}`;
|
|
432
|
-
const cost = m.usd === null || m.usd === undefined ? "not priced" : money(m.usd);
|
|
433
|
-
rows.push(`${(0, exports.violet)(modelName(String(m.model)).padEnd(w))}${(0, exports.dim)(` ${steps.padEnd(9)} ${cost}`)}`);
|
|
434
|
-
}
|
|
435
|
-
}
|
|
436
|
-
if (testsPassed !== undefined)
|
|
437
|
-
rows.push(testsPassed ? (0, exports.green)("tests passed") : (0, exports.red)("tests failed"));
|
|
438
|
-
for (const l of links)
|
|
439
|
-
rows.push((0, exports.dim)(link(l.label, l.url)));
|
|
440
|
-
if (rows.length)
|
|
441
|
-
rows[0] = `${(0, exports.violet)("receipt")} ${rows[0]}`;
|
|
442
|
-
const widest = Math.max(...rows.map((r) => visible(r).length));
|
|
443
|
-
const bar = "\u2500".repeat(widest + 2);
|
|
444
|
-
const body = rows.map((r) => ` ${(0, exports.violet)("\u2502")} ${r}${" ".repeat(widest - visible(r).length)} ${(0, exports.violet)("\u2502")}`);
|
|
445
|
-
return [` ${(0, exports.violet)(`\u256d${bar}\u256e`)}`, ...body, ` ${(0, exports.violet)(`\u2570${bar}\u256f`)}`].join("\n") + "\n";
|
|
446
|
-
}
|
|
447
|
-
/** A markdown table's cells, with emphasis and code marks taken off and a
|
|
448
|
-
* `<br>` kept as a line break inside the cell. */
|
|
449
|
-
function cells(row) {
|
|
450
|
-
return row.trim().replace(/^\|/, "").replace(/\|$/, "").split("|").map((c) => c.trim()
|
|
451
|
-
.replace(/\*\*([^*]+)\*\*/g, "$1").replace(/__([^_]+)__/g, "$1").replace(/`([^`]+)`/g, "$1")
|
|
452
|
-
.replace(/<br\s*\/?>/gi, "\n"));
|
|
453
|
-
}
|
|
454
|
-
/** Widths that fit the terminal: each column its natural width when they all
|
|
455
|
-
* fit, otherwise the narrow ones keep theirs and the wide ones share the rest. */
|
|
456
|
-
function fitWidths(natural, room) {
|
|
457
|
-
const out = natural.map(() => 0);
|
|
458
|
-
let left = room;
|
|
459
|
-
let open = natural.map((_, i) => i).sort((a, b) => natural[a] - natural[b]);
|
|
460
|
-
while (open.length) {
|
|
461
|
-
const share = Math.floor(left / open.length);
|
|
462
|
-
const i = open[0];
|
|
463
|
-
if (natural[i] <= share) {
|
|
464
|
-
out[i] = natural[i];
|
|
465
|
-
left -= natural[i];
|
|
466
|
-
open = open.slice(1);
|
|
467
|
-
continue;
|
|
468
|
-
}
|
|
469
|
-
for (const j of open)
|
|
470
|
-
out[j] = Math.max(6, share);
|
|
471
|
-
break;
|
|
472
|
-
}
|
|
473
|
-
return out;
|
|
474
|
-
}
|
|
475
|
-
/** One cell's text as lines no wider than its column. */
|
|
476
|
-
function cellLines(text, w) {
|
|
477
|
-
const lines = [];
|
|
478
|
-
for (const part of text.split("\n")) {
|
|
479
|
-
const words = wrap(part, w);
|
|
480
|
-
for (const l of words.length ? words : [""]) {
|
|
481
|
-
// A single word longer than the column is cut, never allowed to push the border.
|
|
482
|
-
for (let k = 0; k < Math.max(1, l.length); k += w)
|
|
483
|
-
lines.push(l.slice(k, k + w));
|
|
484
|
-
}
|
|
485
|
-
}
|
|
486
|
-
return lines;
|
|
487
|
-
}
|
|
488
|
-
/**
|
|
489
|
-
* Markdown tables drawn as terminal tables.
|
|
490
|
-
*
|
|
491
|
-
* A model asked for "a summary table" writes pipes and dashes, which a browser
|
|
492
|
-
* draws and a terminal shows as raw markup. Each block of `|` rows with a
|
|
493
|
-
* `|---|` line under its header becomes a boxed table, with long cells wrapped
|
|
494
|
-
* inside their column so the table always fits the window.
|
|
495
|
-
*/
|
|
496
|
-
function tables(text) {
|
|
497
|
-
const lines = text.split("\n");
|
|
498
|
-
const out = [];
|
|
499
|
-
const isRow = (l) => l !== undefined && /^\s*\|.*\|\s*$/.test(l);
|
|
500
|
-
const isRule = (l) => l !== undefined && /^\s*\|?\s*:?-{2,}:?\s*(\|\s*:?-{2,}:?\s*)*\|?\s*$/.test(l);
|
|
501
|
-
for (let i = 0; i < lines.length; i++) {
|
|
502
|
-
if (!(isRow(lines[i]) && isRule(lines[i + 1]))) {
|
|
503
|
-
out.push(lines[i]);
|
|
504
|
-
continue;
|
|
505
|
-
}
|
|
506
|
-
const head = cells(lines[i]);
|
|
507
|
-
const body = [];
|
|
508
|
-
i += 2;
|
|
509
|
-
while (isRow(lines[i]))
|
|
510
|
-
body.push(cells(lines[i++]));
|
|
511
|
-
i--;
|
|
512
|
-
const cols = head.length;
|
|
513
|
-
const rows = [head, ...body].map((r) => Array.from({ length: cols }, (_, k) => r[k] ?? ""));
|
|
514
|
-
const natural = Array.from({ length: cols }, (_, k) => Math.max(3, ...rows.map((r) => Math.max(...r[k].split("\n").map((s) => s.length)))));
|
|
515
|
-
const w = fitWidths(natural, width() - 4 - (3 * cols + 1));
|
|
516
|
-
const bar = (l, m, r) => (0, exports.dim)(` ${l}${w.map((n) => "─".repeat(n + 2)).join(m)}${r}`);
|
|
517
|
-
const draw = (r, strong) => {
|
|
518
|
-
const wrapped = r.map((c, k) => cellLines(c, w[k]));
|
|
519
|
-
const height = Math.max(...wrapped.map((c) => c.length));
|
|
520
|
-
for (let h = 0; h < height; h++) {
|
|
521
|
-
out.push(` ${(0, exports.dim)("│")}${wrapped.map((c, k) => {
|
|
522
|
-
const s = (c[h] ?? "").padEnd(w[k]);
|
|
523
|
-
return ` ${strong ? (0, exports.bold)(s) : s} `;
|
|
524
|
-
}).join((0, exports.dim)("│"))}${(0, exports.dim)("│")}`);
|
|
525
|
-
}
|
|
526
|
-
};
|
|
527
|
-
out.push(bar("┌", "┬", "┐"));
|
|
528
|
-
draw(rows[0], true);
|
|
529
|
-
out.push(bar("├", "┼", "┤"));
|
|
530
|
-
rows.slice(1).forEach((r, n) => {
|
|
531
|
-
draw(r, false);
|
|
532
|
-
if (n < rows.length - 2)
|
|
533
|
-
out.push(bar("├", "┼", "┤"));
|
|
534
|
-
});
|
|
535
|
-
out.push(bar("└", "┴", "┘"));
|
|
536
|
-
}
|
|
537
|
-
return out.join("\n");
|
|
538
|
-
}
|
|
539
|
-
/** The answer's two accents, one pair per terminal background (manual 6.10):
|
|
540
|
-
* the brand violet and amber on a dark terminal, the brand purple and deep
|
|
541
|
-
* blue on a light one, each above 4.5 to 1 against its background. */
|
|
542
|
-
const onLight = () => process.env.COMPRISM_THEME === "light";
|
|
543
|
-
const headingPaint = (t) => paint(onLight() ? "1;38;2;107;91;167" : "1;38;5;141", t);
|
|
544
|
-
const codePaint = (t) => paint(onLight() ? "38;2;31;77;120" : "38;2;217;119;6", t);
|
|
545
|
-
/** Characters of text per line, the width Glow and Claude Code read at. */
|
|
546
|
-
const READING_WIDTH = 80;
|
|
547
|
-
const same = (t) => t;
|
|
548
|
-
/** A line of markdown as words, each piece carrying its own emphasis, so a
|
|
549
|
-
* wrapped line never starts inside a color it cannot see the start of. */
|
|
550
|
-
function words(text) {
|
|
551
|
-
const linked = text.replace(/\[([^\]]+)\]\(([^)]+)\)/g, (_m, t, u) => `${t} (${u})`);
|
|
552
|
-
const parts = [];
|
|
553
|
-
const re = /(\*\*[^*]+\*\*|__[^_]+__|`[^`]+`|\*[^*\s][^*]*\*)/g;
|
|
554
|
-
let last = 0;
|
|
555
|
-
for (let m = re.exec(linked); m; m = re.exec(linked)) {
|
|
556
|
-
parts.push({ t: linked.slice(last, m.index), p: same });
|
|
557
|
-
const s = m[0];
|
|
558
|
-
parts.push(s.startsWith("`") ? { t: s.slice(1, -1), p: codePaint }
|
|
559
|
-
: s.startsWith("**") || s.startsWith("__") ? { t: s.slice(2, -2), p: exports.bold }
|
|
560
|
-
: { t: s.slice(1, -1), p: (t) => paint("3", t) });
|
|
561
|
-
last = m.index + s.length;
|
|
562
|
-
}
|
|
563
|
-
parts.push({ t: linked.slice(last), p: same });
|
|
564
|
-
const out = [];
|
|
565
|
-
let cur = [];
|
|
566
|
-
for (const part of parts) {
|
|
567
|
-
for (const piece of part.t.split(/(\s+)/)) {
|
|
568
|
-
if (!piece)
|
|
569
|
-
continue;
|
|
570
|
-
if (/^\s+$/.test(piece)) {
|
|
571
|
-
if (cur.length) {
|
|
572
|
-
out.push(cur);
|
|
573
|
-
cur = [];
|
|
574
|
-
}
|
|
575
|
-
}
|
|
576
|
-
else
|
|
577
|
-
cur.push({ t: piece, p: part.p });
|
|
578
|
-
}
|
|
579
|
-
}
|
|
580
|
-
if (cur.length)
|
|
581
|
-
out.push(cur);
|
|
582
|
-
return out;
|
|
583
|
-
}
|
|
584
|
-
const show = (w) => w.map((x) => x.p(x.t)).join("");
|
|
585
|
-
const size = (w) => w.reduce((n, x) => n + x.t.length, 0);
|
|
586
|
-
/** Words filled into lines no wider than `room`, a word never split, every
|
|
587
|
-
* line after the first starting under the first line's text. */
|
|
588
|
-
function fill(text, lead, hang, room) {
|
|
589
|
-
const lines = [];
|
|
590
|
-
let line = lead, used = visible(lead).length, empty = true;
|
|
591
|
-
for (const w of words(text)) {
|
|
592
|
-
if (!empty && used + 1 + size(w) > room) {
|
|
593
|
-
lines.push(line);
|
|
594
|
-
line = hang;
|
|
595
|
-
used = visible(hang).length;
|
|
596
|
-
empty = true;
|
|
597
|
-
}
|
|
598
|
-
line += (empty ? "" : " ") + show(w);
|
|
599
|
-
used += (empty ? 0 : 1) + size(w);
|
|
600
|
-
empty = false;
|
|
601
|
-
}
|
|
602
|
-
lines.push(line);
|
|
603
|
-
return lines;
|
|
604
|
-
}
|
|
605
|
-
/**
|
|
606
|
-
* An answer, laid out for reading in a terminal (manual 6.17).
|
|
607
|
-
*
|
|
608
|
-
* The approved design (docs/product/design/cli-answer, option B): the whole
|
|
609
|
-
* answer on one left margin behind a violet rail, so where the question ends
|
|
610
|
-
* and the answer begins is never in doubt; words wrapped at a reading width
|
|
611
|
-
* and never split by the window edge; a blank line between every block;
|
|
612
|
-
* headings in the brand violet; code in its own color; wrapped list items
|
|
613
|
-
* continuing under their own text. Piped to a file or another program, the
|
|
614
|
-
* same answer comes out without the rail or the color, as a log wants it.
|
|
615
|
-
*/
|
|
616
|
-
function plain(text) {
|
|
617
|
-
const screen = supportsColour();
|
|
618
|
-
const lead = screen ? ` ${paint(onLight() ? "38;2;107;91;167" : "38;5;141", "│")} ` : " ";
|
|
619
|
-
const railOnly = screen ? lead.trimEnd() : "";
|
|
620
|
-
const room = Math.min(width() - 1, visible(lead).length + READING_WIDTH);
|
|
621
|
-
const src = text.replace(/\r\n/g, "\n").split("\n");
|
|
622
|
-
const out = [];
|
|
623
|
-
const gap = () => { if (out.length && out[out.length - 1] !== railOnly)
|
|
624
|
-
out.push(railOnly); };
|
|
625
|
-
const isTableRow = (l) => l !== undefined && /^\s*\|.*\|\s*$/.test(l);
|
|
626
|
-
const startsBlock = (l) => /^\s*([-*+•]\s|\d+[.)]\s|#{1,6}\s|```|>|\|)/.test(l) || /^\s*([-*_])\s*\1\s*\1[\s\-*_]*$/.test(l);
|
|
627
|
-
for (let i = 0; i < src.length; i++) {
|
|
628
|
-
const l = src[i];
|
|
629
|
-
if (!l.trim())
|
|
630
|
-
continue;
|
|
631
|
-
if (/^\s*```/.test(l)) {
|
|
632
|
-
gap();
|
|
633
|
-
for (i++; i < src.length && !/^\s*```/.test(src[i]); i++)
|
|
634
|
-
out.push(`${lead} ${codePaint(src[i])}`);
|
|
635
|
-
continue;
|
|
636
|
-
}
|
|
637
|
-
const h = /^\s*#{1,6}\s+(.+?)\s*#*\s*$/.exec(l);
|
|
638
|
-
if (h) {
|
|
639
|
-
gap();
|
|
640
|
-
out.push(lead + headingPaint(h[1].replace(/\*\*/g, "")));
|
|
641
|
-
gap();
|
|
642
|
-
continue;
|
|
643
|
-
}
|
|
644
|
-
if (/^\s*([-*_])\s*\1\s*\1[\s\-*_]*$/.test(l)) {
|
|
645
|
-
gap();
|
|
646
|
-
continue;
|
|
647
|
-
}
|
|
648
|
-
if (isTableRow(l)) {
|
|
649
|
-
const block = [];
|
|
650
|
-
while (isTableRow(src[i]))
|
|
651
|
-
block.push(src[i++]);
|
|
652
|
-
i--;
|
|
653
|
-
gap();
|
|
654
|
-
// The table's own two space indent gives way to the rail, whether or
|
|
655
|
-
// not a color code comes before it.
|
|
656
|
-
const indent = new RegExp(`^((?:${String.fromCharCode(27)}\\[[0-9;]*m)*) {2}`);
|
|
657
|
-
for (const row of tables(block.join("\n")).split("\n"))
|
|
658
|
-
out.push(lead + row.replace(indent, "$1"));
|
|
659
|
-
gap();
|
|
660
|
-
continue;
|
|
661
|
-
}
|
|
662
|
-
const bullet = /^(\s*)([-*+•]|\d+[.)])\s+(.*)$/.exec(l);
|
|
663
|
-
if (bullet) {
|
|
664
|
-
if (out.length && !/^\s*([-*+•]|\d+[.)])\s/.test(src[i - 1] ?? ""))
|
|
665
|
-
gap();
|
|
666
|
-
const depth = Math.floor(bullet[1].replace(/\t/g, " ").length / 2);
|
|
667
|
-
const mark = /\d/.test(bullet[2]) ? `${bullet[2].replace(")", ".")} ` : "• ";
|
|
668
|
-
let body = bullet[3];
|
|
669
|
-
// A list item that runs onto an indented next line is one item.
|
|
670
|
-
while (i + 1 < src.length && src[i + 1].trim() && /^\s{2,}\S/.test(src[i + 1]) && !startsBlock(src[i + 1]))
|
|
671
|
-
body += " " + src[++i].trim();
|
|
672
|
-
const indent = " ".repeat(depth);
|
|
673
|
-
out.push(...fill(body, lead + indent + (0, exports.dim)(mark), lead + indent + " ".repeat(mark.length), room));
|
|
674
|
-
if (i + 1 < src.length && !/^\s*([-*+•]|\d+[.)])\s/.test(src[i + 1]))
|
|
675
|
-
gap();
|
|
676
|
-
continue;
|
|
677
|
-
}
|
|
678
|
-
// A paragraph is every line up to the next blank line or block.
|
|
679
|
-
let para = l.trim().replace(/^>\s?/, "");
|
|
680
|
-
while (i + 1 < src.length && src[i + 1].trim() && !startsBlock(src[i + 1]))
|
|
681
|
-
para += " " + src[++i].trim();
|
|
682
|
-
gap();
|
|
683
|
-
out.push(...fill(para, lead, lead, room));
|
|
684
|
-
gap();
|
|
685
|
-
}
|
|
686
|
-
while (out.length && out[out.length - 1] === railOnly)
|
|
687
|
-
out.pop();
|
|
688
|
-
return out.join("\n");
|
|
689
|
-
}
|