@awebai/oats 0.56.0 → 0.57.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/oats.mjs +18 -10
- package/docs/configuration.md +17 -27
- package/docs/desktop-cli-api.md +59 -3
- package/docs/desktop.md +25 -4
- package/docs/execution-targets.md +6 -1
- package/docs/implementation.md +106 -5
- package/docs/integrations.md +8 -4
- package/docs/knowledge.md +93 -38
- package/docs/official-catalog.md +3 -3
- package/docs/packages.md +12 -12
- package/docs/release-notes/v0.57.0.md +283 -0
- package/docs/release-notes/v0.57.1.md +275 -0
- package/docs/schedules.md +8 -3
- package/docs/servers.md +18 -0
- package/docs/souls-and-instances.md +126 -15
- package/docs/tui.md +333 -95
- package/docs/workspaces.md +2 -2
- package/lib/core.mjs +439 -126
- package/lib/instance-lifecycle.mjs +45 -9
- package/lib/session-viewer.mjs +29 -4
- package/lib/worktree.mjs +23 -2
- package/package-catalog.json +2 -2
- package/package.json +1 -1
- package/packages/client/cli-adapter.mjs +9 -7
- package/packages/client/cli-locator.mjs +2 -2
- package/packages/client/composed-gate.mjs +34 -0
- package/packages/client/inspect-boundary.mjs +157 -0
- package/packages/client/inspect-cache.mjs +86 -0
- package/packages/client/instance-lifecycle.mjs +10 -3
- package/packages/client/keyed-catalog.mjs +132 -0
- package/packages/client/launch-prompt-outcome.mjs +1 -1
- package/packages/client/launch-view.mjs +46 -0
- package/packages/client/session-attach.mjs +7 -5
- package/packages/client/soul-catalog.mjs +77 -0
- package/packages/client/soul-colors.mjs +31 -0
- package/packages/client/soul-facts.mjs +85 -0
- package/packages/client/soul-rows.mjs +48 -0
- package/packages/client/spawn-apply-cli.mjs +11 -6
- package/packages/client/spawn-apply.mjs +212 -0
- package/packages/client/spawn-messages.mjs +131 -85
- package/packages/client/spawn-preview-contract.mjs +3 -0
- package/packages/client/spawn-preview-facts.mjs +119 -0
- package/packages/client/spawn-preview.mjs +152 -0
- package/packages/client/spawn-transaction.mjs +136 -0
- package/packages/tui/README.md +43 -13
- package/packages/tui/lib/actions.mjs +18 -2
- package/packages/tui/lib/client.mjs +4 -0
- package/packages/tui/lib/config.mjs +9 -14
- package/packages/tui/lib/main.mjs +241 -55
- package/packages/tui/lib/model.mjs +230 -29
- package/packages/tui/lib/panel.mjs +955 -0
- package/packages/tui/lib/read.mjs +134 -24
- package/packages/tui/lib/store.mjs +184 -31
- package/packages/tui/lib/term/capture.mjs +260 -0
- package/packages/tui/lib/term/screen.mjs +44 -8
- package/packages/tui/lib/term/style.mjs +78 -6
- package/packages/tui/lib/term/text.mjs +24 -4
- package/packages/tui/lib/term/through.mjs +213 -0
- package/packages/tui/lib/term/tty.mjs +7 -60
- package/packages/tui/lib/views/act.mjs +85 -0
- package/packages/tui/lib/views/frame.mjs +153 -37
- package/packages/tui/lib/views/help.mjs +22 -14
- package/skills/oats-getting-started/SKILL.md +4 -3
- package/packages/tui/lib/open.mjs +0 -45
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
// What is typed through to an agent's terminal (docs/tui.md, "Open an agent's terminal"). While
|
|
2
|
+
// a terminal has the focus the TUI reads no keys: the bytes the outer terminal sends go to the
|
|
3
|
+
// agent exactly as they came, and the scanner here looks for two things only, the back key's own
|
|
4
|
+
// bytes (which take the focus back to the list) and a bracketed paste (which goes to the agent as
|
|
5
|
+
// a paste, so a back key inside it is pasted, not taken). Pure: no IO; the clock is injected.
|
|
6
|
+
//
|
|
7
|
+
// The back key is tui.json's `open.detachKey`. Its grammar is THE TUI'S OWN: the keys it can take
|
|
8
|
+
// back from a terminal and name in the footer. It began as the kernel's `--detach-key` grammar
|
|
9
|
+
// (lib/session-viewer.mjs) and keeps its set and its spelling, so a tui.json written for PR 4 means
|
|
10
|
+
// the same key; it is not tied to the kernel's, which never sees this key any more.
|
|
11
|
+
import { ESC_TIMEOUT_MS, PASTE_LIMIT, PASTE_PAUSE_MS } from "./keys.mjs";
|
|
12
|
+
|
|
13
|
+
/** The keys that can be the back key: Control with a letter other than h, i and m (Backspace on
|
|
14
|
+
* some terminals, Tab, Enter), or one of \ ] ^ _; Meta with a lowercase letter or a digit; or F1
|
|
15
|
+
* to F12 with at most one of C-, M-, S-. Outside it, among others: Escape, and the ESC + C1
|
|
16
|
+
* introducers a terminal's reply can begin with. */
|
|
17
|
+
export const BACK_KEY = /^(?:C-[a-gj-ln-z\\\]^_]|M-[a-z0-9]|(?:[CMS]-)?F(?:[1-9]|1[0-2]))$/;
|
|
18
|
+
/** The grammar as the refusal of a key outside it says it. */
|
|
19
|
+
export const BACK_KEY_RULE = "C-<a letter except h, i and m, or one of \\ ] ^ _>, M-<a lowercase letter or digit>, or F1 to F12 with at most one of C-, M-, S-";
|
|
20
|
+
export const isBackKey = (value) => typeof value === "string" && BACK_KEY.test(value);
|
|
21
|
+
|
|
22
|
+
const ESC = 0x1b;
|
|
23
|
+
/** An escape sequence as bytes: ESC, then the printable ASCII that follows it. Built from the
|
|
24
|
+
* number, as term/keys.mjs does: no module but the screen and the guard writes an ESC in a string. */
|
|
25
|
+
const escaped = (rest) => Uint8Array.of(ESC, ...Array.from(rest, (c) => c.charCodeAt(0)));
|
|
26
|
+
/** The xterm modifier parameter of S-, M- and C-. */
|
|
27
|
+
const MODIFIER = Object.freeze({ S: 2, M: 3, C: 5 });
|
|
28
|
+
/** The number of F5..F12 in `CSI <n> ~`, and of F1..F4 in the same form (the vt220 and rxvt one). */
|
|
29
|
+
const TILDE = Object.freeze({ 1: 11, 2: 12, 3: 13, 4: 14, 5: 15, 6: 17, 7: 18, 8: 19, 9: 20, 10: 21, 11: 23, 12: 24 });
|
|
30
|
+
|
|
31
|
+
/** The byte sequences a terminal sends for a back key, each as bytes: what the scanner takes as
|
|
32
|
+
* the key. A Control key is one byte; a Meta key is ESC and the character; a function key has
|
|
33
|
+
* each form the key parser (term/keys.mjs) reads for it: `SS3 P..S` and `CSI 1 ; m P..S` for F1 to
|
|
34
|
+
* F4, `CSI n ~` and `CSI n ; m ~` for all twelve, and the Linux console's `CSI [ A..E` for an
|
|
35
|
+
* unmodified F1 to F5. A value outside the grammar is refused. */
|
|
36
|
+
export function backKeySequences(key) {
|
|
37
|
+
if (!isBackKey(key)) throw new TypeError("not a back key");
|
|
38
|
+
if (key.startsWith("C-") && !key.startsWith("C-F")) return [Uint8Array.of(key.charCodeAt(2) & 0x1f)];
|
|
39
|
+
if (key.startsWith("M-") && !key.startsWith("M-F")) return [Uint8Array.of(ESC, key.charCodeAt(2))];
|
|
40
|
+
const [, mod, number] = /^(?:([CMS])-)?F(\d+)$/.exec(key);
|
|
41
|
+
const n = Number(number), m = mod ? MODIFIER[mod] : null, forms = [];
|
|
42
|
+
if (n <= 4) {
|
|
43
|
+
const final = "PQRS"[n - 1];
|
|
44
|
+
forms.push(m === null ? `O${final}` : `[1;${m}${final}`);
|
|
45
|
+
if (m !== null) forms.push(`O${m}${final}`);
|
|
46
|
+
}
|
|
47
|
+
forms.push(m === null ? `[${TILDE[n]}~` : `[${TILDE[n]};${m}~`);
|
|
48
|
+
if (m === null && n <= 5) forms.push(`[[${"ABCDE"[n - 1]}`);
|
|
49
|
+
return forms.map(escaped);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const PASTE_START = escaped("[200~");
|
|
53
|
+
const PASTE_END = escaped("[201~");
|
|
54
|
+
/** The first bytes of a paste's start marker that are held when a read ends on them: from `ESC [ 2`
|
|
55
|
+
* on. A lone ESC or `ESC [` is never held for the paste's sake (an agent's Esc must not wait):
|
|
56
|
+
* they go to the agent at once, and when the next read completes the marker (within
|
|
57
|
+
* PASTE_PAUSE_MS) the paste goes on to the agent RAW, as typed, its marks included, with no back
|
|
58
|
+
* key taken from it until its end marker (round 1 of the review: a split there must not let a
|
|
59
|
+
* back key inside a paste, or the keys after it, act). */
|
|
60
|
+
const PASTE_HELD_FROM = 3;
|
|
61
|
+
|
|
62
|
+
/** Whether `data` holds `seq` at `at`, whole. */
|
|
63
|
+
function at(data, from, seq) {
|
|
64
|
+
if (from + seq.length > data.length) return false;
|
|
65
|
+
for (let i = 0; i < seq.length; i++) if (data[from + i] !== seq[i]) return false;
|
|
66
|
+
return true;
|
|
67
|
+
}
|
|
68
|
+
/** Whether `data` from `from` to its end is the beginning of `seq`, and shorter than it. */
|
|
69
|
+
function begins(data, from, seq) {
|
|
70
|
+
const left = data.length - from;
|
|
71
|
+
if (left <= 0 || left >= seq.length) return false;
|
|
72
|
+
for (let i = 0; i < left; i++) if (data[from + i] !== seq[i]) return false;
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
75
|
+
const join = (a, b) => { if (a.length === 0) return b; const out = new Uint8Array(a.length + b.length); out.set(a); out.set(b, a.length); return out; };
|
|
76
|
+
const decoder = () => new TextDecoder("utf-8", { fatal: false });
|
|
77
|
+
|
|
78
|
+
/** A scanner of the bytes typed while a terminal has the focus. `feed(bytes)` answers events in
|
|
79
|
+
* order: `{ input: <bytes> }` (for the agent, as typed), `{ paste: <text> }` (a bracketed paste,
|
|
80
|
+
* unwrapped) and, at most once per feed and last, `{ back: true, rest: <bytes> }` (the back key:
|
|
81
|
+
* what came after it in the same read is no longer the agent's, and `rest` hands it on).
|
|
82
|
+
* A read is never held back from the agent but for a beginning of the back key's own bytes, or of
|
|
83
|
+
* a paste's start marker from its third byte: those wait for the next read, or until `expire()`
|
|
84
|
+
* past `timeoutAt()` (the key parser's Esc timeout) hands them on as input. A paste is held until
|
|
85
|
+
* its end marker; one that reaches PASTE_LIMIT is dropped whole, and one whose end has not come
|
|
86
|
+
* when a read arrives PASTE_PAUSE_MS after the one before is given to the agent as what it held,
|
|
87
|
+
* and that read is scanned anew, as the key parser abandons a stalled paste. */
|
|
88
|
+
export function createThrough({ sequences, now }) {
|
|
89
|
+
if (typeof now !== "function") throw new TypeError("createThrough needs an injected clock: { now: () => milliseconds }");
|
|
90
|
+
const keys = sequences.map((seq) => Uint8Array.from(seq));
|
|
91
|
+
let held = new Uint8Array(0), heldAt = 0;
|
|
92
|
+
/** The paste being read: its content so far (null when none; only the tail that may begin the
|
|
93
|
+
* end marker once it went past the limit), and whether it did. */
|
|
94
|
+
let paste = null, over = false, lastFeed = 0;
|
|
95
|
+
/** The last bytes handed on as input (at most a start marker less one), since the last event
|
|
96
|
+
* that was not input: by a read, or by expire() (round 2 of the review: bytes the Esc timeout
|
|
97
|
+
* handed on are as much the start of a paste as bytes a read did); how many first bytes of a
|
|
98
|
+
* paste's start marker they end with (0 to 5); and the paste passed on raw (see
|
|
99
|
+
* PASTE_HELD_FROM), with the last bytes it passed (`tail`, which may begin its end marker), or
|
|
100
|
+
* null. */
|
|
101
|
+
let sent = new Uint8Array(0), forwarded = 0, raw = null;
|
|
102
|
+
|
|
103
|
+
function endPaste(out, content) {
|
|
104
|
+
if (!over) out.push({ paste: decoder().decode(content) });
|
|
105
|
+
paste = null; over = false;
|
|
106
|
+
}
|
|
107
|
+
/** Inside a paste: content up to the end marker, which may arrive split at any byte (the
|
|
108
|
+
* content's tail is kept until it is known not to begin the marker). Answers what is left. */
|
|
109
|
+
function inPaste(out, data) {
|
|
110
|
+
const content = join(paste, data);
|
|
111
|
+
const scanFrom = Math.max(0, paste.length - (PASTE_END.length - 1));
|
|
112
|
+
for (let i = scanFrom; i < content.length; i++) {
|
|
113
|
+
if (content[i] === ESC && at(content, i, PASTE_END)) {
|
|
114
|
+
endPaste(out, content.subarray(0, i));
|
|
115
|
+
return content.subarray(i + PASTE_END.length);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
if (content.length >= PASTE_LIMIT) { over = true; paste = Uint8Array.from(content.subarray(content.length - (PASTE_END.length - 1))); return new Uint8Array(0); }
|
|
119
|
+
paste = paste.length === 0 ? Uint8Array.from(content) : content; // never a view of the caller's read
|
|
120
|
+
return new Uint8Array(0);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** A raw paste: every byte to the agent as it came, up to and with the end marker, which may be
|
|
124
|
+
* split across reads. Answers what is left after it, or nothing while it goes on. */
|
|
125
|
+
function inRaw(out, data) {
|
|
126
|
+
const seen = join(raw.tail, data);
|
|
127
|
+
for (let i = 0; i + PASTE_END.length <= seen.length; i++) {
|
|
128
|
+
if (seen[i] === ESC && at(seen, i, PASTE_END)) {
|
|
129
|
+
const end = i + PASTE_END.length - raw.tail.length;
|
|
130
|
+
if (end > 0) out.push({ input: data.subarray(0, end) });
|
|
131
|
+
raw = null;
|
|
132
|
+
return data.subarray(Math.max(0, end));
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
if (data.length) out.push({ input: data });
|
|
136
|
+
raw.tail = Uint8Array.from(seen.subarray(Math.max(0, seen.length - (PASTE_END.length - 1))));
|
|
137
|
+
return new Uint8Array(0);
|
|
138
|
+
}
|
|
139
|
+
/** Take note of the events handed out, and answer how many first bytes of a paste's start
|
|
140
|
+
* marker the input handed on now ends with: at any length, and across events and calls. */
|
|
141
|
+
function handedOn(out) {
|
|
142
|
+
for (const event of out) sent = event.input ? join(sent, event.input).slice(-(PASTE_START.length - 1)) : new Uint8Array(0);
|
|
143
|
+
if (held.length || paste !== null || raw !== null) return 0;
|
|
144
|
+
for (let k = Math.min(sent.length, PASTE_START.length - 1); k > 0; k--) if (at(sent, sent.length - k, PASTE_START.subarray(0, k))) return k;
|
|
145
|
+
return 0;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function scan(out, data) {
|
|
149
|
+
for (let i = 0; i < data.length; i++) {
|
|
150
|
+
const b = data[i];
|
|
151
|
+
if (b === ESC && at(data, i, PASTE_START)) {
|
|
152
|
+
if (i > 0) out.push({ input: data.subarray(0, i) });
|
|
153
|
+
paste = new Uint8Array(0); over = false;
|
|
154
|
+
const rest = inPaste(out, data.subarray(i + PASTE_START.length));
|
|
155
|
+
if (paste !== null) return; // the paste goes on in the next read
|
|
156
|
+
return scan(out, rest);
|
|
157
|
+
}
|
|
158
|
+
const key = keys.find((seq) => seq[0] === b && at(data, i, seq));
|
|
159
|
+
if (key) {
|
|
160
|
+
if (i > 0) out.push({ input: data.subarray(0, i) });
|
|
161
|
+
out.push({ back: true, rest: data.subarray(i + key.length) });
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
// The read ends on what may be the beginning of the back key or of a paste: held.
|
|
165
|
+
if (keys.some((seq) => begins(data, i, seq)) || (data.length - i >= PASTE_HELD_FROM && begins(data, i, PASTE_START))) {
|
|
166
|
+
if (i > 0) out.push({ input: data.subarray(0, i) });
|
|
167
|
+
held = Uint8Array.from(data.subarray(i));
|
|
168
|
+
heldAt = now();
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
if (data.length) out.push({ input: data });
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
return {
|
|
176
|
+
feed(bytes) {
|
|
177
|
+
if (!(bytes instanceof Uint8Array)) throw new TypeError("feed expects bytes (a Uint8Array)");
|
|
178
|
+
const out = [], t = now();
|
|
179
|
+
let data = bytes;
|
|
180
|
+
const soon = t - lastFeed < PASTE_PAUSE_MS;
|
|
181
|
+
if (paste !== null && !soon) endPaste(out, paste);
|
|
182
|
+
// A raw paste whose end has not come by the pause is over: what follows is scanned anew.
|
|
183
|
+
if (raw !== null && !soon) raw = null;
|
|
184
|
+
lastFeed = t;
|
|
185
|
+
if (paste !== null) {
|
|
186
|
+
data = inPaste(out, data);
|
|
187
|
+
if (paste !== null) return out;
|
|
188
|
+
}
|
|
189
|
+
if (held.length) { data = join(held, data); held = new Uint8Array(0); }
|
|
190
|
+
// The rest of a start marker whose first bytes the last read handed on: the paste is raw.
|
|
191
|
+
// A beginning of that rest waits for the next read, as a held key does.
|
|
192
|
+
if (raw === null && forwarded > 0 && soon) {
|
|
193
|
+
const rest = PASTE_START.subarray(forwarded);
|
|
194
|
+
if (at(data, 0, rest)) raw = { tail: new Uint8Array(0) };
|
|
195
|
+
else if (begins(data, 0, rest)) { held = Uint8Array.from(data); heldAt = t; return out; }
|
|
196
|
+
}
|
|
197
|
+
forwarded = 0;
|
|
198
|
+
if (raw !== null) data = inRaw(out, data);
|
|
199
|
+
if (raw === null && data.length) scan(out, data);
|
|
200
|
+
forwarded = handedOn(out);
|
|
201
|
+
// Events are handed out as copies: the caller may keep them past the next read.
|
|
202
|
+
return out.map((event) => (event.input ? { input: Uint8Array.from(event.input) } : event.rest ? { back: true, rest: Uint8Array.from(event.rest) } : event));
|
|
203
|
+
},
|
|
204
|
+
expire() {
|
|
205
|
+
if (!held.length || now() < heldAt + ESC_TIMEOUT_MS) return [];
|
|
206
|
+
const out = [{ input: held }];
|
|
207
|
+
held = new Uint8Array(0);
|
|
208
|
+
forwarded = handedOn(out);
|
|
209
|
+
return out;
|
|
210
|
+
},
|
|
211
|
+
timeoutAt() { return held.length ? heldAt + ESC_TIMEOUT_MS : null; },
|
|
212
|
+
};
|
|
213
|
+
}
|
|
@@ -4,8 +4,8 @@
|
|
|
4
4
|
// no mouse or focus reporting, no keyboard protocol, no keypad or cursor-key mode, no title, no
|
|
5
5
|
// clipboard, no bell, no query that expects a reply.
|
|
6
6
|
//
|
|
7
|
-
// Handing the terminal to someone else (the shell at a suspend
|
|
8
|
-
//
|
|
7
|
+
// Handing the terminal to someone else (the shell at a suspend) is leave() and enter(), and only
|
|
8
|
+
// that pair. An agent's terminal is never handed the terminal: the TUI draws it (lib/panel.mjs).
|
|
9
9
|
|
|
10
10
|
const mode = (number, on) => `\x1b[?${number}${on ? "h" : "l"}`;
|
|
11
11
|
/** After raw mode: the alternate screen, the cursor hidden, autowrap off, bracketed paste on.
|
|
@@ -22,9 +22,6 @@ export const SIGNAL_STATUS = Object.freeze({ SIGINT: 130, SIGTERM: 143, SIGHUP:
|
|
|
22
22
|
export const TERMINAL_GONE_STATUS = SIGNAL_STATUS.SIGHUP;
|
|
23
23
|
/** How long a suspend waits for the stop before it concludes there was none. */
|
|
24
24
|
const STOP_GRACE_MS = 100;
|
|
25
|
-
/** How long a run that is ending while the terminal is lent waits for the borrower to close after
|
|
26
|
-
* it was asked to end, before it ends anyway. */
|
|
27
|
-
export const LEND_END_GRACE_MS = 3000;
|
|
28
25
|
|
|
29
26
|
/** A terminal on both ends that can be addressed: no escape byte is ever written to anything else. */
|
|
30
27
|
export function isTerminal({ stdin, stdout, env }) {
|
|
@@ -44,9 +41,6 @@ export function createGuard({ stdin, stdout, proc, frame, onInput, onResize, onR
|
|
|
44
41
|
let entered = false, raw = false, modes = false;
|
|
45
42
|
let suspended = false, stopTimer = null;
|
|
46
43
|
let over = false, blocked = false, stale = false;
|
|
47
|
-
/** While the terminal is lent: `{ end, ending }`, the borrower's end() and, once the run is
|
|
48
|
-
* ending, the `[status, error]` it ends with when the borrower has closed. Else null. */
|
|
49
|
-
let lent = null, endTimer = null;
|
|
50
44
|
let settle;
|
|
51
45
|
const done = new Promise((resolve) => { settle = resolve; });
|
|
52
46
|
const listeners = [];
|
|
@@ -78,7 +72,6 @@ export function createGuard({ stdin, stdout, proc, frame, onInput, onResize, onR
|
|
|
78
72
|
if (over) return;
|
|
79
73
|
over = true;
|
|
80
74
|
leave();
|
|
81
|
-
if (endTimer !== null) { clearTimer(endTimer); endTimer = null; }
|
|
82
75
|
// What it throws must not keep the run from ending, nor replace the status it ends with.
|
|
83
76
|
if (onEnd) quietly(onEnd);
|
|
84
77
|
if (stopTimer !== null) { clearTimer(stopTimer); stopTimer = null; }
|
|
@@ -89,7 +82,7 @@ export function createGuard({ stdin, stdout, proc, frame, onInput, onResize, onR
|
|
|
89
82
|
}
|
|
90
83
|
|
|
91
84
|
/** Run one of the loop's own steps. What it throws ends the run as an uncaught error would,
|
|
92
|
-
* without waiting for the process to say so
|
|
85
|
+
* without waiting for the process to say so. */
|
|
93
86
|
function attempt(step) {
|
|
94
87
|
try { step(); } catch (error) { out(1, error); }
|
|
95
88
|
}
|
|
@@ -97,7 +90,7 @@ export function createGuard({ stdin, stdout, proc, frame, onInput, onResize, onR
|
|
|
97
90
|
/** One write per frame. While the last write has not drained nothing is queued: the newest frame
|
|
98
91
|
* is made when it has. */
|
|
99
92
|
function draw() {
|
|
100
|
-
if (!entered || over
|
|
93
|
+
if (!entered || over) return;
|
|
101
94
|
if (blocked) { stale = true; return; }
|
|
102
95
|
const bytes = frame();
|
|
103
96
|
if (bytes && stdout.write(bytes) === false) blocked = true;
|
|
@@ -150,68 +143,22 @@ export function createGuard({ stdin, stdout, proc, frame, onInput, onResize, onR
|
|
|
150
143
|
/** SIGCONT. After our own suspend: come back. Otherwise the process was stopped and continued
|
|
151
144
|
* from outside while it held the terminal. */
|
|
152
145
|
function continued() {
|
|
153
|
-
if (over
|
|
146
|
+
if (over) return;
|
|
154
147
|
if (suspended) return resume();
|
|
155
148
|
if (onResume && entered) { attempt(() => onResume(false)); if (over) return; }
|
|
156
149
|
reassert();
|
|
157
150
|
}
|
|
158
151
|
|
|
159
|
-
/** Lend the terminal to a program of its own (the kernel's viewer) and take it back when it
|
|
160
|
-
* ends. The suspend path, without the stop: leave() (the modes, then raw mode off), input
|
|
161
|
-
* paused, so two readers do not split the keys; then `run()`, which starts the borrower with
|
|
162
|
-
* the terminal as its own and resolves when it has closed; then enter(), input resumed and a
|
|
163
|
-
* full redraw. Answers a promise of `{ value }` (what run resolved with) or `{ error }` (what it
|
|
164
|
-
* threw or rejected with: the terminal is taken back all the same).
|
|
165
|
-
* WHILE LENT nothing is drawn, whatever asks; a SIGQUIT does not end the run (a second
|
|
166
|
-
* Ctrl+\ after the viewer gave the terminal back is the terminal's QUIT, and the viewer handles
|
|
167
|
-
* its own); a SIGTSTP is not heard, so its default action stops the whole group and SIGCONT
|
|
168
|
-
* continues it, and nothing of the suspend path runs. SIGINT, SIGTERM, SIGHUP, a terminal that
|
|
169
|
-
* goes away and an uncaught error end the run as always, but the borrower first: `end()` is
|
|
170
|
-
* called and the run ends when the borrower has closed, or LEND_END_GRACE_MS later. The
|
|
171
|
-
* terminal is already restored, so nothing more is written to it.
|
|
172
|
-
* Lending a terminal that is not the TUI's now (not entered, lent, over) runs nothing. */
|
|
173
|
-
function lend(run, { end = null, release = null } = {}) {
|
|
174
|
-
if (!entered || over || lent) return Promise.resolve({ error: new Error("the terminal is not the TUI's to lend now") });
|
|
175
|
-
leave();
|
|
176
|
-
quietly(() => stdin.pause());
|
|
177
|
-
proc.removeListener("SIGTSTP", suspend);
|
|
178
|
-
const mine = lent = { end, release, ending: null };
|
|
179
|
-
let running;
|
|
180
|
-
try { running = Promise.resolve(run()); } catch (error) { running = Promise.reject(error); }
|
|
181
|
-
return running.then((value) => ({ value }), (error) => ({ error })).then((outcome) => {
|
|
182
|
-
lent = null;
|
|
183
|
-
if (mine.ending) { finish(...mine.ending); return outcome; }
|
|
184
|
-
if (over) return outcome;
|
|
185
|
-
proc.on("SIGTSTP", suspend);
|
|
186
|
-
try { enter(); } catch { finish(TERMINAL_GONE_STATUS); return outcome; }
|
|
187
|
-
if (!over) stdin.resume();
|
|
188
|
-
attempt(onRedraw);
|
|
189
|
-
return outcome;
|
|
190
|
-
});
|
|
191
|
-
}
|
|
192
|
-
/** A way out that arrives while the terminal is lent: end the borrower and wait for it. A second
|
|
193
|
-
* one does not wait. */
|
|
194
|
-
function endLent(status, error) {
|
|
195
|
-
// The run does not wait for a borrower past this: what holds it (its handle, its pipe) is let
|
|
196
|
-
// go, so the process can end with the status of the signal although the borrower lives on.
|
|
197
|
-
const mine = lent, givenUp = () => { if (mine.release) attempt(mine.release); finish(status, error); };
|
|
198
|
-
if (mine.ending) return givenUp();
|
|
199
|
-
mine.ending = [status, error];
|
|
200
|
-
if (mine.end) attempt(mine.end);
|
|
201
|
-
if (over) return;
|
|
202
|
-
endTimer = setTimer(() => { endTimer = null; givenUp(); }, LEND_END_GRACE_MS);
|
|
203
|
-
}
|
|
204
152
|
/** Every way out the run can be told of: a signal, an error nobody caught, a terminal gone. */
|
|
205
153
|
function out(status, error) {
|
|
206
154
|
if (over) return;
|
|
207
|
-
if (lent) return endLent(status, error);
|
|
208
155
|
finish(status, error);
|
|
209
156
|
}
|
|
210
157
|
|
|
211
158
|
/** Every way out is registered before the terminal is touched; then enter. A signal cannot land
|
|
212
159
|
* inside enter(), which is synchronous, and one that lands after it finds its listener. */
|
|
213
160
|
function start() {
|
|
214
|
-
for (const [signal, status] of Object.entries(SIGNAL_STATUS)) on(proc, signal, () =>
|
|
161
|
+
for (const [signal, status] of Object.entries(SIGNAL_STATUS)) on(proc, signal, () => out(status));
|
|
215
162
|
on(proc, "SIGCONT", continued);
|
|
216
163
|
proc.on("SIGTSTP", suspend);
|
|
217
164
|
on(proc, "uncaughtException", (error) => out(1, error));
|
|
@@ -233,5 +180,5 @@ export function createGuard({ stdin, stdout, proc, frame, onInput, onResize, onR
|
|
|
233
180
|
/** The terminal's size now; 80x24 when the terminal does not say. */
|
|
234
181
|
const size = () => ({ cols: Number.isInteger(stdout.columns) && stdout.columns > 0 ? stdout.columns : 80, rows: Number.isInteger(stdout.rows) && stdout.rows > 0 ? stdout.rows : 24 });
|
|
235
182
|
|
|
236
|
-
return { start, enter, leave, finish, suspend, reassert, attempt, draw,
|
|
183
|
+
return { start, enter, leave, finish, suspend, reassert, attempt, draw, size, done, get entered() { return entered; } };
|
|
237
184
|
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// The act view: one confirmation of a stop, a retire, a start or a restart, over the list of
|
|
2
|
+
// instances (as the help is). Pure: no IO, no clock, no escape byte. (model, area) -> rows.
|
|
3
|
+
// The content (lib/model.mjs actView) scrolls when it is taller than the area; the buttons keep
|
|
4
|
+
// the area's last row. The TUI's own words are lit(); every shared sentence, the kernel's lines,
|
|
5
|
+
// a name and a path go through shown(). Meaning never rests on colour alone: the button with the
|
|
6
|
+
// focus is drawn between `>` and `<` as well as in reverse.
|
|
7
|
+
import { EMPTY, lit, shown, shownLines, textOf } from "../term/text.mjs";
|
|
8
|
+
import { textWidth } from "../term/width.mjs";
|
|
9
|
+
import { style } from "../term/style.mjs";
|
|
10
|
+
import { partRows } from "./frame.mjs";
|
|
11
|
+
import { cutNote } from "./instances.mjs";
|
|
12
|
+
import { windowOf } from "./scroll.mjs";
|
|
13
|
+
|
|
14
|
+
const LEFT = 2;
|
|
15
|
+
const GAP = 3;
|
|
16
|
+
/** The most rows one line of the content takes. */
|
|
17
|
+
const LINE_ROWS = 12;
|
|
18
|
+
const BOLD = style({ bold: true });
|
|
19
|
+
const DIM = style({ dim: true });
|
|
20
|
+
const FOCUSED = style({ bold: true, reverse: true });
|
|
21
|
+
const LOOKS = Object.freeze({ bold: BOLD, dim: DIM });
|
|
22
|
+
|
|
23
|
+
/** A line's own rows of spans from column 0, in `width` cells. */
|
|
24
|
+
function lineRows(line, width, ascii) {
|
|
25
|
+
const look = LOOKS[line.look];
|
|
26
|
+
const styled = (spans) => (look ? spans.map((span) => ({ ...span, style: look })) : spans);
|
|
27
|
+
if (line.gap) return [[]];
|
|
28
|
+
if (line.said) return partRows(line.said, width, LINE_ROWS, { ascii }).map(styled);
|
|
29
|
+
if (line.own !== undefined) return partRows([{ own: line.own }], width, LINE_ROWS, { ascii }).map(styled);
|
|
30
|
+
if (line.pair) {
|
|
31
|
+
// A state row's label is the shared plan's (a target's name among them), so it is text from
|
|
32
|
+
// outside like its value: filtered, cut to half the width, and measured as it is drawn.
|
|
33
|
+
const [label, value] = line.pair;
|
|
34
|
+
const name = shownLines(label, Math.max(1, Math.floor(width / 2)), 1, { ascii })[0] ?? EMPTY;
|
|
35
|
+
const used = textWidth(textOf(name)), at = used + 2;
|
|
36
|
+
return [[{ col: 0, text: name, style: DIM, width: used }, { col: at, text: shown(value), width: Math.max(0, width - at) }]];
|
|
37
|
+
}
|
|
38
|
+
if (line.choice) {
|
|
39
|
+
const box = line.choice.value ? "[x]" : "[ ]";
|
|
40
|
+
return shownLines(line.choice.sentence, Math.max(1, width - 4), LINE_ROWS, { ascii })
|
|
41
|
+
.map((text, i) => [...(i === 0 ? [{ col: 0, text: lit(box), width: 3 }] : []), { col: 4, text, width: Math.max(0, width - 4) }]);
|
|
42
|
+
}
|
|
43
|
+
// A shared sentence or the kernel's line, with a path after it: one text from outside.
|
|
44
|
+
const text = line.path ? `${line.text ?? ""} ${line.path}` : line.text;
|
|
45
|
+
return shownLines(text, width, LINE_ROWS, { ascii }).map((value) => styled([{ col: 0, text: value, width }]));
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Every row of the content for an area `cols` wide, before it is scrolled: the title, then the lines. */
|
|
49
|
+
export function actRows(m, cols) {
|
|
50
|
+
const width = Math.max(1, cols - 2 * LEFT);
|
|
51
|
+
const t = m.act.title;
|
|
52
|
+
const title = t.said ? partRows(t.said, width, 2, { ascii: m.ascii }) : shownLines(t.text, width, 2, { ascii: m.ascii }).map((text) => [{ col: 0, text, width }]);
|
|
53
|
+
const rows = [...title.map((spans) => spans.map((span) => ({ ...span, style: BOLD }))), []];
|
|
54
|
+
for (const line of m.act.lines) rows.push(...lineRows(line, width, m.ascii));
|
|
55
|
+
return rows.map((spans) => spans.map((span) => ({ ...span, col: span.col + LEFT })));
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** The buttons, one per offer of the confirmation and in its order, on one row; the one with the
|
|
59
|
+
* focus between `>` and `<`. */
|
|
60
|
+
function buttonRow(buttons, cols) {
|
|
61
|
+
const spans = [];
|
|
62
|
+
let col = LEFT;
|
|
63
|
+
for (const button of buttons) {
|
|
64
|
+
const label = button.focused ? `> ${button.label} <` : `[ ${button.label} ]`;
|
|
65
|
+
if (col + label.length > cols) break;
|
|
66
|
+
spans.push({ col, text: lit(label), width: label.length, ...(button.focused ? { style: FOCUSED } : {}) });
|
|
67
|
+
col += label.length + GAP;
|
|
68
|
+
}
|
|
69
|
+
return spans;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** `{ rows, extent, room }`: `size.rows` rows for an area `size.cols` wide: the content scrolled
|
|
73
|
+
* to the confirmation's `top`, then an empty row and the buttons on the last row. `extent` and
|
|
74
|
+
* `room` are the content's, for the store's scroll. */
|
|
75
|
+
export function act(m, size) {
|
|
76
|
+
const rows = Array.from({ length: size.rows }, () => []);
|
|
77
|
+
const room = Math.max(0, size.rows - 2);
|
|
78
|
+
const all = actRows(m, size.cols);
|
|
79
|
+
const win = windowOf({ top: m.act.top ?? 0, count: all.length, height: room });
|
|
80
|
+
all.slice(win.top, win.top + win.size).forEach((spans, i) => { rows[i] = spans; });
|
|
81
|
+
const note = cutNote(win.above, win.below);
|
|
82
|
+
if (note && room > 0) rows[room - 1] = [{ col: LEFT, text: lit(note), style: DIM, width: Math.max(0, size.cols - LEFT) }];
|
|
83
|
+
if (size.rows > 0) rows[size.rows - 1] = buttonRow(m.act.buttons, size.cols);
|
|
84
|
+
return { rows, extent: all.length, room: Math.max(1, room) };
|
|
85
|
+
}
|