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,730 @@
|
|
|
1
|
+
// The Network side of `ccdeck tui`: the deck's Local network, as its Accounts
|
|
2
|
+
// panel and its setup dialog offer it. The switch, the decks with their
|
|
3
|
+
// requests, the accounts this deck shares, the pairing settings, adding a deck
|
|
4
|
+
// by address or with an invite — and the keys that change each of them,
|
|
5
|
+
// through the deck's own routes (lan-routes.mjs, prefs-routes.mjs).
|
|
6
|
+
//
|
|
7
|
+
// Pure, like tui-view.mjs: a state and a key in, the next state and the one
|
|
8
|
+
// thing to go and do (`effect`) out; a state in, the panel's rows out. The
|
|
9
|
+
// deck's own rules and words — what a deck's row says, which accounts can be
|
|
10
|
+
// shared — are tui-lan.mjs's, a copy of the web's held to it by its test.
|
|
11
|
+
import { clip, oneLine, textWidth } from "./cli-lines.mjs";
|
|
12
|
+
import { lanDeckRows, lanSectionState, lanShareRows, toggleShared } from "./tui-lan.mjs";
|
|
13
|
+
|
|
14
|
+
/** Whether the screen shows the Local network. */
|
|
15
|
+
export const onLan = (state) => state.side === "lan";
|
|
16
|
+
|
|
17
|
+
/** The deck serves Local network: its status has been read. */
|
|
18
|
+
export const hasLan = (state) => Boolean(state.deck && state.lan);
|
|
19
|
+
|
|
20
|
+
/** The decks asking to pair with this one, waiting on somebody here. */
|
|
21
|
+
export const lanAsking = (state) => (state.lan?.enabled ? (state.lan.pending ?? []).length : 0);
|
|
22
|
+
|
|
23
|
+
/** How long a name the deck keeps for another deck may be (deck-prefs.mjs). */
|
|
24
|
+
export const ALIAS_MAX = 48;
|
|
25
|
+
/** How long this deck's own name on the network may be, here. */
|
|
26
|
+
export const NAME_MAX = 64;
|
|
27
|
+
/** An invite as lan-invite.mjs makes them. */
|
|
28
|
+
const INVITE_PREFIX = "ccdeck1.";
|
|
29
|
+
|
|
30
|
+
const claudeAccountsOf = (state) => (state.roster?.ok && Array.isArray(state.roster.accounts) ? state.roster.accounts : []);
|
|
31
|
+
|
|
32
|
+
// ── the rows ────────────────────────────────────────────────────────────────
|
|
33
|
+
|
|
34
|
+
const PAUSED = "paused while pairing is invite-only";
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Every row of the Network list, top to bottom, in the deck's order: the
|
|
38
|
+
* switch and what stops this deck being found, this deck, the other decks
|
|
39
|
+
* (those asking first), the accounts it shares and how, then pairing and
|
|
40
|
+
* Tailscale. A row with an `id` can be selected; the rest head or explain.
|
|
41
|
+
* While the switch is off the decks are not listed — nothing reaches them —
|
|
42
|
+
* but what will be shared, and how pairing goes, can be set beforehand.
|
|
43
|
+
*/
|
|
44
|
+
export function lanItems(state, now) {
|
|
45
|
+
const s = state.lan;
|
|
46
|
+
if (!s) return [];
|
|
47
|
+
const out = [{ id: "enabled", kind: "switch", on: s.enabled === true }];
|
|
48
|
+
if (s.enabled) {
|
|
49
|
+
for (const n of lanNotes(s)) out.push({ kind: "note", text: n.text, tone: n.tone });
|
|
50
|
+
out.push({ id: "self", kind: "self" });
|
|
51
|
+
out.push({ kind: "head", text: "Decks" });
|
|
52
|
+
const decks = lanDeckRows(s, now);
|
|
53
|
+
for (const row of decks) out.push({ id: `deck:${row.fp}`, kind: "deck", row });
|
|
54
|
+
if (!decks.length) out.push({ kind: "muted", text: "No other deck yet — + adds one by its address or with an invite" });
|
|
55
|
+
}
|
|
56
|
+
out.push({ kind: "head", text: "Share these accounts" });
|
|
57
|
+
const shares = lanShareRows(s, claudeAccountsOf(state), state.codex?.profiles ?? []);
|
|
58
|
+
for (const row of shares) {
|
|
59
|
+
if (row.head) out.push({ kind: "sub", text: row.head });
|
|
60
|
+
out.push({ id: `share:${row.key}`, kind: "share", row });
|
|
61
|
+
}
|
|
62
|
+
if (!shares.length) out.push({ kind: "muted", text: "No account on this deck to share yet" });
|
|
63
|
+
out.push({ id: "shareActive", kind: "setting", label: "Show paired decks which of these this deck is using", on: s.shareActive === true });
|
|
64
|
+
out.push({ id: "sendInUse", kind: "setting", label: "Send the login this deck is using too", on: s.sendInUse === true, warn: true });
|
|
65
|
+
const inviteOnly = s.pairingMode === "invite";
|
|
66
|
+
out.push({ kind: "head", text: "Pairing" });
|
|
67
|
+
out.push({ id: "inviteOnly", kind: "setting", label: "Pair new decks only by invite", on: inviteOnly });
|
|
68
|
+
out.push({ kind: "sub", text: inviteOnly ? `Automatic pairing · ${PAUSED}` : "Automatic pairing" });
|
|
69
|
+
out.push({ id: "autoAsk", kind: "setting", label: "Ask every deck this one finds", on: s.autoAsk === true, paused: inviteOnly });
|
|
70
|
+
out.push({ id: "autoAccept", kind: "setting", label: "Say yes to every deck that asks", on: s.autoAccept === true, paused: inviteOnly, warn: true });
|
|
71
|
+
const ts = s.tailscale;
|
|
72
|
+
if (ts?.found) {
|
|
73
|
+
out.push({ kind: "head", text: "Tailscale" });
|
|
74
|
+
out.push({ id: "tailscale", kind: "setting", label: "Look for my devices", on: ts.on === true, detail: tailscaleDetail(ts) });
|
|
75
|
+
if (ts.on) {
|
|
76
|
+
out.push({ kind: "sub", text: inviteOnly ? `Automatic pairing with my devices · ${PAUSED}` : "Automatic pairing with my devices" });
|
|
77
|
+
out.push({ id: "tailscaleAsk", kind: "setting", label: "Ask each device it finds", on: ts.ask === true, paused: inviteOnly });
|
|
78
|
+
out.push({ id: "tailscaleAccept", kind: "setting", label: "Say yes to each device that asks", on: ts.accept === true, paused: inviteOnly });
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return out;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** What stops other decks finding this one, said under the switch as the
|
|
85
|
+
* panel says it (LanDiscoveryNotes.tsx): the firewall, with the commands
|
|
86
|
+
* that open it; a deck that cannot hear; a tunnel. */
|
|
87
|
+
function lanNotes(s) {
|
|
88
|
+
const out = [];
|
|
89
|
+
if (s.reach?.blocked) {
|
|
90
|
+
out.push({ text: oneLine(s.reach.text ?? "Other decks cannot reach this one."), tone: "warn" });
|
|
91
|
+
for (const step of Array.isArray(s.reach.steps) ? s.reach.steps : []) out.push({ text: `$ ${oneLine(step)}`, tone: "muted" });
|
|
92
|
+
}
|
|
93
|
+
if (s.deaf) out.push({ text: oneLine(s.deaf), tone: "muted" });
|
|
94
|
+
if (s.lanTunneled) {
|
|
95
|
+
out.push({
|
|
96
|
+
text: s.tailscale?.exitNode
|
|
97
|
+
? "Tailscale sends this machine's local network through an exit node, so decks on this network cannot find this one. Turn on Allow local network access in Tailscale's exit node menu to bring them back."
|
|
98
|
+
: "This machine sends its local network through a VPN, so decks on this network cannot find this one. Allowing local network access in the VPN brings them back.",
|
|
99
|
+
tone: "muted",
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
return out;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Tailscale on this machine, as the setup dialog says it (tailscaleDetail). */
|
|
106
|
+
function tailscaleDetail(ts) {
|
|
107
|
+
if (!ts.running) {
|
|
108
|
+
const why = ts.state === "Stopped" ? "turned off"
|
|
109
|
+
: ts.state === "NeedsLogin" || ts.state === "NeedsMachineAuth" ? "signed out"
|
|
110
|
+
: ts.state === "Starting" ? "still starting"
|
|
111
|
+
: "not answering";
|
|
112
|
+
return `Tailscale is ${why} on this machine`;
|
|
113
|
+
}
|
|
114
|
+
const whose = ts.login ?? "your Tailscale account";
|
|
115
|
+
return ts.on ? `${ts.devices ?? 0} online · ${whose}` : whose;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** The rows a person can land on. */
|
|
119
|
+
const selectable = (items) => items.filter(i => i.id);
|
|
120
|
+
|
|
121
|
+
/** The selected row: the one named by `lanSel`, or the first. */
|
|
122
|
+
export function lanSelected(state, now) {
|
|
123
|
+
const rows = selectable(lanItems(state, now));
|
|
124
|
+
return rows.find(i => i.id === state.lanSel) ?? rows[0] ?? null;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// ── drawn ───────────────────────────────────────────────────────────────────
|
|
128
|
+
|
|
129
|
+
const TONE_OF = { bad: "err", warn: "warn", wait: "accent", ok: "ok", idle: "muted" };
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* The panel's rows for `width` columns: `{ lines, selected, foot }`, the
|
|
133
|
+
* selected row's index into `lines`, and what the panel's foot says — when
|
|
134
|
+
* the deck last checked its paired decks.
|
|
135
|
+
*/
|
|
136
|
+
export function lanLines(state, width, look) {
|
|
137
|
+
const { tone, g, unicode, now } = look;
|
|
138
|
+
const s = state.lan;
|
|
139
|
+
const items = lanItems(state, now);
|
|
140
|
+
const sel = lanSelected(state, now);
|
|
141
|
+
const box = (on) => (on ? (unicode ? "[✓]" : "[x]") : "[ ]");
|
|
142
|
+
const lines = [];
|
|
143
|
+
let selected = -1;
|
|
144
|
+
const right = (left, tail) => {
|
|
145
|
+
const gap = width - textWidth(left) - textWidth(tail);
|
|
146
|
+
return tail && gap >= 2 ? `${left}${" ".repeat(gap)}${tail}` : left;
|
|
147
|
+
};
|
|
148
|
+
for (const item of items) {
|
|
149
|
+
if (item === sel) selected = lines.length;
|
|
150
|
+
if (item.kind === "switch") {
|
|
151
|
+
const st = lanSectionState(s, now);
|
|
152
|
+
const tail = item.on ? (st.text ? `${tone[TONE_OF[st.tone]] ?? ""}${oneLine(st.text)}${tone.reset}` : "")
|
|
153
|
+
: `${tone.muted}Off${tone.reset}`;
|
|
154
|
+
lines.push(right(`${box(item.on)} ${tone.bold}Sync with paired decks${tone.reset}`, tail));
|
|
155
|
+
} else if (item.kind === "note") {
|
|
156
|
+
lines.push(...wrap(item.text, width - 4).map((l, i) => `${i ? " " : ` ${item.tone === "warn" ? `${tone.warn}!${tone.reset} ` : " "}`}${tone[item.tone] ?? ""}${l}${tone.reset}`));
|
|
157
|
+
} else if (item.kind === "self") {
|
|
158
|
+
const name = oneLine(s.name) || "";
|
|
159
|
+
const where = s.addrs?.length && s.port ? `${s.addrs[0]}:${s.port}` : "";
|
|
160
|
+
const fp = s.fp ? `fingerprint ${s.fp}` : "fingerprint not known yet";
|
|
161
|
+
const left = `${tone.muted}This deck${tone.reset} ${name || `${tone.muted}this machine${tone.reset}`}${where ? ` ${tone.muted}${g.bullet} ${where}${tone.reset}` : ""}`;
|
|
162
|
+
lines.push(right(left, `${tone.muted}${clip(fp, Math.max(12, width - textWidth(left) - 2), g.ellipsis)}${tone.reset}`));
|
|
163
|
+
} else if (item.kind === "head") {
|
|
164
|
+
// A rule after it, so the section reads as one without colour too.
|
|
165
|
+
const rest = width - textWidth(item.text) - 1;
|
|
166
|
+
lines.push(`${tone.accent}${tone.bold}${item.text}${tone.reset}${rest > 0 ? ` ${tone.muted}${(unicode ? "─" : "-").repeat(rest)}${tone.reset}` : ""}`);
|
|
167
|
+
} else if (item.kind === "sub") {
|
|
168
|
+
lines.push(`${tone.muted}${item.text}${tone.reset}`);
|
|
169
|
+
} else if (item.kind === "muted") {
|
|
170
|
+
lines.push(...wrap(item.text, width - 2).map(l => ` ${tone.muted}${l}${tone.reset}`));
|
|
171
|
+
} else if (item.kind === "deck") {
|
|
172
|
+
lines.push(deckLine(item.row, width, { tone, g, unicode }));
|
|
173
|
+
} else if (item.kind === "share") {
|
|
174
|
+
const r = item.row;
|
|
175
|
+
const mark = r.mark ? `${tone.muted}${r.mark}${tone.reset}` : "";
|
|
176
|
+
lines.push(right(`${box(r.shared)} ${oneLine(r.email) || r.key}`, mark));
|
|
177
|
+
} else if (item.kind === "setting") {
|
|
178
|
+
const lit = item.on && item.warn ? tone.warn : "";
|
|
179
|
+
const label = `${item.paused ? tone.muted : lit}${item.label}${tone.reset}`;
|
|
180
|
+
const tail = item.paused ? `${tone.muted}paused${tone.reset}` : item.detail ? `${tone.muted}${item.detail}${tone.reset}` : "";
|
|
181
|
+
lines.push(right(`${box(item.on)} ${label}`, tail));
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
const foot = s?.enabled ? lanChecked(s, now) : "";
|
|
185
|
+
const plain = look.plain ?? ((l) => l);
|
|
186
|
+
return { lines: lines.map(plain), selected, foot };
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** A deck's row: its dot, its name, how many decks the machine runs, and
|
|
190
|
+
* what it is doing in the deck's words, coloured by how that is going. */
|
|
191
|
+
function deckLine(row, width, { tone, g, unicode }) {
|
|
192
|
+
const dot = row.kind === "asks" ? `${tone.warn}${unicode ? "●" : "*"}${tone.reset}`
|
|
193
|
+
: row.here ? `${tone.ok}${unicode ? "●" : "*"}${tone.reset}`
|
|
194
|
+
: row.kind === "declined" ? `${tone.muted}${unicode ? "×" : "x"}${tone.reset}`
|
|
195
|
+
: `${tone.muted}${unicode ? "○" : "o"}${tone.reset}`;
|
|
196
|
+
const twins = Array.isArray(row.twins) && row.twins.length > 1 ? ` ${tone.muted}${g.bullet} ${row.twins.length} decks${tone.reset}` : "";
|
|
197
|
+
const via = row.via === "tailscale" ? ` ${tone.muted}${g.bullet} Tailscale${tone.reset}` : "";
|
|
198
|
+
const name = `${tone.bold}${clip(oneLine(row.name) || row.addr || row.fp, Math.max(8, Math.floor(width / 3)), g.ellipsis)}${tone.reset}`;
|
|
199
|
+
const left = `${dot} ${name}${via}${twins}`;
|
|
200
|
+
const state = oneLine(row.state);
|
|
201
|
+
const room = width - textWidth(left) - 2;
|
|
202
|
+
if (!state || room < 6) return left;
|
|
203
|
+
// A quiet row — online, nothing to report — says so in the muted ink the
|
|
204
|
+
// panel would not draw it in at all.
|
|
205
|
+
const ink = row.quiet ? tone.muted : tone[TONE_OF[row.tone]] ?? tone.muted;
|
|
206
|
+
return `${left} ${ink}${clip(state, room, g.ellipsis)}${tone.reset}`;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/** When the deck last checked its paired decks, as the list's foot says it. */
|
|
210
|
+
function lanChecked(s, now) {
|
|
211
|
+
const at = s.checkedAt;
|
|
212
|
+
if (at == null) return "not checked yet";
|
|
213
|
+
const ago = Math.max(0, now - at);
|
|
214
|
+
if (ago < 10_000) return "checked just now";
|
|
215
|
+
const m = Math.round(ago / 60_000);
|
|
216
|
+
return m < 1 ? `checked ${Math.round(ago / 1000)}s ago` : m < 60 ? `checked ${m}m ago` : `checked ${Math.round(m / 60)}h ago`;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/** The keys that do something on the selected row, for the footer. */
|
|
220
|
+
export function lanFooterKeys(state, { now, unicode }) {
|
|
221
|
+
const item = lanSelected(state, now);
|
|
222
|
+
const s = state.lan;
|
|
223
|
+
const keys = [[unicode ? "↑↓" : "up/down", "move"]];
|
|
224
|
+
if (item?.kind === "switch") keys.push(["space", item.on ? "turn off" : "turn on"]);
|
|
225
|
+
else if (item?.kind === "setting") keys.push(["space", item.paused ? "paused" : item.on ? "turn off" : "turn on"]);
|
|
226
|
+
else if (item?.kind === "share") keys.push(["space", item.row.shared ? "stop sharing" : "share"]);
|
|
227
|
+
else if (item?.kind === "self") keys.push(["n", "rename this deck"]);
|
|
228
|
+
else if (item?.kind === "deck") {
|
|
229
|
+
const r = item.row;
|
|
230
|
+
if (r.kind === "asks") keys.push(["enter", "accept"], ["x", "decline"]);
|
|
231
|
+
else if (r.kind === "paired") keys.push(...(r.oneWay ? [] : [["enter", "check now"]]), ["n", "name it"], ["x", "unpair"]);
|
|
232
|
+
else if (r.kind === "nearby") keys.push(["enter", s?.pairingMode === "invite" ? "invite" : "ask to pair"]);
|
|
233
|
+
else if (r.kind === "declined") keys.push(["enter", "let it ask again"]);
|
|
234
|
+
else if (r.kind === "dialling") keys.push(["x", "stop dialling"]);
|
|
235
|
+
}
|
|
236
|
+
keys.push(["+", "add a deck"]);
|
|
237
|
+
if (s?.enabled) keys.push(["r", "check all"]);
|
|
238
|
+
return keys;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// ── the keys ────────────────────────────────────────────────────────────────
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* A key on the Network list: `{ state, effect }`, or null for a key this side
|
|
245
|
+
* does not take (quitting, help, the sides, the machine's keys). `note` is
|
|
246
|
+
* tui-view.mjs's.
|
|
247
|
+
*
|
|
248
|
+
* Effects:
|
|
249
|
+
* { type: "lan-prefs", patch, doing, said } POST /api/prefs { lan: patch }
|
|
250
|
+
* { type: "lan-peer", action, fp, name?, doing, said } POST /api/lan/peer
|
|
251
|
+
* { type: "lan-invite", action, token?, doing } POST /api/lan/invite
|
|
252
|
+
* { type: "lan-sync", fp?, doing, said } POST /api/lan/sync
|
|
253
|
+
*/
|
|
254
|
+
export function lanKey(state, str, key, { now, g, note }) {
|
|
255
|
+
if (state.lanForm) return lanFormKey(state, str, key, { now, g, note });
|
|
256
|
+
const name = key.name ?? "";
|
|
257
|
+
const told = (tone, text) => ({ state: { ...state, notice: note(tone, text, now) }, effect: null });
|
|
258
|
+
const rows = selectable(lanItems(state, now));
|
|
259
|
+
const found = rows.findIndex(i => i.id === state.lanSel);
|
|
260
|
+
const at = found === -1 ? 0 : found;
|
|
261
|
+
const item = rows[at] ?? null;
|
|
262
|
+
const go = (i) => ({ state: { ...state, notice: null, lanSel: rows[Math.max(0, Math.min(rows.length - 1, i))]?.id ?? null }, effect: null });
|
|
263
|
+
if (name === "up" || str === "k") return go(at <= 0 ? rows.length - 1 : at - 1);
|
|
264
|
+
if (name === "down" || str === "j" || (name === "tab" && !key.shift)) return go(at >= rows.length - 1 ? 0 : at + 1);
|
|
265
|
+
if (name === "home" || str === "g") return go(0);
|
|
266
|
+
if (name === "end" || str === "G") return go(rows.length - 1);
|
|
267
|
+
if (name === "pageup") return go(at - 5);
|
|
268
|
+
if (name === "pagedown") return go(at + 5);
|
|
269
|
+
|
|
270
|
+
const s = state.lan;
|
|
271
|
+
const enter = name === "return" || name === "enter";
|
|
272
|
+
const space = str === " ";
|
|
273
|
+
const wantsDeck = enter || space || str === "x" || name === "delete" || str === "n" || str === "+" || str === "r";
|
|
274
|
+
if (wantsDeck && !state.deck) return told("info", `Local network is the deck's ${g.dash} d starts one`);
|
|
275
|
+
if (wantsDeck && state.busy) return told("info", `${state.busy.text} ${g.dash} one thing at a time`);
|
|
276
|
+
if (wantsDeck && !s) return told("info", `reading the Local network${g.ellipsis}`);
|
|
277
|
+
|
|
278
|
+
if (str === "+") return addOpen(state, now, g, note);
|
|
279
|
+
if (str === "r") {
|
|
280
|
+
if (!s.enabled) return { state, effect: { type: "refresh" } };
|
|
281
|
+
return { state, effect: { type: "lan-sync", doing: "checking every paired deck", said: "checked every paired deck" } };
|
|
282
|
+
}
|
|
283
|
+
if (!item) return null;
|
|
284
|
+
|
|
285
|
+
if (item.kind === "switch" && (enter || space)) {
|
|
286
|
+
const on = !item.on;
|
|
287
|
+
return {
|
|
288
|
+
state,
|
|
289
|
+
effect: {
|
|
290
|
+
type: "lan-prefs", patch: { enabled: on },
|
|
291
|
+
doing: on ? "turning Local network on" : "turning Local network off",
|
|
292
|
+
said: on ? `Local network on ${g.dash} paired decks get the accounts ticked here` : `Local network off ${g.dash} nothing goes out or comes in`,
|
|
293
|
+
},
|
|
294
|
+
};
|
|
295
|
+
}
|
|
296
|
+
if (item.kind === "setting" && (enter || space)) {
|
|
297
|
+
if (item.paused) return told("info", "Paused while this deck pairs only by invite. Turn that off above to use it.");
|
|
298
|
+
return { state, effect: settingEffect(item, g) };
|
|
299
|
+
}
|
|
300
|
+
if (item.kind === "share" && (enter || space)) {
|
|
301
|
+
const r = item.row;
|
|
302
|
+
const who = oneLine(r.email) || r.key;
|
|
303
|
+
return {
|
|
304
|
+
state,
|
|
305
|
+
effect: {
|
|
306
|
+
type: "lan-prefs", patch: { shared: toggleShared(s.shared ?? [], r.key) },
|
|
307
|
+
doing: r.shared ? `no longer sharing ${who}` : `sharing ${who}`,
|
|
308
|
+
said: r.shared ? `${who} is no longer offered ${g.dash} a copy a deck already has stays there` : `${who} is offered to the paired decks`,
|
|
309
|
+
},
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
if (item.kind === "self" && (enter || str === "n")) {
|
|
313
|
+
return { state: { ...state, lanForm: { view: "name", draft: oneLine(s.name), error: null } }, effect: null };
|
|
314
|
+
}
|
|
315
|
+
if (item.kind === "deck") return deckKey(state, item.row, str, name, { now, g, note });
|
|
316
|
+
return null;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
function settingEffect(item, g) {
|
|
320
|
+
const on = !item.on;
|
|
321
|
+
const patch = item.id === "inviteOnly" ? { pairingMode: on ? "invite" : "automatic" } : { [item.id]: on };
|
|
322
|
+
const said = item.id === "inviteOnly"
|
|
323
|
+
? (on ? `new decks pair only by invite ${g.dash} requests waiting now are turned away` : "decks on this network can pair on their own again")
|
|
324
|
+
: `${item.label} ${g.dash} ${on ? "on" : "off"}`;
|
|
325
|
+
return { type: "lan-prefs", patch, doing: `${on ? "turning on" : "turning off"}: ${item.label.toLowerCase()}`, said };
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/** A key on a deck's row: what its one verb does, and the others. */
|
|
329
|
+
function deckKey(state, row, str, name, { now, g, note }) {
|
|
330
|
+
const told = (tone, text) => ({ state: { ...state, notice: note(tone, text, now) }, effect: null });
|
|
331
|
+
const s = state.lan;
|
|
332
|
+
const enter = name === "return" || name === "enter" || str === " ";
|
|
333
|
+
const drop = str === "x" || name === "delete";
|
|
334
|
+
const who = oneLine(row.name) || row.addr || row.fp;
|
|
335
|
+
const peer = (action, doing, said) => ({ state, effect: { type: "lan-peer", action, fp: row.fp, doing, said } });
|
|
336
|
+
if (str === "n") {
|
|
337
|
+
if (row.kind === "dialling") return told("info", `${who} is an address this deck calls ${g.dash} it has a name once it answers`);
|
|
338
|
+
return { state: { ...state, lanForm: { view: "alias", fp: row.fp, name: who, self: row.self ?? null, draft: row.self ? who : "", error: null } }, effect: null };
|
|
339
|
+
}
|
|
340
|
+
if (row.kind === "asks") {
|
|
341
|
+
if (enter) return peer("accept", `accepting ${who}`, `paired with ${who}`);
|
|
342
|
+
if (drop) return peer("dismiss", `declining ${who}`, `said no to ${who} ${g.dash} enter on its row lets it ask again`);
|
|
343
|
+
}
|
|
344
|
+
if (row.kind === "paired") {
|
|
345
|
+
if (enter) {
|
|
346
|
+
if (row.oneWay) return told("info", "There is no address here to call it on. It calls this deck, and it is up to date each time it does.");
|
|
347
|
+
return { state, effect: { type: "lan-sync", fp: row.fp, doing: `checking ${who}`, said: `checked ${who}` } };
|
|
348
|
+
}
|
|
349
|
+
if (drop) return { state: { ...state, lanForm: { view: "unpair", fp: row.fp, name: who, twins: Array.isArray(row.twins) ? row.twins.length : 1 } }, effect: null };
|
|
350
|
+
}
|
|
351
|
+
if (row.kind === "nearby" && enter) {
|
|
352
|
+
if (s.pairingMode === "invite") return addOpen(state, now, g, note, "invite");
|
|
353
|
+
return peer("accept", `asking ${who} to pair`, `asked ${who} to pair ${g.dash} somebody there says yes, or its deck does`);
|
|
354
|
+
}
|
|
355
|
+
if (row.kind === "declined" && enter) return peer("allow", `letting ${who} ask again`, `${who} can ask to pair again`);
|
|
356
|
+
if (row.kind === "dialling") {
|
|
357
|
+
if (drop) {
|
|
358
|
+
const manual = state.lanPrefs?.manual;
|
|
359
|
+
if (!Array.isArray(manual)) return told("info", `reading the addresses this deck calls${g.ellipsis}`);
|
|
360
|
+
// A dialling row is named by the "host:port" it dials (lan-roster.ts).
|
|
361
|
+
const entry = row.fp;
|
|
362
|
+
return {
|
|
363
|
+
state,
|
|
364
|
+
effect: { type: "lan-prefs", patch: { manual: manual.filter(m => m !== entry) }, doing: `no longer calling ${entry}`, said: `stopped calling ${entry}` },
|
|
365
|
+
};
|
|
366
|
+
}
|
|
367
|
+
if (enter) return told("info", `trying ${who} every round ${g.dash} x stops it`);
|
|
368
|
+
}
|
|
369
|
+
return enter || drop ? { state, effect: null } : null;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
// ── adding a deck, naming, unpairing ────────────────────────────────────────
|
|
373
|
+
|
|
374
|
+
const ADD_ITEMS = Object.freeze([
|
|
375
|
+
{ id: "address", key: "a", label: "By its address" },
|
|
376
|
+
{ id: "invite", key: "i", label: "Make an invite to send them" },
|
|
377
|
+
{ id: "join", key: "p", label: "Paste an invite you were sent" },
|
|
378
|
+
]);
|
|
379
|
+
|
|
380
|
+
function addOpen(state, now, g, note, start = null) {
|
|
381
|
+
const s = state.lan;
|
|
382
|
+
if (start === "invite") return inviteMake(state, g);
|
|
383
|
+
// Invite-only: adding by address is off, so the invite is where to start.
|
|
384
|
+
const at = s?.pairingMode === "invite" ? 1 : 0;
|
|
385
|
+
return { state: { ...state, lanForm: { view: "add", at, error: null } }, effect: null };
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
function inviteMake(state, g) {
|
|
389
|
+
// An invite still good is shown again rather than replaced.
|
|
390
|
+
if (state.lan?.invite?.token) return { state: { ...state, lanForm: { view: "invite", error: null } }, effect: null };
|
|
391
|
+
return {
|
|
392
|
+
state: { ...state, lanForm: { view: "invite", error: null } },
|
|
393
|
+
effect: { type: "lan-invite", action: "make", doing: "making an invite" },
|
|
394
|
+
};
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
/** A key while a form of the Network side is up. */
|
|
398
|
+
function lanFormKey(state, str, key, { now, g, note }) {
|
|
399
|
+
const name = key.name ?? "";
|
|
400
|
+
const f = state.lanForm;
|
|
401
|
+
if (state.busy) return { state, effect: null };
|
|
402
|
+
const set = (patch) => ({ state: { ...state, lanForm: { ...f, ...patch } }, effect: null });
|
|
403
|
+
const close = () => ({ state: { ...state, lanForm: null }, effect: null });
|
|
404
|
+
const enter = name === "return" || name === "enter";
|
|
405
|
+
const s = state.lan ?? {};
|
|
406
|
+
|
|
407
|
+
if (f.view === "add") {
|
|
408
|
+
if (name === "escape" || str === "q") return close();
|
|
409
|
+
if (name === "up" || str === "k") return set({ at: (f.at + ADD_ITEMS.length - 1) % ADD_ITEMS.length, error: null });
|
|
410
|
+
if (name === "down" || str === "j" || name === "tab") return set({ at: (f.at + 1) % ADD_ITEMS.length, error: null });
|
|
411
|
+
const pick = enter ? ADD_ITEMS[f.at] : ADD_ITEMS.find(i => i.key === str);
|
|
412
|
+
if (!pick) return { state, effect: null };
|
|
413
|
+
if (pick.id === "address") {
|
|
414
|
+
if (s.pairingMode === "invite") return set({ at: ADD_ITEMS.indexOf(pick), error: "Adding by address is off while this deck pairs only by invite." });
|
|
415
|
+
return set({ view: "address", draft: "", error: null });
|
|
416
|
+
}
|
|
417
|
+
if (pick.id === "invite") return inviteMake(state, g);
|
|
418
|
+
return set({ view: "join", draft: "", error: null, tried: null });
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
if (f.view === "invite") {
|
|
422
|
+
if (str === "x" && s.invite?.token) {
|
|
423
|
+
return {
|
|
424
|
+
state: { ...state, lanForm: null },
|
|
425
|
+
effect: { type: "lan-invite", action: "withdraw", doing: "cancelling the invite", said: "invite cancelled — the text can no longer be used" },
|
|
426
|
+
};
|
|
427
|
+
}
|
|
428
|
+
return close();
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
if (f.view === "unpair") {
|
|
432
|
+
if (str === "y" || str === "Y") {
|
|
433
|
+
return {
|
|
434
|
+
state: { ...state, lanForm: null },
|
|
435
|
+
effect: { type: "lan-peer", action: "unpair", fp: f.fp, doing: `unpairing ${f.name}`, said: `unpaired ${f.name} ${g.dash} logins no longer go between the two decks` },
|
|
436
|
+
};
|
|
437
|
+
}
|
|
438
|
+
return close();
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
// The fields: this deck's name, a name for another deck, an address, an invite.
|
|
442
|
+
const back = () => (f.view === "address" || f.view === "join"
|
|
443
|
+
? set({ view: "add", at: f.view === "address" ? 0 : 2, draft: "", error: null, tried: null })
|
|
444
|
+
: close());
|
|
445
|
+
if (name === "escape") return back();
|
|
446
|
+
if (name === "backspace") return set({ draft: [...f.draft].slice(0, -1).join(""), error: null });
|
|
447
|
+
if (key.ctrl && name === "u") return set({ draft: "", error: null });
|
|
448
|
+
const max = f.view === "alias" ? ALIAS_MAX : f.view === "name" ? NAME_MAX : f.view === "join" ? 4096 : 260;
|
|
449
|
+
const typed = typeof str === "string" && str.length > 0 && !key.ctrl && !key.meta && !/[\x00-\x1f\x7f]/.test(str);
|
|
450
|
+
if (typed) return set({ draft: [...`${f.draft}${str}`].slice(0, max).join(""), error: null });
|
|
451
|
+
if (!enter) return { state, effect: null };
|
|
452
|
+
|
|
453
|
+
const draft = f.draft.trim();
|
|
454
|
+
if (f.view === "name") {
|
|
455
|
+
return {
|
|
456
|
+
state,
|
|
457
|
+
effect: {
|
|
458
|
+
type: "lan-prefs", patch: { name: draft }, doing: "renaming this deck",
|
|
459
|
+
said: draft ? `other decks see this one as ${draft}` : "other decks see this one by its machine's name",
|
|
460
|
+
},
|
|
461
|
+
};
|
|
462
|
+
}
|
|
463
|
+
if (f.view === "alias") {
|
|
464
|
+
return {
|
|
465
|
+
state,
|
|
466
|
+
effect: {
|
|
467
|
+
type: "lan-peer", action: "alias", fp: f.fp, name: draft, doing: `naming ${f.name}`,
|
|
468
|
+
said: draft ? `${f.name} is ${draft} on this deck` : `${f.name} goes by its own name again`,
|
|
469
|
+
},
|
|
470
|
+
};
|
|
471
|
+
}
|
|
472
|
+
if (f.view === "address") {
|
|
473
|
+
const parsed = parseAddress(draft);
|
|
474
|
+
if (!parsed) return set({ error: addressFault(draft) });
|
|
475
|
+
const entry = `${parsed.addr}:${parsed.port}`;
|
|
476
|
+
const met = (s.peers ?? []).find(p => p.addr === parsed.addr && p.port === parsed.port && p.paired);
|
|
477
|
+
if (met) return set({ error: `Already paired with ${oneLine(met.name)} at that address.` });
|
|
478
|
+
const manual = state.lanPrefs?.manual;
|
|
479
|
+
if (!Array.isArray(manual)) return set({ error: `reading the addresses this deck calls${g.ellipsis} try again in a moment` });
|
|
480
|
+
if (manual.includes(entry)) return set({ error: "This deck already calls that address." });
|
|
481
|
+
return {
|
|
482
|
+
state,
|
|
483
|
+
effect: {
|
|
484
|
+
type: "lan-prefs", patch: { manual: [...manual, entry] }, doing: `adding ${entry}`,
|
|
485
|
+
said: `calling ${entry} ${g.dash} it is asked to pair until somebody there accepts`,
|
|
486
|
+
},
|
|
487
|
+
};
|
|
488
|
+
}
|
|
489
|
+
if (f.view === "join") {
|
|
490
|
+
if (!draft) return set({ error: "Paste the whole line they sent you." });
|
|
491
|
+
if (!draft.startsWith(INVITE_PREFIX)) return set({ error: "That is not an invite — paste the whole line they sent you." });
|
|
492
|
+
return { state, effect: { type: "lan-invite", action: "join", token: draft, doing: "joining with the invite" } };
|
|
493
|
+
}
|
|
494
|
+
return { state, effect: null };
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
/**
|
|
498
|
+
* The form in its box: `{ title, footRight, lines, selected }` for
|
|
499
|
+
* tui-view.mjs to draw, `inner` columns wide.
|
|
500
|
+
*/
|
|
501
|
+
export function lanFormSpec(state, inner, look) {
|
|
502
|
+
const spec = formSpec(state, inner, look);
|
|
503
|
+
const plain = look.plain ?? ((l) => l);
|
|
504
|
+
const each = (v) => (Array.isArray(v) ? v.map(plain) : typeof v === "string" ? plain(v) : v);
|
|
505
|
+
return { ...spec, title: each(spec.title), note: each(spec.note), footRight: each(spec.footRight), lines: spec.lines.map(plain) };
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
function formSpec(state, inner, { tone, g, unicode, now }) {
|
|
509
|
+
const f = state.lanForm;
|
|
510
|
+
const s = state.lan ?? {};
|
|
511
|
+
const say = (text, code = tone.muted) => wrap(text, inner).map(l => `${code}${l}${tone.reset}`);
|
|
512
|
+
const refused = f.error ? wrap(f.error, inner - 3).map((l, i) => `${tone.err}${i ? " " : `${g.fail} `}${l}${tone.reset}`) : [];
|
|
513
|
+
const caret = `${tone.accent}${unicode ? "▏" : "_"}${tone.reset}`;
|
|
514
|
+
// A long draft — an invite — shows its end, where the typing is.
|
|
515
|
+
const field = (text) => {
|
|
516
|
+
const room = inner - 3;
|
|
517
|
+
const shown = textWidth(text) > room ? `${g.ellipsis}${[...text].slice(-(room - 1)).join("")}` : text;
|
|
518
|
+
return `${tone.accent}${unicode ? "›" : ">"}${tone.reset} ${shown}${caret}`;
|
|
519
|
+
};
|
|
520
|
+
const back = (verb) => [`${tone.muted}enter ${verb} ${g.bullet} esc back${tone.reset}`, `${tone.muted}esc back${tone.reset}`];
|
|
521
|
+
if (f.view === "add") {
|
|
522
|
+
const inviteOnly = s.pairingMode === "invite";
|
|
523
|
+
const lines = [];
|
|
524
|
+
let selected = -1;
|
|
525
|
+
ADD_ITEMS.forEach((item, i) => {
|
|
526
|
+
if (i === f.at) selected = lines.length;
|
|
527
|
+
const pointer = i === f.at ? `${tone.accent}${tone.bold}${unicode ? "›" : ">"}${tone.reset}` : " ";
|
|
528
|
+
const off = item.id === "address" && inviteOnly;
|
|
529
|
+
const room = inner - 5;
|
|
530
|
+
lines.push(`${pointer} ${off ? tone.muted : ""}${padEnd(clip(item.label, room, g.ellipsis), room)}${tone.reset} ${tone.muted}${item.key}${tone.reset}`);
|
|
531
|
+
});
|
|
532
|
+
const where = s.addrs?.length && s.port ? `${s.addrs[0]}:${s.port}` : null;
|
|
533
|
+
return {
|
|
534
|
+
title: `${tone.bold}Add a deck${tone.reset}`,
|
|
535
|
+
footRight: [`${tone.muted}enter chooses ${g.bullet} esc closes${tone.reset}`, `${tone.muted}esc closes${tone.reset}`],
|
|
536
|
+
lines: [
|
|
537
|
+
...lines,
|
|
538
|
+
"",
|
|
539
|
+
...(inviteOnly ? say("Adding by address is off while this deck pairs only by invite.")
|
|
540
|
+
: where ? say(`They type this deck's own address into the same field on theirs: ${where}`) : []),
|
|
541
|
+
...refused,
|
|
542
|
+
],
|
|
543
|
+
selected,
|
|
544
|
+
};
|
|
545
|
+
}
|
|
546
|
+
if (f.view === "address") {
|
|
547
|
+
return {
|
|
548
|
+
title: `${tone.bold}Add a deck by its address${tone.reset}`,
|
|
549
|
+
footRight: back("adds it"),
|
|
550
|
+
lines: [...say("The other deck's address and port, like 192.168.1.5:54340. This deck calls it until somebody there accepts.", ""), "", field(f.draft), ...refused],
|
|
551
|
+
};
|
|
552
|
+
}
|
|
553
|
+
if (f.view === "join") {
|
|
554
|
+
return {
|
|
555
|
+
title: `${tone.bold}Paste an invite${tone.reset}`,
|
|
556
|
+
footRight: back("joins"),
|
|
557
|
+
lines: [...say("Pasting one pairs both decks on the spot — nobody has to accept.", ""), "", field(f.draft), ...refused,
|
|
558
|
+
...(Array.isArray(f.tried) && f.tried.length ? f.tried.map(t => `${tone.muted} ${oneLine(t.addr)}: ${oneLine(t.why)}${tone.reset}`) : [])],
|
|
559
|
+
};
|
|
560
|
+
}
|
|
561
|
+
if (f.view === "invite") {
|
|
562
|
+
const inv = s.invite;
|
|
563
|
+
if (!inv?.token) {
|
|
564
|
+
return {
|
|
565
|
+
title: `${tone.bold}Invite to pair${tone.reset}`,
|
|
566
|
+
footRight: `${tone.muted}esc closes${tone.reset}`,
|
|
567
|
+
lines: [...(state.busy ? say(`making the invite${g.ellipsis}`) : f.error ? [] : say("The invite ran out or was cancelled. + makes a fresh one.")), ...refused],
|
|
568
|
+
};
|
|
569
|
+
}
|
|
570
|
+
const left = Math.max(0, Math.round(((inv.expiresAt ?? now) - now) / 1000));
|
|
571
|
+
const clock = `${Math.floor(left / 60)}:${String(left % 60).padStart(2, "0")} left`;
|
|
572
|
+
return {
|
|
573
|
+
title: `${tone.bold}Send this to them${tone.reset}`,
|
|
574
|
+
note: `${tone.muted}${clock}${tone.reset}`,
|
|
575
|
+
footRight: [`${tone.muted}x cancels it ${g.bullet} esc closes${tone.reset}`, `${tone.muted}esc closes${tone.reset}`],
|
|
576
|
+
lines: [
|
|
577
|
+
...say("They paste it in Add a deck on theirs, and the two decks pair. Select it to copy it.", ""),
|
|
578
|
+
"",
|
|
579
|
+
...chunks(inv.token, inner).map(l => `${tone.accent}${l}${tone.reset}`),
|
|
580
|
+
"",
|
|
581
|
+
...say("Anyone who gets this text can pair with this deck until it runs out."),
|
|
582
|
+
...refused,
|
|
583
|
+
],
|
|
584
|
+
};
|
|
585
|
+
}
|
|
586
|
+
if (f.view === "unpair") {
|
|
587
|
+
return {
|
|
588
|
+
title: `${tone.err}${tone.bold}Unpair ${f.name}?${tone.reset}`,
|
|
589
|
+
footRight: `${tone.muted}any other key keeps it${tone.reset}`,
|
|
590
|
+
lines: [
|
|
591
|
+
...say(f.twins > 1
|
|
592
|
+
? `Logins stop going between this deck and the ${f.twins} decks on that machine. Pairing again takes a request or an invite.`
|
|
593
|
+
: "Logins stop going between the two decks. Pairing again takes a request or an invite.", ""),
|
|
594
|
+
"",
|
|
595
|
+
`${tone.bold}y${tone.reset} ${tone.muted}unpairs it${tone.reset}`,
|
|
596
|
+
...refused,
|
|
597
|
+
],
|
|
598
|
+
};
|
|
599
|
+
}
|
|
600
|
+
if (f.view === "alias") {
|
|
601
|
+
return {
|
|
602
|
+
title: `${tone.bold}Name ${f.name}${tone.reset}`,
|
|
603
|
+
footRight: back("saves"),
|
|
604
|
+
lines: [...say(`Only this deck shows the name.${f.self ? ` It calls itself ${f.self}.` : ""} Empty puts back its own.`, ""), "", field(f.draft), ...refused],
|
|
605
|
+
};
|
|
606
|
+
}
|
|
607
|
+
return {
|
|
608
|
+
title: `${tone.bold}This deck's name on the network${tone.reset}`,
|
|
609
|
+
footRight: back("saves"),
|
|
610
|
+
lines: [...say("The name other decks show for this one. Empty uses the machine's name.", ""), "", field(f.draft), ...refused],
|
|
611
|
+
};
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
// ── the invite, to send ─────────────────────────────────────────────────────
|
|
615
|
+
|
|
616
|
+
/** Whether the invite shows as a page of its own: once there is one to send.
|
|
617
|
+
* In a box its text would wrap between borders and copy with them. */
|
|
618
|
+
export const lanInvitePage = (state) => state.lanForm?.view === "invite" && Boolean(state.lan?.invite?.token);
|
|
619
|
+
|
|
620
|
+
/** The keys the invite page takes. */
|
|
621
|
+
export const lanInviteKeys = () => [["x", "cancel it"], ["esc", "close"]];
|
|
622
|
+
|
|
623
|
+
/**
|
|
624
|
+
* The invite as a page, `room` rows under the title bar: what to do with it,
|
|
625
|
+
* the text itself written edge to edge as one run the terminal wraps — so a
|
|
626
|
+
* selection copies it whole, as the sign-in link is — and the warning that
|
|
627
|
+
* goes with it. `{ lines, run }`, as signinLines gives.
|
|
628
|
+
*/
|
|
629
|
+
export function lanInviteLines(state, room, width, columns, look) {
|
|
630
|
+
const { tone, g, now } = look;
|
|
631
|
+
const plain = look.plain ?? ((l) => l);
|
|
632
|
+
const inv = state.lan.invite;
|
|
633
|
+
const token = String(inv.token);
|
|
634
|
+
const say = (text, code = tone.muted) => wrap(text, width - 4).map(l => plain(` ${code}${l}${tone.reset}`));
|
|
635
|
+
const left = Math.max(0, Math.round(((inv.expiresAt ?? now) - now) / 1000));
|
|
636
|
+
const clock = `${Math.floor(left / 60)}:${String(left % 60).padStart(2, "0")} left`;
|
|
637
|
+
const pieces = chunks(token, columns);
|
|
638
|
+
const blocks = {
|
|
639
|
+
top: [""],
|
|
640
|
+
head: [plain(` ${tone.bold}Send this to them${tone.reset} ${tone.muted}${g.bullet} ${clock}${tone.reset}`)],
|
|
641
|
+
what: say("They paste it in Add a deck on theirs, and the two decks pair. Nobody has to press anything here. Select it to copy it."),
|
|
642
|
+
gap: [""],
|
|
643
|
+
token: pieces,
|
|
644
|
+
after: ["", ...say("Anyone who gets this text can pair with this deck until it runs out.")],
|
|
645
|
+
error: state.lanForm.error ? ["", ...say(state.lanForm.error, tone.err)] : [],
|
|
646
|
+
};
|
|
647
|
+
const order = ["top", "head", "what", "gap", "token", "error", "after"];
|
|
648
|
+
const shown = new Set(order);
|
|
649
|
+
const height = () => order.filter(b => shown.has(b)).reduce((n, b) => n + blocks[b].length, 0);
|
|
650
|
+
for (const drop of ["after", "what", "top", "gap"]) {
|
|
651
|
+
if (height() <= room) break;
|
|
652
|
+
shown.delete(drop);
|
|
653
|
+
}
|
|
654
|
+
if (height() > room) blocks.token = say(`make the window taller to see the invite ${g.dash} it needs ${pieces.length} rows`, tone.warn);
|
|
655
|
+
const lines = [];
|
|
656
|
+
let run = null;
|
|
657
|
+
for (const b of order) {
|
|
658
|
+
if (!shown.has(b)) continue;
|
|
659
|
+
if (b === "token" && blocks.token === pieces) run = { row: lines.length, rows: pieces.length, full: token.length % columns === 0, text: `${tone.accent}${token}${tone.reset}` };
|
|
660
|
+
lines.push(...blocks[b]);
|
|
661
|
+
}
|
|
662
|
+
return { lines: [...lines, ...Array.from({ length: Math.max(0, room - lines.length) }, () => "")].slice(0, room), run: run && run.row + run.rows <= room ? run : null };
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
// ── the deck's answers ──────────────────────────────────────────────────────
|
|
666
|
+
|
|
667
|
+
/** What a Local network refusal means, in the deck's words. */
|
|
668
|
+
export const LAN_REFUSED = Object.freeze({
|
|
669
|
+
not_seen: "this deck has not heard that one yet, or pairs only by invite",
|
|
670
|
+
not_running: "This deck is not on the network yet. Turn Local network on, then try again.",
|
|
671
|
+
no_address: "There is no address here to call it on. It calls this deck, and it is up to date each time it does.",
|
|
672
|
+
not_an_invite: "That is not an invite — paste the whole line they sent you.",
|
|
673
|
+
expired: "That invite has run out. Ask them for a fresh one.",
|
|
674
|
+
unreachable: "Nothing answered at any address in that invite. Check that deck is running with its Local network switch on.",
|
|
675
|
+
prefs_unreadable: "this deck cannot read its settings file, so it will not write over it. The deck's log names the file.",
|
|
676
|
+
prefs_not_writable: "this deck is not allowed to save its settings. The deck's log names the folder.",
|
|
677
|
+
bad_request: "the deck did not take that",
|
|
678
|
+
unknown_action: "this deck does not know that yet — an update of ccdeck brings it",
|
|
679
|
+
});
|
|
680
|
+
|
|
681
|
+
// ── the address, as the add dialog reads it (lan-add-deck.ts) ───────────────
|
|
682
|
+
|
|
683
|
+
/** An address somebody typed, or null: a host — a name, an IPv4, a bracketed
|
|
684
|
+
* IPv6 — and a port from 1 to 65535, split on the last colon. */
|
|
685
|
+
export function parseAddress(raw) {
|
|
686
|
+
const s = String(raw ?? "").trim();
|
|
687
|
+
const at = s.lastIndexOf(":");
|
|
688
|
+
if (at <= 0 || at === s.length - 1) return null;
|
|
689
|
+
const addr = s.slice(0, at).trim();
|
|
690
|
+
const port = Number(s.slice(at + 1).trim());
|
|
691
|
+
if (!addr || !Number.isInteger(port) || port < 1 || port > 65_535) return null;
|
|
692
|
+
if (addr.includes(":") && !(addr.startsWith("[") && addr.endsWith("]"))) return null;
|
|
693
|
+
return { addr, port };
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
/** Why an address was refused, in the terms of what is wrong with it. */
|
|
697
|
+
export function addressFault(raw) {
|
|
698
|
+
const s = String(raw ?? "").trim();
|
|
699
|
+
if (s === "") return "Type an address and a port, like 192.168.1.5:54340.";
|
|
700
|
+
if (!s.startsWith("[") && (s.match(/:/g) ?? []).length > 1) {
|
|
701
|
+
return "Too many colons to split. Use that deck's IPv4 address or its name, like 192.168.1.5:54340.";
|
|
702
|
+
}
|
|
703
|
+
if (!s.includes(":")) return `Add the port too, like ${s}:54340.`;
|
|
704
|
+
const port = s.slice(s.lastIndexOf(":") + 1).trim();
|
|
705
|
+
if (port === "") return "That is missing its port — try 192.168.1.5:54340.";
|
|
706
|
+
if (!/^\d+$/.test(port)) return `A port is a number, and this one is "${port}".`;
|
|
707
|
+
return `A port runs from 1 to 65535, and this one is ${port}.`;
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
// ── text ────────────────────────────────────────────────────────────────────
|
|
711
|
+
|
|
712
|
+
const padEnd = (s, n) => s + " ".repeat(Math.max(0, n - textWidth(s)));
|
|
713
|
+
|
|
714
|
+
function wrap(text, width) {
|
|
715
|
+
const out = [];
|
|
716
|
+
let line = "";
|
|
717
|
+
for (const word of String(text).split(" ")) {
|
|
718
|
+
if (line && textWidth(`${line} ${word}`) > width) { out.push(line); line = word; } else line = line ? `${line} ${word}` : word;
|
|
719
|
+
}
|
|
720
|
+
if (line) out.push(line);
|
|
721
|
+
return out.flatMap(l => (textWidth(l) > width ? chunks(l, width) : [l]));
|
|
722
|
+
}
|
|
723
|
+
|
|
724
|
+
/** Text with no spaces — an invite, a command — cut into lines `width` wide. */
|
|
725
|
+
function chunks(text, width) {
|
|
726
|
+
const cells = [...String(text)];
|
|
727
|
+
const out = [];
|
|
728
|
+
for (let i = 0; i < cells.length; i += Math.max(1, width)) out.push(cells.slice(i, i + Math.max(1, width)).join(""));
|
|
729
|
+
return out.length ? out : [""];
|
|
730
|
+
}
|