t3code-session-manager 0.0.0-stage → 0.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/.claude-plugin/marketplace.json +21 -0
- package/.claude-plugin/plugin.json +19 -0
- package/CHANGELOG.md +138 -0
- package/LICENSE +21 -0
- package/README.md +660 -2
- package/README.pt.md +616 -0
- package/bin/entry.d.mts +2 -0
- package/bin/entry.mjs +19 -0
- package/bin/t3sm.mjs +44 -0
- package/commands/import.md +30 -0
- package/commands/organize.md +36 -0
- package/commands/status.md +24 -0
- package/commands/sync.md +26 -0
- package/dist/apply/apply.js +993 -0
- package/dist/apply/claudeWriter.js +168 -0
- package/dist/apply/copyTranscript.js +116 -0
- package/dist/apply/importer.js +697 -0
- package/dist/apply/materialize.js +335 -0
- package/dist/apply/waves.js +108 -0
- package/dist/assistant/core.js +295 -0
- package/dist/assistant/deps.js +256 -0
- package/dist/assistant/i18n.js +38 -0
- package/dist/assistant/messages.en.js +494 -0
- package/dist/assistant/messages.pt.js +495 -0
- package/dist/assistant/runner.js +64 -0
- package/dist/assistant/steps.js +1362 -0
- package/dist/build.json +1 -0
- package/dist/cli/applyScreen.js +158 -0
- package/dist/cli/commands/accounts.js +254 -0
- package/dist/cli/commands/apply.js +199 -0
- package/dist/cli/commands/changelog.js +61 -0
- package/dist/cli/commands/doctor.js +105 -0
- package/dist/cli/commands/help.js +158 -0
- package/dist/cli/commands/merge.js +224 -0
- package/dist/cli/commands/organize.js +872 -0
- package/dist/cli/commands/plan.js +161 -0
- package/dist/cli/commands/projects.js +430 -0
- package/dist/cli/commands/quarantine.js +203 -0
- package/dist/cli/commands/recover.js +230 -0
- package/dist/cli/commands/rollback.js +281 -0
- package/dist/cli/commands/scan.js +117 -0
- package/dist/cli/commands/schedule.js +288 -0
- package/dist/cli/commands/start.js +45 -0
- package/dist/cli/commands/status.js +615 -0
- package/dist/cli/commands/sync.js +336 -0
- package/dist/cli/commands/update.js +398 -0
- package/dist/cli/errors.js +53 -0
- package/dist/cli/failures.js +91 -0
- package/dist/cli/flags.js +53 -0
- package/dist/cli/help.en.js +549 -0
- package/dist/cli/help.pt.js +566 -0
- package/dist/cli/helpText.js +211 -0
- package/dist/cli/helpTopics.js +58 -0
- package/dist/cli/locked.js +12 -0
- package/dist/cli/main.js +155 -0
- package/dist/cli/screen.js +3 -0
- package/dist/cli/ui.js +111 -0
- package/dist/cli/words.js +2 -0
- package/dist/core/activity.js +16 -0
- package/dist/core/changelog.js +47 -0
- package/dist/core/config.js +530 -0
- package/dist/core/fsutil.js +171 -0
- package/dist/core/glob.js +34 -0
- package/dist/core/history.js +32 -0
- package/dist/core/journal.js +274 -0
- package/dist/core/lock.js +337 -0
- package/dist/core/mask.js +31 -0
- package/dist/core/paths.js +68 -0
- package/dist/core/pid.js +101 -0
- package/dist/core/run.js +4 -0
- package/dist/core/semver.js +18 -0
- package/dist/core/sysroot.js +29 -0
- package/dist/core/types.js +229 -0
- package/dist/core/uuid.js +29 -0
- package/dist/core/version.js +72 -0
- package/dist/help/convert.md +20 -0
- package/dist/help/convert.pt.md +21 -0
- package/dist/help/cwd-unreadable.md +22 -0
- package/dist/help/cwd-unreadable.pt.md +22 -0
- package/dist/help/decisions.md +20 -0
- package/dist/help/decisions.pt.md +20 -0
- package/dist/help/errors.md +20 -0
- package/dist/help/errors.pt.md +22 -0
- package/dist/help/held.md +20 -0
- package/dist/help/held.pt.md +20 -0
- package/dist/help/history.md +27 -0
- package/dist/help/history.pt.md +29 -0
- package/dist/help/instance-unavailable.md +18 -0
- package/dist/help/instance-unavailable.pt.md +19 -0
- package/dist/help/not-imported.md +19 -0
- package/dist/help/not-imported.pt.md +19 -0
- package/dist/help/orphaned.md +19 -0
- package/dist/help/orphaned.pt.md +19 -0
- package/dist/help/project-mismatch.md +15 -0
- package/dist/help/project-mismatch.pt.md +15 -0
- package/dist/help/t3-native.md +22 -0
- package/dist/help/t3-native.pt.md +22 -0
- package/dist/help/updates.md +26 -0
- package/dist/help/updates.pt.md +28 -0
- package/dist/help/watcher.md +24 -0
- package/dist/help/watcher.pt.md +25 -0
- package/dist/i18n/cli.js +41 -0
- package/dist/i18n/lang.js +55 -0
- package/dist/i18n/messages.en.js +1140 -0
- package/dist/i18n/messages.pt.js +1149 -0
- package/dist/i18n/words.js +12 -0
- package/dist/ops/accounts.js +170 -0
- package/dist/ops/autoUpdate.js +223 -0
- package/dist/ops/discoverAccounts.js +343 -0
- package/dist/ops/doctor.js +811 -0
- package/dist/ops/elevated.js +47 -0
- package/dist/ops/errorCodes.js +39 -0
- package/dist/ops/follow.js +558 -0
- package/dist/ops/merge.js +830 -0
- package/dist/ops/notify.js +249 -0
- package/dist/ops/plugin.js +92 -0
- package/dist/ops/projects.js +113 -0
- package/dist/ops/quarantine.js +415 -0
- package/dist/ops/rollback.js +868 -0
- package/dist/ops/schedule.js +464 -0
- package/dist/ops/successor.js +51 -0
- package/dist/ops/sync.js +859 -0
- package/dist/ops/syncStatus.js +290 -0
- package/dist/ops/update.js +273 -0
- package/dist/ops/updater.js +641 -0
- package/dist/ops/watch.js +676 -0
- package/dist/ops/watcherControl.js +212 -0
- package/dist/ops/watcherHealth.js +75 -0
- package/dist/organize/apply.js +495 -0
- package/dist/organize/history.js +192 -0
- package/dist/organize/plan.js +283 -0
- package/dist/organize/rename.js +58 -0
- package/dist/organize/sessions.js +100 -0
- package/dist/organize/titles.js +70 -0
- package/dist/organize/types.js +73 -0
- package/dist/plan/categories.js +104 -0
- package/dist/plan/dedupe.js +137 -0
- package/dist/plan/folders.js +82 -0
- package/dist/plan/names.js +93 -0
- package/dist/plan/ownership.js +316 -0
- package/dist/plan/planner.js +669 -0
- package/dist/plan/rehome.js +101 -0
- package/dist/plan/report.js +407 -0
- package/dist/sources/cache.js +89 -0
- package/dist/sources/claude.js +297 -0
- package/dist/sources/codex.js +496 -0
- package/dist/sources/flatten.js +23 -0
- package/dist/sources/jsonl.js +169 -0
- package/dist/sources/native.js +233 -0
- package/dist/sources/opencode.js +483 -0
- package/dist/sources/pi.js +133 -0
- package/dist/sources/registry.js +67 -0
- package/dist/sources/types.js +80 -0
- package/dist/t3/api.js +492 -0
- package/dist/t3/auth.js +248 -0
- package/dist/t3/compat.js +62 -0
- package/dist/t3/decode.js +408 -0
- package/dist/t3/errors.js +185 -0
- package/dist/t3/rpc.js +185 -0
- package/dist/t3/runtime.js +80 -0
- package/dist/ui/caps.js +56 -0
- package/dist/ui/components.js +202 -0
- package/dist/ui/layout.js +205 -0
- package/dist/ui/list.js +423 -0
- package/dist/ui/locale.js +73 -0
- package/dist/ui/organizePreview.js +72 -0
- package/dist/ui/progress.js +237 -0
- package/dist/ui/style.js +32 -0
- package/dist/ui/terminal.js +126 -0
- package/dist/ui/text.js +186 -0
- package/package.json +58 -4
- package/skills/t3code-sessions/SKILL.md +183 -0
package/dist/t3/rpc.js
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import { RpcClosed, RpcError, RpcTimeout, T3AuthError, registerSecret } from "./errors.js";
|
|
2
|
+
import { parseLocalOrigin } from "./runtime.js";
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
/** ws URL per S1 §3. The ticket is URL-encoded; never log this URL. */
|
|
12
|
+
export function buildWsUrl(origin , ticket ) {
|
|
13
|
+
const base = parseLocalOrigin(origin);
|
|
14
|
+
if (!base) throw new T3AuthError("refusing to open a websocket to a non-local origin");
|
|
15
|
+
registerSecret(ticket);
|
|
16
|
+
const u = new URL("/ws", base);
|
|
17
|
+
u.protocol = base.protocol === "https:" ? "wss:" : "ws:";
|
|
18
|
+
u.search = `?orchestrationProtocol=2&wsTicket=${encodeURIComponent(ticket)}`;
|
|
19
|
+
return u.href;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Node's global WebSocket. Rejects after 10 s or on a failed open (message never includes the URL/ticket). */
|
|
23
|
+
export function webSocketTransport(url ) {
|
|
24
|
+
return new Promise((resolve, reject) => {
|
|
25
|
+
let ws ;
|
|
26
|
+
try { ws = new WebSocket(url); ws.binaryType = "arraybuffer"; } catch { reject(new RpcClosed("could not open websocket to T3 Code")); return; }
|
|
27
|
+
let opened = false;
|
|
28
|
+
let frameCb = () => {};
|
|
29
|
+
let closeCb = () => {};
|
|
30
|
+
const timer = setTimeout(() => { if (!opened) { try { ws.close(); } catch { /* ignore */ } reject(new RpcClosed("websocket connect timed out after 10 s")); } }, 10_000);
|
|
31
|
+
ws.addEventListener("open", () => {
|
|
32
|
+
opened = true;
|
|
33
|
+
clearTimeout(timer);
|
|
34
|
+
resolve({
|
|
35
|
+
send: (f) => ws.send(f),
|
|
36
|
+
onFrame: (cb) => { frameCb = cb; },
|
|
37
|
+
onClose: (cb) => { closeCb = cb; },
|
|
38
|
+
close: () => { try { ws.close(); } catch { /* ignore */ } },
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
ws.addEventListener("message", (ev) => frameCb(typeof ev.data === "string" ? ev.data : new TextDecoder("utf-8").decode(ev.data )));
|
|
42
|
+
ws.addEventListener("close", (ev) => {
|
|
43
|
+
clearTimeout(timer);
|
|
44
|
+
if (!opened) reject(new RpcClosed("websocket closed before opening (dead server, bad ticket or protocol mismatch)"));
|
|
45
|
+
else closeCb(ev.code);
|
|
46
|
+
});
|
|
47
|
+
ws.addEventListener("error", () => {
|
|
48
|
+
if (!opened) { clearTimeout(timer); reject(new RpcClosed("websocket failed to open (dead server, bad ticket or protocol mismatch)")); }
|
|
49
|
+
else closeCb(1006);
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
const SCAN_TIMEOUT_MS = 120_000;
|
|
63
|
+
const DEFAULT_TIMEOUT_MS = 15_000;
|
|
64
|
+
|
|
65
|
+
/** Minimal client for effect/rpc JSON frames (S1 §4). Unary calls only; streams are interrupted and rejected. */
|
|
66
|
+
export class RpcClient {
|
|
67
|
+
t ;
|
|
68
|
+
next = 0;
|
|
69
|
+
pending = new Map ();
|
|
70
|
+
closed = false;
|
|
71
|
+
|
|
72
|
+
constructor(t ) {
|
|
73
|
+
this.t = t;
|
|
74
|
+
t.onFrame((f) => this.handle(f));
|
|
75
|
+
t.onClose(() => this.failAll(() => new RpcClosed()));
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
get pendingCount() { return this.pending.size; }
|
|
79
|
+
|
|
80
|
+
call (tag , payload , decode , timeoutMs ) {
|
|
81
|
+
if (this.closed) return Promise.reject(new RpcClosed());
|
|
82
|
+
const ms = timeoutMs ?? (tag.startsWith("agentSessions.") ? SCAN_TIMEOUT_MS : DEFAULT_TIMEOUT_MS);
|
|
83
|
+
const id = String(++this.next);
|
|
84
|
+
return new Promise ((resolve, reject) => {
|
|
85
|
+
const timer = setTimeout(() => {
|
|
86
|
+
if (this.pending.delete(id)) {
|
|
87
|
+
reject(new RpcTimeout(tag, ms));
|
|
88
|
+
this.trySend({ _tag: "Interrupt", requestId: id });
|
|
89
|
+
}
|
|
90
|
+
}, ms);
|
|
91
|
+
this.pending.set(id, { tag, decode, resolve: resolve , reject, timer });
|
|
92
|
+
try {
|
|
93
|
+
this.t.send(JSON.stringify({ _tag: "Request", id, tag, payload, headers: [] }));
|
|
94
|
+
} catch (e) {
|
|
95
|
+
clearTimeout(timer);
|
|
96
|
+
this.pending.delete(id);
|
|
97
|
+
reject(new RpcClosed(`send failed: ${e instanceof Error ? e.message : String(e)}`));
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
close() {
|
|
103
|
+
if (this.closed) return;
|
|
104
|
+
this.closed = true;
|
|
105
|
+
this.failAll(() => new RpcClosed());
|
|
106
|
+
try { this.t.close(); } catch { /* ignore */ }
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
trySend(m ) {
|
|
110
|
+
try { this.t.send(JSON.stringify(m)); } catch { /* connection already gone */ }
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
take(id ) {
|
|
114
|
+
if (typeof id !== "string" && typeof id !== "number") return undefined;
|
|
115
|
+
const key = String(id);
|
|
116
|
+
const p = this.pending.get(key);
|
|
117
|
+
if (p) { clearTimeout(p.timer); this.pending.delete(key); }
|
|
118
|
+
return p;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
failAll(mk ) {
|
|
122
|
+
this.closed = true;
|
|
123
|
+
const all = [...this.pending.values()];
|
|
124
|
+
this.pending.clear();
|
|
125
|
+
for (const p of all) { clearTimeout(p.timer); p.reject(mk()); }
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
handle(frame ) {
|
|
129
|
+
let parsed ;
|
|
130
|
+
try { parsed = JSON.parse(frame); } catch { return; }
|
|
131
|
+
for (const m of Array.isArray(parsed) ? parsed : [parsed]) {
|
|
132
|
+
if (m && typeof m === "object") this.handleOne(m );
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
handleOne(m ) {
|
|
137
|
+
switch (m._tag) {
|
|
138
|
+
case "Exit": {
|
|
139
|
+
const p = this.take(m.requestId);
|
|
140
|
+
if (!p) return;
|
|
141
|
+
const exit = m.exit ;
|
|
142
|
+
if (exit?._tag === "Success") {
|
|
143
|
+
try { p.resolve(p.decode(exit.value)); } catch (e) { p.reject(e); }
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
const cause = Array.isArray(exit?.cause) ? (exit .cause ) : [];
|
|
147
|
+
const fail = cause.find((c) => c?._tag === "Fail");
|
|
148
|
+
const die = cause.find((c) => c?._tag === "Die");
|
|
149
|
+
if (fail) p.reject(new RpcError(p.tag, fail.error));
|
|
150
|
+
else if (die) p.reject(new RpcError(p.tag, { _tag: "Defect", defect: die.defect, message: typeof die.defect === "string" ? die.defect : (die.defect )?.message }));
|
|
151
|
+
else p.reject(new RpcError(p.tag, { _tag: cause[0]?._tag ?? "UnknownFailure" }));
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
case "Chunk": {
|
|
155
|
+
// t3sm uses no streams: stop it and fail the call.
|
|
156
|
+
const p = this.take(m.requestId);
|
|
157
|
+
if (!p) return;
|
|
158
|
+
this.trySend({ _tag: "Interrupt", requestId: m.requestId });
|
|
159
|
+
p.reject(new RpcError(p.tag, { _tag: "StreamUnsupported", message: "stream-unsupported" }));
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
case "Defect":
|
|
163
|
+
// Socket-level defect ends every pending request (S1 §4.4).
|
|
164
|
+
this.failAllWith(m.defect);
|
|
165
|
+
return;
|
|
166
|
+
case "ClientProtocolError": {
|
|
167
|
+
const ids = Array.isArray(m.requestIds) ? (m.requestIds ) : [];
|
|
168
|
+
for (const id of ids) {
|
|
169
|
+
const p = this.take(id);
|
|
170
|
+
if (p) p.reject(new RpcError(p.tag, { _tag: "ClientProtocolError", message: "client protocol error" }));
|
|
171
|
+
}
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
default: // Pong and anything unknown
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
failAllWith(defect ) {
|
|
180
|
+
const all = [...this.pending.values()];
|
|
181
|
+
this.pending.clear();
|
|
182
|
+
const message = typeof defect === "string" ? defect : (defect )?.message;
|
|
183
|
+
for (const p of all) { clearTimeout(p.timer); p.reject(new RpcError(p.tag, { _tag: "Defect", defect, message })); }
|
|
184
|
+
}
|
|
185
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
2
|
+
import { statSync } from "node:fs";
|
|
3
|
+
import { join, posix, resolve } from "node:path";
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
const LOCAL_HOSTS = new Set(["127.0.0.1", "::1", "[::1]", "localhost"]);
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Parse an origin and accept it only if it is a plain local http(s) origin: loopback host, no credentials,
|
|
11
|
+
* and (when `port` is given) the explicit/default port equals it. Anything else could make us send a
|
|
12
|
+
* bearer token to a foreign host.
|
|
13
|
+
*/
|
|
14
|
+
export function parseLocalOrigin(origin , port ) {
|
|
15
|
+
if (typeof origin !== "string") return null;
|
|
16
|
+
let u ;
|
|
17
|
+
try { u = new URL(origin); } catch { return null; }
|
|
18
|
+
if (u.protocol !== "http:" && u.protocol !== "https:") return null;
|
|
19
|
+
if (!LOCAL_HOSTS.has(u.hostname)) return null;
|
|
20
|
+
if (u.username !== "" || u.password !== "") return null;
|
|
21
|
+
if (port !== undefined) {
|
|
22
|
+
const eff = u.port !== "" ? Number(u.port) : u.protocol === "https:" ? 443 : 80;
|
|
23
|
+
if (eff !== port) return null;
|
|
24
|
+
}
|
|
25
|
+
return u;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* ~/.t3/userdata/server-runtime.json. The file is NOT removed when T3 Code quits (S1 §1),
|
|
30
|
+
* so its presence proves nothing: use probeServer / isT3Running for liveness.
|
|
31
|
+
* Returns null when missing, unparseable, malformed or pointing at a non-local origin.
|
|
32
|
+
*/
|
|
33
|
+
export async function readServerRuntime(home ) {
|
|
34
|
+
let raw ;
|
|
35
|
+
try {
|
|
36
|
+
raw = JSON.parse(await readFile(join(home, ".t3", "userdata", "server-runtime.json"), "utf8"));
|
|
37
|
+
} catch { return null; }
|
|
38
|
+
if (!raw || typeof raw !== "object") return null;
|
|
39
|
+
const o = raw ;
|
|
40
|
+
if (typeof o.origin !== "string" || typeof o.port !== "number" || typeof o.serverVersion !== "string" || typeof o.pid !== "number") return null;
|
|
41
|
+
const u = parseLocalOrigin(o.origin, o.port);
|
|
42
|
+
if (!u) return null;
|
|
43
|
+
return { origin: u.origin, port: o.port, serverVersion: o.serverVersion, pid: o.pid };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** GET /.well-known/t3/environment (no auth). True only on a 2xx answer within 2 s, from a local origin. */
|
|
47
|
+
export async function probeServer(rt , fetchImpl = fetch) {
|
|
48
|
+
const base = parseLocalOrigin(rt.origin);
|
|
49
|
+
if (!base) return false;
|
|
50
|
+
try {
|
|
51
|
+
const res = await fetchImpl(new URL("/.well-known/t3/environment", base).href, { signal: AbortSignal.timeout(2000), redirect: "error" });
|
|
52
|
+
return res.ok;
|
|
53
|
+
} catch { return false; }
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export async function isT3Running(home , fetchImpl = fetch) {
|
|
57
|
+
const rt = await readServerRuntime(home);
|
|
58
|
+
return rt !== null && (await probeServer(rt, fetchImpl));
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function isFile(p ) {
|
|
62
|
+
try { return statSync(p).isFile(); } catch { return false; }
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** ~/.t3/bin/t3(.cmd) first, then PATH (PATHEXT on win32). Returns an absolute path or null. */
|
|
66
|
+
export function resolveT3Cli(home , platform , pathEnv , pathExt = process.env.PATHEXT) {
|
|
67
|
+
const win = platform === "win32";
|
|
68
|
+
const own = join(home, ".t3", "bin", win ? "t3.cmd" : "t3");
|
|
69
|
+
if (isFile(own)) return resolve(own);
|
|
70
|
+
const exts = win ? (pathExt ?? ".COM;.EXE;.BAT;.CMD").split(";").filter(Boolean) : [""];
|
|
71
|
+
for (const dir of pathEnv.split(win ? ";" : posix.delimiter).filter(Boolean)) {
|
|
72
|
+
for (const ext of exts) {
|
|
73
|
+
for (const e of new Set([ext.toLowerCase(), ext])) {
|
|
74
|
+
const p = join(dir, `t3${e}`);
|
|
75
|
+
if (isFile(p)) return resolve(p);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return null;
|
|
80
|
+
}
|
package/dist/ui/caps.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
// What the terminal can do: colour depth, Unicode, TTY, width. Pure: the environment, platform and stream facts are
|
|
2
|
+
// passed in so every screen can be tested under any combination.
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Whether the terminal draws Unicode symbols. Windows: Windows Terminal, the VS Code terminal and ConEmu do; plain
|
|
13
|
+
* conhost often does not. Elsewhere: a UTF-8 locale (an unset one counts as UTF-8: every current terminal is).
|
|
14
|
+
* TERM=dumb never does.
|
|
15
|
+
*/
|
|
16
|
+
export function detectUnicode(env , platform ) {
|
|
17
|
+
if (env["TERM"] === "dumb") return false;
|
|
18
|
+
if (platform === "win32") return Boolean(env["WT_SESSION"]) || env["TERM_PROGRAM"] === "vscode" || env["ConEmuANSI"] === "ON";
|
|
19
|
+
const loc = env["LC_ALL"] || env["LC_CTYPE"] || env["LANG"];
|
|
20
|
+
if (loc === undefined || loc === "") return true;
|
|
21
|
+
return /utf-?8/i.test(loc);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Precedence: NO_COLOR and TERM=dumb (0) > FORCE_COLOR (0, 1/true, 2, 3; works without a TTY) > no TTY (0) > the depth
|
|
26
|
+
* the stream reports (`hasColors`) or COLORTERM / TERM / Windows Terminal hints, at least 1.
|
|
27
|
+
*/
|
|
28
|
+
export function detectCaps(
|
|
29
|
+
env ,
|
|
30
|
+
platform ,
|
|
31
|
+
s ,
|
|
32
|
+
) {
|
|
33
|
+
const off = env["TERM"] === "dumb" || Boolean(env["NO_COLOR"]);
|
|
34
|
+
return { color: off ? 0 : colorLevel(env, platform, s), unicode: detectUnicode(env, platform), isTTY: s.isTTY, columns: s.columns || 80, cursor: s.isTTY && env["TERM"] !== "dumb" };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function colorLevel(env , platform , s ) {
|
|
38
|
+
const force = env["FORCE_COLOR"];
|
|
39
|
+
if (force !== undefined && force !== "") {
|
|
40
|
+
if (force === "0" || force === "false") return 0;
|
|
41
|
+
if (force === "2") return 2;
|
|
42
|
+
if (force === "3") return 3;
|
|
43
|
+
return 1;
|
|
44
|
+
}
|
|
45
|
+
if (!s.isTTY) return 0;
|
|
46
|
+
if (typeof s.hasColors === "function") {
|
|
47
|
+
try {
|
|
48
|
+
if (s.hasColors(2 ** 24)) return 3;
|
|
49
|
+
if (s.hasColors(256)) return 2;
|
|
50
|
+
if (s.hasColors(16)) return 1;
|
|
51
|
+
} catch { /* fall through to the hints */ }
|
|
52
|
+
}
|
|
53
|
+
const ct = env["COLORTERM"];
|
|
54
|
+
if (ct === "truecolor" || ct === "24bit" || (platform === "win32" && env["WT_SESSION"])) return 3;
|
|
55
|
+
if (/256color/i.test(env["TERM"] ?? "")) return 2;
|
|
56
|
+
return 1;
|
|
57
|
+
}
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
// Terminal components: pure functions from (style, data, width) to lines. They never write, every returned line is at
|
|
2
|
+
// most `width` cells wide (`displayWidth`), no line has trailing spaces, there is no ESC when `s.caps.color` is 0, and
|
|
3
|
+
// every glyph comes from `s.sym`, so the ASCII fallback draws the same layout. Text that does not fit is cut with
|
|
4
|
+
// `s.sym.ellipsis`. Content passed in may carry ANSI (it is measured by its visible width); when such a line has to be
|
|
5
|
+
// wrapped or cut, its styling is dropped rather than split mid-sequence. Callers sanitise untrusted text (session
|
|
6
|
+
// titles, paths from disk) with `shownText` (src/plan/names.ts) before passing it in; here only line breaks and tabs
|
|
7
|
+
// are flattened.
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
import { displayWidth, padEnd, stripAnsi, truncateEnd, truncateMiddle, wrapText } from "./text.js";
|
|
11
|
+
|
|
12
|
+
const flat = (s ) => s.replace(/[\r\n\t]+/g, " ");
|
|
13
|
+
const rtrim = (s ) => s.replace(/ +$/, "");
|
|
14
|
+
const cap = (w ) => Math.max(1, Math.floor(w) || 1);
|
|
15
|
+
|
|
16
|
+
/** One logical line as lines of at most `w` cells: kept as is when it fits, else wrapped (plain). */
|
|
17
|
+
function wrapLine(line , w ) {
|
|
18
|
+
const t = flat(line);
|
|
19
|
+
return displayWidth(t) <= w ? [t] : wrapText(stripAnsi(t), w);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** `t3sm 0.3.0 · subtitle` over a rule. */
|
|
23
|
+
export function appHeader(s , o , width ) {
|
|
24
|
+
const w = cap(width);
|
|
25
|
+
const name = `t3sm ${o.version}`;
|
|
26
|
+
const sep = ` ${s.sym.sep} `;
|
|
27
|
+
const room = w - displayWidth(name) - displayWidth(sep);
|
|
28
|
+
let line ;
|
|
29
|
+
if (displayWidth(name) > w) line = s("title", truncateEnd(name, w, s.sym.ellipsis));
|
|
30
|
+
else if (o.subtitle && room >= 4) line = s("title", "t3sm") + " " + s("muted", o.version + sep + truncateEnd(flat(o.subtitle), room, s.sym.ellipsis));
|
|
31
|
+
else line = s("title", "t3sm") + " " + s("muted", o.version);
|
|
32
|
+
return [line, s("muted", s.sym.h.repeat(w))];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** `● ● ● ○ ○ Step 3 of 11 · Accounts`; under 60 columns (or when it cannot fit) `3/11 · Accounts`. */
|
|
36
|
+
export function stepHeader(
|
|
37
|
+
s ,
|
|
38
|
+
o ,
|
|
39
|
+
width ,
|
|
40
|
+
) {
|
|
41
|
+
const w = cap(width);
|
|
42
|
+
const sep = ` ${s.sym.sep} `;
|
|
43
|
+
const word = o.word;
|
|
44
|
+
const label = `${word.step} ${o.n} ${word.of} ${o.total}`;
|
|
45
|
+
const title = flat(o.title);
|
|
46
|
+
const railW = Math.max(0, 2 * o.total - 1);
|
|
47
|
+
const room = w - railW - 2;
|
|
48
|
+
if (w >= 60 && room >= displayWidth(label) + displayWidth(sep) + 4) {
|
|
49
|
+
const dots = [];
|
|
50
|
+
for (let i = 1; i <= o.total; i++) dots.push(i === o.n ? s("title", s.sym.cur) : o.done.includes(i) ? s("accent", s.sym.dot) : s("muted", s.sym.ring));
|
|
51
|
+
const text = truncateEnd(label + sep + title, room, s.sym.ellipsis);
|
|
52
|
+
return [dots.join(" ") + " " + s("title", text)];
|
|
53
|
+
}
|
|
54
|
+
const short = `${o.n}/${o.total}`;
|
|
55
|
+
const rest = w - displayWidth(short) - displayWidth(sep);
|
|
56
|
+
if (rest < 1) return [s("accent", truncateEnd(short, w, s.sym.ellipsis))];
|
|
57
|
+
return [s("accent", short) + s("muted", sep) + s("title", truncateEnd(title, rest, s.sym.ellipsis))];
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** A rounded box (`╭─ Title ──╮`) around `body`; the tone colours the border and puts a symbol before the title. */
|
|
61
|
+
export function panel(s , o , width ) {
|
|
62
|
+
const w = cap(width);
|
|
63
|
+
const tone = o.tone ?? "plain";
|
|
64
|
+
const tok = tone === "plain" ? "muted" : tone;
|
|
65
|
+
const toneSym = tone === "ok" ? s.sym.ok : tone === "warn" ? s.sym.warn : tone === "err" ? s.sym.err : "";
|
|
66
|
+
const heading = [toneSym, o.title ? flat(o.title) : ""].filter((x) => x !== "").join(" ");
|
|
67
|
+
const sym = s.sym;
|
|
68
|
+
if (w < 6) return [...(heading ? [truncateEnd(heading, w, sym.ellipsis)] : []), ...o.body.flatMap((b) => wrapLine(b, w))];
|
|
69
|
+
const inner = w - 4;
|
|
70
|
+
// A title too long for the top border is wrapped at a word, never cut: the rest opens the body, in bold. A title made
|
|
71
|
+
// of parts ("Plan R1 · T3 Code 0.1.9") breaks between its parts first, so a part ("T3 Code") stays together.
|
|
72
|
+
const parts = heading.split(` ${sym.sep} `);
|
|
73
|
+
const titleLines = heading === "" ? []
|
|
74
|
+
: displayWidth(heading) > w - 6 && parts.length > 1 ? parts.flatMap((x, i) => wrapText(x, i === 0 ? w - 6 : inner))
|
|
75
|
+
: wrapText(heading, w - 6);
|
|
76
|
+
const first = titleLines[0] ?? "";
|
|
77
|
+
const top = heading !== ""
|
|
78
|
+
? s(tok, sym.tl + sym.h + " ") + s("title", first) + s(tok, " " + sym.h.repeat(Math.max(0, w - 5 - displayWidth(first))) + sym.tr)
|
|
79
|
+
: s(tok, sym.tl + sym.h.repeat(w - 2) + sym.tr);
|
|
80
|
+
const out = [top];
|
|
81
|
+
for (const l of titleLines.slice(1).flatMap((x) => wrapLine(x, inner))) out.push(s(tok, sym.v) + " " + padEnd(s("title", l), inner) + " " + s(tok, sym.v));
|
|
82
|
+
for (const b of o.body) for (const l of wrapLine(b, inner)) out.push(s(tok, sym.v) + " " + padEnd(l, inner) + " " + s(tok, sym.v));
|
|
83
|
+
out.push(s(tok, sym.bl + sym.h.repeat(w - 2) + sym.br));
|
|
84
|
+
return out;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Aligned `label value` rows. A label is never cut: the label column is as wide as the longest label, and values wrap
|
|
89
|
+
* under the value column. When that leaves the values less than 10 cells, every label stands over its value instead.
|
|
90
|
+
*/
|
|
91
|
+
export function kv(s , rows , width ) {
|
|
92
|
+
const w = cap(width);
|
|
93
|
+
if (rows.length === 0) return [];
|
|
94
|
+
const labels = rows.map(([k]) => flat(k));
|
|
95
|
+
const labelW = Math.max(...labels.map(displayWidth));
|
|
96
|
+
const valueW = w - labelW - 2;
|
|
97
|
+
// A value's longest word (an id, a path, a command) is never broken when stacking keeps it whole: only the rows that
|
|
98
|
+
// need it stand over their value; the others stay aligned.
|
|
99
|
+
const longest = (v ) => Math.max(0, ...stripAnsi(flat(v)).split(" ").map(displayWidth));
|
|
100
|
+
const out = [];
|
|
101
|
+
const ind = w >= 4 ? 2 : 0;
|
|
102
|
+
const stacked = (label , v ) => {
|
|
103
|
+
out.push(s("muted", truncateEnd(label, w, s.sym.ellipsis)));
|
|
104
|
+
for (const l of wrapLine(v, w - ind)) if (l !== "") out.push(" ".repeat(ind) + l);
|
|
105
|
+
};
|
|
106
|
+
if (valueW < 10) {
|
|
107
|
+
rows.forEach(([, v], i) => stacked(labels[i] , v));
|
|
108
|
+
return out.map(rtrim);
|
|
109
|
+
}
|
|
110
|
+
rows.forEach(([, v], i) => {
|
|
111
|
+
const word = longest(v);
|
|
112
|
+
if (word > valueW && word <= w - ind) { stacked(labels[i] , v); return; }
|
|
113
|
+
const label = truncateEnd(labels[i] , labelW, s.sym.ellipsis);
|
|
114
|
+
const lines = wrapLine(v, valueW);
|
|
115
|
+
out.push(rtrim(s("muted", label) + " ".repeat(labelW - displayWidth(label) + 2) + lines[0] ));
|
|
116
|
+
for (const l of lines.slice(1)) out.push(rtrim(" ".repeat(labelW + 2) + l));
|
|
117
|
+
});
|
|
118
|
+
return out;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
/** A table fitted to `width`: whole low-priority columns are dropped first, then the widest columns are cut. Never wraps. */
|
|
133
|
+
export function table(s , cols , rows , width ) {
|
|
134
|
+
const w = cap(width);
|
|
135
|
+
if (cols.length === 0) return [];
|
|
136
|
+
const GAP = 2;
|
|
137
|
+
const cells = cols.map((c) => rows.map((r) => flat(c.get(r))));
|
|
138
|
+
const natural = cols.map((c, i) => Math.max(displayWidth(flat(c.header)), ...cells[i] .map(displayWidth)));
|
|
139
|
+
const min = cols.map((c, i) => Math.max(1, Math.min(natural[i] , c.min ?? (c.align === "right" ? natural[i] : c.truncate === "middle" ? 16 : 8))));
|
|
140
|
+
let visible = cols.map((_, i) => i);
|
|
141
|
+
const need = (v ) => v.reduce((a, i) => a + min[i] , 0) + GAP * (v.length - 1);
|
|
142
|
+
while (visible.length > 1 && need(visible) > w) {
|
|
143
|
+
let drop = visible[0] ;
|
|
144
|
+
for (const i of visible) if ((cols[i] .priority ?? 0) < (cols[drop] .priority ?? 0) || ((cols[i] .priority ?? 0) === (cols[drop] .priority ?? 0) && i > drop)) drop = i;
|
|
145
|
+
visible = visible.filter((i) => i !== drop);
|
|
146
|
+
}
|
|
147
|
+
const widths = new Map (visible.map((i) => [i, natural[i] ]));
|
|
148
|
+
let excess = visible.reduce((a, i) => a + widths.get(i) , 0) + GAP * (visible.length - 1) - w;
|
|
149
|
+
// Shrink the lowest priority columns first (each down to its min), the widest of equal priority first.
|
|
150
|
+
const prio = (i ) => cols[i] .priority ?? 0;
|
|
151
|
+
for (const level of [...new Set(visible.map(prio))].sort((x, y) => x - y)) {
|
|
152
|
+
while (excess > 0) {
|
|
153
|
+
let pick = -1;
|
|
154
|
+
for (const i of visible) if (prio(i) === level && widths.get(i) > (visible.length === 1 ? 1 : min[i] ) && (pick < 0 || widths.get(i) > widths.get(pick) )) pick = i;
|
|
155
|
+
if (pick < 0) break;
|
|
156
|
+
widths.set(pick, widths.get(pick) - 1);
|
|
157
|
+
excess--;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
const last = visible[visible.length - 1] ;
|
|
161
|
+
const put = (i , text , tok ) => {
|
|
162
|
+
const col = cols[i] ;
|
|
163
|
+
const cw = widths.get(i) ;
|
|
164
|
+
let t = text;
|
|
165
|
+
if (displayWidth(t) > cw) t = (col.truncate === "middle" ? truncateMiddle : truncateEnd)(stripAnsi(t), cw, s.sym.ellipsis);
|
|
166
|
+
const styled = tok ? s(tok, t) : t;
|
|
167
|
+
const pad = " ".repeat(Math.max(0, cw - displayWidth(t)));
|
|
168
|
+
return col.align === "right" ? pad + styled : i === last ? styled : styled + pad;
|
|
169
|
+
};
|
|
170
|
+
const join = (f ) => rtrim(visible.map(f).join(" ".repeat(GAP)));
|
|
171
|
+
const total = visible.reduce((a, i) => a + widths.get(i) , 0) + GAP * (visible.length - 1);
|
|
172
|
+
return [
|
|
173
|
+
join((i) => put(i, flat(cols[i] .header), "muted")),
|
|
174
|
+
s("muted", s.sym.h.repeat(total)),
|
|
175
|
+
...rows.map((_, r) => join((i) => put(i, cells[i] [r] ))),
|
|
176
|
+
];
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
const KIND = {
|
|
180
|
+
// Not the arrow: that marks the callout's command, so an item and its command never look alike.
|
|
181
|
+
info: { sym: (s ) => s.sym.sep, tok: "accent" },
|
|
182
|
+
warn: { sym: (s ) => s.sym.warn, tok: "warn" },
|
|
183
|
+
"needs-you": { sym: (s ) => s.sym.err, tok: "err" },
|
|
184
|
+
} ;
|
|
185
|
+
|
|
186
|
+
/** `✗ Needs you`, what is going on, and the exact command to run. */
|
|
187
|
+
export function callout(s , o , width ) {
|
|
188
|
+
const w = cap(width);
|
|
189
|
+
const k = KIND[o.kind];
|
|
190
|
+
const sym = k.sym(s);
|
|
191
|
+
const title = flat(o.title);
|
|
192
|
+
// A long title wraps at a word, under itself (never cut): the reader needs all of what it says.
|
|
193
|
+
const out = w < 8 ? [truncateEnd(`${sym} ${title}`, w, s.sym.ellipsis)]
|
|
194
|
+
: wrapText(title, w - 2).map((l, i) => (i === 0 ? s(k.tok, sym) + " " : " ") + s("title", l));
|
|
195
|
+
const ind = w >= 8 ? 2 : 0;
|
|
196
|
+
for (const b of o.body) for (const l of wrapLine(b, w - ind)) out.push(l === "" ? "" : " ".repeat(ind) + l);
|
|
197
|
+
if (o.command) {
|
|
198
|
+
const lead = w >= 8 ? ` ${s.sym.arrow} ` : "";
|
|
199
|
+
wrapText(flat(o.command), w - displayWidth(lead), { hang: 0 }).forEach((l, i) => out.push(i === 0 ? s("muted", lead) + s("key", l) : " ".repeat(displayWidth(lead)) + s("key", l)));
|
|
200
|
+
}
|
|
201
|
+
return out.map(rtrim);
|
|
202
|
+
}
|