@peer2peer/cli 0.0.0-stage → 0.1.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/LICENSE +201 -0
- package/README.md +366 -2
- package/dist/ancestry.d.ts +13 -0
- package/dist/ancestry.js +71 -0
- package/dist/app.d.ts +363 -0
- package/dist/app.js +1177 -0
- package/dist/bin.d.ts +2 -0
- package/dist/bin.js +56 -0
- package/dist/circle-ops.d.ts +402 -0
- package/dist/circle-ops.js +1725 -0
- package/dist/cli.d.ts +611 -0
- package/dist/cli.js +1650 -0
- package/dist/codes.d.ts +231 -0
- package/dist/codes.js +998 -0
- package/dist/config.d.ts +112 -0
- package/dist/config.js +243 -0
- package/dist/doctor.d.ts +99 -0
- package/dist/doctor.js +817 -0
- package/dist/errors.d.ts +8 -0
- package/dist/errors.js +29 -0
- package/dist/ext-host.d.ts +32 -0
- package/dist/ext-host.js +259 -0
- package/dist/extensions.d.ts +386 -0
- package/dist/extensions.js +395 -0
- package/dist/features.d.ts +91 -0
- package/dist/features.js +164 -0
- package/dist/format.d.ts +57 -0
- package/dist/format.js +248 -0
- package/dist/identity.d.ts +236 -0
- package/dist/identity.js +751 -0
- package/dist/inbound.d.ts +32 -0
- package/dist/inbound.js +258 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +14 -0
- package/dist/join-answers.d.ts +49 -0
- package/dist/join-answers.js +149 -0
- package/dist/outbox.d.ts +109 -0
- package/dist/outbox.js +337 -0
- package/dist/pretty.d.ts +100 -0
- package/dist/pretty.js +256 -0
- package/dist/procstart.d.ts +1 -0
- package/dist/procstart.js +66 -0
- package/dist/progress.d.ts +30 -0
- package/dist/progress.js +78 -0
- package/dist/projects.d.ts +240 -0
- package/dist/projects.js +699 -0
- package/dist/prompt/picker.d.ts +64 -0
- package/dist/prompt/picker.js +152 -0
- package/dist/prompt/tty.d.ts +29 -0
- package/dist/prompt/tty.js +122 -0
- package/dist/readonly.d.ts +20 -0
- package/dist/readonly.js +50 -0
- package/dist/receipts.d.ts +99 -0
- package/dist/receipts.js +180 -0
- package/dist/relay-client.d.ts +277 -0
- package/dist/relay-client.js +873 -0
- package/dist/relay-info.d.ts +24 -0
- package/dist/relay-info.js +53 -0
- package/dist/rotation.d.ts +402 -0
- package/dist/rotation.js +2054 -0
- package/dist/running.d.ts +5 -0
- package/dist/running.js +74 -0
- package/dist/sessions.d.ts +340 -0
- package/dist/sessions.js +1152 -0
- package/dist/shortcode.d.ts +149 -0
- package/dist/shortcode.js +465 -0
- package/dist/social.d.ts +139 -0
- package/dist/social.js +485 -0
- package/dist/state.d.ts +483 -0
- package/dist/state.js +217 -0
- package/dist/stdin.d.ts +17 -0
- package/dist/stdin.js +46 -0
- package/dist/sync.d.ts +77 -0
- package/dist/sync.js +375 -0
- package/dist/transient.d.ts +1 -0
- package/dist/transient.js +28 -0
- package/dist/trust.d.ts +37 -0
- package/dist/trust.js +134 -0
- package/dist/tty-confirm.d.ts +2 -0
- package/dist/tty-confirm.js +53 -0
- package/dist/tui/command.d.ts +20 -0
- package/dist/tui/command.js +70 -0
- package/dist/tui/frame.d.ts +43 -0
- package/dist/tui/frame.js +111 -0
- package/dist/tui/model.d.ts +215 -0
- package/dist/tui/model.js +255 -0
- package/dist/tui/sanitize.d.ts +13 -0
- package/dist/tui/sanitize.js +151 -0
- package/dist/tui/terminal.d.ts +88 -0
- package/dist/tui/terminal.js +354 -0
- package/dist/tui/tui.d.ts +71 -0
- package/dist/tui/tui.js +571 -0
- package/dist/tui/view.d.ts +4 -0
- package/dist/tui/view.js +356 -0
- package/dist/unknown-kinds.d.ts +12 -0
- package/dist/unknown-kinds.js +66 -0
- package/dist/version.d.ts +1 -0
- package/dist/version.js +5 -0
- package/dist/want-pow.d.ts +12 -0
- package/dist/want-pow.js +46 -0
- package/dist/watch.d.ts +35 -0
- package/dist/watch.js +325 -0
- package/dist/xstate.d.ts +125 -0
- package/dist/xstate.js +422 -0
- package/package.json +60 -4
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** Display width of one code point: 0 (combining), 2 (wide / emoji) or 1. */
|
|
2
|
+
export declare function cpWidth(cp: number): number;
|
|
3
|
+
export declare function displayWidth(s: string): number;
|
|
4
|
+
/** Peer-safe text: escapes and invisible or control characters removed, whitespace collapsed and trimmed. No width limit. */
|
|
5
|
+
export declare function clean(s: unknown): string;
|
|
6
|
+
/** clean() without the trim: the renderer's own spacing (" Peer2Peer ", "files: ") survives. */
|
|
7
|
+
export declare function cleanKeep(s: unknown): string;
|
|
8
|
+
/** clean(), then cut to `width` columns ("…" marks a cut). */
|
|
9
|
+
export declare function cell(s: unknown, width: number): string;
|
|
10
|
+
/** cell(), padded with spaces to exactly `width` columns. */
|
|
11
|
+
export declare function pad(s: unknown, width: number): string;
|
|
12
|
+
/** Word-wraps clean text into lines of at most `width` columns (at most `maxLines`, the last one cut). */
|
|
13
|
+
export declare function wrap(s: unknown, width: number, maxLines?: number): string[];
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
// Copyright 2026 Ix Infrastructure Inc.
|
|
2
|
+
//
|
|
3
|
+
// The TUI's one gate for text on its way to the terminal. Everything a peer
|
|
4
|
+
// controls (names, labels, statuses, file paths, message text, kinds, relay
|
|
5
|
+
// answers) and everything else too passes through cell() before the renderer
|
|
6
|
+
// sees it: the frame is built only from what this module returns.
|
|
7
|
+
//
|
|
8
|
+
// - Escape sequences (CSI, OSC including OSC 8 links, DCS/SOS/PM/APC, and
|
|
9
|
+
// their 8-bit C1 forms) are removed whole, then every remaining C0/C1
|
|
10
|
+
// control, DEL, line and paragraph separator, format character (bidi
|
|
11
|
+
// overrides and isolates, zero-width characters, tags), lone surrogate,
|
|
12
|
+
// noncharacter and invisible filler goes.
|
|
13
|
+
// - Line breaks and tabs become spaces; runs of whitespace collapse.
|
|
14
|
+
// - Private-use and unassigned code points become "?" (their width is unknown).
|
|
15
|
+
// - Combining marks are kept, at most two in a row (no "zalgo" towers).
|
|
16
|
+
// - The result is cut to a display width (wide CJK and emoji count two),
|
|
17
|
+
// with "…" when it was longer.
|
|
18
|
+
/** Whole escape sequences: CSI, OSC (BEL or ST terminated), DCS/SOS/PM/APC, two-byte ESC sequences; 7- and 8-bit forms. */
|
|
19
|
+
const ESCAPES = /(?:\u001b\[|\u009b)[ -?]*[@-~]?|(?:\u001b[\]P^_X]|[\u009d\u0090\u009e\u009f\u0098])[\s\S]*?(?:\u0007|\u001b\\|\u009c|$)|\u001b[ -/]*[0-~]?/g;
|
|
20
|
+
const BREAKS = /[\t\n\v\f\r\u0085\u2028\u2029]/g;
|
|
21
|
+
/** Controls, format characters (incl. bidi and zero-width), surrogates, noncharacters, invisible fillers. */
|
|
22
|
+
const STRIP = /[\u0000-\u001f\u007f-\u009fᅟᅠㅤᅠ--]|͏|឴|឵|\p{Cf}|\p{Cs}/gu;
|
|
23
|
+
/** Private use and unassigned: rendered as "?". */
|
|
24
|
+
const UNKNOWN = /[\p{Co}\p{Cn}]/gu;
|
|
25
|
+
const MARK = /\p{M}/u;
|
|
26
|
+
/** Display width of one code point: 0 (combining), 2 (wide / emoji) or 1. */
|
|
27
|
+
export function cpWidth(cp) {
|
|
28
|
+
if (cp === 0x200d)
|
|
29
|
+
return 0;
|
|
30
|
+
if (MARK.test(String.fromCodePoint(cp)))
|
|
31
|
+
return 0;
|
|
32
|
+
if ((cp >= 0x1100 && cp <= 0x115f) ||
|
|
33
|
+
(cp >= 0x2e80 && cp <= 0x303e) ||
|
|
34
|
+
(cp >= 0x3041 && cp <= 0x33ff) ||
|
|
35
|
+
(cp >= 0x3400 && cp <= 0x4dbf) ||
|
|
36
|
+
(cp >= 0x4e00 && cp <= 0x9fff) ||
|
|
37
|
+
(cp >= 0xa000 && cp <= 0xa4cf) ||
|
|
38
|
+
(cp >= 0xac00 && cp <= 0xd7a3) ||
|
|
39
|
+
(cp >= 0xf900 && cp <= 0xfaff) ||
|
|
40
|
+
(cp >= 0xfe30 && cp <= 0xfe4f) ||
|
|
41
|
+
(cp >= 0xff00 && cp <= 0xff60) ||
|
|
42
|
+
(cp >= 0xffe0 && cp <= 0xffe6) ||
|
|
43
|
+
(cp >= 0x1f300 && cp <= 0x1f64f) ||
|
|
44
|
+
(cp >= 0x1f900 && cp <= 0x1f9ff) ||
|
|
45
|
+
(cp >= 0x1f680 && cp <= 0x1f6ff) ||
|
|
46
|
+
(cp >= 0x20000 && cp <= 0x3fffd)) {
|
|
47
|
+
return 2;
|
|
48
|
+
}
|
|
49
|
+
return 1;
|
|
50
|
+
}
|
|
51
|
+
export function displayWidth(s) {
|
|
52
|
+
let w = 0;
|
|
53
|
+
for (const ch of s)
|
|
54
|
+
w += cpWidth(ch.codePointAt(0));
|
|
55
|
+
return w;
|
|
56
|
+
}
|
|
57
|
+
/** Peer-safe text: escapes and invisible or control characters removed, whitespace collapsed and trimmed. No width limit. */
|
|
58
|
+
export function clean(s) {
|
|
59
|
+
return cleanKeep(s).trim();
|
|
60
|
+
}
|
|
61
|
+
/** clean() without the trim: the renderer's own spacing (" Peer2Peer ", "files: ") survives. */
|
|
62
|
+
export function cleanKeep(s) {
|
|
63
|
+
const str = typeof s === "string" ? s : s === undefined || s === null ? "" : String(s);
|
|
64
|
+
let out = str.replace(ESCAPES, "").replace(BREAKS, " ").replace(STRIP, "").replace(UNKNOWN, "?").replace(/\s+/gu, " ");
|
|
65
|
+
// At most two combining marks after a base character.
|
|
66
|
+
let marks = 0;
|
|
67
|
+
let kept = "";
|
|
68
|
+
for (const ch of out) {
|
|
69
|
+
if (MARK.test(ch)) {
|
|
70
|
+
if (++marks > 2)
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
else
|
|
74
|
+
marks = 0;
|
|
75
|
+
kept += ch;
|
|
76
|
+
}
|
|
77
|
+
out = kept;
|
|
78
|
+
// A leading combining mark would attach to whatever the renderer drew before it.
|
|
79
|
+
return out.replace(/^\p{M}+/u, "");
|
|
80
|
+
}
|
|
81
|
+
/** clean(), then cut to `width` columns ("…" marks a cut). */
|
|
82
|
+
export function cell(s, width) {
|
|
83
|
+
const t = cleanKeep(s);
|
|
84
|
+
if (width <= 0)
|
|
85
|
+
return "";
|
|
86
|
+
if (displayWidth(t) <= width)
|
|
87
|
+
return t;
|
|
88
|
+
let w = 0;
|
|
89
|
+
let out = "";
|
|
90
|
+
for (const ch of t) {
|
|
91
|
+
const cw = cpWidth(ch.codePointAt(0));
|
|
92
|
+
if (w + cw > width - 1)
|
|
93
|
+
break;
|
|
94
|
+
out += ch;
|
|
95
|
+
w += cw;
|
|
96
|
+
}
|
|
97
|
+
return `${out}…`;
|
|
98
|
+
}
|
|
99
|
+
/** cell(), padded with spaces to exactly `width` columns. */
|
|
100
|
+
export function pad(s, width) {
|
|
101
|
+
const t = cell(s, width);
|
|
102
|
+
return t + " ".repeat(Math.max(0, width - displayWidth(t)));
|
|
103
|
+
}
|
|
104
|
+
/** Word-wraps clean text into lines of at most `width` columns (at most `maxLines`, the last one cut). */
|
|
105
|
+
export function wrap(s, width, maxLines = 50) {
|
|
106
|
+
const t = clean(s);
|
|
107
|
+
if (width <= 0)
|
|
108
|
+
return [];
|
|
109
|
+
const lines = [];
|
|
110
|
+
let line = "";
|
|
111
|
+
for (const word of t.split(" ")) {
|
|
112
|
+
let w = word;
|
|
113
|
+
while (displayWidth(w) > width) {
|
|
114
|
+
// A word longer than the line: hard-cut it.
|
|
115
|
+
if (line) {
|
|
116
|
+
lines.push(line);
|
|
117
|
+
line = "";
|
|
118
|
+
}
|
|
119
|
+
let head = "";
|
|
120
|
+
let hw = 0;
|
|
121
|
+
const chars = [...w];
|
|
122
|
+
let i = 0;
|
|
123
|
+
for (; i < chars.length; i++) {
|
|
124
|
+
const cw = cpWidth(chars[i].codePointAt(0));
|
|
125
|
+
// Always at least one character per line (a wide one on a 1-column line), so this ends.
|
|
126
|
+
if (hw + cw > width && i > 0)
|
|
127
|
+
break;
|
|
128
|
+
head += chars[i];
|
|
129
|
+
hw += cw;
|
|
130
|
+
}
|
|
131
|
+
lines.push(head);
|
|
132
|
+
w = chars.slice(i).join("");
|
|
133
|
+
}
|
|
134
|
+
if (!line)
|
|
135
|
+
line = w;
|
|
136
|
+
else if (displayWidth(line) + 1 + displayWidth(w) <= width)
|
|
137
|
+
line += ` ${w}`;
|
|
138
|
+
else {
|
|
139
|
+
lines.push(line);
|
|
140
|
+
line = w;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
if (line)
|
|
144
|
+
lines.push(line);
|
|
145
|
+
if (lines.length > maxLines) {
|
|
146
|
+
const cut = lines.slice(0, maxLines);
|
|
147
|
+
cut[maxLines - 1] = cell(`${cut[maxLines - 1]} ${lines[maxLines]}`, width);
|
|
148
|
+
return cut;
|
|
149
|
+
}
|
|
150
|
+
return lines;
|
|
151
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import type { ReadStream, WriteStream } from "node:tty";
|
|
2
|
+
import type { Key } from "./model.js";
|
|
3
|
+
export interface Terminal {
|
|
4
|
+
readonly cols: number;
|
|
5
|
+
readonly rows: number;
|
|
6
|
+
readonly color: boolean;
|
|
7
|
+
enter(): void;
|
|
8
|
+
leave(): void;
|
|
9
|
+
write(s: string): void;
|
|
10
|
+
onKey(fn: (k: Key) => void): void;
|
|
11
|
+
onResize(fn: () => void): void;
|
|
12
|
+
}
|
|
13
|
+
/** NO_COLOR (any non-empty value) and TERM=dumb turn colour off; otherwise the terminal says. */
|
|
14
|
+
export declare function colorWanted(env: Record<string, string | undefined>, out: {
|
|
15
|
+
hasColors?: (n?: number) => boolean;
|
|
16
|
+
isTTY?: boolean;
|
|
17
|
+
}): boolean;
|
|
18
|
+
/** After a paste ends, keys this soon after it count as pasted too (a fake end marker inside the paste). */
|
|
19
|
+
export declare const PASTE_QUIET_MS = 150;
|
|
20
|
+
/** A paste, or a terminal answer, with no input for this long is over (its end marker never came). */
|
|
21
|
+
export declare const STUCK_MS = 1000;
|
|
22
|
+
/** A lone ESC with nothing after it for this long is the Escape key. */
|
|
23
|
+
export declare const ESC_MS = 50;
|
|
24
|
+
/**
|
|
25
|
+
* Splits raw input into keys, keeping state between chunks: a terminal's
|
|
26
|
+
* answer or a key's escape sequence split across reads is completed by the
|
|
27
|
+
* next one, never leaked as typed characters.
|
|
28
|
+
*
|
|
29
|
+
* - Escape sequences of the keys we use are recognised; any other escape
|
|
30
|
+
* or string sequence is dropped whole, however long (a string sequence
|
|
31
|
+
* is discarded up to its terminator, across chunks).
|
|
32
|
+
* - Text inside a bracketed paste comes through with `paste: true` (line
|
|
33
|
+
* breaks as spaces, controls dropped): the model types it only into a
|
|
34
|
+
* message or a prompt, never as command keys. For PASTE_QUIET_MS after
|
|
35
|
+
* a paste ends, keys still count as pasted: a paste carrying its own
|
|
36
|
+
* end marker (`ESC[201~ay`) can't press `a` or `y`.
|
|
37
|
+
* - Ctrl-C always gets through, even mid-paste or mid-answer: the way out.
|
|
38
|
+
* - Time-outs (idle(), called by the terminal after waitMs of no input): a
|
|
39
|
+
* lone ESC is the Escape key; a paste or answer whose end never came is
|
|
40
|
+
* over (STUCK_MS), so the TUI can't lock up.
|
|
41
|
+
*/
|
|
42
|
+
export declare class KeyParser {
|
|
43
|
+
private readonly now;
|
|
44
|
+
private pending;
|
|
45
|
+
private pasting;
|
|
46
|
+
/** Inside a string sequence (OSC, DCS, ...) whose terminator hasn't come: dropping input. */
|
|
47
|
+
private discarding;
|
|
48
|
+
private quietUntil;
|
|
49
|
+
constructor(now?: () => number);
|
|
50
|
+
feed(chunk: string): Key[];
|
|
51
|
+
/** How long the terminal should wait for more input before calling idle() (0: no need). */
|
|
52
|
+
get waitMs(): number;
|
|
53
|
+
/** No more input came: a lone ESC is Escape, and a paste or answer whose end never came is over. */
|
|
54
|
+
idle(): Key[];
|
|
55
|
+
/** idle(), for callers that just want what's held. */
|
|
56
|
+
flush(): Key[];
|
|
57
|
+
get waiting(): boolean;
|
|
58
|
+
/**
|
|
59
|
+
* Text after a paste's start marker: the paste ends at its end marker (the rest is returned, to be
|
|
60
|
+
* read as keys), or waits for more (undefined). Ctrl-C before the end drops the paste and the rest
|
|
61
|
+
* of the read (undefined).
|
|
62
|
+
*/
|
|
63
|
+
private paste;
|
|
64
|
+
private endPaste;
|
|
65
|
+
}
|
|
66
|
+
/** One chunk on its own (tests): parse, then flush. */
|
|
67
|
+
export declare function parseKeys(data: string): Key[];
|
|
68
|
+
export declare class TtyTerminal implements Terminal {
|
|
69
|
+
private readonly input;
|
|
70
|
+
private readonly output;
|
|
71
|
+
readonly color: boolean;
|
|
72
|
+
private active;
|
|
73
|
+
private readonly exitHook;
|
|
74
|
+
private keyFn?;
|
|
75
|
+
private readonly parser;
|
|
76
|
+
/** Multi-byte characters split across reads stay whole. */
|
|
77
|
+
private readonly decoder;
|
|
78
|
+
private escTimer?;
|
|
79
|
+
private readonly onData;
|
|
80
|
+
constructor(input: ReadStream, output: WriteStream, env: Record<string, string | undefined>);
|
|
81
|
+
get cols(): number;
|
|
82
|
+
get rows(): number;
|
|
83
|
+
enter(): void;
|
|
84
|
+
leave(): void;
|
|
85
|
+
write(s: string): void;
|
|
86
|
+
onKey(fn: (k: Key) => void): void;
|
|
87
|
+
onResize(fn: () => void): void;
|
|
88
|
+
}
|
|
@@ -0,0 +1,354 @@
|
|
|
1
|
+
// Copyright 2026 Ix Infrastructure Inc.
|
|
2
|
+
//
|
|
3
|
+
// The terminal under the TUI: raw keyboard input, the alternate screen,
|
|
4
|
+
// resize events and colour detection, with nothing but node:tty. A fake
|
|
5
|
+
// implementation (tests) records frames instead.
|
|
6
|
+
//
|
|
7
|
+
// Restoring the terminal is the one thing that must always happen: leave()
|
|
8
|
+
// is idempotent and runs on quit, on errors, on SIGTERM/SIGHUP and, as a
|
|
9
|
+
// last resort, on process exit.
|
|
10
|
+
import { StringDecoder } from "node:string_decoder";
|
|
11
|
+
/** NO_COLOR (any non-empty value) and TERM=dumb turn colour off; otherwise the terminal says. */
|
|
12
|
+
export function colorWanted(env, out) {
|
|
13
|
+
if (env.NO_COLOR !== undefined && env.NO_COLOR !== "")
|
|
14
|
+
return false;
|
|
15
|
+
if (env.TERM === "dumb")
|
|
16
|
+
return false;
|
|
17
|
+
if (env.FORCE_COLOR !== undefined && env.FORCE_COLOR !== "" && env.FORCE_COLOR !== "0")
|
|
18
|
+
return true;
|
|
19
|
+
try {
|
|
20
|
+
return Boolean(out.isTTY) && (out.hasColors?.(8) ?? true);
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
const CSI_KEYS = {
|
|
27
|
+
A: "up",
|
|
28
|
+
B: "down",
|
|
29
|
+
C: "right",
|
|
30
|
+
D: "left",
|
|
31
|
+
H: "home",
|
|
32
|
+
F: "end",
|
|
33
|
+
Z: "backtab",
|
|
34
|
+
"1~": "home",
|
|
35
|
+
"4~": "end",
|
|
36
|
+
"5~": "pageup",
|
|
37
|
+
"6~": "pagedown",
|
|
38
|
+
"7~": "home",
|
|
39
|
+
"8~": "end",
|
|
40
|
+
"3~": "delete",
|
|
41
|
+
};
|
|
42
|
+
/** A bracketed paste longer than this is cut (the rest is dropped). */
|
|
43
|
+
const PASTE_MAX = 64 * 1024;
|
|
44
|
+
/** An escape sequence left open this long is given up (and nothing of it is typed). */
|
|
45
|
+
const PENDING_MAX = 4096;
|
|
46
|
+
/** After a paste ends, keys this soon after it count as pasted too (a fake end marker inside the paste). */
|
|
47
|
+
export const PASTE_QUIET_MS = 150;
|
|
48
|
+
/** A paste, or a terminal answer, with no input for this long is over (its end marker never came). */
|
|
49
|
+
export const STUCK_MS = 1000;
|
|
50
|
+
/** A lone ESC with nothing after it for this long is the Escape key. */
|
|
51
|
+
export const ESC_MS = 50;
|
|
52
|
+
const CTRL_C = { name: "c", ctrl: true };
|
|
53
|
+
/**
|
|
54
|
+
* Splits raw input into keys, keeping state between chunks: a terminal's
|
|
55
|
+
* answer or a key's escape sequence split across reads is completed by the
|
|
56
|
+
* next one, never leaked as typed characters.
|
|
57
|
+
*
|
|
58
|
+
* - Escape sequences of the keys we use are recognised; any other escape
|
|
59
|
+
* or string sequence is dropped whole, however long (a string sequence
|
|
60
|
+
* is discarded up to its terminator, across chunks).
|
|
61
|
+
* - Text inside a bracketed paste comes through with `paste: true` (line
|
|
62
|
+
* breaks as spaces, controls dropped): the model types it only into a
|
|
63
|
+
* message or a prompt, never as command keys. For PASTE_QUIET_MS after
|
|
64
|
+
* a paste ends, keys still count as pasted: a paste carrying its own
|
|
65
|
+
* end marker (`ESC[201~ay`) can't press `a` or `y`.
|
|
66
|
+
* - Ctrl-C always gets through, even mid-paste or mid-answer: the way out.
|
|
67
|
+
* - Time-outs (idle(), called by the terminal after waitMs of no input): a
|
|
68
|
+
* lone ESC is the Escape key; a paste or answer whose end never came is
|
|
69
|
+
* over (STUCK_MS), so the TUI can't lock up.
|
|
70
|
+
*/
|
|
71
|
+
export class KeyParser {
|
|
72
|
+
now;
|
|
73
|
+
pending = "";
|
|
74
|
+
pasting;
|
|
75
|
+
/** Inside a string sequence (OSC, DCS, ...) whose terminator hasn't come: dropping input. */
|
|
76
|
+
discarding = false;
|
|
77
|
+
quietUntil = 0;
|
|
78
|
+
constructor(now = Date.now) {
|
|
79
|
+
this.now = now;
|
|
80
|
+
}
|
|
81
|
+
feed(chunk) {
|
|
82
|
+
const keys = [];
|
|
83
|
+
const push = (k) => {
|
|
84
|
+
// Shortly after a paste ended, everything is still pasted (except Ctrl-C).
|
|
85
|
+
if (!k.paste && !(k.ctrl && k.name === "c") && this.now() < this.quietUntil) {
|
|
86
|
+
if (k.ch !== undefined)
|
|
87
|
+
keys.push({ ...k, paste: true });
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
keys.push(k);
|
|
91
|
+
};
|
|
92
|
+
let data = this.pending + chunk;
|
|
93
|
+
this.pending = "";
|
|
94
|
+
if (this.discarding) {
|
|
95
|
+
const end = data.search(/\u0007|\u001b\\|\u0003/);
|
|
96
|
+
if (end < 0) {
|
|
97
|
+
// Keep a final ESC: it may be the first half of ST.
|
|
98
|
+
this.pending = data.endsWith("\u001b") ? "\u001b" : "";
|
|
99
|
+
return keys;
|
|
100
|
+
}
|
|
101
|
+
this.discarding = false;
|
|
102
|
+
if (data[end] === "\u0003") {
|
|
103
|
+
// Ctrl-C: the way out; whatever came with it in this read is dropped.
|
|
104
|
+
push(CTRL_C);
|
|
105
|
+
return keys;
|
|
106
|
+
}
|
|
107
|
+
data = data.slice(end + 2 - (data[end] === "\u0007" ? 1 : 0));
|
|
108
|
+
}
|
|
109
|
+
if (this.pasting !== undefined) {
|
|
110
|
+
// The paste so far and the new data, searched as one: an end marker split across reads is found.
|
|
111
|
+
const buf = this.pasting + data;
|
|
112
|
+
this.pasting = undefined;
|
|
113
|
+
const after = this.paste(keys, buf, push);
|
|
114
|
+
if (after === undefined)
|
|
115
|
+
return keys;
|
|
116
|
+
data = after;
|
|
117
|
+
}
|
|
118
|
+
let i = 0;
|
|
119
|
+
while (i < data.length) {
|
|
120
|
+
const c = data[i];
|
|
121
|
+
if (c === "\u001b") {
|
|
122
|
+
const next = data[i + 1];
|
|
123
|
+
if (next === undefined) {
|
|
124
|
+
// Maybe the start of a sequence: wait for more (idle() makes it Escape).
|
|
125
|
+
this.pending = "\u001b";
|
|
126
|
+
break;
|
|
127
|
+
}
|
|
128
|
+
if (next === "\u0003") {
|
|
129
|
+
// ESC then Ctrl-C: Ctrl-C still gets through.
|
|
130
|
+
push(CTRL_C);
|
|
131
|
+
return keys;
|
|
132
|
+
}
|
|
133
|
+
if (next === "[" || next === "O") {
|
|
134
|
+
let j = i + 2;
|
|
135
|
+
while (j < data.length && /[0-9;?]/.test(data[j]))
|
|
136
|
+
j++;
|
|
137
|
+
if (j >= data.length) {
|
|
138
|
+
// The final byte hasn't arrived yet.
|
|
139
|
+
this.pending = data.slice(i, i + PENDING_MAX);
|
|
140
|
+
break;
|
|
141
|
+
}
|
|
142
|
+
if (data[j] === "\u0003") {
|
|
143
|
+
// Ctrl-C in the middle of a key's sequence: still the way out.
|
|
144
|
+
push(CTRL_C);
|
|
145
|
+
return keys;
|
|
146
|
+
}
|
|
147
|
+
const body = data.slice(i + 2, j + 1);
|
|
148
|
+
i = j + 1;
|
|
149
|
+
if (body === "200~") {
|
|
150
|
+
const after = this.paste(keys, data.slice(i), push);
|
|
151
|
+
if (after === undefined)
|
|
152
|
+
return keys;
|
|
153
|
+
data = after;
|
|
154
|
+
i = 0;
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
const name = CSI_KEYS[body] ?? CSI_KEYS[body.replace(/^1;\d/, "")];
|
|
158
|
+
if (name === "backtab")
|
|
159
|
+
push({ name: "tab", shift: true });
|
|
160
|
+
else if (name)
|
|
161
|
+
push({ name });
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
if (next === "]" || next === "P" || next === "_" || next === "^" || next === "X") {
|
|
165
|
+
// A string sequence (a terminal's answer): skip to its terminator, across chunks, however long.
|
|
166
|
+
const rest = data.slice(i + 2);
|
|
167
|
+
const end = rest.search(/\u0007|\u001b\\|\u0003/);
|
|
168
|
+
if (end < 0) {
|
|
169
|
+
this.discarding = true;
|
|
170
|
+
this.pending = rest.endsWith("\u001b") ? "\u001b" : "";
|
|
171
|
+
break;
|
|
172
|
+
}
|
|
173
|
+
if (rest[end] === "\u0003") {
|
|
174
|
+
push(CTRL_C);
|
|
175
|
+
return keys;
|
|
176
|
+
}
|
|
177
|
+
i += 2 + end + (rest[end] === "\u001b" ? 2 : 1);
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
if (next !== "\u001b") {
|
|
181
|
+
// Alt+key: ignored.
|
|
182
|
+
i += 2;
|
|
183
|
+
continue;
|
|
184
|
+
}
|
|
185
|
+
push({ name: "escape" });
|
|
186
|
+
i++;
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
const code = c.codePointAt(0);
|
|
190
|
+
if (c === "\r" || c === "\n")
|
|
191
|
+
push({ name: "return" });
|
|
192
|
+
else if (c === "\t")
|
|
193
|
+
push({ name: "tab" });
|
|
194
|
+
else if (c === "\u007f" || c === "\b")
|
|
195
|
+
push({ name: "backspace" });
|
|
196
|
+
else if (c === " ")
|
|
197
|
+
push({ name: "space", ch: " " });
|
|
198
|
+
else if (code < 32)
|
|
199
|
+
push({ name: String.fromCharCode(code + 96), ctrl: true });
|
|
200
|
+
else if (code >= 0x80 && code < 0xa0) {
|
|
201
|
+
// C1 controls: never typed.
|
|
202
|
+
}
|
|
203
|
+
else {
|
|
204
|
+
const ch = String.fromCodePoint(data.codePointAt(i));
|
|
205
|
+
push({ name: ch, ch });
|
|
206
|
+
i += ch.length;
|
|
207
|
+
continue;
|
|
208
|
+
}
|
|
209
|
+
i++;
|
|
210
|
+
}
|
|
211
|
+
return keys;
|
|
212
|
+
}
|
|
213
|
+
/** How long the terminal should wait for more input before calling idle() (0: no need). */
|
|
214
|
+
get waitMs() {
|
|
215
|
+
if (this.pasting !== undefined || this.discarding)
|
|
216
|
+
return STUCK_MS;
|
|
217
|
+
return this.pending !== "" ? ESC_MS : 0;
|
|
218
|
+
}
|
|
219
|
+
/** No more input came: a lone ESC is Escape, and a paste or answer whose end never came is over. */
|
|
220
|
+
idle() {
|
|
221
|
+
const keys = [];
|
|
222
|
+
if (this.pasting !== undefined) {
|
|
223
|
+
this.endPaste(keys, this.pasting);
|
|
224
|
+
this.pending = "";
|
|
225
|
+
return keys;
|
|
226
|
+
}
|
|
227
|
+
if (this.discarding) {
|
|
228
|
+
this.discarding = false;
|
|
229
|
+
this.pending = "";
|
|
230
|
+
return keys;
|
|
231
|
+
}
|
|
232
|
+
const p = this.pending;
|
|
233
|
+
this.pending = "";
|
|
234
|
+
return p === "\u001b" ? [{ name: "escape" }] : [];
|
|
235
|
+
}
|
|
236
|
+
/** idle(), for callers that just want what's held. */
|
|
237
|
+
flush() {
|
|
238
|
+
return this.idle();
|
|
239
|
+
}
|
|
240
|
+
get waiting() {
|
|
241
|
+
return this.waitMs > 0;
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* Text after a paste's start marker: the paste ends at its end marker (the rest is returned, to be
|
|
245
|
+
* read as keys), or waits for more (undefined). Ctrl-C before the end drops the paste and the rest
|
|
246
|
+
* of the read (undefined).
|
|
247
|
+
*/
|
|
248
|
+
paste(keys, buf, push) {
|
|
249
|
+
const cc = buf.indexOf("\u0003");
|
|
250
|
+
const end = buf.indexOf("\u001b[201~");
|
|
251
|
+
if (cc >= 0 && (end < 0 || cc < end)) {
|
|
252
|
+
push(CTRL_C);
|
|
253
|
+
return undefined;
|
|
254
|
+
}
|
|
255
|
+
if (end < 0) {
|
|
256
|
+
// Bounded: the head of an over-long paste, and its last few characters (a split end marker).
|
|
257
|
+
this.pasting = buf.length > PASTE_MAX + 16 ? buf.slice(0, PASTE_MAX) + buf.slice(-5) : buf;
|
|
258
|
+
return undefined;
|
|
259
|
+
}
|
|
260
|
+
this.endPaste(keys, buf.slice(0, end));
|
|
261
|
+
return buf.slice(end + 6);
|
|
262
|
+
}
|
|
263
|
+
endPaste(keys, text) {
|
|
264
|
+
this.pasting = undefined;
|
|
265
|
+
for (const ch of text.slice(0, PASTE_MAX).replace(/[\r\n\t]+/g, " ")) {
|
|
266
|
+
const cp = ch.codePointAt(0);
|
|
267
|
+
if (cp >= 32 && !(cp >= 0x7f && cp < 0xa0))
|
|
268
|
+
keys.push({ name: ch, ch, paste: true });
|
|
269
|
+
}
|
|
270
|
+
this.quietUntil = this.now() + PASTE_QUIET_MS;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
/** One chunk on its own (tests): parse, then flush. */
|
|
274
|
+
export function parseKeys(data) {
|
|
275
|
+
const p = new KeyParser();
|
|
276
|
+
return [...p.feed(data), ...p.flush()];
|
|
277
|
+
}
|
|
278
|
+
export class TtyTerminal {
|
|
279
|
+
input;
|
|
280
|
+
output;
|
|
281
|
+
color;
|
|
282
|
+
active = false;
|
|
283
|
+
exitHook = () => this.leave();
|
|
284
|
+
keyFn;
|
|
285
|
+
parser = new KeyParser();
|
|
286
|
+
/** Multi-byte characters split across reads stay whole. */
|
|
287
|
+
decoder = new StringDecoder("utf8");
|
|
288
|
+
escTimer;
|
|
289
|
+
onData = (b) => {
|
|
290
|
+
if (this.escTimer)
|
|
291
|
+
clearTimeout(this.escTimer);
|
|
292
|
+
for (const k of this.parser.feed(typeof b === "string" ? b : this.decoder.write(b)))
|
|
293
|
+
this.keyFn?.(k);
|
|
294
|
+
// A lone ESC is Escape unless the rest follows at once; a paste or answer that never ends is over.
|
|
295
|
+
const ms = this.parser.waitMs;
|
|
296
|
+
if (ms > 0) {
|
|
297
|
+
this.escTimer = setTimeout(() => {
|
|
298
|
+
for (const k of this.parser.idle())
|
|
299
|
+
this.keyFn?.(k);
|
|
300
|
+
}, ms);
|
|
301
|
+
this.escTimer.unref();
|
|
302
|
+
}
|
|
303
|
+
};
|
|
304
|
+
constructor(input, output, env) {
|
|
305
|
+
this.input = input;
|
|
306
|
+
this.output = output;
|
|
307
|
+
this.color = colorWanted(env, output);
|
|
308
|
+
}
|
|
309
|
+
get cols() {
|
|
310
|
+
return this.output.columns || 80;
|
|
311
|
+
}
|
|
312
|
+
get rows() {
|
|
313
|
+
return this.output.rows || 24;
|
|
314
|
+
}
|
|
315
|
+
enter() {
|
|
316
|
+
if (this.active)
|
|
317
|
+
return;
|
|
318
|
+
this.active = true;
|
|
319
|
+
process.on("exit", this.exitHook);
|
|
320
|
+
this.input.setRawMode(true);
|
|
321
|
+
this.input.resume();
|
|
322
|
+
this.input.on("data", this.onData);
|
|
323
|
+
// Alternate screen, hidden cursor, no line wrap, bracketed paste (pasted text is typed only into a
|
|
324
|
+
// message or a prompt, and a pasted line break is a space there: it never acts as a key).
|
|
325
|
+
this.output.write("\u001b[?1049h\u001b[?25l\u001b[?7l\u001b[?2004h\u001b[2J");
|
|
326
|
+
}
|
|
327
|
+
leave() {
|
|
328
|
+
if (!this.active)
|
|
329
|
+
return;
|
|
330
|
+
this.active = false;
|
|
331
|
+
process.off("exit", this.exitHook);
|
|
332
|
+
this.input.off("data", this.onData);
|
|
333
|
+
if (this.escTimer)
|
|
334
|
+
clearTimeout(this.escTimer);
|
|
335
|
+
try {
|
|
336
|
+
this.input.setRawMode(false);
|
|
337
|
+
}
|
|
338
|
+
catch {
|
|
339
|
+
// already gone
|
|
340
|
+
}
|
|
341
|
+
this.input.pause();
|
|
342
|
+
this.output.write("\u001b[0m\u001b[?2004l\u001b[?7h\u001b[?25h\u001b[?1049l");
|
|
343
|
+
}
|
|
344
|
+
write(s) {
|
|
345
|
+
if (this.active)
|
|
346
|
+
this.output.write(s);
|
|
347
|
+
}
|
|
348
|
+
onKey(fn) {
|
|
349
|
+
this.keyFn = fn;
|
|
350
|
+
}
|
|
351
|
+
onResize(fn) {
|
|
352
|
+
this.output.on("resize", fn);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import type { App } from "../app.js";
|
|
2
|
+
import { onKey, type Data, type Effect, type Ui } from "./model.js";
|
|
3
|
+
import type { Terminal } from "./terminal.js";
|
|
4
|
+
export declare const TUI_SESSION = "tui";
|
|
5
|
+
/** A relay's `/.well-known/p2p-relay.json` read at most this far (it is a few hundred bytes). */
|
|
6
|
+
export declare const WELL_KNOWN_MAX: number;
|
|
7
|
+
/** Messages kept in memory (the newest). */
|
|
8
|
+
export declare const MESSAGES_MAX = 100;
|
|
9
|
+
/** A response body as text, refusing more than `max` bytes (the read stops there). */
|
|
10
|
+
export declare function readCapped(body: ReadableStream<Uint8Array>, max: number): Promise<string>;
|
|
11
|
+
export interface TuiOptions {
|
|
12
|
+
term: Terminal;
|
|
13
|
+
session?: string;
|
|
14
|
+
version: string;
|
|
15
|
+
/** Stops the TUI (tests, signals). */
|
|
16
|
+
signal?: AbortSignal;
|
|
17
|
+
now?: () => number;
|
|
18
|
+
/** Lines the app or watch would print to stderr: kept here, shown in the status line. */
|
|
19
|
+
log?: (m: string) => void;
|
|
20
|
+
/** Tests: run without the in-process watch. */
|
|
21
|
+
noWatch?: boolean;
|
|
22
|
+
}
|
|
23
|
+
export declare class Tui {
|
|
24
|
+
readonly app: App;
|
|
25
|
+
private readonly o;
|
|
26
|
+
readonly data: Data;
|
|
27
|
+
ui: Ui;
|
|
28
|
+
private readonly now;
|
|
29
|
+
private readonly stop;
|
|
30
|
+
private renderQueued;
|
|
31
|
+
private timers;
|
|
32
|
+
private refreshTimers;
|
|
33
|
+
private promptAnswer?;
|
|
34
|
+
private sent;
|
|
35
|
+
private closed?;
|
|
36
|
+
lastFrame: string;
|
|
37
|
+
constructor(app: App, o: TuiOptions);
|
|
38
|
+
/** Runs until quit or abort; the terminal is always restored. */
|
|
39
|
+
run(): Promise<void>;
|
|
40
|
+
quit(): void;
|
|
41
|
+
private log;
|
|
42
|
+
private every;
|
|
43
|
+
/** Coalesces refreshes of one kind triggered by bursts of events. */
|
|
44
|
+
private soon;
|
|
45
|
+
draw(): void;
|
|
46
|
+
drawNow(): void;
|
|
47
|
+
private startWatch;
|
|
48
|
+
/** One event from the stream (already in the shared log). */
|
|
49
|
+
onEvent(e: Record<string, unknown>): void;
|
|
50
|
+
private messageFromEvent;
|
|
51
|
+
loadAll(): Promise<void>;
|
|
52
|
+
loadPeople(): Promise<void>;
|
|
53
|
+
loadHeld(): Promise<void>;
|
|
54
|
+
/** Received messages from the event log, and sent ones: this run's, receipts asked for, the outbox. */
|
|
55
|
+
loadMessages(): Promise<void>;
|
|
56
|
+
/** Keeps the newest MESSAGES_MAX messages (this run's sends are capped the same way). */
|
|
57
|
+
private capMessages;
|
|
58
|
+
/** Relay up/down from the in-memory pool (no network). */
|
|
59
|
+
relayState(): Promise<void>;
|
|
60
|
+
/** Each relay's `/.well-known/p2p-relay.json` (access, software, pow): read once per doctor run. */
|
|
61
|
+
private relayInfo;
|
|
62
|
+
runDoctor(user?: string): Promise<void>;
|
|
63
|
+
key(k: Parameters<typeof onKey>[2]): void;
|
|
64
|
+
flash(text: string, error?: boolean): void;
|
|
65
|
+
private busy;
|
|
66
|
+
/** Asks one line in a modal (Codex release codes, circle-join confirmations). */
|
|
67
|
+
private ask;
|
|
68
|
+
effect(e: Effect): Promise<void>;
|
|
69
|
+
/** For tests: resolves once the TUI stopped. */
|
|
70
|
+
done(): Promise<void>;
|
|
71
|
+
}
|