@awebai/oats 0.52.0 → 0.53.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 +2 -1
- package/bin/oats.mjs +304 -92
- package/docs/capabilities.md +66 -4
- package/docs/configuration.md +66 -3
- package/docs/desktop-cli-api.md +673 -58
- package/docs/desktop-instance-start.md +25 -12
- package/docs/execution-targets.md +1 -0
- package/docs/implementation.md +175 -11
- package/docs/integrations.md +6 -3
- package/docs/knowledge.md +2 -2
- package/docs/macos-test-runs.md +246 -0
- package/docs/official-catalog.md +5 -5
- package/docs/packages.md +14 -14
- package/docs/release-lane.md +210 -6
- package/docs/release-notes/v0.53.0.md +1274 -0
- package/docs/schedules.md +7 -1
- package/docs/souls-and-instances.md +237 -15
- package/docs/tui.md +453 -21
- package/docs/upgrading.md +298 -0
- package/docs/workspaces.md +3 -3
- package/lib/automations.mjs +22 -22
- package/lib/claim.mjs +93 -4
- package/lib/config-data.mjs +4 -1
- package/lib/core.mjs +569 -121
- package/lib/instance-events.mjs +19 -1
- package/lib/instance-git.mjs +39 -3
- package/lib/instance-inspect.mjs +5 -6
- package/lib/instance-lifecycle.mjs +145 -18
- package/lib/instance-resolution.mjs +59 -6
- package/lib/launch-preference.mjs +226 -15
- package/lib/local-edit.mjs +118 -0
- package/lib/packages.mjs +13 -4
- package/lib/pi-bridge.mjs +308 -0
- package/lib/refused-text.mjs +11 -0
- package/lib/remote.mjs +190 -25
- package/lib/resolve.mjs +2 -1
- package/lib/retained-after-retire.mjs +2 -2
- package/lib/retire-output.mjs +12 -1
- package/lib/schedule-host.mjs +6 -35
- package/lib/shell-word.mjs +13 -0
- package/lib/soul-launch-verbs.mjs +153 -0
- package/lib/stable-node.mjs +62 -0
- package/lib/teams-verbs.mjs +18 -46
- package/lib/workspace.mjs +49 -19
- package/package-catalog.json +4 -4
- package/package.json +2 -1
- package/packages/client/attach-exit.mjs +25 -0
- package/packages/client/cli-adapter.mjs +16 -3
- package/packages/client/cli-locator.mjs +2 -2
- package/packages/client/deployment-contract.mjs +4 -0
- package/packages/client/deployment-facts.mjs +71 -10
- package/packages/client/deployment-panel.mjs +49 -11
- package/packages/client/deployment-read-cli.mjs +7 -3
- package/packages/client/display-text.mjs +8 -2
- package/packages/client/instance-acts.mjs +46 -10
- package/packages/client/instance-events-contract.mjs +4 -2
- package/packages/client/instance-presentation.mjs +66 -2
- package/packages/client/instance-tree.mjs +23 -14
- package/packages/client/kernel-lines.mjs +31 -0
- package/packages/client/kernel-made.mjs +23 -0
- package/packages/client/launch-prompt-outcome.mjs +51 -16
- package/packages/client/lifecycle-contract.mjs +45 -4
- package/packages/client/liveness.mjs +14 -1
- package/packages/client/own-codes.mjs +31 -0
- package/packages/client/panel-views.mjs +18 -11
- package/packages/client/readiness-contract.mjs +3 -1
- package/packages/client/remote-address.mjs +34 -9
- package/packages/client/remote-identity-memory.mjs +94 -0
- package/packages/client/remote-roster.mjs +92 -6
- package/packages/client/session-attach.mjs +175 -0
- package/packages/client/spawn-apply-contract.mjs +16 -4
- package/packages/client/spawn-messages.mjs +26 -3
- package/packages/client/spawn-preview-cli.mjs +8 -2
- package/packages/client/spawn-preview-contract.mjs +5 -1
- package/packages/client/terminal-exec.mjs +21 -0
- package/packages/client/workspace-choices.mjs +95 -0
- package/packages/client/workspace-views.mjs +39 -3
- package/packages/record/README.md +21 -0
- package/packages/record/bin/setup.mjs +108 -15
- package/packages/tui/README.md +30 -12
- package/packages/tui/lib/actions.mjs +16 -1
- package/packages/tui/lib/client.mjs +32 -1
- package/packages/tui/lib/config.mjs +151 -0
- package/packages/tui/lib/main.mjs +117 -46
- package/packages/tui/lib/model.mjs +392 -0
- package/packages/tui/lib/read.mjs +473 -0
- package/packages/tui/lib/store.mjs +364 -0
- package/packages/tui/lib/term/keys.mjs +44 -0
- package/packages/tui/lib/term/screen.mjs +16 -4
- package/packages/tui/lib/term/text.mjs +60 -0
- package/packages/tui/lib/term/tty.mjs +13 -3
- package/packages/tui/lib/views/detail.mjs +46 -0
- package/packages/tui/lib/views/frame.mjs +230 -36
- package/packages/tui/lib/views/help.mjs +125 -13
- package/packages/tui/lib/views/instances.mjs +137 -0
- package/packages/tui/lib/views/scroll.mjs +19 -0
- package/packages/tui/lib/views/switcher.mjs +97 -0
- package/skills/oats-getting-started/SKILL.md +2 -2
|
@@ -0,0 +1,364 @@
|
|
|
1
|
+
// The state of `oats tui` and the one function that changes it: reduce(state, event). Pure: no
|
|
2
|
+
// IO, no clock, no terminal size. What only the loop can do comes back as an effect, as data.
|
|
3
|
+
//
|
|
4
|
+
// The state holds what the user chose (which workspace is open, which instance, what is
|
|
5
|
+
// collapsed, the filters, where the panes are scrolled) and what the reader gave, exactly as the
|
|
6
|
+
// shared rules gave it: each served deployment's entry (deployment-panel.mjs), the roster of other
|
|
7
|
+
// machines (remote-roster.mjs `remoteRosterState`) and the memory of what they reported
|
|
8
|
+
// (remote-identity-memory.mjs), all through lib/read.mjs. Nothing is added to any of them, nothing
|
|
9
|
+
// removed, and nothing here decides what a failed read means or which view a deployment is in.
|
|
10
|
+
// Selection is an identity (the shared `instanceId`), never an index, so it survives every read;
|
|
11
|
+
// the rows it moves among are the model's, in drawn order.
|
|
12
|
+
import { ACTIONS, bindings } from "./actions.mjs";
|
|
13
|
+
import { collapseKey, displayLine, instanceId, pendingEntry } from "./client.mjs";
|
|
14
|
+
import { disclosure, roster, switcher } from "./model.mjs";
|
|
15
|
+
import { windowOf } from "./views/scroll.mjs";
|
|
16
|
+
|
|
17
|
+
/** The longest filter, in UTF-16 units. */
|
|
18
|
+
export const QUERY_LIMIT = 200;
|
|
19
|
+
const GRAPHEMES = new Intl.Segmenter(undefined, { granularity: "grapheme" });
|
|
20
|
+
const LINE_END = /\r\n|[\n\r\u2028\u2029]/;
|
|
21
|
+
const NO_FILTER = Object.freeze({ editing: false, query: "" });
|
|
22
|
+
|
|
23
|
+
/** The state before the first answer. `deployment` is the one `oats tui` was started in or was
|
|
24
|
+
* told of, with its shared pending entry (lib/main.mjs makes it); null for a run started outside
|
|
25
|
+
* every deployment, which serves none and shows the switcher.
|
|
26
|
+
* `ctxs`: the served deployments, in the order they were opened. `open`: the id of the deployment
|
|
27
|
+
* the Instances view was opened on (a path, or `remote:<group id>`), or null. `entries`,
|
|
28
|
+
* `numbers`: each served deployment's entry and the number of the cycle that gave it. `remote`,
|
|
29
|
+
* `rosterAt`, `memory`, `notes`, `collecting`: the roster of other machines as the shared rule
|
|
30
|
+
* left it, the time of the last outcome taken, what the machines reported, and whether a roster
|
|
31
|
+
* read is in flight. `unread`: per served deployment whose read failed with nothing kept, the
|
|
32
|
+
* instances it last held (see `took`). `loose`: the list was scrolled away from the selection
|
|
33
|
+
* on purpose (see `scrolledList`); the switcher has its own.
|
|
34
|
+
* `selected`, `top`, `detailTop` are the open view's; `places` keeps them for
|
|
35
|
+
* the views left behind, and `collapsed` holds every view's keys (each carries its `open`).
|
|
36
|
+
* `back`: the view under the help. `offered`: the deployments of `~/Agents` not served. */
|
|
37
|
+
export function initialState({ deployment = null, cli, home, entry = null, actions = ACTIONS, ascii = false }) {
|
|
38
|
+
return Object.freeze({
|
|
39
|
+
view: deployment === null ? "switcher" : "instances", back: deployment === null ? "switcher" : "instances",
|
|
40
|
+
ctxs: Object.freeze(deployment === null ? [] : [deployment]), open: deployment,
|
|
41
|
+
entries: new Map(deployment === null ? [] : [[deployment, entry]]), numbers: new Map(),
|
|
42
|
+
remote: Object.freeze({ remoteGroups: Object.freeze([]), rosterAnswered: false, rosterFailure: null }), rosterAt: null,
|
|
43
|
+
memory: new Map(), notes: new Map(), collecting: false, cli, home,
|
|
44
|
+
selected: null, collapsed: new Set(), filter: NO_FILTER, focus: "list", top: 0, loose: false, detailTop: 0, places: new Map(), unread: new Map(),
|
|
45
|
+
switcher: Object.freeze({ editing: false, query: "", selected: null, top: 0, loose: false, line: null, refused: null }),
|
|
46
|
+
offered: Object.freeze({ offered: Object.freeze([]), limited: false }),
|
|
47
|
+
helpTop: 0, actions, ascii,
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const next = (state, changes) => Object.freeze({ ...state, ...changes });
|
|
52
|
+
const indexOf = (r, id) => (id === null ? -1 : r.index.get(id) ?? -1);
|
|
53
|
+
/** Whether an item of the list is an instance: the only kind the selection stands on. */
|
|
54
|
+
const isRow = (item) => item?.id !== undefined;
|
|
55
|
+
|
|
56
|
+
/** The list's `top` as the screen shows it for this state in a pane `page` rows tall. The
|
|
57
|
+
* window holds the selected instance, unless it was scrolled away from it on purpose (`loose`). */
|
|
58
|
+
const settledTop = (state, r, page) => windowOf({ top: state.top, count: r.items.length, height: page, keep: state.loose ? -1 : indexOf(r, state.selected) }).top;
|
|
59
|
+
|
|
60
|
+
/** Select the instance at `index` of the drawn order (an item that is not an instance is passed
|
|
61
|
+
* over in the direction of travel), and scroll the least that shows it. The first instance
|
|
62
|
+
* brings what stands above it into the pane (its heading), and the last what stands under it
|
|
63
|
+
* (a deployment without rows, the roster's failure), as far as the pane holds them beside it.
|
|
64
|
+
* A new selection starts its detail at the top. */
|
|
65
|
+
function selectAt(state, r, index, page, step = 1) {
|
|
66
|
+
const count = r.items.length;
|
|
67
|
+
if (!r.rows.length) return state;
|
|
68
|
+
let at = Math.min(Math.max(0, index), count - 1);
|
|
69
|
+
while (at >= 0 && at < count && !isRow(r.items[at])) at += step;
|
|
70
|
+
if (at < 0 || at >= count) { at = Math.min(Math.max(0, index), count - 1); while (!isRow(r.items[at])) at -= step; }
|
|
71
|
+
const id = r.items[at].id;
|
|
72
|
+
const from = id === r.rows[0].id ? 0 : id === r.rows.at(-1).id ? count : settledTop(state, r, page);
|
|
73
|
+
const top = windowOf({ top: from, count, height: page, keep: at }).top;
|
|
74
|
+
return next(state, { selected: id, top, loose: false, detailTop: id === state.selected ? state.detailTop : 0 });
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** A movement key with no instance to go to in its direction (the selection is on the first or
|
|
78
|
+
* the last one, or the list holds none): the list scrolls instead, by what the key moves, so
|
|
79
|
+
* whatever stands beyond the instances can always be read, however much of it there is. The
|
|
80
|
+
* selection stays where it is and may leave the pane (`loose`) until it moves again. */
|
|
81
|
+
function scrolledList(state, r, action, page) {
|
|
82
|
+
const count = r.items.length, from = settledTop(state, r, page);
|
|
83
|
+
const top = windowOf({ top: moved(action, from, count, page), count, height: page }).top;
|
|
84
|
+
return top === from ? state : next(state, { top, loose: true });
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** After the rows changed (a read, the filter, a collapse): the selection stays on its instance
|
|
88
|
+
* while that is still drawn; else it moves to the row now at the same place in the drawn order,
|
|
89
|
+
* else to the last row, else there is none. With none selected, the first row is.
|
|
90
|
+
* A filter that matches nothing leaves no row to move to: the selection then stays on its
|
|
91
|
+
* instance while the view still has it, and is found again when the filter is cleared. */
|
|
92
|
+
function rehomed(before, state) {
|
|
93
|
+
const now = roster(state);
|
|
94
|
+
if (state.selected !== null && now.index.has(state.selected)) return state;
|
|
95
|
+
if (state.selected !== null && now.rows.length === 0 && now.instances.some((row) => instanceId(row) === state.selected)) return state;
|
|
96
|
+
const place = state.selected === null ? 0 : Math.max(0, before.rows.findIndex((item) => item.id === state.selected));
|
|
97
|
+
const item = now.rows[Math.min(place, now.rows.length - 1)] ?? null;
|
|
98
|
+
return next(state, { selected: item ? item.id : null, loose: false, detailTop: 0 });
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** The switcher, shown: over whatever view was there. `said`: a line of its own to show. */
|
|
102
|
+
const toSwitcher = (state, said = null) => next(state, { view: "switcher", switcher: Object.freeze({ ...state.switcher, editing: false, loose: false, line: said, refused: null }) });
|
|
103
|
+
|
|
104
|
+
/** After what the reader gave changed: when no view holds what was open any more (a machine that
|
|
105
|
+
* is no longer listed), the switcher is shown and says so, and nothing is open. Else the
|
|
106
|
+
* collapse keys of this view whose instance is gone are forgotten, and the selection finds its
|
|
107
|
+
* place.
|
|
108
|
+
* THAT AN INSTANCE IS GONE IS KNOWN ONLY OF A DEPLOYMENT THAT WAS READ. One whose read failed
|
|
109
|
+
* with nothing kept draws no row (and, reporting no workspace, is in no view but its own): its
|
|
110
|
+
* instances are not gone, they are not known. `unread` holds them (see `took`): the collapse of
|
|
111
|
+
* such an instance is not forgotten and a selection on one is not moved, whoever answered
|
|
112
|
+
* meanwhile (another deployment, the roster of other machines), so the next good read finds
|
|
113
|
+
* the user where they were. A machine's rows are kept by the shared rule while it does not
|
|
114
|
+
* answer, so they need no such care. */
|
|
115
|
+
function settled(state, taken) {
|
|
116
|
+
const now = roster(taken);
|
|
117
|
+
if (taken.open !== null && now.gone) {
|
|
118
|
+
const places = new Map(taken.places); places.delete(taken.open);
|
|
119
|
+
return { state: toSwitcher(next(taken, { open: null, places, selected: null, top: 0, loose: false, detailTop: 0, focus: "list", filter: NO_FILTER, back: "switcher" }), "gone"), effect: { offer: true } };
|
|
120
|
+
}
|
|
121
|
+
const before = roster(state);
|
|
122
|
+
const unknown = new Set([...taken.unread.values()].flatMap((ids) => [...ids]));
|
|
123
|
+
const mine = `${collapseKey(taken.open, "")}`;
|
|
124
|
+
const present = new Set(now.instances.map((row) => collapseKey(taken.open, instanceId(row))));
|
|
125
|
+
const stillThere = [...taken.collapsed].filter((key) => !key.startsWith(mine) || present.has(key) || unknown.has(key.slice(mine.length)));
|
|
126
|
+
const pruned = stillThere.length === taken.collapsed.size ? taken : next(taken, { collapsed: new Set(stillThere) });
|
|
127
|
+
return { state: unknown.has(pruned.selected) ? pruned : rehomed(before, pruned), effect: null };
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** A served deployment's entry from the reader, with the number of the cycle that gave it. An
|
|
131
|
+
* answer for a deployment that is not served, or one that is not newer than the last taken for
|
|
132
|
+
* it, is dropped. Only an observed entry moves the selection or forgets a collapse: a failed
|
|
133
|
+
* read has no rows to move to, and the next good one finds the user where they were.
|
|
134
|
+
* `unread`: for a deployment whose read failed with nothing kept, the instances it held when it
|
|
135
|
+
* was last read, until it is read again. They are what `settled` must not take for gone. */
|
|
136
|
+
function took(state, { id, entry, number }) {
|
|
137
|
+
if (!state.ctxs.includes(id) || !(number > (state.numbers.get(id) ?? 0))) return { state, effect: null };
|
|
138
|
+
const observed = entry?.deployment?.status === "observed", held = state.entries.get(id);
|
|
139
|
+
let unread = state.unread;
|
|
140
|
+
if (observed && unread.has(id)) { unread = new Map(unread); unread.delete(id); }
|
|
141
|
+
else if (!observed && held?.deployment?.status === "observed") unread = new Map(unread).set(id, new Set((Array.isArray(held.instances) ? held.instances : []).map(instanceId)));
|
|
142
|
+
const taken = next(state, { entries: new Map(state.entries).set(id, entry), numbers: new Map(state.numbers).set(id, number), unread });
|
|
143
|
+
return observed ? settled(state, taken) : { state: taken, effect: null };
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** The roster of other machines after one outcome, as the shared rule made it (lib/read.mjs),
|
|
147
|
+
* with the memory of what the machines reported and the time of the outcome. */
|
|
148
|
+
function heard(state, { remote, memory, notes, at }) {
|
|
149
|
+
return settled(state, next(state, { remote, memory, notes, rosterAt: at, collecting: false }));
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** Open the view of deployment `id`: the place the view left behind is kept, and found again
|
|
153
|
+
* on the way back (the row, where the list and the detail were scrolled). A filter is not kept. */
|
|
154
|
+
function opened(state, id) {
|
|
155
|
+
const shown = next(state, { view: "instances", back: "instances", switcher: Object.freeze({ ...state.switcher, editing: false, query: "", line: null, refused: null }) });
|
|
156
|
+
if (id === state.open) return shown;
|
|
157
|
+
const places = new Map(state.places);
|
|
158
|
+
if (state.open !== null) places.set(state.open, Object.freeze({ selected: state.selected, top: state.top, detailTop: state.detailTop }));
|
|
159
|
+
const place = places.get(id) ?? { selected: null, top: 0, detailTop: 0 };
|
|
160
|
+
return rehomed({ rows: [] }, next(shown, { open: id, places, focus: "list", filter: NO_FILTER, loose: false, ...place }));
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** A picked folder of `~/Agents` that is a deployment (lib/read.mjs made it canonical and asked
|
|
164
|
+
* the shared rule): it is served from now on, pending until its first read, and opened. */
|
|
165
|
+
function admitted(state, { admitted: id, at }) {
|
|
166
|
+
if (state.ctxs.includes(id)) return opened(state, id);
|
|
167
|
+
const joined = next(state, { ctxs: Object.freeze([...state.ctxs, id]), entries: new Map(state.entries).set(id, pendingEntry(null, at)) });
|
|
168
|
+
return opened(joined, id);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** The filter's text changed. */
|
|
172
|
+
function queried(state, query) {
|
|
173
|
+
if (query === state.filter.query) return state;
|
|
174
|
+
const before = roster(state);
|
|
175
|
+
return rehomed(before, next(state, { filter: Object.freeze({ ...state.filter, query }), top: 0, loose: false }));
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** Text for a filter field: the query with it added when the filter stays within its limit and
|
|
179
|
+
* the display filter shows the text as it is; else the query as it was. A paste is held to that
|
|
180
|
+
* as it stands. The space KEY is the one text the display filter would change that is still
|
|
181
|
+
* taken: between words, so never first and never after another space. */
|
|
182
|
+
function typed(query, text, { key = false } = {}) {
|
|
183
|
+
const fits = text !== "" && query.length + text.length <= QUERY_LIMIT;
|
|
184
|
+
const shows = key && text === " " ? query !== "" && !query.endsWith(" ") : displayLine(text) === text;
|
|
185
|
+
return fits && shows ? query + text : query;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** The two filter fields, each as what the keys of an open field need: its text, and the state
|
|
189
|
+
* with another text or with the field closed on one. */
|
|
190
|
+
const FIELDS = Object.freeze({
|
|
191
|
+
instances: Object.freeze({
|
|
192
|
+
query: (state) => state.filter.query,
|
|
193
|
+
put: (state, query) => queried(state, query),
|
|
194
|
+
close: (state, query) => next(queried(state, query), { filter: Object.freeze({ editing: false, query }) }),
|
|
195
|
+
}),
|
|
196
|
+
switcher: Object.freeze({
|
|
197
|
+
query: (state) => state.switcher.query,
|
|
198
|
+
put: (state, query) => (query === state.switcher.query ? state : next(state, { switcher: Object.freeze({ ...state.switcher, query, top: 0, loose: false }) })),
|
|
199
|
+
close: (state, query) => next(state, { switcher: Object.freeze({ ...state.switcher, query, editing: false, top: 0, loose: false }) }),
|
|
200
|
+
}),
|
|
201
|
+
});
|
|
202
|
+
/** The filter field that is open now, or null. */
|
|
203
|
+
const fieldOf = (state) => (state.view === "instances" && state.filter.editing ? FIELDS.instances : state.view === "switcher" && state.switcher.editing ? FIELDS.switcher : null);
|
|
204
|
+
|
|
205
|
+
/** A key while a filter field is open. A key that is text is text, whatever the action table
|
|
206
|
+
* says, so `q` and `j` can be typed; Backspace removes one grapheme, Enter keeps the filter and
|
|
207
|
+
* the close key clears it. The three keys that are not about the view keep their actions. */
|
|
208
|
+
function edited(state, field, event, action) {
|
|
209
|
+
const query = field.query(state);
|
|
210
|
+
if (typeof event.text === "string") return { state: field.put(state, typed(query, event.text, { key: true })), effect: null };
|
|
211
|
+
if (event.key === "backspace") {
|
|
212
|
+
const graphemes = [...GRAPHEMES.segment(query)].map((g) => g.segment);
|
|
213
|
+
return { state: field.put(state, graphemes.slice(0, -1).join("")), effect: null };
|
|
214
|
+
}
|
|
215
|
+
if (event.key === "enter") return { state: field.close(state, query.trim()), effect: null };
|
|
216
|
+
if (action === "close") return { state: field.close(state, ""), effect: null };
|
|
217
|
+
if (action === "quit" || action === "suspend" || action === "redraw") return { state, effect: action };
|
|
218
|
+
return { state, effect: null };
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
const MOVES = Object.freeze(["up", "down", "top", "bottom", "page-up", "page-down"]);
|
|
222
|
+
/** Where a movement key takes a position `from` among `count` rows, a page being `page` rows. */
|
|
223
|
+
function moved(action, from, count, page) {
|
|
224
|
+
const step = Math.max(1, page - 1);
|
|
225
|
+
switch (action) {
|
|
226
|
+
case "up": return from - 1;
|
|
227
|
+
case "down": return from + 1;
|
|
228
|
+
case "top": return 0;
|
|
229
|
+
case "bottom": return count - 1;
|
|
230
|
+
case "page-up": return from - step;
|
|
231
|
+
default: return from + step;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/** A key in the switcher. The selection moves among the choices (a heading is passed over);
|
|
236
|
+
* the open key opens the view of a workspace here, and asks the loop to admit a folder. */
|
|
237
|
+
function switched(state, action, page) {
|
|
238
|
+
const stay = { state, effect: null };
|
|
239
|
+
const s = switcher(state), sw = state.switcher;
|
|
240
|
+
const put = (changes) => next(state, { switcher: Object.freeze({ ...sw, ...changes }) });
|
|
241
|
+
switch (action) {
|
|
242
|
+
case "filter": return { state: put({ editing: true }), effect: null };
|
|
243
|
+
case "close":
|
|
244
|
+
if (sw.query !== "") return { state: put({ query: "", top: 0, loose: false }), effect: null };
|
|
245
|
+
// Back to the view it was opened over; with nothing open there is none.
|
|
246
|
+
return state.open === null ? stay : { state: next(put({ line: null, refused: null }), { view: "instances", back: "instances" }), effect: null };
|
|
247
|
+
case "instances": return state.open === null ? stay : { state: next(put({ line: null, refused: null }), { view: "instances", back: "instances" }), effect: null };
|
|
248
|
+
case "up": case "down": case "top": case "bottom": case "page-up": case "page-down": {
|
|
249
|
+
// The rows of the pane the choices have: the line that says what happened takes one (lib/model.mjs).
|
|
250
|
+
const count = s.items.length, height = Math.max(1, page - (sw.line ? 1 : 0));
|
|
251
|
+
const drawn = windowOf({ top: sw.top, count, height, keep: sw.loose ? -1 : s.items.findIndex((item) => item.key !== undefined && item.key === s.selected) }).top;
|
|
252
|
+
const from = Math.max(0, s.keys.indexOf(s.selected));
|
|
253
|
+
const key = s.keys.length ? s.keys[Math.min(Math.max(0, moved(action, from, s.keys.length, page)), s.keys.length - 1)] : null;
|
|
254
|
+
// No choice to go to in that direction (the first, the last, or none at all): the rows
|
|
255
|
+
// scroll instead, so a line under the last choice (that the folder holds more) is read.
|
|
256
|
+
if (key === null || key === s.selected) {
|
|
257
|
+
const top = windowOf({ top: moved(action, drawn, count, height), count, height }).top;
|
|
258
|
+
return top === drawn ? stay : { state: put({ top, loose: true }), effect: null };
|
|
259
|
+
}
|
|
260
|
+
const at = s.items.findIndex((item) => item.key === key);
|
|
261
|
+
// The headings above the first choice come into the pane with it, and what stands under the last one.
|
|
262
|
+
const top = windowOf({ top: key === s.keys[0] ? 0 : key === s.keys.at(-1) ? count : drawn, count, height, keep: at }).top;
|
|
263
|
+
return { state: put({ selected: key, top, loose: false }), effect: null };
|
|
264
|
+
}
|
|
265
|
+
case "open-workspace": {
|
|
266
|
+
const item = s.items.find((each) => each.key !== undefined && each.key === s.selected);
|
|
267
|
+
if (!item) return stay;
|
|
268
|
+
if (item.path !== undefined) return { state: put({ refused: null }), effect: { admit: item.path } };
|
|
269
|
+
return item.primary === null ? stay : { state: opened(state, item.primary), effect: { refresh: { live: true } } };
|
|
270
|
+
}
|
|
271
|
+
default: return stay;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/** (state, event) -> { state, effect }.
|
|
276
|
+
* An event is a key, `{ key, text?, page, extent, room, cols }` (`key` a key id, `text` the
|
|
277
|
+
* grapheme a text key typed; `page` the rows of the list pane and `cols` its width, and `extent`
|
|
278
|
+
* and `room` the rows the other scrolling pane holds and shows, as the last frame drew them: the
|
|
279
|
+
* store keeps no size); a paste, `{ paste: <text> }`; or what the loop read:
|
|
280
|
+
* `{ id, entry, number }` (a served deployment's entry), `{ roster: { remote, memory, notes, at } }`
|
|
281
|
+
* (one outcome of the roster read), `{ collecting }`, `{ offered: { offered, limited } }` (the
|
|
282
|
+
* deployments of `~/Agents` not served), `{ admitted: <deployment>, at }` and `{ refused: <path> }`
|
|
283
|
+
* (what a picked folder turned out to be).
|
|
284
|
+
* An effect is something only the loop can do: "quit", "suspend", "redraw", or an object with
|
|
285
|
+
* any of `refresh: { live }` (read now), `offer: true` (list `~/Agents` again) and
|
|
286
|
+
* `admit: <path>` (ask whether a picked folder is a deployment); or null. */
|
|
287
|
+
export function reduce(state, event, keymap = bindings(state.actions)) {
|
|
288
|
+
const stay = { state, effect: null };
|
|
289
|
+
if (event.entry !== undefined) return took(state, event);
|
|
290
|
+
if (event.roster !== undefined) return heard(state, event.roster);
|
|
291
|
+
if (event.collecting !== undefined) return { state: event.collecting === state.collecting ? state : next(state, { collecting: event.collecting === true }), effect: null };
|
|
292
|
+
if (event.offered !== undefined) return { state: next(state, { offered: Object.freeze({ offered: Object.freeze([...event.offered.offered]), limited: event.offered.limited === true }) }), effect: null };
|
|
293
|
+
if (event.admitted !== undefined) return { state: admitted(state, event), effect: { refresh: { live: true } } };
|
|
294
|
+
if (event.refused !== undefined) return { state: next(state, { switcher: Object.freeze({ ...state.switcher, refused: event.refused }) }), effect: null };
|
|
295
|
+
const field = fieldOf(state);
|
|
296
|
+
// A paste goes only into an open filter field: its first line. Anywhere else it is dropped whole.
|
|
297
|
+
if ("paste" in event) return field && typeof event.paste === "string" ? { state: field.put(state, typed(field.query(state), event.paste.split(LINE_END)[0])), effect: null } : stay;
|
|
298
|
+
if (typeof event.key !== "string") return stay;
|
|
299
|
+
const action = keymap.get(event.key);
|
|
300
|
+
if (field) return edited(state, field, event, action);
|
|
301
|
+
const page = Math.max(1, Number.isInteger(event.page) ? event.page : 1);
|
|
302
|
+
// A pane that scrolls on its own (the help, the detail) moves from where it is DRAWN: the top
|
|
303
|
+
// the state holds may lie past the end since the terminal grew, and the view shows it corrected.
|
|
304
|
+
const scrolled = (top) => {
|
|
305
|
+
const count = event.extent ?? 0, height = event.room ?? 1;
|
|
306
|
+
const from = windowOf({ top, count, height }).top;
|
|
307
|
+
return windowOf({ top: moved(action, from, count, height), count, height }).top;
|
|
308
|
+
};
|
|
309
|
+
|
|
310
|
+
switch (action) {
|
|
311
|
+
case "quit": case "suspend": case "redraw": return { state, effect: action };
|
|
312
|
+
// In the switcher the folders of ~/Agents are listed again too.
|
|
313
|
+
case "refresh": return { state, effect: { refresh: { live: true }, ...((state.view === "help" ? state.back : state.view) === "switcher" ? { offer: true } : {}) } };
|
|
314
|
+
case "help": return { state: next(state, state.view === "help" ? { view: state.back } : { view: "help", back: state.view, helpTop: 0 }), effect: null };
|
|
315
|
+
case "workspaces": return state.view === "switcher" ? stay : { state: toSwitcher(next(state, { back: "switcher" })), effect: { offer: true } };
|
|
316
|
+
default:
|
|
317
|
+
}
|
|
318
|
+
if (state.view === "help") {
|
|
319
|
+
if (action === "close") return { state: next(state, { view: state.back }), effect: null };
|
|
320
|
+
if (action === "instances") return state.open === null ? stay : { state: next(state, { view: "instances", back: "instances" }), effect: null };
|
|
321
|
+
return MOVES.includes(action) ? { state: next(state, { helpTop: scrolled(state.helpTop) }), effect: null } : stay;
|
|
322
|
+
}
|
|
323
|
+
if (state.view === "switcher") return switched(state, action, page);
|
|
324
|
+
|
|
325
|
+
if (action === "close") {
|
|
326
|
+
// In this order: a kept filter, the detail.
|
|
327
|
+
if (state.filter.query !== "") return { state: queried(state, ""), effect: null };
|
|
328
|
+
return state.focus === "detail" ? { state: next(state, { focus: "list" }), effect: null } : stay;
|
|
329
|
+
}
|
|
330
|
+
const r = roster(state, undefined, Number.isInteger(event.cols) ? event.cols : undefined);
|
|
331
|
+
const at = indexOf(r, state.selected);
|
|
332
|
+
switch (action) {
|
|
333
|
+
case "filter": return { state: next(state, { focus: "list", filter: Object.freeze({ ...state.filter, editing: true }) }), effect: null };
|
|
334
|
+
case "detail": return at < 0 ? stay : { state: next(state, { focus: state.focus === "detail" ? "list" : "detail" }), effect: null };
|
|
335
|
+
case "up": case "down": case "top": case "bottom": case "page-up": case "page-down": {
|
|
336
|
+
if (state.focus === "detail") return { state: next(state, { detailTop: scrolled(state.detailTop) }), effect: null };
|
|
337
|
+
// With nothing selected, any movement selects the first row.
|
|
338
|
+
const target = at < 0 ? 0 : moved(action, at, r.items.length, page);
|
|
339
|
+
const picked = selectAt(state, r, target, page, target < at ? -1 : 1);
|
|
340
|
+
return { state: picked.selected === state.selected ? scrolledList(state, r, action, page) : picked, effect: null };
|
|
341
|
+
}
|
|
342
|
+
case "collapse": case "expand": {
|
|
343
|
+
if (state.focus !== "list" || at < 0) return stay;
|
|
344
|
+
const item = r.items[at], fold = disclosure(state, r, item), key = collapseKey(state.open, item.id);
|
|
345
|
+
const settledState = next(state, { top: settledTop(state, r, page) });
|
|
346
|
+
if (!r.filtering && fold === (action === "collapse" ? "open" : "closed")) {
|
|
347
|
+
const collapsed = new Set(state.collapsed);
|
|
348
|
+
if (action === "collapse") collapsed.add(key); else collapsed.delete(key);
|
|
349
|
+
return { state: rehomed(r, next(settledState, { collapsed, loose: false })), effect: null };
|
|
350
|
+
}
|
|
351
|
+
// Nothing to fold or unfold: collapse goes to the parent, the nearest row above that is one
|
|
352
|
+
// level less deep; expand to the first child, the next row when it is one level deeper.
|
|
353
|
+
// Both are read from the drawn order, which is the shared grouping's, and neither leaves
|
|
354
|
+
// the run of rows the instance stands in (a gap or a heading ends it).
|
|
355
|
+
if (action === "collapse") {
|
|
356
|
+
for (let i = at - 1; i >= 0 && isRow(r.items[i]); i--) if (r.items[i].depth === item.depth - 1) return { state: selectAt(state, r, i, page), effect: null };
|
|
357
|
+
return stay;
|
|
358
|
+
}
|
|
359
|
+
const below = r.items[at + 1];
|
|
360
|
+
return isRow(below) && below.depth === item.depth + 1 ? { state: selectAt(state, r, at + 1, page), effect: null } : stay;
|
|
361
|
+
}
|
|
362
|
+
default: return stay;
|
|
363
|
+
}
|
|
364
|
+
}
|
|
@@ -321,3 +321,47 @@ export function keyId(event) {
|
|
|
321
321
|
const id = event.text === " " ? "space" : event.text;
|
|
322
322
|
return event.alt ? `alt+${id}` : id;
|
|
323
323
|
}
|
|
324
|
+
|
|
325
|
+
/** The names that take every modifier: the keys read by `modified` from a CSI or an SS3. */
|
|
326
|
+
const MODIFIED_NAMES = new Set([...Object.values(LETTER_KEYS), ...Object.values(TILDE_KEYS)]);
|
|
327
|
+
const NAMED_ID = /^(ctrl\+)?(alt\+)?(shift\+)?([a-z][a-z0-9]*)$/;
|
|
328
|
+
/** What no text event holds: a control character (C0, DEL, C1) or half a surrogate pair. */
|
|
329
|
+
const NOT_TEXT = /[\p{Cc}\p{Cs}]/u;
|
|
330
|
+
/** The characters that, after an ESC, begin a sequence (CSI, SS3, OSC, DCS, SOS, PM, APC): Alt
|
|
331
|
+
* with one of them never arrives as a key. */
|
|
332
|
+
const INTRODUCERS = "[O]PX^_";
|
|
333
|
+
const oneGrapheme = (text) => GRAPHEMES.segment(text).containing(0).segment === text;
|
|
334
|
+
const oneCodePoint = (text) => text.length === (text.codePointAt(0) > 0xffff ? 2 : 1);
|
|
335
|
+
|
|
336
|
+
/** Whether a string is a key id: true exactly for the strings keyId answers for an event the
|
|
337
|
+
* parser above can emit, so a table that binds only key ids binds nothing that can never arrive
|
|
338
|
+
* from it. Whether a given terminal sends the key is not asked: `f12` and `alt+` an accented
|
|
339
|
+
* letter are key ids on a terminal that sends neither. The grammar is the parser's, branch for
|
|
340
|
+
* branch (the Linux console's F1..F5 are bare names the others already hold):
|
|
341
|
+
*
|
|
342
|
+
* text one grapheme without a control character, as itself (`+` is the plus key), and
|
|
343
|
+
* `space` for the space, which is never written " "
|
|
344
|
+
* alt+text `alt+` and ONE CODE POINT (`alt++`, `alt+space`): Alt reaches a single
|
|
345
|
+
* character, so `alt+` and a grapheme of several code points is no key; and not
|
|
346
|
+
* `alt+` with one of `[ O ] P X ^ _`, which after an ESC begin a sequence
|
|
347
|
+
* named up down left right home end insert delete pageup pagedown f1..f12, with any of
|
|
348
|
+
* `ctrl+`, `alt+`, `shift+`, in that order
|
|
349
|
+
* enter, backspace: bare or `alt+`. escape: bare
|
|
350
|
+
* tab: bare, `alt+tab`, and `shift+tab` with any of `ctrl+`, `alt+` before it
|
|
351
|
+
* ctrl `ctrl+` or `ctrl+alt+` and a letter a..z whose C0 byte is a key of its own:
|
|
352
|
+
* not h, i, j or m, which arrive as Backspace, Tab and Enter */
|
|
353
|
+
export function isKeyId(id) {
|
|
354
|
+
if (typeof id !== "string" || id === "" || NOT_TEXT.test(id)) return false;
|
|
355
|
+
if (id === "space" || id === "alt+space") return true;
|
|
356
|
+
if (id === " " || id === "alt+ ") return false;
|
|
357
|
+
if (oneGrapheme(id)) return true;
|
|
358
|
+
if (id.startsWith("alt+") && id.length > 4 && oneCodePoint(id.slice(4))) return !INTRODUCERS.includes(id.slice(4));
|
|
359
|
+
const match = NAMED_ID.exec(id);
|
|
360
|
+
if (!match) return false;
|
|
361
|
+
const [, ctrl, alt, shift, name] = match;
|
|
362
|
+
if (MODIFIED_NAMES.has(name)) return true;
|
|
363
|
+
if (name === "tab") return shift !== undefined || ctrl === undefined;
|
|
364
|
+
if (name === "enter" || name === "backspace") return ctrl === undefined && shift === undefined;
|
|
365
|
+
if (name === "escape") return ctrl === undefined && alt === undefined && shift === undefined;
|
|
366
|
+
return name.length === 1 && ctrl !== undefined && shift === undefined && c0Key(name.charCodeAt(0) - 0x60).name === name;
|
|
367
|
+
}
|
|
@@ -34,8 +34,11 @@ export function glyphs(env, { ascii = false } = {}) {
|
|
|
34
34
|
}
|
|
35
35
|
|
|
36
36
|
/** A screen. `render(rows, size, { full })` answers the bytes for one frame and remembers it.
|
|
37
|
-
* `rows[r]` is the spans of row r
|
|
38
|
-
* cells the span may take (default:
|
|
37
|
+
* `rows[r]` is the spans of row r, drawn in order, a later one over an earlier one:
|
|
38
|
+
* `{ col, text, style?, width? }`, `text` safe text, `width` the cells the span may take (default:
|
|
39
|
+
* to the right edge); or a fill, `{ col, width, style? }`, `width` blank cells in that style.
|
|
40
|
+
* `cells(rows, size)` answers the same frame as cells and writes nothing: `{ text, width, style }`
|
|
41
|
+
* per cell, the second cell of a two-cell grapheme with `width: 0`. */
|
|
39
42
|
export function createScreen({ utf8 = true, ascii = !utf8, color = true } = {}) {
|
|
40
43
|
const mark = cells(ascii ? "..." : "\u2026");
|
|
41
44
|
let previous = null;
|
|
@@ -48,10 +51,19 @@ export function createScreen({ utf8 = true, ascii = !utf8, color = true } = {})
|
|
|
48
51
|
}
|
|
49
52
|
function place(line, span) {
|
|
50
53
|
const { col, style = PLAIN } = span;
|
|
51
|
-
const source = textOf(span.text);
|
|
52
54
|
if (!isStyle(style)) throw new TypeError("a span's style is a style value: make one with style()");
|
|
53
55
|
if (!Number.isInteger(col) || col < 0) throw new RangeError("a span's column is a cell index from 0");
|
|
54
56
|
const limit = span.width === undefined ? line.length : Math.min(line.length, col + span.width);
|
|
57
|
+
// A fill: no text, a width. Blank cells in the span's style, which is how a bar is drawn
|
|
58
|
+
// (a selected row in reverse video): text.mjs rightly makes no text of a run of spaces.
|
|
59
|
+
// A span that names a text, even an absent one, is never a fill: it is refused below.
|
|
60
|
+
if (!Object.hasOwn(span, "text") && span.width !== undefined) {
|
|
61
|
+
if (!Number.isInteger(span.width) || span.width < 0) throw new RangeError("a fill has a width in cells");
|
|
62
|
+
const blank = cell(" ", 1, style);
|
|
63
|
+
for (let x = col; x < limit; x++) { clear(line, x); line[x] = blank; }
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
const source = textOf(span.text);
|
|
55
67
|
if (col >= limit) return;
|
|
56
68
|
let drawn = cells(source);
|
|
57
69
|
// A terminal that is not reading UTF-8 would show each byte of a character on its own.
|
|
@@ -118,5 +130,5 @@ export function createScreen({ utf8 = true, ascii = !utf8, color = true } = {})
|
|
|
118
130
|
previous = { cols: size.cols, rows: size.rows, lines };
|
|
119
131
|
return out;
|
|
120
132
|
}
|
|
121
|
-
return { render };
|
|
133
|
+
return { render, cells: layout };
|
|
122
134
|
}
|
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
// by column (`{ col, text, style }`), never one padded string: the gap between two spans is the
|
|
11
11
|
// screen's blank cells, not text.
|
|
12
12
|
import { displayLine, DETAIL_WITHHELD } from "../client.mjs";
|
|
13
|
+
import { cells } from "./width.mjs";
|
|
13
14
|
|
|
14
15
|
/** The brand: a value is safe text when it is a key here. Nothing outside this module can add one,
|
|
15
16
|
* and a look-alike object is not one. */
|
|
@@ -39,6 +40,65 @@ export function shown(value) {
|
|
|
39
40
|
return line === null ? EMPTY : line === DETAIL_WITHHELD ? WITHHELD : make(line);
|
|
40
41
|
}
|
|
41
42
|
|
|
43
|
+
/** The cut mark, the screen's own (term/screen.mjs): one cell, or three in ASCII. */
|
|
44
|
+
const cutMark = (ascii) => (ascii ? "..." : "\u2026");
|
|
45
|
+
const widthOf = (graphemes) => graphemes.reduce((sum, g) => sum + g.width, 0);
|
|
46
|
+
const joined = (graphemes) => graphemes.map((g) => g.text).join("");
|
|
47
|
+
|
|
48
|
+
/** Text the TUI did not write, as the END of its display line: what fits `width` cells, with the
|
|
49
|
+
* cut mark in front when the start is left out. For a value read from its end (a path).
|
|
50
|
+
* Filtered first and cut after, by cells and between graphemes: a part of a display line is
|
|
51
|
+
* itself fit to show, so nothing reaches the screen that shown() would not have let through. */
|
|
52
|
+
export function shownEnd(value, width, { ascii = false } = {}) {
|
|
53
|
+
const line = displayLine(value);
|
|
54
|
+
if (line === null) return EMPTY;
|
|
55
|
+
if (line === DETAIL_WITHHELD) return WITHHELD;
|
|
56
|
+
const all = cells(line);
|
|
57
|
+
if (widthOf(all) <= width) return make(joined(all));
|
|
58
|
+
const mark = cutMark(ascii), room = width - mark.length;
|
|
59
|
+
const kept = [];
|
|
60
|
+
for (let i = all.length - 1, used = 0; i >= 0 && used + all[i].width <= room; used += all[i].width, i--) kept.unshift(all[i]);
|
|
61
|
+
return make(room < 0 ? "" : mark + joined(kept).replace(/^ +/, ""));
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Text the TUI did not write, as at most `max` rows of at most `width` cells: its display line
|
|
65
|
+
* broken at spaces where it can be and between graphemes where it cannot, the last row ending
|
|
66
|
+
* in the cut mark when more was left. For a sentence that must be read whole (a kernel's
|
|
67
|
+
* reason). Filtered first and cut after, as shownEnd is; nothing to show gives no row, and
|
|
68
|
+
* neither does a row too narrow for the first grapheme. */
|
|
69
|
+
export function shownLines(value, width, max, { ascii = false } = {}) {
|
|
70
|
+
const line = displayLine(value);
|
|
71
|
+
if (line === null || width < 1 || max < 1) return [];
|
|
72
|
+
if (line === DETAIL_WITHHELD) return [WITHHELD];
|
|
73
|
+
const rows = [];
|
|
74
|
+
let rest = cells(line);
|
|
75
|
+
while (rest.length && rows.length < max) {
|
|
76
|
+
if (widthOf(rest) <= width) { rows.push(rest); rest = []; break; }
|
|
77
|
+
let end = 0;
|
|
78
|
+
for (let used = 0; end < rest.length && used + rest[end].width <= width; used += rest[end].width) end++;
|
|
79
|
+
// Back to the last space inside the row, when there is one: the row ends before it.
|
|
80
|
+
let cut = end;
|
|
81
|
+
while (cut > 0 && rest[cut]?.text !== " " && rest[cut - 1].text !== " ") cut--;
|
|
82
|
+
if (cut === 0) cut = end;
|
|
83
|
+
if (cut === 0) break; // a grapheme wider than the row: nothing more can be set
|
|
84
|
+
const row = rest.slice(0, cut);
|
|
85
|
+
while (row.length && row[row.length - 1].text === " ") row.pop();
|
|
86
|
+
rest = rest.slice(cut);
|
|
87
|
+
while (rest.length && rest[0].text === " ") rest.shift();
|
|
88
|
+
rows.push(row);
|
|
89
|
+
}
|
|
90
|
+
const mark = cutMark(ascii);
|
|
91
|
+
if (rest.length && rows.length && mark.length <= width) {
|
|
92
|
+
// More was left: the last row gives up its end to the mark. (A row too narrow for the mark
|
|
93
|
+
// keeps what it has: no row is ever wider than `width`.)
|
|
94
|
+
const last = rows[rows.length - 1];
|
|
95
|
+
while (last.length && widthOf(last) + mark.length > width) last.pop();
|
|
96
|
+
while (last.length && last[last.length - 1].text === " ") last.pop();
|
|
97
|
+
last.push(...cells(mark));
|
|
98
|
+
}
|
|
99
|
+
return rows.map((row) => make(joined(row)));
|
|
100
|
+
}
|
|
101
|
+
|
|
42
102
|
export const isSafeText = (value) => TEXT.has(value);
|
|
43
103
|
|
|
44
104
|
/** The string of a safe value, for the screen (and the tests that read a row). A raw string and a
|
|
@@ -31,8 +31,13 @@ export function isTerminal({ stdin, stdout, env }) {
|
|
|
31
31
|
/** The guard of one run. `frame()` answers the bytes of the newest frame when it is time to write
|
|
32
32
|
* one; `onInput(bytes)`, `onResize()` and `onRedraw()` (the screen must be drawn in full: after a
|
|
33
33
|
* suspend, after SIGCONT) are the events. `done` resolves once, with `{ status, error }`, after the
|
|
34
|
-
* terminal is restored and every listener is removed.
|
|
35
|
-
|
|
34
|
+
* terminal is restored and every listener is removed.
|
|
35
|
+
* Three more, for whoever started work that outlives a frame (the reads of lib/read.mjs), each
|
|
36
|
+
* optional: `onStop()` is called when the process is about to be stopped by its own suspend, after
|
|
37
|
+
* the terminal is handed back and before the signal; `onResume(asked)` when it runs again, `asked`
|
|
38
|
+
* true after its own suspend and false when it was stopped and continued from outside; `onEnd()`
|
|
39
|
+
* once on every way out, after the terminal is restored and before `done` resolves. */
|
|
40
|
+
export function createGuard({ stdin, stdout, proc, frame, onInput, onResize, onRedraw, onStop = null, onResume = null, onEnd = null, setTimer = setTimeout, clearTimer = clearTimeout }) {
|
|
36
41
|
let entered = false, raw = false, modes = false;
|
|
37
42
|
let suspended = false, stopTimer = null;
|
|
38
43
|
let over = false, blocked = false, stale = false;
|
|
@@ -67,6 +72,8 @@ export function createGuard({ stdin, stdout, proc, frame, onInput, onResize, onR
|
|
|
67
72
|
if (over) return;
|
|
68
73
|
over = true;
|
|
69
74
|
leave();
|
|
75
|
+
// What it throws must not keep the run from ending, nor replace the status it ends with.
|
|
76
|
+
if (onEnd) quietly(onEnd);
|
|
70
77
|
if (stopTimer !== null) { clearTimer(stopTimer); stopTimer = null; }
|
|
71
78
|
for (const [emitter, name, listener] of listeners) emitter.removeListener(name, listener);
|
|
72
79
|
proc.removeListener("SIGTSTP", suspend);
|
|
@@ -100,6 +107,7 @@ export function createGuard({ stdin, stdout, proc, frame, onInput, onResize, onR
|
|
|
100
107
|
function suspend() {
|
|
101
108
|
if (!entered || over) return;
|
|
102
109
|
leave();
|
|
110
|
+
if (onStop) { attempt(onStop); if (over) return; }
|
|
103
111
|
suspended = true;
|
|
104
112
|
proc.removeListener("SIGTSTP", suspend); // the default action stops the process
|
|
105
113
|
quietly(() => proc.kill(0, "SIGTSTP"));
|
|
@@ -113,6 +121,7 @@ export function createGuard({ stdin, stdout, proc, frame, onInput, onResize, onR
|
|
|
113
121
|
if (stopTimer !== null) { clearTimer(stopTimer); stopTimer = null; }
|
|
114
122
|
proc.on("SIGTSTP", suspend);
|
|
115
123
|
try { enter(); } catch { return finish(TERMINAL_GONE_STATUS); }
|
|
124
|
+
if (onResume) attempt(() => onResume(true));
|
|
116
125
|
attempt(onRedraw);
|
|
117
126
|
}
|
|
118
127
|
/** Take the terminal again where it may no longer be as enter() left it: something outside the
|
|
@@ -136,6 +145,7 @@ export function createGuard({ stdin, stdout, proc, frame, onInput, onResize, onR
|
|
|
136
145
|
function continued() {
|
|
137
146
|
if (over) return;
|
|
138
147
|
if (suspended) return resume();
|
|
148
|
+
if (onResume && entered) { attempt(() => onResume(false)); if (over) return; }
|
|
139
149
|
reassert();
|
|
140
150
|
}
|
|
141
151
|
|
|
@@ -147,7 +157,7 @@ export function createGuard({ stdin, stdout, proc, frame, onInput, onResize, onR
|
|
|
147
157
|
proc.on("SIGTSTP", suspend);
|
|
148
158
|
on(proc, "uncaughtException", (error) => finish(1, error));
|
|
149
159
|
on(proc, "unhandledRejection", (reason) => finish(1, reason));
|
|
150
|
-
on(proc, "exit", leave); // the last resort: a process.exit() nobody expected
|
|
160
|
+
on(proc, "exit", () => { leave(); if (onEnd) quietly(onEnd); }); // the last resort: a process.exit() nobody expected
|
|
151
161
|
// A dead terminal answers a write or a read with an error event. These two listeners are never
|
|
152
162
|
// removed: the write that restores the terminal can fail after the run is over, and an error
|
|
153
163
|
// event nobody hears is an uncaught exception, which would turn the status into 1.
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// The detail of the selected instance: (model, pane) -> rows of spans. Pure: no IO, no clock, no
|
|
2
|
+
// escape byte. A label of the TUI's own and a value per line; every value is text the TUI did not
|
|
3
|
+
// write (a kernel's field, a shared sentence) and goes through shown(), cut or wrapped by cells.
|
|
4
|
+
import { lit, shown, shownEnd, shownLines } from "../term/text.mjs";
|
|
5
|
+
import { style } from "../term/style.mjs";
|
|
6
|
+
import { cutNote } from "./instances.mjs";
|
|
7
|
+
import { windowOf } from "./scroll.mjs";
|
|
8
|
+
|
|
9
|
+
const BOLD = style({ bold: true });
|
|
10
|
+
const DIM = style({ dim: true });
|
|
11
|
+
/** The title and the labels start here: the first cell holds the mark of the pane with the focus. */
|
|
12
|
+
const LEFT = 2;
|
|
13
|
+
/** The values start here: the longest label, `harness`, and a gap. */
|
|
14
|
+
const VALUE = LEFT + 9;
|
|
15
|
+
/** The most rows one wrapped value takes. */
|
|
16
|
+
const WRAP_ROWS = 6;
|
|
17
|
+
|
|
18
|
+
/** Every row of the detail for a pane `cols` wide, before it is scrolled: the title, then the
|
|
19
|
+
* facts. A sentence is wrapped onto rows of its own under its label; a path is read from its
|
|
20
|
+
* end. */
|
|
21
|
+
export function detailRows(m, cols) {
|
|
22
|
+
if (!m.detail) return [];
|
|
23
|
+
const room = Math.max(0, cols - VALUE);
|
|
24
|
+
const rows = [[{ col: LEFT, text: shown(m.detail.title), style: BOLD, width: Math.max(0, cols - LEFT) }]];
|
|
25
|
+
for (const fact of m.detail.facts) {
|
|
26
|
+
const label = fact.label ? [{ col: LEFT, text: lit(fact.label), style: DIM, width: VALUE - LEFT - 1 }] : [];
|
|
27
|
+
if (room < 1) { rows.push(label); continue; }
|
|
28
|
+
if (fact.form === "wrap") shownLines(fact.value, room, WRAP_ROWS, { ascii: m.ascii }).forEach((line, i) => rows.push([...(i === 0 ? label : []), { col: VALUE, text: line, width: room }]));
|
|
29
|
+
else rows.push([...label, { col: VALUE, text: fact.form === "end" ? shownEnd(fact.value, room, { ascii: m.ascii }) : shown(fact.value), width: room }]);
|
|
30
|
+
}
|
|
31
|
+
return rows;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** `{ rows, extent }`: `size.rows` rows for a pane `size.cols` wide, scrolled to the model's
|
|
35
|
+
* `detailTop`, and how many rows the whole detail has. The pane that has the focus says so with
|
|
36
|
+
* `>` beside the title, never by an attribute alone. */
|
|
37
|
+
export function detail(m, size) {
|
|
38
|
+
const all = detailRows(m, size.cols);
|
|
39
|
+
const rows = Array.from({ length: size.rows }, () => []);
|
|
40
|
+
const win = windowOf({ top: m.detailTop, count: all.length, height: size.rows });
|
|
41
|
+
all.slice(win.top, win.top + win.size).forEach((spans, i) => { rows[i] = spans; });
|
|
42
|
+
const note = cutNote(win.above, win.below);
|
|
43
|
+
if (note && size.rows > 0) rows[size.rows - 1] = [{ col: LEFT, text: lit(note), style: DIM, width: Math.max(0, size.cols - LEFT) }];
|
|
44
|
+
if (m.focus === "detail" && size.rows > 0 && all.length) rows[0] = [{ col: 0, text: lit(">"), style: BOLD, width: 1 }, ...rows[0]];
|
|
45
|
+
return { rows, extent: all.length };
|
|
46
|
+
}
|