t3code-session-manager 0.0.0-stage → 0.4.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.
Files changed (172) hide show
  1. package/.claude-plugin/marketplace.json +21 -0
  2. package/.claude-plugin/plugin.json +19 -0
  3. package/CHANGELOG.md +149 -0
  4. package/LICENSE +21 -0
  5. package/README.md +661 -2
  6. package/README.pt.md +617 -0
  7. package/bin/entry.d.mts +2 -0
  8. package/bin/entry.mjs +19 -0
  9. package/bin/t3sm.mjs +44 -0
  10. package/commands/import.md +30 -0
  11. package/commands/organize.md +36 -0
  12. package/commands/status.md +24 -0
  13. package/commands/sync.md +26 -0
  14. package/dist/apply/apply.js +993 -0
  15. package/dist/apply/claudeWriter.js +168 -0
  16. package/dist/apply/copyTranscript.js +116 -0
  17. package/dist/apply/importer.js +697 -0
  18. package/dist/apply/materialize.js +335 -0
  19. package/dist/apply/waves.js +108 -0
  20. package/dist/assistant/core.js +295 -0
  21. package/dist/assistant/deps.js +256 -0
  22. package/dist/assistant/i18n.js +38 -0
  23. package/dist/assistant/messages.en.js +494 -0
  24. package/dist/assistant/messages.pt.js +495 -0
  25. package/dist/assistant/runner.js +64 -0
  26. package/dist/assistant/steps.js +1362 -0
  27. package/dist/build.json +1 -0
  28. package/dist/cli/applyScreen.js +158 -0
  29. package/dist/cli/commands/accounts.js +254 -0
  30. package/dist/cli/commands/apply.js +199 -0
  31. package/dist/cli/commands/changelog.js +61 -0
  32. package/dist/cli/commands/doctor.js +105 -0
  33. package/dist/cli/commands/help.js +158 -0
  34. package/dist/cli/commands/merge.js +224 -0
  35. package/dist/cli/commands/organize.js +872 -0
  36. package/dist/cli/commands/plan.js +161 -0
  37. package/dist/cli/commands/projects.js +430 -0
  38. package/dist/cli/commands/quarantine.js +203 -0
  39. package/dist/cli/commands/recover.js +230 -0
  40. package/dist/cli/commands/rollback.js +281 -0
  41. package/dist/cli/commands/scan.js +117 -0
  42. package/dist/cli/commands/schedule.js +288 -0
  43. package/dist/cli/commands/start.js +45 -0
  44. package/dist/cli/commands/status.js +615 -0
  45. package/dist/cli/commands/sync.js +336 -0
  46. package/dist/cli/commands/update.js +398 -0
  47. package/dist/cli/errors.js +53 -0
  48. package/dist/cli/failures.js +91 -0
  49. package/dist/cli/flags.js +53 -0
  50. package/dist/cli/help.en.js +549 -0
  51. package/dist/cli/help.pt.js +566 -0
  52. package/dist/cli/helpText.js +211 -0
  53. package/dist/cli/helpTopics.js +58 -0
  54. package/dist/cli/locked.js +12 -0
  55. package/dist/cli/main.js +155 -0
  56. package/dist/cli/screen.js +3 -0
  57. package/dist/cli/ui.js +111 -0
  58. package/dist/cli/words.js +2 -0
  59. package/dist/core/activity.js +16 -0
  60. package/dist/core/changelog.js +47 -0
  61. package/dist/core/config.js +530 -0
  62. package/dist/core/fsutil.js +171 -0
  63. package/dist/core/glob.js +34 -0
  64. package/dist/core/history.js +32 -0
  65. package/dist/core/journal.js +274 -0
  66. package/dist/core/lock.js +337 -0
  67. package/dist/core/mask.js +31 -0
  68. package/dist/core/paths.js +68 -0
  69. package/dist/core/pid.js +101 -0
  70. package/dist/core/run.js +4 -0
  71. package/dist/core/semver.js +18 -0
  72. package/dist/core/sysroot.js +29 -0
  73. package/dist/core/types.js +229 -0
  74. package/dist/core/uuid.js +29 -0
  75. package/dist/core/version.js +72 -0
  76. package/dist/help/convert.md +20 -0
  77. package/dist/help/convert.pt.md +21 -0
  78. package/dist/help/cwd-unreadable.md +22 -0
  79. package/dist/help/cwd-unreadable.pt.md +22 -0
  80. package/dist/help/decisions.md +20 -0
  81. package/dist/help/decisions.pt.md +20 -0
  82. package/dist/help/errors.md +20 -0
  83. package/dist/help/errors.pt.md +22 -0
  84. package/dist/help/held.md +20 -0
  85. package/dist/help/held.pt.md +20 -0
  86. package/dist/help/history.md +27 -0
  87. package/dist/help/history.pt.md +29 -0
  88. package/dist/help/instance-unavailable.md +18 -0
  89. package/dist/help/instance-unavailable.pt.md +19 -0
  90. package/dist/help/not-imported.md +19 -0
  91. package/dist/help/not-imported.pt.md +19 -0
  92. package/dist/help/orphaned.md +19 -0
  93. package/dist/help/orphaned.pt.md +19 -0
  94. package/dist/help/project-mismatch.md +15 -0
  95. package/dist/help/project-mismatch.pt.md +15 -0
  96. package/dist/help/t3-native.md +22 -0
  97. package/dist/help/t3-native.pt.md +22 -0
  98. package/dist/help/updates.md +26 -0
  99. package/dist/help/updates.pt.md +28 -0
  100. package/dist/help/watcher.md +24 -0
  101. package/dist/help/watcher.pt.md +25 -0
  102. package/dist/i18n/cli.js +41 -0
  103. package/dist/i18n/lang.js +55 -0
  104. package/dist/i18n/messages.en.js +1140 -0
  105. package/dist/i18n/messages.pt.js +1149 -0
  106. package/dist/i18n/words.js +12 -0
  107. package/dist/ops/accounts.js +170 -0
  108. package/dist/ops/autoUpdate.js +223 -0
  109. package/dist/ops/discoverAccounts.js +343 -0
  110. package/dist/ops/doctor.js +811 -0
  111. package/dist/ops/elevated.js +47 -0
  112. package/dist/ops/errorCodes.js +39 -0
  113. package/dist/ops/follow.js +558 -0
  114. package/dist/ops/merge.js +830 -0
  115. package/dist/ops/notify.js +249 -0
  116. package/dist/ops/plugin.js +92 -0
  117. package/dist/ops/projects.js +113 -0
  118. package/dist/ops/quarantine.js +415 -0
  119. package/dist/ops/rollback.js +868 -0
  120. package/dist/ops/schedule.js +464 -0
  121. package/dist/ops/successor.js +51 -0
  122. package/dist/ops/sync.js +859 -0
  123. package/dist/ops/syncStatus.js +290 -0
  124. package/dist/ops/update.js +273 -0
  125. package/dist/ops/updater.js +641 -0
  126. package/dist/ops/watch.js +676 -0
  127. package/dist/ops/watcherControl.js +212 -0
  128. package/dist/ops/watcherHealth.js +75 -0
  129. package/dist/organize/apply.js +495 -0
  130. package/dist/organize/history.js +192 -0
  131. package/dist/organize/plan.js +283 -0
  132. package/dist/organize/rename.js +58 -0
  133. package/dist/organize/sessions.js +100 -0
  134. package/dist/organize/titles.js +70 -0
  135. package/dist/organize/types.js +73 -0
  136. package/dist/plan/categories.js +104 -0
  137. package/dist/plan/dedupe.js +137 -0
  138. package/dist/plan/folders.js +82 -0
  139. package/dist/plan/names.js +93 -0
  140. package/dist/plan/ownership.js +316 -0
  141. package/dist/plan/planner.js +669 -0
  142. package/dist/plan/rehome.js +101 -0
  143. package/dist/plan/report.js +407 -0
  144. package/dist/sources/cache.js +89 -0
  145. package/dist/sources/claude.js +297 -0
  146. package/dist/sources/codex.js +496 -0
  147. package/dist/sources/flatten.js +23 -0
  148. package/dist/sources/jsonl.js +169 -0
  149. package/dist/sources/native.js +233 -0
  150. package/dist/sources/opencode.js +483 -0
  151. package/dist/sources/pi.js +133 -0
  152. package/dist/sources/registry.js +67 -0
  153. package/dist/sources/types.js +80 -0
  154. package/dist/t3/api.js +492 -0
  155. package/dist/t3/auth.js +248 -0
  156. package/dist/t3/compat.js +62 -0
  157. package/dist/t3/decode.js +408 -0
  158. package/dist/t3/errors.js +185 -0
  159. package/dist/t3/rpc.js +185 -0
  160. package/dist/t3/runtime.js +80 -0
  161. package/dist/ui/caps.js +56 -0
  162. package/dist/ui/components.js +202 -0
  163. package/dist/ui/layout.js +205 -0
  164. package/dist/ui/list.js +423 -0
  165. package/dist/ui/locale.js +73 -0
  166. package/dist/ui/organizePreview.js +72 -0
  167. package/dist/ui/progress.js +237 -0
  168. package/dist/ui/style.js +32 -0
  169. package/dist/ui/terminal.js +126 -0
  170. package/dist/ui/text.js +186 -0
  171. package/package.json +58 -4
  172. package/skills/t3code-sessions/SKILL.md +183 -0
@@ -0,0 +1,73 @@
1
+ // Locale formatting for numbers, dates, relative times and durations, in English and Portuguese.
2
+ //
3
+ // The output is built by hand from fixed tables, not taken from full-format Intl output: ICU differs between Node
4
+ // versions and operating systems (pt-PT does not group "4007", and groups with U+202F elsewhere). Intl is used only for
5
+ // what is stable everywhere: the numeric parts of a date in a given time zone.
6
+ //
7
+ // Thousands are separated by a plain space in Portuguese, not a no-break space: it has the same width in every console
8
+ // and survives copy and paste. The cost is that a line wrap can split a number, which the tables never do.
9
+
10
+
11
+
12
+
13
+
14
+
15
+
16
+
17
+
18
+
19
+ const MONTHS_EN = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
20
+
21
+
22
+
23
+ function partsOf(ms , timeZone ) {
24
+ const dtf = new Intl.DateTimeFormat("en-US", {
25
+ ...(timeZone ? { timeZone } : {}), year: "numeric", month: "numeric", day: "numeric", hour: "2-digit", minute: "2-digit", hourCycle: "h23",
26
+ });
27
+ const get = (t ) => dtf.formatToParts(ms).find((p) => p.type === t)?.value ?? "0";
28
+ return { year: Number(get("year")), month: Number(get("month")), day: Number(get("day")), hour: get("hour").padStart(2, "0"), minute: get("minute").padStart(2, "0") };
29
+ }
30
+
31
+ const pad2 = (x ) => String(x).padStart(2, "0");
32
+
33
+ export function makeFmt(lang , now , timeZone ) {
34
+ const pt = lang === "pt";
35
+ const n = (x ) => {
36
+ const r = Math.round(x);
37
+ const digits = String(Math.abs(r)).replace(/\B(?=(\d{3})+(?!\d))/g, pt ? " " : ",");
38
+ return (r < 0 ? "-" : "") + digits;
39
+ };
40
+ const date = (iso ) => {
41
+ const t = Date.parse(iso);
42
+ if (Number.isNaN(t)) return iso;
43
+ const p = partsOf(t, timeZone);
44
+ const thisYear = partsOf(now(), timeZone).year === p.year;
45
+ if (pt) return thisYear ? `${pad2(p.day)}/${pad2(p.month)} às ${p.hour}:${p.minute}` : `${pad2(p.day)}/${pad2(p.month)}/${p.year}`;
46
+ const md = `${MONTHS_EN[p.month - 1]} ${p.day}`;
47
+ return thisYear ? `${md}, ${p.hour}:${p.minute}` : `${md}, ${p.year}`;
48
+ };
49
+ const ago = (iso ) => {
50
+ const t = Date.parse(iso);
51
+ if (Number.isNaN(t)) return iso;
52
+ const s = Math.floor((now() - t) / 1000);
53
+ if (s < 60) return pt ? "agora" : "just now"; // also a time in the future
54
+ const m = Math.floor(s / 60), h = Math.floor(m / 60), d = Math.floor(h / 24);
55
+ const mo = Math.floor(d / 30), y = Math.floor(d / 365);
56
+ const [v, pt1, ptN, en1, enN] =
57
+ y >= 1 ? [y, "ano", "anos", "year", "years"]
58
+ : mo >= 1 ? [mo, "mês", "meses", "month", "months"]
59
+ : d >= 1 ? [d, "dia", "dias", "day", "days"]
60
+ : h >= 1 ? [h, "h", "h", "h", "h"]
61
+ : [m, "min", "min", "min", "min"];
62
+ const unit = pt ? (v === 1 ? pt1 : ptN) : (v === 1 ? en1 : enN);
63
+ return pt ? `há ${v} ${unit}` : `${v} ${unit} ago`;
64
+ };
65
+ const dur = (ms ) => {
66
+ const total = Math.max(0, Math.floor(ms / 1000));
67
+ const h = Math.floor(total / 3600), m = Math.floor((total % 3600) / 60), s = total % 60;
68
+ if (h > 0) return `${h} h ${pad2(m)} min ${pad2(s)} s`;
69
+ if (m > 0) return `${m} min ${pad2(s)} s`;
70
+ return `${s} s`;
71
+ };
72
+ return { n, date, ago, dur };
73
+ }
@@ -0,0 +1,72 @@
1
+ // The organize preview as one before/after table per project (replaces the two side-by-side columns of
2
+ // sidebarPreview.ts, which misaligned): the project, the conversations it shows inside it now and after the plan, and
3
+ // how many the plan settles, retitles and archives there. Pure: lines in, no writes. Project titles come from T3 Code:
4
+ // untrusted, through shownText. The words come from the caller (the CLI and the assistant have their own catalogues).
5
+
6
+
7
+ import { shownText } from "../plan/names.js";
8
+ import { table, } from "./components.js";
9
+
10
+
11
+ import { truncateEnd, wrapText } from "./text.js";
12
+
13
+ /** Narrower than this the table becomes a list (five columns would drop the archived count). */
14
+ const LIST_BELOW = 60;
15
+
16
+
17
+  
18
+
19
+
20
+
21
+
22
+
23
+
24
+
25
+
26
+
27
+
28
+
29
+
30
+ /** The plan, per project: shown now -> after, settled, retitled, archived. */
31
+ export function organizePreviewTable(s , f , plan , words , width , o = {}) {
32
+ const max = Math.max(1, o.max ?? 10);
33
+ const to = s.caps.unicode ? "→" : "->";
34
+ const count = (kind ) => {
35
+ const m = new Map ();
36
+ for (const a of plan.actions) if (a.kind === kind && a.projectId !== null) m.set(a.projectId, (m.get(a.projectId) ?? 0) + 1);
37
+ return m;
38
+ };
39
+ const settled = count("settle");
40
+ const retitled = count("title");
41
+ const archived = count("archive");
42
+ const all = plan.byProject.map((p) => ({
43
+ title: shownText(p.title, 80), before: p.visibleBefore, after: p.visibleAfter,
44
+ settled: settled.get(p.projectId) ?? 0, retitled: retitled.get(p.projectId) ?? 0, archived: archived.get(p.projectId) ?? 0,
45
+ }));
46
+ if (all.length === 0) return [];
47
+ const changed = (r ) => r.before !== r.after || r.settled + r.retitled + r.archived > 0;
48
+ const rows = [...all.filter(changed), ...all.filter((r) => !changed(r))].slice(0, max);
49
+ const n = (x ) => (x === 0 ? "-" : f.n(x));
50
+ const cols = [
51
+ { header: words.project, get: (r) => (r ).title, priority: 9, min: 10 },
52
+ { header: words.visible.replace("{to}", to), get: (r) => `${f.n((r ).before)} ${to} ${f.n((r ).after)}`, align: "right", priority: 8 },
53
+ { header: words.settled, get: (r) => n((r ).settled), align: "right", priority: 6 },
54
+ { header: words.retitled, get: (r) => n((r ).retitled), align: "right", priority: 4 },
55
+ { header: words.archived, get: (r) => n((r ).archived), align: "right", priority: 5 },
56
+ ];
57
+ // Narrow: a list, so no count (the archived one above all) is ever dropped or cut. A header names what the numbers
58
+ // are; each project's title is cut if it must be, never its counts, which go on their own line(s).
59
+ const w = Math.max(1, width);
60
+ const list = () => [
61
+ ...wrapText(`${words.project}: ${words.visible.replace("{to}", to)}`, w).map((l) => s("muted", l)),
62
+ ...rows.flatMap((r) => [
63
+ s("title", truncateEnd(r.title, w, s.sym.ellipsis)),
64
+ ...wrapText([`${f.n(r.before)} ${to} ${f.n(r.after)}`, ...[[words.settled, r.settled], [words.retitled, r.retitled], [words.archived, r.archived]]
65
+ .filter(([, x]) => (x ) > 0).map(([label, x]) => `${label} ${f.n(x )}`)].join(", "), Math.max(1, w - 2), { hang: 0 })
66
+ .filter((l) => l !== "").map((l) => (w >= 4 ? ` ${l}` : l)),
67
+ ]),
68
+ ];
69
+ const out = width >= LIST_BELOW ? table(s, cols, rows, width) : list();
70
+ if (all.length > rows.length) out.push(s("muted", truncateEnd(`${s.sym.ellipsis} ${words.more(all.length - rows.length)}`, Math.max(1, width), s.sym.ellipsis)));
71
+ return out;
72
+ }
@@ -0,0 +1,237 @@
1
+ // Progress feedback: a bar (one line redrawn in place on a TTY, milestone lines otherwise) and a spinner for waits over
2
+ // 300 ms. Both take the terminal, the style, and an injected clock; both leave the terminal clean when the process is
3
+ // interrupted (exit hook and SIGINT), which is why the cursor is only ever hidden while one of them is active.
4
+
5
+
6
+
7
+ import { capsOf, } from "./terminal.js";
8
+ import { displayWidth, truncateEnd, wrapText } from "./text.js";
9
+
10
+
11
+
12
+
13
+
14
+
15
+
16
+
17
+
18
+
19
+
20
+
21
+
22
+
23
+ /** The slice of `process` the hooks need; injected so tests can fire the events. */
24
+
25
+
26
+
27
+
28
+
29
+
30
+
31
+
32
+
33
+
34
+
35
+
36
+
37
+
38
+
39
+
40
+
41
+
42
+ const HIDE = "\x1b[?25l";
43
+ const SHOW = "\x1b[?25h";
44
+ const CLEAR = "\r\x1b[K";
45
+
46
+ /**
47
+ * Runs `abort` once when the process exits, and on SIGINT when nothing else handles it (then exits 130). When another
48
+ * SIGINT owner exists (a graceful stop-after-this-wave handler) the bar or spinner keeps working: only the exit hook
49
+ * stays, so the cursor is still restored when the process ends. Returns the unhook.
50
+ */
51
+ function hookExit(proc , abort , release ) {
52
+ const onExit = () => { unhook(); abort(); };
53
+ const onSigint = () => {
54
+ if ((proc.listenerCount?.("SIGINT") ?? 0) > 0) {
55
+ // Someone else owns Ctrl+C. It may re-raise the signal once it is the last listener, and death by signal emits no
56
+ // "exit": give the cursor back now. The bar or spinner stays usable.
57
+ release();
58
+ return;
59
+ }
60
+ onExit();
61
+ proc.exit?.(130);
62
+ };
63
+ const unhook = () => { proc.off("exit", onExit); proc.off("SIGINT", onSigint); };
64
+ proc.once("exit", onExit);
65
+ proc.once("SIGINT", onSigint);
66
+ return unhook;
67
+ }
68
+
69
+ /**
70
+ * Control, format, line / paragraph separator, surrogate and private-use characters (ESC, CR/LF, bidi overrides such as
71
+ * U+202E, U+2028, U+2029, ...) become one space: progress text is safe by itself. U+200D (zero-width joiner) and the
72
+ * emoji tag characters U+E0020 to U+E007F stay, so an emoji sequence joined by a ZWJ, or a subdivision flag, is kept whole.
73
+ */
74
+ const flat = (s ) => s.replace(/(?:(?![\u200D\u{E0020}-\u{E007F}])[\p{Cc}\p{Cf}\p{Zl}\p{Zp}\p{Cs}\p{Co}])+/gu, " ");
75
+ /** Cursor control available: a TTY that is not TERM=dumb. */
76
+ const canRedraw = (t ) => { const c = capsOf(t); return c.cursor ?? c.isTTY; };
77
+
78
+ export function progressBar(t , s , fmt , clock , o ) {
79
+ const proc = o.proc ?? process;
80
+ const left = o.words.left;
81
+ const sep = ` ${s.sym.sep} `;
82
+ const ell = s.sym.ellipsis;
83
+ let active = false;
84
+ let total = 0;
85
+ let label = "";
86
+ let startMs = 0;
87
+ let lastDraw = -Infinity;
88
+ let lastCols = -1;
89
+ let lastStep = 0;
90
+ let unhook = null;
91
+ let live = false;
92
+ let lastDone = 0;
93
+ let lastNote ;
94
+
95
+ const bar = (done , w ) => {
96
+ const ratio = total > 0 ? Math.min(1, Math.max(0, done / total)) : 0;
97
+ const full = Math.round(w * ratio);
98
+ return s("muted", s.sym.barL) + s("accent", s.sym.barFull.repeat(full)) + s("muted", s.sym.barEmpty.repeat(w - full) + s.sym.barR);
99
+ };
100
+
101
+ /** The most informative single line that fits `limit` cells. */
102
+ const line = (done , note , limit ) => {
103
+ const elapsed = clock() - startMs;
104
+ const counts = `${fmt.n(done)}/${fmt.n(total)}`;
105
+ const rate = elapsed >= 1000 && done > 0 ? (done * 1000) / elapsed : 0;
106
+ const rateText = rate > 0 ? (rate >= 1 ? `${fmt.n(rate)}/s` : "<1/s") : "";
107
+ const eta = (d ) => (left.includes("{t}") ? left.replace("{t}", d) : `${left} ${d}`);
108
+ const etaText = rate > 0 && done < total ? eta(fmt.dur(((total - done) / rate) * 1000)) : "";
109
+ const stats = (withRate , withEta ) => counts + (withRate && rateText ? sep + rateText : "") + (withEta && etaText ? sep + etaText : "");
110
+ const tries = [[20, true, true], [12, true, true], [12, true, false], [10, false, false], [6, false, false]];
111
+ for (const [bw, r, e] of tries) {
112
+ const st = stats(r, e);
113
+ const used = bw + 2 + 1 + displayWidth(st);
114
+ if (used > limit) continue;
115
+ const text = note ?? label;
116
+ const room = limit - used - displayWidth(sep);
117
+ const tail = text !== "" && room >= 6 ? s("muted", sep + truncateEnd(flat(text), room, ell)) : "";
118
+ return bar(done, bw) + " " + s("num", st) + tail;
119
+ }
120
+ return s("num", truncateEnd(counts, limit, ell));
121
+ };
122
+
123
+ const draw = (done , note , force ) => {
124
+ const cols = t.columns || 80;
125
+ const now = clock();
126
+ if (!force && cols === lastCols && now - lastDraw < 33) return;
127
+ lastDraw = now;
128
+ lastCols = cols;
129
+ lastDone = done;
130
+ lastNote = note;
131
+ t.write("\r" + line(done, note, Math.max(1, cols - 1)) + "\x1b[K");
132
+ };
133
+
134
+ const abort = () => {
135
+ if (!active) return;
136
+ active = false;
137
+ unhook?.();
138
+ unhook = null;
139
+ if (live) t.write(SHOW + "\n");
140
+ };
141
+
142
+ const milestone = (text ) => t.write(truncateEnd(flat(text), Math.max(1, t.columns || 80), ell) + "\n");
143
+
144
+ return {
145
+ start(n, l) {
146
+ unhook?.(); // a second start must not leak the first one's hooks
147
+ unhook = null;
148
+ active = true;
149
+ live = canRedraw(t);
150
+ total = Math.max(0, n);
151
+ label = l;
152
+ startMs = clock();
153
+ lastStep = 0;
154
+ lastDraw = -Infinity;
155
+ if (live) {
156
+ t.write(HIDE);
157
+ unhook = hookExit(proc, abort, () => t.write(SHOW));
158
+ draw(0, undefined, true);
159
+ } else milestone(total > 0 && o.showTotal !== false ? `${l} (${fmt.n(total)})` : l);
160
+ },
161
+ tick(done, note) {
162
+ if (!active) return;
163
+ if (live) { draw(done, note, done >= total && total > 0); return; }
164
+ if (total <= 0) return;
165
+ const step = Math.min(90, Math.floor((done * 10) / total) * 10);
166
+ if (step > lastStep) {
167
+ lastStep = step;
168
+ milestone(` ${step}% ${fmt.n(done)}/${fmt.n(total)}${note ? sep + note : ""}`);
169
+ }
170
+ },
171
+ end(summary) {
172
+ if (!active) return;
173
+ active = false;
174
+ unhook?.();
175
+ unhook = null;
176
+ const cols = Math.max(2, t.columns || 80);
177
+ // Off a cursor terminal the summary is one line, so the 12-line cap holds.
178
+ if (live) t.write(CLEAR + wrapText(flat(summary), cols - 1).join("\n") + "\n" + SHOW);
179
+ else {
180
+ const all = wrapText(flat(summary), cols);
181
+ const two = all.length > 2 ? [all[0] , truncateEnd(all.slice(1).join(" "), cols, ell)] : all;
182
+ t.write(two.join("\n") + "\n");
183
+ }
184
+ },
185
+ note(text) {
186
+ const lines = wrapText(flat(text), Math.max(1, (t.columns || 80) - 1)).join("\n") + "\n";
187
+ if (active && live) { t.write(CLEAR + lines); draw(lastDone, lastNote, true); }
188
+ else t.write(lines);
189
+ },
190
+ dispose: abort,
191
+ };
192
+ }
193
+
194
+
195
+
196
+ /**
197
+ * A spinner for a wait of unknown length. Silent for the first 300 ms (by `clock`) and never drawn without cursor control
198
+ * (no TTY, TERM=dumb). `stop(final)` always prints `final` as a line: replacing the spinner line when it was shown, plainly
199
+ * otherwise. Safe to stop twice; `dispose()` stops without a final line (use it in a finally).
200
+ */
201
+ export function spinner(t , s , label , clock , o = {}) {
202
+ if (!canRedraw(t)) {
203
+ let done = false;
204
+ return { stop(final) { if (!done && final) t.write(flat(final) + "\n"); done = true; }, dispose() { done = true; } };
205
+ }
206
+ const proc = o.proc ?? process;
207
+ const start = clock();
208
+ const frames = [...s.sym.spin];
209
+ let i = 0;
210
+ let shown = false;
211
+ let stopped = false;
212
+ let unhook = null;
213
+ const abort = () => {
214
+ if (stopped) return;
215
+ stopped = true;
216
+ clearInterval(timer);
217
+ unhook = null;
218
+ if (shown) t.write(CLEAR + SHOW);
219
+ };
220
+ const timer = setInterval(() => {
221
+ if (stopped || clock() - start < 300) return;
222
+ if (!shown) { shown = true; t.write(HIDE); unhook = hookExit(proc, abort, () => t.write(SHOW)); }
223
+ const room = Math.max(1, (t.columns || 80) - 3);
224
+ t.write("\r" + s("accent", frames[i++ % frames.length] ) + " " + truncateEnd(flat(label), room, s.sym.ellipsis) + "\x1b[K");
225
+ }, o.intervalMs ?? 80);
226
+ timer.unref();
227
+ const stop = (final ) => {
228
+ if (stopped) return;
229
+ stopped = true;
230
+ clearInterval(timer);
231
+ unhook?.();
232
+ unhook = null;
233
+ if (shown) t.write(CLEAR + (final ? flat(final) + "\n" : "") + SHOW);
234
+ else if (final) t.write(flat(final) + "\n");
235
+ };
236
+ return { stop, dispose: () => stop() };
237
+ }
@@ -0,0 +1,32 @@
1
+ // Semantic style tokens on Node's util.styleText, and the symbols (Unicode with an ASCII fallback). Components use
2
+ // tokens, never raw ANSI. `validateStream: false` (typed in @types/node 22, honoured by Node >= 22.13) makes styleText
3
+ // emit ANSI whatever process.stdout is, so FORCE_COLOR works through a pipe; the NO_COLOR / no-TTY rules live in
4
+ // detectCaps, and colour 0 is the identity here.
5
+
6
+ import { styleText } from "node:util";
7
+
8
+
9
+
10
+
11
+
12
+
13
+
14
+
15
+
16
+
17
+
18
+
19
+
20
+
21
+
22
+ const FORMAT = {
23
+ title: "bold", accent: "green", muted: "gray", ok: "green", warn: "yellow", err: "red", key: "cyan", path: "blue", num: "magenta",
24
+ };
25
+
26
+ const UNICODE = { ok: "✓", err: "✗", warn: "!", dot: "●", ring: "○", arrow: "›", ellipsis: "…", h: "─", v: "│", tl: "╭", tr: "╮", bl: "╰", br: "╯", barFull: "█", barEmpty: "░", barL: "▕", barR: "▏", spin: "⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏", sep: "·", cur: "◉", bullet: "•" };
27
+ const ASCII = { ok: "v", err: "x", warn: "!", dot: "*", ring: "o", arrow: ">", ellipsis: "...", h: "-", v: "|", tl: "+", tr: "+", bl: "+", br: "+", barFull: "#", barEmpty: ".", barL: "[", barR: "]", spin: "|/-\\", sep: "-", cur: "@", bullet: "-" };
28
+
29
+ export function makeStyle(c ) {
30
+ const paint = (t , s ) => (c.color === 0 ? s : styleText(FORMAT[t], s, { validateStream: false }));
31
+ return Object.assign(paint, { sym: c.unicode ? UNICODE : ASCII, caps: c });
32
+ }
@@ -0,0 +1,126 @@
1
+ // The terminal as the review screens see it: a small injectable surface (size, colour, write, keys, raw mode, one
2
+ // line of input) so tests drive them with a fake, plus the Node implementation. Zero dependencies: `readline` and raw
3
+ // mode only.
4
+
5
+ import { createInterface, emitKeypressEvents } from "node:readline";
6
+ import { detectCaps, detectUnicode, } from "./caps.js";
7
+
8
+ /**
9
+ * One key press. `name` is the key's name for special keys ("up", "down", "pageup", "pagedown", "home", "end",
10
+ * "return", "escape", "space", "backspace", "tab") and the letter for plain ones; `ch` is the typed text ("" for a
11
+ * special key without one).
12
+ */
13
+
14
+
15
+
16
+
17
+
18
+
19
+
20
+
21
+
22
+
23
+
24
+
25
+
26
+
27
+
28
+
29
+
30
+
31
+
32
+
33
+
34
+
35
+
36
+
37
+
38
+ /** The Term's capabilities: `term.caps` when present, else rebuilt from its colour/unicode/isTTY/columns fields (test fakes). */
39
+ export function capsOf(term ) {
40
+ if (term.caps) return term.caps;
41
+ return { color: term.color ? 1 : 0, unicode: term.unicode, isTTY: term.isTTY, columns: term.columns || 80 };
42
+ }
43
+
44
+ /** Node's keypress event as a TermKey. */
45
+ export function toTermKey(str , key ) {
46
+ const ctrl = key?.ctrl === true;
47
+ const meta = key?.meta === true;
48
+ const raw = key?.name ?? str ?? "";
49
+ const name = raw === "enter" ? "return" : raw; // a bare
50
+
51
+ const ch = str !== undefined && str.length > 0 && !ctrl && !meta && str >= " " && str !== "\x7f" ? str : "";
52
+ return { name, ch, ctrl, meta };
53
+ }
54
+
55
+ export { detectUnicode };
56
+
57
+
58
+
59
+
60
+
61
+
62
+
63
+
64
+
65
+
66
+ export function nodeTerm(o = {}) {
67
+ const stdin = o.stdin ?? process.stdin;
68
+ const stdout = o.stdout ?? process.stdout;
69
+ const env = o.env ?? process.env;
70
+ const platform = o.platform ?? process.platform;
71
+ const isTTY = Boolean(stdin.isTTY && stdout.isTTY);
72
+ const caps = detectCaps(env, platform, { isTTY, columns: stdout.columns, hasColors: typeof stdout.hasColors === "function" ? (n) => stdout.hasColors(n) : undefined });
73
+ const proc = o.proc ?? process;
74
+ let keysWired = false;
75
+ let raw = false;
76
+ // A process.exit or a signal skips the screens' own cleanup: while raw mode is on, an exit hook gives the terminal back.
77
+ const onExit = () => {
78
+ try { stdout.write("[?25h"); } catch { /* gone */ }
79
+ if (raw) { try { stdin.setRawMode(false); } catch { /* gone */ } raw = false; }
80
+ };
81
+ // One input interface for the term's lifetime: lines that arrive together (piped input) are queued, not lost.
82
+ let rl = null;
83
+ const queue = [];
84
+ const waiters = [];
85
+ const open = () => {
86
+ if (rl !== null) return;
87
+ const r = createInterface({ input: stdin, crlfDelay: Infinity });
88
+ rl = r;
89
+ r.on("line", (l) => { const w = waiters.shift(); if (w) w(l); else queue.push(l); });
90
+ r.on("close", () => { rl = null; for (const w of waiters.splice(0)) w(null); });
91
+ r.on("SIGINT", () => r.close());
92
+ };
93
+ return {
94
+ isTTY,
95
+ get columns() { return stdout.columns || 80; },
96
+ get rows() { return stdout.rows || 24; },
97
+ color: caps.color > 0,
98
+ caps,
99
+ unicode: caps.unicode,
100
+ write: (s) => void stdout.write(s),
101
+ onKey(cb, onEnd) {
102
+ if (!keysWired) { emitKeypressEvents(stdin); keysWired = true; }
103
+ const h = (str , key ) => cb(toTermKey(str, key));
104
+ let ending = false;
105
+ const end = () => { if (!ending) { ending = true; onEnd?.(); } };
106
+ stdin.on("keypress", h);
107
+ if (onEnd) { stdin.once("end", end); stdin.once("close", end); }
108
+ return () => { stdin.off("keypress", h); stdin.off("end", end); stdin.off("close", end); };
109
+ },
110
+ setRaw(on) {
111
+ if (!isTTY || typeof stdin.setRawMode !== "function") return;
112
+ stdin.setRawMode(on);
113
+ if (on) { stdin.resume(); if (!raw) proc.once("exit", onExit); raw = true; }
114
+ else { stdin.pause(); proc.off("exit", onExit); raw = false; }
115
+ },
116
+ readLine(question) {
117
+ stdout.write(question);
118
+ const queued = queue.shift();
119
+ if (queued !== undefined) return Promise.resolve(queued);
120
+ if (stdin.readableEnded) return Promise.resolve(null);
121
+ open();
122
+ return new Promise((resolve) => void waiters.push(resolve));
123
+ },
124
+ dispose() { const r = rl; rl = null; queue.length = 0; r?.close(); },
125
+ };
126
+ }