ccdeck 3.53.3 → 3.81.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/README.md +63 -7
- package/THIRD_PARTY_NOTICES.md +31 -1
- package/bin/cli/tui.js +433 -11
- package/dist/web/assets/{BrowserWatchModal-CJ0CW4Go.js → BrowserWatchModal-DKvBSlGT.js} +1 -1
- package/dist/web/assets/{IntegrationModal-DlJzBN8b.js → IntegrationModal-D3NLczRe.js} +1 -1
- package/dist/web/assets/{RadioPanel-CHl3s3OH.js → RadioPanel-dCIMGTd9.js} +1 -1
- package/dist/web/assets/{TrafficRadar-Cw3i0Hux.js → TrafficRadar-CMbwnKe7.js} +1 -1
- package/dist/web/assets/{UsageHistoryModal-CiB5-CTH.js → UsageHistoryModal-DkGBQaM4.js} +1 -1
- package/dist/web/assets/{app-xTrs4aEH.css → app-BIrh5Q0W.css} +1 -1
- package/dist/web/assets/{app-2vj7uk2U.js → app-l0spNrc0.js} +93 -93
- package/dist/web/assets/{index-D1YZbdq3.js → index-inV7HzhI.js} +1 -1
- package/dist/web/index.html +3 -3
- package/node_modules/ws/LICENSE +20 -0
- package/node_modules/ws/README.md +540 -0
- package/node_modules/ws/browser.js +8 -0
- package/node_modules/ws/index.js +22 -0
- package/node_modules/ws/lib/buffer-util.js +142 -0
- package/node_modules/ws/lib/constants.js +19 -0
- package/node_modules/ws/lib/event-target.js +292 -0
- package/node_modules/ws/lib/extension.js +203 -0
- package/node_modules/ws/lib/limiter.js +55 -0
- package/node_modules/ws/lib/permessage-deflate.js +530 -0
- package/node_modules/ws/lib/receiver.js +743 -0
- package/node_modules/ws/lib/sender.js +610 -0
- package/node_modules/ws/lib/stream.js +161 -0
- package/node_modules/ws/lib/subprotocol.js +62 -0
- package/node_modules/ws/lib/validation.js +152 -0
- package/node_modules/ws/lib/websocket-server.js +562 -0
- package/node_modules/ws/lib/websocket.js +1426 -0
- package/node_modules/ws/package.json +74 -0
- package/node_modules/ws/wrapper.mjs +21 -0
- package/package.json +5 -3
- package/release-notes.json +71 -0
- package/src/server/account-engine.mjs +87 -0
- package/src/server/account-notify.mjs +81 -16
- package/src/server/account-routes.mjs +49 -3
- package/src/server/account-watch.mjs +21 -0
- package/src/server/codex-auto.mjs +163 -54
- package/src/server/codex-engine-adapter.mjs +272 -0
- package/src/server/codex-engine-bridge.mjs +47 -0
- package/src/server/codex-engine-config.mjs +42 -0
- package/src/server/codex-engine-rpc.mjs +88 -0
- package/src/server/codex-handoff.mjs +20 -0
- package/src/server/codex-interactive-engine.mjs +335 -0
- package/src/server/codex-lan-identity.mjs +61 -0
- package/src/server/codex-live-checkpoint.mjs +55 -0
- package/src/server/codex-live-credentials.mjs +214 -0
- package/src/server/codex-live-engine.mjs +577 -0
- package/src/server/codex-live-handoff.mjs +300 -0
- package/src/server/codex-live-lease.mjs +64 -0
- package/src/server/codex-live-rpc.mjs +158 -0
- package/src/server/codex-profiles.mjs +34 -4
- package/src/server/codex-projects.mjs +308 -0
- package/src/server/codex-quota.mjs +36 -2
- package/src/server/codex-selection.mjs +78 -9
- package/src/server/codex-share.mjs +238 -0
- package/src/server/codex-terminal-runner.mjs +38 -1
- package/src/server/codex-terminal.mjs +15 -3
- package/src/server/codex-usage.mjs +69 -0
- package/src/server/codex-watch.mjs +3 -1
- package/src/server/codex-windows-private.mjs +96 -0
- package/src/server/codex-windows-private.ps1 +140 -0
- package/src/server/codex-windows-proxy.mjs +53 -0
- package/src/server/index.mjs +12 -4
- package/src/server/lan-codex-store.mjs +170 -0
- package/src/server/lan-codex.mjs +103 -0
- package/src/server/lan-deck.mjs +5 -0
- package/src/server/lan-engine.mjs +34 -6
- package/src/server/lan-manifest.mjs +16 -2
- package/src/server/lan-serve.mjs +16 -6
- package/src/server/login-service.mjs +2 -0
- package/src/server/pinned-build.mjs +5 -0
- package/src/server/request-gates.mjs +3 -0
- package/src/server/tui-codex.mjs +229 -0
- package/src/server/tui-lan.mjs +684 -0
- package/src/server/tui-machine.mjs +492 -0
- package/src/server/tui-network.mjs +730 -0
- package/src/server/tui-view.mjs +735 -60
|
@@ -0,0 +1,492 @@
|
|
|
1
|
+
// The Machine panel of `ccdeck tui`: what the machine is doing, drawn as live
|
|
2
|
+
// charts, for somebody who reached it over SSH and wants to know before
|
|
3
|
+
// anything else whether it is pegged, swapping or cooking.
|
|
4
|
+
//
|
|
5
|
+
// The readings are the deck's (/api/system, src/server/system-metrics.mjs), the
|
|
6
|
+
// numbers its Machine panel shows and the series its history charts draw, under
|
|
7
|
+
// the same names: All cores, Busiest core, Physical RAM, Swap, Queued work, the
|
|
8
|
+
// temperatures and Throttling. The deck samples on its own timer, so the screen
|
|
9
|
+
// asks it every three seconds and keeps what it is told, newest last; a chart
|
|
10
|
+
// is the last of them, one dot column a reading. The deck's own minute of CPU
|
|
11
|
+
// history seeds All cores, so the first frame already has a line in it.
|
|
12
|
+
//
|
|
13
|
+
// Drawn in braille where the terminal has Unicode — two dot columns and four
|
|
14
|
+
// dot rows a cell, the way btop draws — an area under a line: the cell the line
|
|
15
|
+
// passes through in the series' colour, the cells under it the same colour
|
|
16
|
+
// faint, so it reads as a line with a fill. The colour is the reading's own
|
|
17
|
+
// band, column by column: a temperature past its warning is yellow where it
|
|
18
|
+
// was past it and blue where it was not. A band is drawn as a dashed rule. No
|
|
19
|
+
// chart library: there is none without dependencies that keeps Node 18, and a
|
|
20
|
+
// braille cell is eight bits.
|
|
21
|
+
//
|
|
22
|
+
// ASCII consoles get a column chart in `#`, `.` and `-`; NO_COLOR gets the same
|
|
23
|
+
// shapes with no escape at all. Pure: readings in, rows out.
|
|
24
|
+
import { fitPainted, oneLine, textWidth } from "./cli-lines.mjs";
|
|
25
|
+
import { sinceLabel } from "./term.mjs";
|
|
26
|
+
|
|
27
|
+
/** How many readings are kept: at three seconds each, 24 minutes, which is a
|
|
28
|
+
* wider chart than a 200-column terminal draws. */
|
|
29
|
+
export const MACHINE_SAMPLES = 480;
|
|
30
|
+
|
|
31
|
+
/** The spelling the deck's thermal chart uses for its throttling row
|
|
32
|
+
* (thermal-sampler.mjs THROTTLE_LABEL). */
|
|
33
|
+
const THROTTLING = "Throttling";
|
|
34
|
+
|
|
35
|
+
/** An empty ring, before the deck has said anything. */
|
|
36
|
+
export function emptyMachine() {
|
|
37
|
+
return { order: [], series: {}, count: 0, cores: null, platform: null, uptimeSec: null, intervalMs: 3_000, readAt: null };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* What one /api/system answer says, as readings: `{ key, label, unit, warnAt,
|
|
42
|
+
* critAt, restsAtZero, v }`. A reading the deck could not take is left out
|
|
43
|
+
* rather than drawn as a zero — the deck's own rule (#789).
|
|
44
|
+
*/
|
|
45
|
+
export function readingsOf(snap) {
|
|
46
|
+
if (!snap || typeof snap !== "object" || snap.ok === false) return [];
|
|
47
|
+
const out = [];
|
|
48
|
+
const num = (v) => (typeof v === "number" && Number.isFinite(v) ? v : null);
|
|
49
|
+
const add = (key, label, unit, v, extra = {}) => {
|
|
50
|
+
if (v != null) out.push({ key, label, unit, v, warnAt: null, critAt: null, restsAtZero: false, ...extra });
|
|
51
|
+
};
|
|
52
|
+
add("cpu:all", "All cores", "%", num(snap.cpu));
|
|
53
|
+
const per = Array.isArray(snap.perCore) ? snap.perCore.map(num).filter(v => v != null) : [];
|
|
54
|
+
add("cpu:busiest", "Busiest core", "%", per.length ? Math.max(...per) : null);
|
|
55
|
+
add("mem:physical", "Physical RAM", "%", num(snap.memory?.usedPct), { warnAt: 90 });
|
|
56
|
+
const swapTotal = num(snap.swap?.total);
|
|
57
|
+
const swapUsed = num(snap.swap?.used);
|
|
58
|
+
add("mem:swap", snap.platform === "win32" ? "Commit" : "Swap", "%",
|
|
59
|
+
swapTotal && swapUsed != null ? Math.round((swapUsed / swapTotal) * 1000) / 10 : null, { warnAt: 90 });
|
|
60
|
+
const cores = num(snap.cores);
|
|
61
|
+
add("load:1m", "Queued work", "", Array.isArray(snap.loadavg) ? num(snap.loadavg[0]) : null, { warnAt: cores });
|
|
62
|
+
for (const r of Array.isArray(snap.thermal?.celsius) ? snap.thermal.celsius : []) {
|
|
63
|
+
const label = oneLine(r?.label);
|
|
64
|
+
if (!label) continue;
|
|
65
|
+
add(`thermal:${label}`, /temp/i.test(label) ? label : `${label} temp`, "°", num(r.celsius), {
|
|
66
|
+
warnAt: num(r.warnAt), critAt: num(r.critAt),
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
// Linux and Windows say how long the CPU was held back; a Mac says how much
|
|
70
|
+
// speed it still allows. One number either way, the deck's own (heldShare in
|
|
71
|
+
// machine-live.ts): the share held back.
|
|
72
|
+
const throttle = snap.thermal?.throttle;
|
|
73
|
+
const speed = num(throttle?.speedLimit);
|
|
74
|
+
const held = num(throttle?.timeHeld) ?? (speed != null ? 100 - speed : null);
|
|
75
|
+
add(`thermal:${THROTTLING}`, THROTTLING, "%", held != null ? Math.max(0, Math.min(100, held)) : null, { restsAtZero: true });
|
|
76
|
+
return out;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* The ring with one more answer in it. Every series gets a point for every
|
|
81
|
+
* answer — null where this one did not carry it — so the charts stay in step
|
|
82
|
+
* with each other and a gap is drawn as a gap. The first answer seeds All
|
|
83
|
+
* cores with the deck's own last minute.
|
|
84
|
+
*/
|
|
85
|
+
export function withReading(machine, snap, now) {
|
|
86
|
+
const m = machine ?? emptyMachine();
|
|
87
|
+
const readings = readingsOf(snap);
|
|
88
|
+
if (!readings.length) return m;
|
|
89
|
+
const seed = m.count === 0 && Array.isArray(snap.cpuHistory)
|
|
90
|
+
? snap.cpuHistory.filter(v => typeof v === "number" && Number.isFinite(v)).slice(0, -1)
|
|
91
|
+
: [];
|
|
92
|
+
const count = Math.min(MACHINE_SAMPLES, m.count + seed.length + 1);
|
|
93
|
+
const order = m.order.slice();
|
|
94
|
+
const series = {};
|
|
95
|
+
const byKey = new Map(readings.map(r => [r.key, r]));
|
|
96
|
+
for (const r of readings) if (!order.includes(r.key)) order.push(r.key);
|
|
97
|
+
for (const key of order) {
|
|
98
|
+
const before = m.series[key];
|
|
99
|
+
const r = byKey.get(key);
|
|
100
|
+
const head = before ? before.points : [];
|
|
101
|
+
const lead = key === "cpu:all" ? seed : seed.map(() => null);
|
|
102
|
+
const pad = Array.from({ length: Math.max(0, m.count - head.length) }, () => null);
|
|
103
|
+
const points = [...pad, ...head, ...lead, r ? r.v : null].slice(-count);
|
|
104
|
+
const { v: _v, ...meta } = r ?? before ?? { key };
|
|
105
|
+
series[key] = { ...meta, points };
|
|
106
|
+
}
|
|
107
|
+
return {
|
|
108
|
+
order, series, count,
|
|
109
|
+
cores: typeof snap.cores === "number" ? snap.cores : m.cores,
|
|
110
|
+
platform: snap.platform ?? m.platform,
|
|
111
|
+
uptimeSec: typeof snap.uptimeSec === "number" ? snap.uptimeSec : m.uptimeSec,
|
|
112
|
+
intervalMs: typeof snap.intervalMs === "number" && snap.intervalMs > 0 ? snap.intervalMs : m.intervalMs,
|
|
113
|
+
readAt: now,
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** The groups /api/system/history answers, one request each. */
|
|
118
|
+
export const HISTORY_GROUPS = Object.freeze(["cores", "memory", "load", "thermal"]);
|
|
119
|
+
|
|
120
|
+
/** The deck's history names for what the live panel calls otherwise. */
|
|
121
|
+
const HISTORY_LABELS = Object.freeze({ "mem:physical": "Physical RAM" });
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* The deck's minute history (/api/system/history, one answer a group) as a
|
|
125
|
+
* ring like the live one: a point a minute, oldest first, up to `now`, a
|
|
126
|
+
* minute nothing was recorded in a gap. The names and the bands are the live
|
|
127
|
+
* ring's where it has the series, so the two views read alike.
|
|
128
|
+
*/
|
|
129
|
+
export function historyMachine(answers, live, now) {
|
|
130
|
+
const step = 60_000;
|
|
131
|
+
const series = {};
|
|
132
|
+
const order = [];
|
|
133
|
+
let oldest = Infinity;
|
|
134
|
+
for (const a of answers ?? []) {
|
|
135
|
+
for (const s of Array.isArray(a?.series) ? a.series : []) {
|
|
136
|
+
if (!s?.key || !Array.isArray(s.points) || !s.points.length) continue;
|
|
137
|
+
if (s.key.startsWith("net:")) continue;
|
|
138
|
+
order.push(s.key);
|
|
139
|
+
series[s.key] = s;
|
|
140
|
+
for (const p of s.points) if (typeof p?.t === "number") oldest = Math.min(oldest, p.t);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
if (!order.length) return null;
|
|
144
|
+
const end = Math.floor(now / step) * step;
|
|
145
|
+
const count = Math.min(1440, Math.max(1, Math.round((end - Math.floor(oldest / step) * step) / step) + 1));
|
|
146
|
+
const start = end - (count - 1) * step;
|
|
147
|
+
const out = {};
|
|
148
|
+
for (const key of order) {
|
|
149
|
+
const s = series[key];
|
|
150
|
+
const points = new Array(count).fill(null);
|
|
151
|
+
for (const p of s.points) {
|
|
152
|
+
const i = Math.round((Math.floor(p.t / step) * step - start) / step);
|
|
153
|
+
if (i >= 0 && i < count && typeof p.v === "number") points[i] = p.v;
|
|
154
|
+
}
|
|
155
|
+
const mine = live?.series?.[key];
|
|
156
|
+
out[key] = {
|
|
157
|
+
key,
|
|
158
|
+
label: mine?.label ?? HISTORY_LABELS[key] ?? (key.startsWith("thermal:") && s.unit === "C" ? `${s.label} temp` : s.label),
|
|
159
|
+
unit: mine?.unit ?? (s.unit === "C" ? "°" : s.unit),
|
|
160
|
+
warnAt: mine ? mine.warnAt : s.warnAt ?? null,
|
|
161
|
+
critAt: mine ? mine.critAt : s.critAt ?? null,
|
|
162
|
+
restsAtZero: Boolean(s.restsAtZero),
|
|
163
|
+
points,
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
return {
|
|
167
|
+
order, series: out, count,
|
|
168
|
+
cores: live?.cores ?? null, platform: live?.platform ?? null, uptimeSec: live?.uptimeSec ?? null,
|
|
169
|
+
intervalMs: step, readAt: now,
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** The order the charts are read in, the deck's own: the cores, the memory,
|
|
174
|
+
* the queue, the heat. */
|
|
175
|
+
const READ_ORDER = ["cpu:all", "cpu:busiest", "mem:physical", "mem:swap", "load:1m"];
|
|
176
|
+
/** Which charts stay when not all of them fit, most wanted first. */
|
|
177
|
+
const KEEP_ORDER = ["cpu:all", "mem:physical", "load:1m", "thermal", "cpu:busiest", "mem:swap", `thermal:${THROTTLING}`];
|
|
178
|
+
|
|
179
|
+
const rank = (key, list) => {
|
|
180
|
+
const i = list.indexOf(key);
|
|
181
|
+
if (i !== -1) return i;
|
|
182
|
+
if (key === `thermal:${THROTTLING}`) return list.length + 1;
|
|
183
|
+
return key.startsWith("thermal:") ? (list.includes("thermal") ? list.indexOf("thermal") : list.length) : list.length + 2;
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* The series with something to draw, in reading order. The busiest core is
|
|
188
|
+
* drawn on the cores' chart rather than beside it — dots over the area where
|
|
189
|
+
* one core runs hotter than the machine — since the two read together: all
|
|
190
|
+
* cores at 20 with the busiest at 100 is ONE core pinned (system-metrics.mjs
|
|
191
|
+
* coreSeries), and a chart of its own would be a second full box saying 100%.
|
|
192
|
+
*/
|
|
193
|
+
export function shownSeries(machine) {
|
|
194
|
+
if (!machine) return [];
|
|
195
|
+
const list = machine.order
|
|
196
|
+
.map(key => ({ key, ...machine.series[key] }))
|
|
197
|
+
.filter(s => s.points?.some(v => v != null))
|
|
198
|
+
.sort((a, b) => rank(a.key, READ_ORDER) - rank(b.key, READ_ORDER) || machine.order.indexOf(a.key) - machine.order.indexOf(b.key));
|
|
199
|
+
const all = list.find(s => s.key === "cpu:all");
|
|
200
|
+
const busiest = list.find(s => s.key === "cpu:busiest");
|
|
201
|
+
if (!all || !busiest) return list;
|
|
202
|
+
all.peak = busiest.points;
|
|
203
|
+
return list.filter(s => s !== busiest);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/** A scale a person would choose for the queue, the deck's loadTop: just above
|
|
207
|
+
* the peak, never under one and a half times the cores. */
|
|
208
|
+
function loadTop(points, cores) {
|
|
209
|
+
const peak = points.reduce((a, v) => (v == null ? a : Math.max(a, v)), 0);
|
|
210
|
+
const floor = Math.max(4, Math.ceil((cores ?? 1) * 1.5));
|
|
211
|
+
const want = Math.max(floor, peak * 1.15);
|
|
212
|
+
const step = want <= 20 ? 5 : want <= 100 ? 10 : 50;
|
|
213
|
+
return Math.ceil(want / step) * step;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** The value as the panel says it: 54%, 54.24, 90°. */
|
|
217
|
+
export function valueText(s, v, unicode = true) {
|
|
218
|
+
if (v == null) return "—";
|
|
219
|
+
if (s.unit === "%") return `${Math.round(v)}%`;
|
|
220
|
+
if (s.unit === "°") return `${Math.round(v)}${unicode ? "°" : "C"}`;
|
|
221
|
+
return v >= 100 ? String(Math.round(v)) : v.toFixed(2);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/** The tone a value is drawn in: its band, or the chart's own blue. */
|
|
225
|
+
function bandTone(s, v, tone) {
|
|
226
|
+
if (v == null) return tone.accent;
|
|
227
|
+
if (s.critAt != null && v >= s.critAt) return tone.err;
|
|
228
|
+
if (s.warnAt != null && v >= s.warnAt) return tone.warn;
|
|
229
|
+
return tone.accent;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
// Braille: the bit of each dot, by dot row (top first) and dot column.
|
|
233
|
+
const DOT = [[0x01, 0x08], [0x02, 0x10], [0x04, 0x20], [0x40, 0x80]];
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* A chart `width` cells by `height` rows of the newest of `points`, as painted
|
|
237
|
+
* rows. `top` is the scale's top; the bands are the series' own.
|
|
238
|
+
*/
|
|
239
|
+
export function chartRows(s, points, { width, height, top, unicode, tone }) {
|
|
240
|
+
if (width < 1 || height < 1) return [];
|
|
241
|
+
const per = unicode ? 2 : 1;
|
|
242
|
+
const vals = points.slice(-width * per);
|
|
243
|
+
const lead = width * per - vals.length;
|
|
244
|
+
const at = (x) => (x < lead ? null : vals[x - lead]);
|
|
245
|
+
// The peak drawn over the area, lined up with it by its newest end.
|
|
246
|
+
const peaks = Array.isArray(s.peak) ? s.peak.slice(-width * per) : [];
|
|
247
|
+
const peakLead = width * per - peaks.length;
|
|
248
|
+
const peakAt = (x) => (x < peakLead ? null : peaks[x - peakLead]);
|
|
249
|
+
const scale = top > 0 ? top : 100;
|
|
250
|
+
const faint = tone.dim ?? "";
|
|
251
|
+
const bands = [[s.warnAt, tone.warn], [s.critAt, tone.err]].filter(([b]) => b != null && b > 0 && b < scale);
|
|
252
|
+
|
|
253
|
+
// Each cell: its character, and whether it is the line, the fill, a band or nothing.
|
|
254
|
+
const grid = Array.from({ length: height }, () => Array.from({ length: width }, () => ({ ch: " ", kind: "none", tone: "" })));
|
|
255
|
+
const colTone = (cc) => {
|
|
256
|
+
let v = null;
|
|
257
|
+
for (let i = 0; i < per; i++) {
|
|
258
|
+
const p = at(cc * per + i);
|
|
259
|
+
if (p != null) v = v == null ? p : Math.max(v, p);
|
|
260
|
+
}
|
|
261
|
+
return bandTone(s, v, tone);
|
|
262
|
+
};
|
|
263
|
+
|
|
264
|
+
if (unicode) {
|
|
265
|
+
// The line is the top two dots of each column, drawn whole; under it the
|
|
266
|
+
// area is every other dot, so the fill reads as a tint and the line as
|
|
267
|
+
// the line — the deck's chart, a line over a translucent area.
|
|
268
|
+
const dotsH = height * 4;
|
|
269
|
+
const bits = Array.from({ length: height }, () => new Array(width).fill(0));
|
|
270
|
+
const lineCell = Array.from({ length: height }, () => new Array(width).fill(false));
|
|
271
|
+
for (let x = 0; x < width * 2; x++) {
|
|
272
|
+
const v = at(x);
|
|
273
|
+
if (v == null) continue;
|
|
274
|
+
let n = Math.round((Math.min(scale, Math.max(0, v)) / scale) * dotsH);
|
|
275
|
+
if (n === 0 && v > 0) n = 1;
|
|
276
|
+
if (n === 0) continue;
|
|
277
|
+
const cc = Math.floor(x / 2);
|
|
278
|
+
const crest = dotsH - n;
|
|
279
|
+
for (let R = crest; R < dotsH; R++) {
|
|
280
|
+
const onLine = R <= crest + 1;
|
|
281
|
+
if (!onLine && (x + R) % 2 !== 0) continue;
|
|
282
|
+
const cr = Math.floor(R / 4);
|
|
283
|
+
bits[cr][cc] |= DOT[R % 4][x % 2];
|
|
284
|
+
if (onLine) lineCell[cr][cc] = true;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
const peakBits = Array.from({ length: height }, () => new Array(width).fill(0));
|
|
288
|
+
for (let x = 0; x < width * 2; x++) {
|
|
289
|
+
const p = peakAt(x);
|
|
290
|
+
const v = at(x);
|
|
291
|
+
if (p == null || p <= (v ?? 0)) continue;
|
|
292
|
+
const n = Math.max(1, Math.round((Math.min(scale, p) / scale) * dotsH));
|
|
293
|
+
const R = dotsH - n;
|
|
294
|
+
const crest = v == null ? dotsH : dotsH - Math.round((Math.min(scale, Math.max(0, v)) / scale) * dotsH);
|
|
295
|
+
if (R >= crest - 1) continue;
|
|
296
|
+
peakBits[Math.floor(R / 4)][Math.floor(x / 2)] |= DOT[R % 4][x % 2];
|
|
297
|
+
}
|
|
298
|
+
for (let r = 0; r < height; r++) {
|
|
299
|
+
for (let cc = 0; cc < width; cc++) {
|
|
300
|
+
if (peakBits[r][cc] && !bits[r][cc]) grid[r][cc] = { ch: String.fromCodePoint(0x2800 + peakBits[r][cc]), kind: "peak", tone: tone.muted };
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
for (const [b, bt] of bands) {
|
|
304
|
+
const R = Math.min(dotsH - 1, Math.max(0, dotsH - Math.round((b / scale) * dotsH)));
|
|
305
|
+
const row = Math.floor(R / 4);
|
|
306
|
+
for (let cc = 0; cc < width; cc += 2) {
|
|
307
|
+
if (bits[row][cc] === 0 && grid[row][cc].kind === "none") {
|
|
308
|
+
grid[row][cc] = { ch: String.fromCodePoint(0x2800 + DOT[R % 4][0] + DOT[R % 4][1]), kind: "band", tone: bt };
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
for (let r = 0; r < height; r++) {
|
|
313
|
+
for (let cc = 0; cc < width; cc++) {
|
|
314
|
+
if (!bits[r][cc]) continue;
|
|
315
|
+
grid[r][cc] = { ch: String.fromCodePoint(0x2800 + bits[r][cc]), kind: lineCell[r][cc] ? "line" : "fill", tone: colTone(cc) };
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
} else {
|
|
319
|
+
for (let cc = 0; cc < width; cc++) {
|
|
320
|
+
const v = at(cc);
|
|
321
|
+
if (v == null) continue;
|
|
322
|
+
const level = (Math.min(scale, Math.max(0, v)) / scale) * height;
|
|
323
|
+
for (let r = 0; r < height; r++) {
|
|
324
|
+
const fill = level - (height - 1 - r);
|
|
325
|
+
let ch = fill >= 0.75 ? "#" : fill >= 0.25 ? "." : " ";
|
|
326
|
+
if (r === height - 1 && ch === " " && v > 0) ch = ".";
|
|
327
|
+
if (ch !== " ") grid[r][cc] = { ch, kind: fill >= 1 ? "fill" : "line", tone: colTone(cc) };
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
for (let cc = 0; cc < width; cc++) {
|
|
331
|
+
const p = peakAt(cc);
|
|
332
|
+
const v = at(cc);
|
|
333
|
+
if (p == null || p <= (v ?? 0)) continue;
|
|
334
|
+
const row = Math.min(height - 1, Math.max(0, height - Math.ceil((Math.min(scale, p) / scale) * height)));
|
|
335
|
+
if (grid[row][cc].kind === "none") grid[row][cc] = { ch: "'", kind: "peak", tone: tone.muted };
|
|
336
|
+
}
|
|
337
|
+
for (const [b, bt] of bands) {
|
|
338
|
+
const row = Math.min(height - 1, Math.max(0, height - 1 - Math.floor((b / scale) * height)));
|
|
339
|
+
for (let cc = 0; cc < width; cc += 2) if (grid[row][cc].kind === "none") grid[row][cc] = { ch: "-", kind: "band", tone: bt };
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
// One escape per run of cells drawn alike.
|
|
344
|
+
return grid.map(cellsOf => {
|
|
345
|
+
let out = "";
|
|
346
|
+
let open = null;
|
|
347
|
+
for (const c of cellsOf) {
|
|
348
|
+
const code = c.kind === "none" ? "" : c.kind === "line" || c.kind === "peak" ? c.tone : `${faint}${c.tone}`;
|
|
349
|
+
if (code !== open) {
|
|
350
|
+
if (open) out += tone.reset;
|
|
351
|
+
if (code) out += code;
|
|
352
|
+
open = code || null;
|
|
353
|
+
}
|
|
354
|
+
out += c.ch;
|
|
355
|
+
}
|
|
356
|
+
return open ? out + tone.reset : out;
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/** Under this a chart column is too narrow to read, and the grid takes fewer. */
|
|
361
|
+
const MIN_COLUMN = 28;
|
|
362
|
+
const GAP = 3;
|
|
363
|
+
/** Taller than this a chart says nothing more; the panel keeps the rest. */
|
|
364
|
+
const MAX_CHART_ROWS = 12;
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* How the charts are laid out in `width` by `height`: how many columns, how
|
|
368
|
+
* many charts, and how tall — the most charts first, then the most chart.
|
|
369
|
+
* A chart is a header (its name, its value) over its rows when it has three
|
|
370
|
+
* rows or more to itself, and one row with its name beside it when it does not.
|
|
371
|
+
*/
|
|
372
|
+
export function machineLayout(count, width, height) {
|
|
373
|
+
let best = null;
|
|
374
|
+
for (let c = 1; c <= 3; c++) {
|
|
375
|
+
const colW = Math.floor((width - GAP * (c - 1)) / c);
|
|
376
|
+
if (c > 1 && colW < MIN_COLUMN) break;
|
|
377
|
+
let n = count;
|
|
378
|
+
while (n > 0 && Math.ceil(n / c) > height) n--;
|
|
379
|
+
if (!n) continue;
|
|
380
|
+
const rows = Math.ceil(n / c);
|
|
381
|
+
const per = Math.floor(height / rows);
|
|
382
|
+
const header = per >= 3;
|
|
383
|
+
const chartH = Math.min(MAX_CHART_ROWS, header ? per - 1 : per);
|
|
384
|
+
const score = n * 10_000 + chartH * colW * Math.min(c, n);
|
|
385
|
+
if (!best || score > best.score) best = { columns: Math.min(c, n), colW, shown: n, rows, per, header, chartH, score };
|
|
386
|
+
}
|
|
387
|
+
return best;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
/**
|
|
391
|
+
* The panel's inside: `height` rows `width` wide, and what its borders say.
|
|
392
|
+
* `working` is how many sessions are working, for the foot; `fresh` says the
|
|
393
|
+
* deck answered recently enough to call the charts live.
|
|
394
|
+
*/
|
|
395
|
+
export function machinePanel(live, { width, height, look, working = 0, deck = true, starting = false, view = "live", history = null }) {
|
|
396
|
+
const { tone, g, unicode } = look;
|
|
397
|
+
const title = `${tone.bold}Machine${tone.reset}`;
|
|
398
|
+
const footLeft = working ? `${tone.muted}${working} session${working === 1 ? "" : "s"} working${tone.reset}` : "";
|
|
399
|
+
const blank = (said) => ({ title, footLeft, lines: [`${tone.muted}${said}${tone.reset}`] });
|
|
400
|
+
if (!deck) return blank(starting ? `starting the deck${g.ellipsis}` : `the machine's readings come from the deck ${g.dash} d starts one`);
|
|
401
|
+
// The hours draw the deck's minutes; the numbers are always the live ones.
|
|
402
|
+
const hours = view === "hours" && Boolean(history);
|
|
403
|
+
const machine = hours ? history : live;
|
|
404
|
+
const all = shownSeries(machine);
|
|
405
|
+
if (!all.length) return blank(`reading the machine${g.ellipsis}`);
|
|
406
|
+
const plan = machineLayout(all.length, width, height);
|
|
407
|
+
if (!plan) return { title, lines: [] };
|
|
408
|
+
// What stays when not every chart fits, read in the order the deck reads them.
|
|
409
|
+
const keep = new Set(all.slice().sort((a, b) => rank(a.key, KEEP_ORDER) - rank(b.key, KEEP_ORDER)).slice(0, plan.shown).map(s => s.key));
|
|
410
|
+
const shown = all.filter(s => keep.has(s.key));
|
|
411
|
+
|
|
412
|
+
const newest = (points) => {
|
|
413
|
+
for (let i = points.length - 1; i >= 0; i--) if (points[i] != null) return points[i];
|
|
414
|
+
return null;
|
|
415
|
+
};
|
|
416
|
+
const latest = (s) => newest(live?.series?.[s.key]?.points ?? s.points);
|
|
417
|
+
const values = shown.map(s => valueText(s, latest(s), unicode));
|
|
418
|
+
const valueW = Math.max(...values.map(v => textWidth(v)));
|
|
419
|
+
const labelW = Math.min(14, Math.max(...shown.map(s => textWidth(oneLine(s.label)))));
|
|
420
|
+
const per = unicode ? 2 : 1;
|
|
421
|
+
|
|
422
|
+
const block = (s, i, chartH) => {
|
|
423
|
+
const v = latest(s);
|
|
424
|
+
const vt = bandTone(s, v, tone);
|
|
425
|
+
const value = `${tone.bold}${vt === tone.accent ? "" : vt}${values[i]}${tone.reset}`;
|
|
426
|
+
const visible = s.points.slice(-plan.colW * per);
|
|
427
|
+
const top = s.unit === "" ? loadTop(visible, machine.cores) : 100;
|
|
428
|
+
const label = oneLine(s.label);
|
|
429
|
+
if (plan.header) {
|
|
430
|
+
const rows = chartRows(s, s.points, { width: plan.colW, height: chartH, top, unicode, tone });
|
|
431
|
+
// The busiest core beside the name, where there is room for it.
|
|
432
|
+
const peak = s.peak ? newest(live?.series?.["cpu:busiest"]?.points ?? s.peak) : null;
|
|
433
|
+
const said = peak != null ? ` ${g.bullet} busiest ${valueText(s, peak, unicode)}` : "";
|
|
434
|
+
const head = textWidth(label) + textWidth(said) + 1 + textWidth(values[i]) <= plan.colW ? said : "";
|
|
435
|
+
// A sensor's name can be longer than its column: the name gives way, the
|
|
436
|
+
// value never does.
|
|
437
|
+
const name = fitPainted(label, Math.max(1, plan.colW - textWidth(head) - 1 - textWidth(values[i])), g.ellipsis);
|
|
438
|
+
const gap = Math.max(1, plan.colW - textWidth(name) - textWidth(head) - textWidth(values[i]));
|
|
439
|
+
return [`${tone.muted}${name}${tone.reset}${tone.muted}${head}${tone.reset}${" ".repeat(gap)}${value}`, ...rows];
|
|
440
|
+
}
|
|
441
|
+
const chartW = Math.max(1, plan.colW - labelW - valueW - 2);
|
|
442
|
+
const rows = chartRows(s, s.points, { width: chartW, height: chartH, top, unicode, tone });
|
|
443
|
+
return rows.map((row, r) => {
|
|
444
|
+
const name = r === 0 ? fitPainted(`${tone.muted}${label}${tone.reset}`, labelW, g.ellipsis) : "";
|
|
445
|
+
const said = r === 0 ? value : "";
|
|
446
|
+
return `${name}${" ".repeat(Math.max(0, labelW - textWidth(name)))} ${row} ${" ".repeat(Math.max(0, valueW - textWidth(said)))}${said}`;
|
|
447
|
+
});
|
|
448
|
+
};
|
|
449
|
+
|
|
450
|
+
// Row by row of the grid, the columns side by side. The rows the grid does
|
|
451
|
+
// not need go to the charts, from the top, so they reach the foot.
|
|
452
|
+
const lines = [];
|
|
453
|
+
const spare = height - plan.rows * plan.per;
|
|
454
|
+
for (let r = 0; r < plan.rows; r++) {
|
|
455
|
+
const chartH = Math.min(MAX_CHART_ROWS, plan.chartH + (r < spare ? 1 : 0));
|
|
456
|
+
const row = shown.slice(r * plan.columns, (r + 1) * plan.columns).map((s, k) => block(s, r * plan.columns + k, chartH));
|
|
457
|
+
const tall = Math.max(...row.map(b => b.length));
|
|
458
|
+
for (let i = 0; i < tall; i++) {
|
|
459
|
+
lines.push(row.map(b => {
|
|
460
|
+
const l = b[i] ?? "";
|
|
461
|
+
return l + " ".repeat(Math.max(0, plan.colW - textWidth(l)));
|
|
462
|
+
}).join(" ".repeat(GAP)));
|
|
463
|
+
}
|
|
464
|
+
const room = plan.per + (r < spare ? 1 : 0) - tall;
|
|
465
|
+
for (let i = 0; i < room; i++) lines.push("");
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
const span = Math.min(machine.count, plan.header ? plan.colW * per : Math.max(1, plan.colW - labelW - valueW - 2) * per);
|
|
469
|
+
const spanMs = span * (machine.intervalMs ?? 3_000);
|
|
470
|
+
const facts = [
|
|
471
|
+
live?.cores ? `${live.cores} cores` : "",
|
|
472
|
+
live?.uptimeSec != null ? `up ${sinceLabel(live.uptimeSec * 1000)}` : "",
|
|
473
|
+
].filter(Boolean);
|
|
474
|
+
const sep = ` ${g.bullet} `;
|
|
475
|
+
// How far back the charts reach, and the key that reaches the other way.
|
|
476
|
+
const reach = spanMs > 0 ? `last ${sinceLabel(spanMs)}` : "";
|
|
477
|
+
const other = `${tone.reset}${tone.bold}t${tone.reset}${tone.muted} ${hours ? "live" : "hours"}`;
|
|
478
|
+
// Asked for the hours and not read yet: the live charts stay meanwhile.
|
|
479
|
+
const said = view === "hours" && !history ? `reading the hours${g.ellipsis}`
|
|
480
|
+
: (hours ? [reach, "by the minute"] : ["live", reach]).filter(Boolean).join(sep);
|
|
481
|
+
return {
|
|
482
|
+
title,
|
|
483
|
+
note: facts.length ? [`${tone.muted}${facts.join(sep)}${tone.reset}`, `${tone.muted}${facts[0]}${tone.reset}`] : "",
|
|
484
|
+
footLeft,
|
|
485
|
+
footRight: [
|
|
486
|
+
`${tone.muted}${said}${sep}${other}${tone.reset}`,
|
|
487
|
+
`${tone.muted}${said}${tone.reset}`,
|
|
488
|
+
`${tone.muted}${hours ? "by the minute" : "live"}${tone.reset}`,
|
|
489
|
+
],
|
|
490
|
+
lines: lines.slice(0, height),
|
|
491
|
+
};
|
|
492
|
+
}
|