@operad-io/tui 0.0.0-stage → 0.0.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/LICENSE +21 -0
- package/README.md +68 -2
- package/package.json +27 -4
- package/src/actions.ts +997 -0
- package/src/app.ts +1665 -0
- package/src/args.ts +198 -0
- package/src/autostart.ts +302 -0
- package/src/client.ts +478 -0
- package/src/discover.ts +90 -0
- package/src/events.ts +345 -0
- package/src/index.ts +11 -0
- package/src/layout.ts +260 -0
- package/src/live.ts +113 -0
- package/src/markdown.ts +85 -0
- package/src/recent.ts +40 -0
- package/src/sanitize.ts +18 -0
- package/src/state.ts +3200 -0
- package/src/theme.ts +525 -0
- package/src/themes.ts +60 -0
- package/src/view/index.ts +1204 -0
- package/src/wrap.ts +156 -0
- package/theme.schema.json +197 -0
- package/themes/LICENSE-catppuccin +21 -0
- package/themes/LICENSE-dracula +21 -0
- package/themes/LICENSE-github +21 -0
- package/themes/LICENSE-kanagawa +21 -0
- package/themes/LICENSE-nord +21 -0
- package/themes/LICENSE-one +20 -0
- package/themes/LICENSE-rose-pine +21 -0
- package/themes/LICENSE-solarized +20 -0
- package/themes/LICENSE-tokyo-night +22 -0
- package/themes/README.md +81 -0
- package/themes/catppuccin-frappe.json +19 -0
- package/themes/catppuccin-latte.json +19 -0
- package/themes/catppuccin-macchiato.json +19 -0
- package/themes/catppuccin-mocha.json +19 -0
- package/themes/dracula.json +19 -0
- package/themes/github-dark.json +19 -0
- package/themes/github-light.json +19 -0
- package/themes/kanagawa-dragon.json +19 -0
- package/themes/kanagawa-lotus.json +19 -0
- package/themes/kanagawa-wave.json +19 -0
- package/themes/nord.json +19 -0
- package/themes/one-dark.json +19 -0
- package/themes/one-light.json +19 -0
- package/themes/rose-pine-dawn.json +19 -0
- package/themes/rose-pine-main.json +19 -0
- package/themes/solarized-dark.json +19 -0
- package/themes/solarized-light.json +19 -0
- package/themes/tokyo-night-day.json +19 -0
- package/themes/tokyo-night-night.json +19 -0
- package/tui.schema.json +24 -0
package/src/state.ts
ADDED
|
@@ -0,0 +1,3200 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The TUI's whole state and its one reducer. Every event is a client result, a
|
|
3
|
+
* key or a resize; the reducer never fetches, never reads a clock and never
|
|
4
|
+
* touches the terminal, so a test drives it as a plain value.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type {
|
|
8
|
+
PendingItem,
|
|
9
|
+
RowMark,
|
|
10
|
+
RunningItem,
|
|
11
|
+
ViewListPage,
|
|
12
|
+
ViewAction,
|
|
13
|
+
ViewObject,
|
|
14
|
+
ViewRow,
|
|
15
|
+
ViewSectionContent,
|
|
16
|
+
ViewSectionInfo,
|
|
17
|
+
ViewSectionPage,
|
|
18
|
+
} from "@operad-io/engine";
|
|
19
|
+
import type { TuiSetting, TuiTypeInfo } from "./client.ts";
|
|
20
|
+
import { cachedLayout, layoutMarkdown, type LayoutLine } from "./layout.ts";
|
|
21
|
+
import type { ArgField } from "./args.ts";
|
|
22
|
+
import { sanitize } from "./sanitize.ts";
|
|
23
|
+
import { wrapText } from "./wrap.ts";
|
|
24
|
+
import {
|
|
25
|
+
actionFailed,
|
|
26
|
+
bindingFor,
|
|
27
|
+
chosenEntry,
|
|
28
|
+
confirmText,
|
|
29
|
+
dropLast,
|
|
30
|
+
fuzzyScore,
|
|
31
|
+
inputKey,
|
|
32
|
+
keyLabel,
|
|
33
|
+
mainAction,
|
|
34
|
+
mainKey,
|
|
35
|
+
MAIN_KEY,
|
|
36
|
+
runKey,
|
|
37
|
+
startAction,
|
|
38
|
+
typed,
|
|
39
|
+
isReserved,
|
|
40
|
+
resolveBindings,
|
|
41
|
+
remember,
|
|
42
|
+
startAnswer,
|
|
43
|
+
withNotice,
|
|
44
|
+
type TuiCommand,
|
|
45
|
+
pageActions,
|
|
46
|
+
} from "./actions.ts";
|
|
47
|
+
|
|
48
|
+
/** The two terminal themes the view knows. */
|
|
49
|
+
export type TuiTheme = "light" | "dark";
|
|
50
|
+
|
|
51
|
+
/** How a page draws its next level: nested by `after`, or one flat list. */
|
|
52
|
+
export type TuiChildLayout = "tree" | "list";
|
|
53
|
+
|
|
54
|
+
/** What a tab lists. */
|
|
55
|
+
export type TuiTabKind = "pending" | "running" | "plugin" | "settings";
|
|
56
|
+
|
|
57
|
+
/** The marker a row shows: one of the plugin's marks, or nothing. */
|
|
58
|
+
export type TuiMark = RowMark | "none";
|
|
59
|
+
|
|
60
|
+
/** The marks that stay when the kernel says a person waits; others draw `waiting`. */
|
|
61
|
+
const WAITING_KEEPS: readonly RowMark[] = [
|
|
62
|
+
"waiting",
|
|
63
|
+
"proposed",
|
|
64
|
+
"rework_pending",
|
|
65
|
+
"ready_to_merge",
|
|
66
|
+
];
|
|
67
|
+
|
|
68
|
+
/** One pending question a person answers with one of its choices. */
|
|
69
|
+
export interface TuiAnswer {
|
|
70
|
+
readonly runId: string;
|
|
71
|
+
readonly waitId: string;
|
|
72
|
+
readonly prompt: string;
|
|
73
|
+
readonly choices: readonly string[];
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** One line of a list, in the words the view renders. */
|
|
77
|
+
export interface TuiRow {
|
|
78
|
+
readonly id: string;
|
|
79
|
+
readonly title: string;
|
|
80
|
+
readonly state: string;
|
|
81
|
+
readonly note?: string | undefined;
|
|
82
|
+
readonly waiting: boolean;
|
|
83
|
+
readonly running: boolean;
|
|
84
|
+
/** The plugin's semantic mark, when the row carries one. */
|
|
85
|
+
readonly mark?: RowMark | undefined;
|
|
86
|
+
/** A short text shown after the state. */
|
|
87
|
+
readonly badge?: string | undefined;
|
|
88
|
+
/** The sibling ids this row waits for. */
|
|
89
|
+
readonly after?: readonly string[] | undefined;
|
|
90
|
+
/** Set on an error row; the view adds one red line under the row. */
|
|
91
|
+
readonly error?: string | undefined;
|
|
92
|
+
/** False for a row that names no object (a run, a candidate): it never opens. */
|
|
93
|
+
readonly openable?: boolean | undefined;
|
|
94
|
+
/** The parts of the dim second line; unset means state, badge and note. */
|
|
95
|
+
readonly detail?: readonly string[] | undefined;
|
|
96
|
+
/** A pending question whose choices are not the object's actions. */
|
|
97
|
+
readonly answer?: TuiAnswer | undefined;
|
|
98
|
+
/** The row names no object: its first line is the title alone. */
|
|
99
|
+
readonly unnamed?: boolean | undefined;
|
|
100
|
+
/** The object type, for a row of a plugin tab or of a page's next level. */
|
|
101
|
+
readonly type?: string | undefined;
|
|
102
|
+
/** The id of the top-level object whose group the row is in. */
|
|
103
|
+
readonly parent?: string | undefined;
|
|
104
|
+
/** On the first row of a group: the top-level object the group is named for. */
|
|
105
|
+
readonly group?: TuiRow | undefined;
|
|
106
|
+
/** The row is a top-level object with nothing under it, shown as its own group row. */
|
|
107
|
+
readonly bare?: boolean | undefined;
|
|
108
|
+
/** A group's heading shown as its own row, so Enter can open the object it names. */
|
|
109
|
+
readonly heading?: boolean | undefined;
|
|
110
|
+
/** The row is indented under a heading row, not under a group line. */
|
|
111
|
+
readonly grouped?: boolean | undefined;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** The next level under the open object: its rows and where the cursor is in them. */
|
|
115
|
+
export interface TuiChildren {
|
|
116
|
+
readonly rows: readonly TuiRow[];
|
|
117
|
+
readonly cursor: number;
|
|
118
|
+
/** The keys move in this list instead of the left one. */
|
|
119
|
+
readonly focused: boolean;
|
|
120
|
+
/** How many children the list's filter left out. */
|
|
121
|
+
readonly hidden?: number | undefined;
|
|
122
|
+
/** How many hidden children are done. */
|
|
123
|
+
readonly hiddenDone?: number | undefined;
|
|
124
|
+
/** How many hidden children are cancelled. */
|
|
125
|
+
readonly hiddenCancelled?: number | undefined;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** One section of the open object, with the content fetched so far. */
|
|
129
|
+
export interface TuiSection {
|
|
130
|
+
readonly id: string;
|
|
131
|
+
readonly title: string;
|
|
132
|
+
readonly live: boolean;
|
|
133
|
+
readonly markdown: string;
|
|
134
|
+
readonly next?: string | undefined;
|
|
135
|
+
readonly loaded: boolean;
|
|
136
|
+
readonly loading: boolean;
|
|
137
|
+
/** The whole section has been fetched with `full=true`. */
|
|
138
|
+
readonly full: boolean;
|
|
139
|
+
/** The server cut the content at its size cap. */
|
|
140
|
+
readonly capped: boolean;
|
|
141
|
+
/** How many pages of content are in `markdown`; a refresh fetches as many. */
|
|
142
|
+
readonly pages: number;
|
|
143
|
+
readonly error?: string | undefined;
|
|
144
|
+
/** The section's table of contents, when the plugin sent one. */
|
|
145
|
+
readonly toc?: readonly ViewSectionPage[] | undefined;
|
|
146
|
+
/** The index in `toc` of the first page `markdown` holds. */
|
|
147
|
+
readonly pageStart: number;
|
|
148
|
+
/** Where in `markdown` each page after the first one it holds begins (a character offset). */
|
|
149
|
+
readonly pageMarks: readonly number[];
|
|
150
|
+
/** The first content line the pane shows. */
|
|
151
|
+
readonly scroll: number;
|
|
152
|
+
/** A live section keeps its end in view. */
|
|
153
|
+
readonly following: boolean;
|
|
154
|
+
/** The opaque position the finished content has reached; a refresh reads after it. */
|
|
155
|
+
readonly mark?: string | undefined;
|
|
156
|
+
/** The part that is still changing: drawn after the content, in the weak colour. */
|
|
157
|
+
readonly tail?: string | undefined;
|
|
158
|
+
/** Grows with each content change; an older answer is dropped. */
|
|
159
|
+
readonly version: number;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** The object the right pane shows, or the one that fills a narrow screen. */
|
|
163
|
+
export interface TuiObject {
|
|
164
|
+
readonly id: string;
|
|
165
|
+
/** The id of the list row it was opened from; the same row opens it once. */
|
|
166
|
+
readonly source: string;
|
|
167
|
+
readonly type: string;
|
|
168
|
+
readonly link: string;
|
|
169
|
+
readonly row: TuiRow;
|
|
170
|
+
readonly sections: readonly TuiSection[];
|
|
171
|
+
readonly active: number;
|
|
172
|
+
/** The next level under the object, once it has arrived. */
|
|
173
|
+
readonly children?: TuiChildren | undefined;
|
|
174
|
+
/** The page this one was opened from (a child opened from its parent). */
|
|
175
|
+
readonly back?: TuiObject | undefined;
|
|
176
|
+
/** The object response has not arrived yet; its row comes from the list. */
|
|
177
|
+
readonly loading: boolean;
|
|
178
|
+
/** The actions the object offers now, in the server's order. */
|
|
179
|
+
readonly actions: readonly ViewAction[];
|
|
180
|
+
readonly error?: string | undefined;
|
|
181
|
+
readonly unavailable?: string | undefined;
|
|
182
|
+
/** Grows with each open; an answer to an older open is dropped. */
|
|
183
|
+
readonly version: number;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/** One key binding in force, from `GET /v1/keys`. */
|
|
187
|
+
export interface TuiBinding {
|
|
188
|
+
readonly key: string;
|
|
189
|
+
/** `<plugin>:<action>`. */
|
|
190
|
+
readonly action: string;
|
|
191
|
+
readonly source: "plugin" | "user";
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/** What the keys type into: the `:` palette, a note, or a confirmation. */
|
|
195
|
+
export type TuiInput =
|
|
196
|
+
| { readonly kind: "palette"; readonly query: string; readonly cursor: number }
|
|
197
|
+
| { readonly kind: "pages"; readonly query: string; readonly cursor: number }
|
|
198
|
+
| { readonly kind: "themes"; readonly cursor: number }
|
|
199
|
+
| {
|
|
200
|
+
readonly kind: "note";
|
|
201
|
+
readonly action: string;
|
|
202
|
+
readonly id: string;
|
|
203
|
+
readonly text: string;
|
|
204
|
+
readonly required: boolean;
|
|
205
|
+
/** The action asks to confirm after the note. */
|
|
206
|
+
readonly confirm: boolean;
|
|
207
|
+
readonly args?: Readonly<Record<string, unknown>> | undefined;
|
|
208
|
+
}
|
|
209
|
+
| {
|
|
210
|
+
/** One choice of a pending question is being chosen. */
|
|
211
|
+
readonly kind: "answer";
|
|
212
|
+
readonly answer: TuiAnswer;
|
|
213
|
+
readonly cursor: number;
|
|
214
|
+
}
|
|
215
|
+
| {
|
|
216
|
+
/** One argument of an action is being asked: a list, a number or a text. */
|
|
217
|
+
readonly kind: "args";
|
|
218
|
+
readonly action: string;
|
|
219
|
+
readonly id: string;
|
|
220
|
+
readonly fields: readonly ArgField[];
|
|
221
|
+
/** The field being asked. */
|
|
222
|
+
readonly at: number;
|
|
223
|
+
/** The answers so far, by argument name. */
|
|
224
|
+
readonly values: Readonly<Record<string, unknown>>;
|
|
225
|
+
/** The cursor in a list. */
|
|
226
|
+
readonly cursor: number;
|
|
227
|
+
/** What is typed into a number or a text. */
|
|
228
|
+
readonly text: string;
|
|
229
|
+
/** Why the last answer was refused. */
|
|
230
|
+
readonly error?: string | undefined;
|
|
231
|
+
/** The note comes after the arguments: whether it is required. */
|
|
232
|
+
readonly note?: { readonly required: boolean } | undefined;
|
|
233
|
+
readonly confirm: boolean;
|
|
234
|
+
}
|
|
235
|
+
| {
|
|
236
|
+
/** A setting is open: its details, and a value prompt when it can change. */
|
|
237
|
+
readonly kind: "setting";
|
|
238
|
+
readonly setting: TuiSetting;
|
|
239
|
+
/** The prompt; absent for a setting that cannot change. */
|
|
240
|
+
readonly field?: ArgField | undefined;
|
|
241
|
+
readonly cursor: number;
|
|
242
|
+
readonly text: string;
|
|
243
|
+
}
|
|
244
|
+
| {
|
|
245
|
+
readonly kind: "setting-confirm";
|
|
246
|
+
readonly setting: TuiSetting;
|
|
247
|
+
readonly value: unknown;
|
|
248
|
+
}
|
|
249
|
+
| {
|
|
250
|
+
readonly kind: "confirm";
|
|
251
|
+
readonly action: string;
|
|
252
|
+
readonly id: string;
|
|
253
|
+
readonly note?: string | undefined;
|
|
254
|
+
readonly args?: Readonly<Record<string, unknown>> | undefined;
|
|
255
|
+
};
|
|
256
|
+
|
|
257
|
+
/** An action the reducer wants the caller to send with `POST /v1/act`. */
|
|
258
|
+
export interface TuiActionRequest {
|
|
259
|
+
readonly action: string;
|
|
260
|
+
readonly id: string;
|
|
261
|
+
readonly note?: string | undefined;
|
|
262
|
+
readonly args?: Readonly<Record<string, unknown>> | undefined;
|
|
263
|
+
readonly confirmed?: boolean | undefined;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/** An answer the reducer wants the caller to send with `POST /v1/answer`. */
|
|
267
|
+
export interface TuiAnswerRequest {
|
|
268
|
+
readonly runId: string;
|
|
269
|
+
readonly waitId: string;
|
|
270
|
+
readonly choice: string;
|
|
271
|
+
readonly note?: string | undefined;
|
|
272
|
+
readonly confirmed?: boolean | undefined;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/** A setting change the reducer wants the caller to send with `POST /v1/settings`. */
|
|
276
|
+
export interface TuiSettingRequest {
|
|
277
|
+
readonly name: string;
|
|
278
|
+
readonly scope: string;
|
|
279
|
+
readonly value: unknown;
|
|
280
|
+
readonly confirmed?: boolean | undefined;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/** An object the reducer wants the caller to fetch. */
|
|
284
|
+
export interface TuiObjectRequest {
|
|
285
|
+
readonly id: string;
|
|
286
|
+
readonly version: number;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/** A section page the reducer wants the caller to fetch. */
|
|
290
|
+
export interface TuiSectionRequest {
|
|
291
|
+
readonly objectId: string;
|
|
292
|
+
readonly sectionId: string;
|
|
293
|
+
readonly cursor?: string | undefined;
|
|
294
|
+
/** The index in the table of contents of the page asked for; it replaces the text. */
|
|
295
|
+
readonly jump?: number | undefined;
|
|
296
|
+
readonly full: boolean;
|
|
297
|
+
/** The section version the page was asked for. */
|
|
298
|
+
readonly version: number;
|
|
299
|
+
/** The open that asked: a page for an older open is dropped. */
|
|
300
|
+
readonly objectVersion: number;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/** A list page the reducer wants the caller to fetch next. */
|
|
304
|
+
export interface TuiPageRequest {
|
|
305
|
+
readonly tabId: string;
|
|
306
|
+
readonly cursor: string;
|
|
307
|
+
/** The list version the page was asked for; a newer list drops the answer. */
|
|
308
|
+
readonly version: number;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/** The rows, cursor and paging state of one tab. */
|
|
312
|
+
export interface TuiList {
|
|
313
|
+
readonly rows: readonly TuiRow[];
|
|
314
|
+
readonly cursor: number;
|
|
315
|
+
readonly next?: string | undefined;
|
|
316
|
+
/** The first page has arrived. */
|
|
317
|
+
readonly loaded: boolean;
|
|
318
|
+
/** A next page is being fetched; a second request waits for its result. */
|
|
319
|
+
readonly loading: boolean;
|
|
320
|
+
/** A failed page fetch, kept until the next successful page. */
|
|
321
|
+
readonly error?: string | undefined;
|
|
322
|
+
/** Grows with each replace; an appended page for an older one is dropped. */
|
|
323
|
+
readonly version: number;
|
|
324
|
+
/** The filter changed and the rows of the new one have not arrived yet. */
|
|
325
|
+
readonly switching?: boolean | undefined;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/** One tab: Pending, Running or one plugin. */
|
|
329
|
+
export interface TuiTab {
|
|
330
|
+
readonly id: string;
|
|
331
|
+
readonly kind: TuiTabKind;
|
|
332
|
+
/** The plugin a plugin tab is for. */
|
|
333
|
+
readonly plugin?: string | undefined;
|
|
334
|
+
/** The plugin's object types, in the server's order; set only for a plugin tab. */
|
|
335
|
+
readonly types?: readonly TuiTypeInfo[] | undefined;
|
|
336
|
+
/** What a plugin tab lists: its top-level objects, or their next level in groups. */
|
|
337
|
+
readonly level?: "roots" | "children" | undefined;
|
|
338
|
+
readonly title: string;
|
|
339
|
+
readonly list: TuiList;
|
|
340
|
+
/** The filters the type declares, in the server's order; unset without any. */
|
|
341
|
+
readonly filters?: readonly TuiFilterInfo[] | undefined;
|
|
342
|
+
/** The filter the list shows now; per tab, never saved. */
|
|
343
|
+
readonly filter?: string | undefined;
|
|
344
|
+
/** How many rows of the type the filter left out; unset on `all`. */
|
|
345
|
+
readonly hidden?: number | undefined;
|
|
346
|
+
/** How many times `f` cycled this tab's filter. */
|
|
347
|
+
readonly filterSeq?: number | undefined;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/** One filter of a type: its name and the states it shows (`all` has none). */
|
|
351
|
+
export interface TuiFilterInfo {
|
|
352
|
+
readonly name: string;
|
|
353
|
+
readonly states?: readonly string[] | undefined;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/** The first page of a tab's list under a new filter, to be fetched. */
|
|
357
|
+
export interface TuiFilterRequest {
|
|
358
|
+
readonly tabId: string;
|
|
359
|
+
readonly filter: string;
|
|
360
|
+
/** The tab's filter-cycle count when `f` was pressed; a later answer wins. */
|
|
361
|
+
readonly seq: number;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/** Everything the view needs; nothing here is derived from a terminal handle. */
|
|
365
|
+
export interface TuiState {
|
|
366
|
+
readonly tabs: readonly TuiTab[];
|
|
367
|
+
/**
|
|
368
|
+
* The objects whose types a plugin put under settings; fetched like a tab but
|
|
369
|
+
* never in the top bar. The palette lists them by type.
|
|
370
|
+
*/
|
|
371
|
+
readonly settingsTab?: TuiTab | undefined;
|
|
372
|
+
readonly active: number;
|
|
373
|
+
readonly width: number;
|
|
374
|
+
readonly height: number;
|
|
375
|
+
readonly theme: TuiTheme;
|
|
376
|
+
/** How the next level of the open object is drawn. */
|
|
377
|
+
readonly childLayout: TuiChildLayout;
|
|
378
|
+
/** The `?` key list is open. */
|
|
379
|
+
readonly keys: boolean;
|
|
380
|
+
/** The first key list row drawn; the list scrolls when the terminal is short. */
|
|
381
|
+
readonly helpScroll: number;
|
|
382
|
+
readonly quit: boolean;
|
|
383
|
+
/** `r` asked for a fresh set of lists; the caller clears it after fetching. */
|
|
384
|
+
readonly refreshRequested: boolean;
|
|
385
|
+
/** Set when `f` chose a filter and its first page must be fetched. */
|
|
386
|
+
readonly filterRequest?: TuiFilterRequest | undefined;
|
|
387
|
+
/** Set when the cursor nears the end of a list that has another page. */
|
|
388
|
+
readonly pageRequest?: TuiPageRequest | undefined;
|
|
389
|
+
/** The open object, if any. */
|
|
390
|
+
readonly object?: TuiObject | undefined;
|
|
391
|
+
/** The version of the next open; it never restarts, even after a close. */
|
|
392
|
+
readonly objectVersion: number;
|
|
393
|
+
/** Set when a row is opened and the caller must fetch the object. */
|
|
394
|
+
readonly objectRequest?: TuiObjectRequest | undefined;
|
|
395
|
+
/**
|
|
396
|
+
* `c` on a tab: the parent page is opening so the action that creates `type`
|
|
397
|
+
* can start on it; the page's answer starts it.
|
|
398
|
+
*/
|
|
399
|
+
readonly creating?: { readonly parent: string; readonly type: string } | undefined;
|
|
400
|
+
/** Set when a section must be fetched. */
|
|
401
|
+
readonly sectionRequest?: TuiSectionRequest | undefined;
|
|
402
|
+
/** Grows each time a list cursor moves; the shell opens what it lands on. */
|
|
403
|
+
readonly moveSeq: number;
|
|
404
|
+
/** The cursor moved and the object pane has not followed it yet. */
|
|
405
|
+
readonly followPending: boolean;
|
|
406
|
+
/** Grows with each refresh; an answer to an older refresh is dropped. */
|
|
407
|
+
readonly generation: number;
|
|
408
|
+
/** The last failure to show in the footer; cleared by a good refresh. */
|
|
409
|
+
readonly status?: string | undefined;
|
|
410
|
+
/** The key bindings in force; a user binding beats a plugin default. */
|
|
411
|
+
readonly bindings: readonly TuiBinding[];
|
|
412
|
+
/** The palette entries used last, the latest first; the shell keeps them between runs. */
|
|
413
|
+
readonly recent: readonly string[];
|
|
414
|
+
/** The installed theme names the theme list shows. */
|
|
415
|
+
readonly themeNames: readonly string[];
|
|
416
|
+
/** The theme in force. */
|
|
417
|
+
readonly themeCurrent: string;
|
|
418
|
+
/** The theme the list cursor is on; the shell shows it until the list closes. */
|
|
419
|
+
readonly themePreview?: string | undefined;
|
|
420
|
+
/** Set when the chosen theme must be kept; the caller clears it as it saves. */
|
|
421
|
+
readonly themeRequest?: string | undefined;
|
|
422
|
+
/** Set when the chosen child layout must be saved; the caller clears it. */
|
|
423
|
+
readonly layoutRequest?: TuiChildLayout | undefined;
|
|
424
|
+
/** The palette, note or confirmation the keys are typing into. */
|
|
425
|
+
readonly input?: TuiInput | undefined;
|
|
426
|
+
/** One footer line (a refusal, or "not available") until the next key. */
|
|
427
|
+
readonly notice?: string | undefined;
|
|
428
|
+
/** Set when an action must be sent; the caller clears it as it sends. */
|
|
429
|
+
readonly actionRequest?: TuiActionRequest | undefined;
|
|
430
|
+
/** Set when an answer must be sent; the caller clears it as it sends. */
|
|
431
|
+
readonly answerRequest?: TuiAnswerRequest | undefined;
|
|
432
|
+
/** The settings the palette lists, global and the open object's repository. */
|
|
433
|
+
readonly settings: readonly TuiSetting[];
|
|
434
|
+
/** Set when the palette opened and the settings must be fetched again. */
|
|
435
|
+
readonly settingsRequest: boolean;
|
|
436
|
+
/** Set when a setting change must be sent; the caller clears it as it sends. */
|
|
437
|
+
readonly settingRequest?: TuiSettingRequest | undefined;
|
|
438
|
+
/** An action is on its way; a second one waits for the answer. */
|
|
439
|
+
readonly acting: boolean;
|
|
440
|
+
/** The server this screen talks to, named in the unreachable line. */
|
|
441
|
+
readonly url?: string | undefined;
|
|
442
|
+
/** Whether the server answers; the lists stay as they were when it does not. */
|
|
443
|
+
readonly connection: TuiConnection;
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
/** What the last request said about the server. */
|
|
447
|
+
export type TuiConnection = "ok" | "unreachable" | "unauthorized";
|
|
448
|
+
|
|
449
|
+
/** The number of rows from the end that make the reducer ask for a page. */
|
|
450
|
+
export const PAGE_AHEAD = 2;
|
|
451
|
+
|
|
452
|
+
/** Initial dimensions, matching a conventional 80 by 24 terminal. */
|
|
453
|
+
export const DEFAULT_WIDTH = 80;
|
|
454
|
+
export const DEFAULT_HEIGHT = 24;
|
|
455
|
+
|
|
456
|
+
export interface TuiStateOptions {
|
|
457
|
+
readonly width?: number | undefined;
|
|
458
|
+
readonly height?: number | undefined;
|
|
459
|
+
readonly theme?: TuiTheme | undefined;
|
|
460
|
+
readonly childLayout?: TuiChildLayout | undefined;
|
|
461
|
+
readonly url?: string | undefined;
|
|
462
|
+
readonly recent?: readonly string[] | undefined;
|
|
463
|
+
readonly themeNames?: readonly string[] | undefined;
|
|
464
|
+
readonly themeCurrent?: string | undefined;
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
function makeList(): TuiList {
|
|
468
|
+
return {
|
|
469
|
+
rows: [],
|
|
470
|
+
cursor: 0,
|
|
471
|
+
loaded: false,
|
|
472
|
+
loading: false,
|
|
473
|
+
version: 0,
|
|
474
|
+
};
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
function makeTab(
|
|
478
|
+
id: string,
|
|
479
|
+
kind: TuiTabKind,
|
|
480
|
+
title: string,
|
|
481
|
+
types?: readonly TuiTypeInfo[],
|
|
482
|
+
): TuiTab {
|
|
483
|
+
const filters = filtersOf(types);
|
|
484
|
+
const defaulted = types?.find((info) => info.defaultFilter !== undefined)?.defaultFilter;
|
|
485
|
+
const filter =
|
|
486
|
+
filters === undefined
|
|
487
|
+
? undefined
|
|
488
|
+
: (filters.find((f) => f.name === defaulted) ?? filters[0])?.name;
|
|
489
|
+
return {
|
|
490
|
+
id,
|
|
491
|
+
kind,
|
|
492
|
+
...(types === undefined ? {} : { types }),
|
|
493
|
+
title,
|
|
494
|
+
list: makeList(),
|
|
495
|
+
...(filters === undefined ? {} : { filters, filter }),
|
|
496
|
+
};
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
/** The filters of a plugin's types: each name once, in the order they first appear. */
|
|
500
|
+
function filtersOf(types: readonly TuiTypeInfo[] | undefined): readonly TuiFilterInfo[] | undefined {
|
|
501
|
+
const out: TuiFilterInfo[] = [];
|
|
502
|
+
for (const info of types ?? []) {
|
|
503
|
+
for (const filter of info.filters ?? []) {
|
|
504
|
+
if (!out.some((seen) => seen.name === filter.name)) out.push(filter);
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
return out.length === 0 ? undefined : out;
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
/** The filter to ask a type for: the tab's, when the type declares it. */
|
|
511
|
+
export function filterFor(tab: TuiTab, info: TuiTypeInfo): string | undefined {
|
|
512
|
+
const name = tab.filter;
|
|
513
|
+
if (name === undefined) return undefined;
|
|
514
|
+
return info.filters?.some((f) => f.name === name) === true ? name : undefined;
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
/** The plugin tab or settings list that holds `type`, if any. */
|
|
518
|
+
export function pluginTabOf(state: TuiState, type: string): TuiTab | undefined {
|
|
519
|
+
return (
|
|
520
|
+
state.tabs.find((tab) => tab.types?.some((info) => info.type === type) === true) ??
|
|
521
|
+
(state.settingsTab?.types?.some((info) => info.type === type) === true
|
|
522
|
+
? state.settingsTab
|
|
523
|
+
: undefined)
|
|
524
|
+
);
|
|
525
|
+
}
|
|
526
|
+
/** The two fixed tabs; `/v1/types` adds the rest. */
|
|
527
|
+
export function initialState(options: TuiStateOptions = {}): TuiState {
|
|
528
|
+
return {
|
|
529
|
+
tabs: [
|
|
530
|
+
makeTab("pending", "pending", "Pending"),
|
|
531
|
+
makeTab("running", "running", "Running"),
|
|
532
|
+
],
|
|
533
|
+
active: 0,
|
|
534
|
+
width: Math.max(1, Math.trunc(options.width ?? DEFAULT_WIDTH)),
|
|
535
|
+
height: Math.max(1, Math.trunc(options.height ?? DEFAULT_HEIGHT)),
|
|
536
|
+
theme: options.theme ?? "dark",
|
|
537
|
+
childLayout: options.childLayout ?? "tree",
|
|
538
|
+
keys: false,
|
|
539
|
+
helpScroll: 0,
|
|
540
|
+
quit: false,
|
|
541
|
+
refreshRequested: false,
|
|
542
|
+
objectVersion: 0,
|
|
543
|
+
moveSeq: 0,
|
|
544
|
+
followPending: false,
|
|
545
|
+
generation: 0,
|
|
546
|
+
bindings: [],
|
|
547
|
+
recent: options.recent ?? [],
|
|
548
|
+
settings: [],
|
|
549
|
+
settingsRequest: false,
|
|
550
|
+
themeNames: options.themeNames ?? [],
|
|
551
|
+
themeCurrent: options.themeCurrent ?? "auto",
|
|
552
|
+
acting: false,
|
|
553
|
+
connection: "ok",
|
|
554
|
+
...(options.url === undefined ? {} : { url: options.url }),
|
|
555
|
+
};
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
/** A short alias for callers that build the first state. */
|
|
559
|
+
export const createInitialState = initialState;
|
|
560
|
+
|
|
561
|
+
/** One row of a plugin tab's list, with its type and, first in a group, the group's heading. */
|
|
562
|
+
export interface TuiTreeEntry {
|
|
563
|
+
readonly row: ViewRow;
|
|
564
|
+
readonly type: string;
|
|
565
|
+
/** The top-level object this row is under, when the list is grouped. */
|
|
566
|
+
readonly parent?: string | undefined;
|
|
567
|
+
/** Set on the first row of a group: the top-level object's own row and type. */
|
|
568
|
+
readonly heading?:
|
|
569
|
+
| {
|
|
570
|
+
readonly row: ViewRow;
|
|
571
|
+
readonly type: string;
|
|
572
|
+
/** The heading's text when it names a parent outside the tab's own objects. */
|
|
573
|
+
readonly label?: string | undefined;
|
|
574
|
+
}
|
|
575
|
+
| undefined;
|
|
576
|
+
/** A top-level object with nothing under it: its row is its own group row. */
|
|
577
|
+
readonly bare?: boolean | undefined;
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
/** What the reducer accepts. */
|
|
581
|
+
export type TuiStateEvent =
|
|
582
|
+
| {
|
|
583
|
+
readonly type: "types";
|
|
584
|
+
readonly types: readonly TuiTypeInfo[];
|
|
585
|
+
readonly generation?: number | undefined;
|
|
586
|
+
}
|
|
587
|
+
| { readonly type: "filter-launched" }
|
|
588
|
+
/** A plugin tab's whole list: its top-level objects, or their next level in groups. */
|
|
589
|
+
| {
|
|
590
|
+
readonly type: "tree";
|
|
591
|
+
readonly tabId: string;
|
|
592
|
+
readonly level: "roots" | "children";
|
|
593
|
+
readonly entries: readonly TuiTreeEntry[];
|
|
594
|
+
/** How many rows the filter left out. */
|
|
595
|
+
readonly hidden?: number | undefined;
|
|
596
|
+
readonly generation?: number | undefined;
|
|
597
|
+
readonly filter?: string | undefined;
|
|
598
|
+
readonly filterSeq?: number | undefined;
|
|
599
|
+
}
|
|
600
|
+
/** The next level of the open object arrived. */
|
|
601
|
+
| {
|
|
602
|
+
readonly type: "children";
|
|
603
|
+
readonly objectId: string;
|
|
604
|
+
readonly version?: number | undefined;
|
|
605
|
+
readonly entries: readonly TuiTreeEntry[];
|
|
606
|
+
/** How many children the list's filter left out. */
|
|
607
|
+
readonly hidden?: number | undefined;
|
|
608
|
+
/** How many hidden children are done. */
|
|
609
|
+
readonly hiddenDone?: number | undefined;
|
|
610
|
+
/** How many hidden children are cancelled. */
|
|
611
|
+
readonly hiddenCancelled?: number | undefined;
|
|
612
|
+
/** The filter the children were asked under; another filter's answer is dropped. */
|
|
613
|
+
readonly filter?: string | undefined;
|
|
614
|
+
/** The filter-cycle count they were asked under; an older one is dropped. */
|
|
615
|
+
readonly filterSeq?: number | undefined;
|
|
616
|
+
}
|
|
617
|
+
| {
|
|
618
|
+
readonly type: "pending";
|
|
619
|
+
readonly items: readonly PendingItem[];
|
|
620
|
+
readonly generation?: number | undefined;
|
|
621
|
+
}
|
|
622
|
+
| {
|
|
623
|
+
readonly type: "running";
|
|
624
|
+
readonly items: readonly RunningItem[];
|
|
625
|
+
readonly generation?: number | undefined;
|
|
626
|
+
}
|
|
627
|
+
| {
|
|
628
|
+
readonly type: "page";
|
|
629
|
+
readonly tabId: string;
|
|
630
|
+
readonly page: ViewListPage;
|
|
631
|
+
/** Replace the list instead of appending one page. */
|
|
632
|
+
readonly replace?: boolean | undefined;
|
|
633
|
+
/** The filter the page was asked under; a page for another one is dropped. */
|
|
634
|
+
readonly filter?: string | undefined;
|
|
635
|
+
/** The filter-cycle count it was asked under; an older one is dropped. */
|
|
636
|
+
readonly filterSeq?: number | undefined;
|
|
637
|
+
/** For a replace: the refresh it belongs to. */
|
|
638
|
+
readonly generation?: number | undefined;
|
|
639
|
+
/** For an append: the list version it was asked for. */
|
|
640
|
+
readonly version?: number | undefined;
|
|
641
|
+
}
|
|
642
|
+
| {
|
|
643
|
+
readonly type: "rows";
|
|
644
|
+
readonly tabId: string;
|
|
645
|
+
readonly rows: readonly ViewRow[];
|
|
646
|
+
readonly next?: string | undefined;
|
|
647
|
+
readonly replace?: boolean | undefined;
|
|
648
|
+
}
|
|
649
|
+
| {
|
|
650
|
+
readonly type: "page-error";
|
|
651
|
+
readonly tabId: string;
|
|
652
|
+
readonly message: string;
|
|
653
|
+
readonly version?: number | undefined;
|
|
654
|
+
}
|
|
655
|
+
/** One object arrived for an open request. */
|
|
656
|
+
| {
|
|
657
|
+
readonly type: "object";
|
|
658
|
+
readonly object: ViewObject;
|
|
659
|
+
readonly version?: number | undefined;
|
|
660
|
+
}
|
|
661
|
+
| {
|
|
662
|
+
readonly type: "object-error";
|
|
663
|
+
readonly id: string;
|
|
664
|
+
readonly message: string;
|
|
665
|
+
readonly version?: number | undefined;
|
|
666
|
+
}
|
|
667
|
+
/** One section arrived; `cursor` means its next page was appended. */
|
|
668
|
+
| {
|
|
669
|
+
readonly type: "section";
|
|
670
|
+
readonly objectId: string;
|
|
671
|
+
readonly sectionId: string;
|
|
672
|
+
readonly content: ViewSectionContent;
|
|
673
|
+
readonly cursor?: string | undefined;
|
|
674
|
+
/** The table-of-contents index of a page that replaces the text. */
|
|
675
|
+
readonly jump?: number | undefined;
|
|
676
|
+
readonly full?: boolean | undefined;
|
|
677
|
+
readonly version?: number | undefined;
|
|
678
|
+
/** The open it was asked for; an older open drops it. */
|
|
679
|
+
readonly objectVersion?: number | undefined;
|
|
680
|
+
/** For a replace: how many pages the text holds. */
|
|
681
|
+
readonly pages?: number | undefined;
|
|
682
|
+
/** For a replace: where the pages after the first begin in the text. */
|
|
683
|
+
readonly marks?: readonly number[] | undefined;
|
|
684
|
+
/** For a replace: the table-of-contents index of the first page, kept from before. */
|
|
685
|
+
readonly start?: number | undefined;
|
|
686
|
+
/** The read asked with the last mark; the answer appends when it is a delta. */
|
|
687
|
+
readonly after?: boolean | undefined;
|
|
688
|
+
}
|
|
689
|
+
| {
|
|
690
|
+
readonly type: "section-error";
|
|
691
|
+
readonly objectId: string;
|
|
692
|
+
readonly sectionId: string;
|
|
693
|
+
readonly message: string;
|
|
694
|
+
readonly version?: number | undefined;
|
|
695
|
+
readonly objectVersion?: number | undefined;
|
|
696
|
+
}
|
|
697
|
+
/** The debounce after a cursor move ended: open the row it landed on. */
|
|
698
|
+
| { readonly type: "follow" }
|
|
699
|
+
/** The caller launched the object request; it is asked for once. */
|
|
700
|
+
| { readonly type: "object-launched" }
|
|
701
|
+
/** The caller launched the section request; it is asked for once. */
|
|
702
|
+
| { readonly type: "section-launched" }
|
|
703
|
+
/** A refresh began; answers to the one before it are dropped. */
|
|
704
|
+
| { readonly type: "refresh-start" }
|
|
705
|
+
/** The caller launched the pending page request; it is asked for once. */
|
|
706
|
+
| { readonly type: "page-launched" }
|
|
707
|
+
/** Show a failure in the footer, or clear it with no message. */
|
|
708
|
+
| {
|
|
709
|
+
readonly type: "status";
|
|
710
|
+
readonly message?: string | undefined;
|
|
711
|
+
readonly generation?: number | undefined;
|
|
712
|
+
}
|
|
713
|
+
| {
|
|
714
|
+
readonly type: "key";
|
|
715
|
+
readonly key: string;
|
|
716
|
+
/** The printable text the key types, when it types any. */
|
|
717
|
+
readonly text?: string | undefined;
|
|
718
|
+
readonly shift?: boolean | undefined;
|
|
719
|
+
readonly ctrl?: boolean | undefined;
|
|
720
|
+
readonly meta?: boolean | undefined;
|
|
721
|
+
}
|
|
722
|
+
| {
|
|
723
|
+
readonly type: "resize";
|
|
724
|
+
readonly width: number;
|
|
725
|
+
readonly height: number;
|
|
726
|
+
readonly theme?: TuiTheme | undefined;
|
|
727
|
+
}
|
|
728
|
+
| { readonly type: "theme"; readonly theme: TuiTheme }
|
|
729
|
+
/** The key bindings in force arrived from `GET /v1/keys`. */
|
|
730
|
+
| {
|
|
731
|
+
readonly type: "keys";
|
|
732
|
+
readonly bindings: readonly TuiBinding[];
|
|
733
|
+
readonly generation?: number | undefined;
|
|
734
|
+
}
|
|
735
|
+
/** The caller sent the action request; it is sent once. */
|
|
736
|
+
| { readonly type: "action-launched" }
|
|
737
|
+
/** The caller sent the answer request; it is sent once. */
|
|
738
|
+
| { readonly type: "answer-launched" }
|
|
739
|
+
| { readonly type: "setting-launched" }
|
|
740
|
+
/** The objects under settings arrived: each settings type's top level. */
|
|
741
|
+
| {
|
|
742
|
+
readonly type: "settings-objects";
|
|
743
|
+
readonly entries: readonly TuiTreeEntry[];
|
|
744
|
+
readonly generation?: number | undefined;
|
|
745
|
+
}
|
|
746
|
+
| { readonly type: "settings"; readonly settings: readonly TuiSetting[] }
|
|
747
|
+
/** The installed themes or the theme in force changed. */
|
|
748
|
+
| { readonly type: "theme-names"; readonly names: readonly string[]; readonly current: string }
|
|
749
|
+
/** The caller saved the chosen theme; it is saved once. */
|
|
750
|
+
| { readonly type: "theme-launched" }
|
|
751
|
+
/** The caller saved the chosen child layout; it is saved once. */
|
|
752
|
+
| { readonly type: "layout-launched" }
|
|
753
|
+
/** The action ran, or its result was already true and `notice` says so. */
|
|
754
|
+
| {
|
|
755
|
+
readonly type: "action-done";
|
|
756
|
+
readonly notice?: string | undefined;
|
|
757
|
+
/** The object the action created, as "<type>:<id>"; its page opens. */
|
|
758
|
+
readonly opens?: string | undefined;
|
|
759
|
+
}
|
|
760
|
+
/** The server refused the action; the message shows as the line, the status does not. */
|
|
761
|
+
| {
|
|
762
|
+
readonly type: "action-error";
|
|
763
|
+
readonly message: string;
|
|
764
|
+
readonly status?: number | undefined;
|
|
765
|
+
}
|
|
766
|
+
| { readonly type: "refreshed" }
|
|
767
|
+
/** The server answered, or did not, or refused the token. */
|
|
768
|
+
| { readonly type: "connection"; readonly connection: TuiConnection }
|
|
769
|
+
/** One row was fetched again; it replaces the rows with its id in every list. */
|
|
770
|
+
| { readonly type: "row"; readonly row: ViewRow }
|
|
771
|
+
| { readonly type: "row-gone"; readonly id: string };
|
|
772
|
+
|
|
773
|
+
/** A duration as the Running list writes it. */
|
|
774
|
+
export function formatElapsed(ms: number): string {
|
|
775
|
+
const value = Number.isFinite(ms) ? Math.max(0, ms) : 0;
|
|
776
|
+
if (value < 1000) return `${Math.floor(value)}ms`;
|
|
777
|
+
if (value < 60_000) return `${(value / 1000).toFixed(1)}s`;
|
|
778
|
+
const whole = Math.floor(value / 1000);
|
|
779
|
+
return `${Math.floor(whole / 60)}m ${whole % 60}s`;
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
function rowId(row: ViewRow): string {
|
|
783
|
+
return row.scope === undefined ? row.id : `${row.scope}/${row.id}`;
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
/** A `/v1/objects` row, unchanged except for the scope in its id. */
|
|
787
|
+
export function rowFromView(row: ViewRow, type?: string): TuiRow {
|
|
788
|
+
return {
|
|
789
|
+
id: sanitize(rowId(row)),
|
|
790
|
+
...(type === undefined ? {} : { type }),
|
|
791
|
+
title: sanitize(row.title),
|
|
792
|
+
state: sanitize(row.state),
|
|
793
|
+
...(row.note === undefined ? {} : { note: sanitize(row.note) }),
|
|
794
|
+
waiting: row.waiting,
|
|
795
|
+
running: row.running,
|
|
796
|
+
...(row.mark === undefined ? {} : { mark: row.mark }),
|
|
797
|
+
...(row.badge === undefined ? {} : { badge: sanitize(row.badge) }),
|
|
798
|
+
...(row.after === undefined ? {} : { after: row.after.map(sanitize) }),
|
|
799
|
+
...(row.error === undefined ? {} : { error: sanitize(row.error) }),
|
|
800
|
+
};
|
|
801
|
+
}
|
|
802
|
+
|
|
803
|
+
/** The item's answer, when its choices are not the object's actions. */
|
|
804
|
+
function answerOfPending(item: PendingItem): TuiAnswer | undefined {
|
|
805
|
+
if (item.waitId === undefined || item.choices.length === 0) return undefined;
|
|
806
|
+
const names = new Set(item.actions.map((action) => action.action));
|
|
807
|
+
// A choice that is also an action stays with the actions, as it works today.
|
|
808
|
+
if (item.choices.every((choice) => names.has(choice))) return undefined;
|
|
809
|
+
return {
|
|
810
|
+
runId: item.runId,
|
|
811
|
+
waitId: item.waitId,
|
|
812
|
+
prompt: item.prompt,
|
|
813
|
+
choices: item.choices,
|
|
814
|
+
};
|
|
815
|
+
}
|
|
816
|
+
|
|
817
|
+
function rowFromPending(item: PendingItem): TuiRow {
|
|
818
|
+
const answer = answerOfPending(item);
|
|
819
|
+
const answerDetail = answer === undefined ? [] : [`choices: ${answer.choices.join(", ")}`];
|
|
820
|
+
// A server that sends the object's row saves a read per item: the row names
|
|
821
|
+
// the object, and the question stays visible as the note. An older server
|
|
822
|
+
// sends no row, and the item shows its question and flow as before.
|
|
823
|
+
if (item.row !== undefined) {
|
|
824
|
+
return {
|
|
825
|
+
id: sanitize(item.object ?? item.runId),
|
|
826
|
+
title: sanitize(item.row.title),
|
|
827
|
+
state: sanitize(item.row.state),
|
|
828
|
+
note: sanitize(item.prompt),
|
|
829
|
+
detail: detailOf([item.prompt, repositoryOf(item.object), ...answerDetail]),
|
|
830
|
+
waiting: true,
|
|
831
|
+
running: false,
|
|
832
|
+
...(answer === undefined ? {} : { answer }),
|
|
833
|
+
...(item.row.mark === undefined ? {} : { mark: item.row.mark }),
|
|
834
|
+
...(item.row.badge === undefined ? {} : { badge: sanitize(item.row.badge) }),
|
|
835
|
+
...(item.row.error === undefined ? {} : { error: sanitize(item.row.error) }),
|
|
836
|
+
...(item.object === undefined ? { openable: false, unnamed: true } : {}),
|
|
837
|
+
};
|
|
838
|
+
}
|
|
839
|
+
return {
|
|
840
|
+
id: sanitize(item.object ?? item.runId),
|
|
841
|
+
title: sanitize(item.prompt),
|
|
842
|
+
state: sanitize(item.flow),
|
|
843
|
+
...(item.step === null ? {} : { note: sanitize(item.step) }),
|
|
844
|
+
detail: detailOf([item.flow, item.step ?? "", repositoryOf(item.object), ...answerDetail]),
|
|
845
|
+
waiting: true,
|
|
846
|
+
running: false,
|
|
847
|
+
...(answer === undefined ? {} : { answer }),
|
|
848
|
+
...(item.object === undefined ? { openable: false, unnamed: true } : {}),
|
|
849
|
+
};
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
function rowFromRunning(item: RunningItem): TuiRow {
|
|
853
|
+
const named = item.object !== null && item.object !== undefined;
|
|
854
|
+
const stepMs = item.stepElapsedMs ?? item.elapsedMs;
|
|
855
|
+
return {
|
|
856
|
+
id: sanitize(item.object ?? item.runId),
|
|
857
|
+
title: sanitize(named ? (item.title ?? "") : (item.title ?? item.flow)),
|
|
858
|
+
state: sanitize(item.stepLabel ?? "-"),
|
|
859
|
+
note: formatElapsed(stepMs),
|
|
860
|
+
detail: detailOf([
|
|
861
|
+
item.stepLabel ?? "",
|
|
862
|
+
item.stepElapsedMs === null || item.stepElapsedMs === undefined
|
|
863
|
+
? ""
|
|
864
|
+
: formatElapsed(item.stepElapsedMs),
|
|
865
|
+
item.queuedMs === undefined || item.queuedMs <= 0
|
|
866
|
+
? ""
|
|
867
|
+
: `queued ${formatElapsed(item.queuedMs)}`,
|
|
868
|
+
formatElapsed(item.elapsedMs),
|
|
869
|
+
repositoryOf(item.object),
|
|
870
|
+
]),
|
|
871
|
+
waiting: false,
|
|
872
|
+
running: true,
|
|
873
|
+
...(item.mark === undefined ? {} : { mark: item.mark }),
|
|
874
|
+
...(named ? {} : { openable: false, unnamed: true }),
|
|
875
|
+
};
|
|
876
|
+
}
|
|
877
|
+
|
|
878
|
+
/** The dim line's parts, cleaned, without the empty ones. */
|
|
879
|
+
function detailOf(parts: readonly string[]): readonly string[] {
|
|
880
|
+
return parts.map((part) => sanitize(part).trim()).filter((part) => part !== "");
|
|
881
|
+
}
|
|
882
|
+
|
|
883
|
+
/** The repository an object name carries: `operad` in `ops/T-1-x`. */
|
|
884
|
+
function repositoryOf(object: string | null | undefined): string {
|
|
885
|
+
if (object === null || object === undefined) return "";
|
|
886
|
+
const bare = object.slice(object.indexOf(":") + 1);
|
|
887
|
+
const slash = bare.lastIndexOf("/");
|
|
888
|
+
return slash < 0 ? "" : bare.slice(0, slash);
|
|
889
|
+
}
|
|
890
|
+
|
|
891
|
+
/** The marker a row puts in its first column. */
|
|
892
|
+
export function rowMark(row: TuiRow): TuiMark {
|
|
893
|
+
if (row.waiting) {
|
|
894
|
+
return row.mark !== undefined && WAITING_KEEPS.includes(row.mark)
|
|
895
|
+
? row.mark
|
|
896
|
+
: "waiting";
|
|
897
|
+
}
|
|
898
|
+
if (row.mark !== undefined) return row.mark;
|
|
899
|
+
if (row.running) return "running";
|
|
900
|
+
return "none";
|
|
901
|
+
}
|
|
902
|
+
|
|
903
|
+
/** A child the plugin marked cancelled, or whose state says so. */
|
|
904
|
+
export function cancelledRow(row: TuiRow): boolean {
|
|
905
|
+
return rowMark(row) === "cancelled" || /(^|\.)cancelled$/i.test(row.state);
|
|
906
|
+
}
|
|
907
|
+
|
|
908
|
+
/** A child that is done: its mark says so, and no state word can. */
|
|
909
|
+
function doneRow(row: TuiRow): boolean {
|
|
910
|
+
return rowMark(row) === "done";
|
|
911
|
+
}
|
|
912
|
+
|
|
913
|
+
interface ChildParent {
|
|
914
|
+
readonly index: number;
|
|
915
|
+
/** The id as the row's `after` wrote it; an unknown id is left out. */
|
|
916
|
+
readonly id: string;
|
|
917
|
+
}
|
|
918
|
+
|
|
919
|
+
/** The rows in a child's `after` that are in the list, without itself and duplicates. */
|
|
920
|
+
function childParents(
|
|
921
|
+
rows: readonly TuiRow[],
|
|
922
|
+
row: TuiRow,
|
|
923
|
+
at: number,
|
|
924
|
+
): readonly ChildParent[] {
|
|
925
|
+
const seen = new Set<number>();
|
|
926
|
+
const out: ChildParent[] = [];
|
|
927
|
+
for (const id of row.after ?? []) {
|
|
928
|
+
const index = rows.findIndex((candidate, place) =>
|
|
929
|
+
place !== at && sameObject(candidate.id, id));
|
|
930
|
+
if (index < 0 || seen.has(index)) continue;
|
|
931
|
+
seen.add(index);
|
|
932
|
+
out.push({ index, id });
|
|
933
|
+
}
|
|
934
|
+
return out;
|
|
935
|
+
}
|
|
936
|
+
|
|
937
|
+
/** One row of the next level as the page draws it. */
|
|
938
|
+
export interface TuiChildEntry {
|
|
939
|
+
readonly row: TuiRow;
|
|
940
|
+
/** 0 in the flat list and for a root in the tree. */
|
|
941
|
+
readonly depth: number;
|
|
942
|
+
/** `├─`/`└─` and their prefix in the tree; empty in the flat list. */
|
|
943
|
+
readonly connector: string;
|
|
944
|
+
/** The other parents in `after`, printed as `also after <id>`. */
|
|
945
|
+
readonly alsoAfter: readonly string[];
|
|
946
|
+
}
|
|
947
|
+
|
|
948
|
+
/** Cancelled rows last, keeping every other row in its place. */
|
|
949
|
+
function cancelledLast(
|
|
950
|
+
indexes: readonly number[],
|
|
951
|
+
rows: readonly TuiRow[],
|
|
952
|
+
): number[] {
|
|
953
|
+
return [
|
|
954
|
+
...indexes.filter((index) => !cancelledRow(rows[index] as TuiRow)),
|
|
955
|
+
...indexes.filter((index) => cancelledRow(rows[index] as TuiRow)),
|
|
956
|
+
];
|
|
957
|
+
}
|
|
958
|
+
|
|
959
|
+
function flatChildEntries(rows: readonly TuiRow[]): TuiChildEntry[] {
|
|
960
|
+
const ordered = cancelledLast(rows.map((_, index) => index), rows);
|
|
961
|
+
return ordered.map((index) => {
|
|
962
|
+
const row = rows[index] as TuiRow;
|
|
963
|
+
return {
|
|
964
|
+
row,
|
|
965
|
+
depth: 0,
|
|
966
|
+
connector: "",
|
|
967
|
+
alsoAfter: childParents(rows, row, index).map((parent) => parent.id),
|
|
968
|
+
};
|
|
969
|
+
});
|
|
970
|
+
}
|
|
971
|
+
|
|
972
|
+
/** The parent the tree can use: a cancelled row hangs only under cancelled rows. */
|
|
973
|
+
function treeParent(
|
|
974
|
+
rows: readonly TuiRow[],
|
|
975
|
+
row: TuiRow,
|
|
976
|
+
at: number,
|
|
977
|
+
): ChildParent | undefined {
|
|
978
|
+
const cancelled = cancelledRow(row);
|
|
979
|
+
return childParents(rows, row, at).find(
|
|
980
|
+
(parent) => cancelledRow(rows[parent.index] as TuiRow) === cancelled,
|
|
981
|
+
);
|
|
982
|
+
}
|
|
983
|
+
|
|
984
|
+
/** A chain of tree parents that comes back to itself: the tree cannot place it. */
|
|
985
|
+
function hasChildCycle(rows: readonly TuiRow[]): boolean {
|
|
986
|
+
const parents = rows.map((row, index) => treeParent(rows, row, index)?.index);
|
|
987
|
+
for (let start = 0; start < rows.length; start += 1) {
|
|
988
|
+
const seen = new Set<number>();
|
|
989
|
+
let at: number | undefined = start;
|
|
990
|
+
while (at !== undefined) {
|
|
991
|
+
if (seen.has(at)) return true;
|
|
992
|
+
seen.add(at);
|
|
993
|
+
at = parents[at];
|
|
994
|
+
}
|
|
995
|
+
}
|
|
996
|
+
return false;
|
|
997
|
+
}
|
|
998
|
+
|
|
999
|
+
function treeChildEntries(rows: readonly TuiRow[]): TuiChildEntry[] {
|
|
1000
|
+
// A cycle has no root: the list order is the only order that shows every row.
|
|
1001
|
+
if (hasChildCycle(rows)) return flatChildEntries(rows);
|
|
1002
|
+
const parents = rows.map((row, index) => treeParent(rows, row, index));
|
|
1003
|
+
const children = new Map<number, number[]>();
|
|
1004
|
+
for (let index = 0; index < rows.length; index += 1) {
|
|
1005
|
+
const parent = parents[index]?.index ?? -1;
|
|
1006
|
+
const list = children.get(parent);
|
|
1007
|
+
if (list === undefined) children.set(parent, [index]);
|
|
1008
|
+
else list.push(index);
|
|
1009
|
+
}
|
|
1010
|
+
|
|
1011
|
+
const out: TuiChildEntry[] = [];
|
|
1012
|
+
const visited = new Set<number>();
|
|
1013
|
+
const walk = (index: number, prefix: string, root: boolean, last: boolean): void => {
|
|
1014
|
+
if (visited.has(index)) return;
|
|
1015
|
+
visited.add(index);
|
|
1016
|
+
const row = rows[index] as TuiRow;
|
|
1017
|
+
const all = childParents(rows, row, index);
|
|
1018
|
+
const parent = parents[index];
|
|
1019
|
+
out.push({
|
|
1020
|
+
row,
|
|
1021
|
+
depth: root ? 0 : Math.trunc(prefix.length / 3) + 1,
|
|
1022
|
+
connector: root ? "" : `${prefix}${last ? "└─" : "├─"}`,
|
|
1023
|
+
alsoAfter:
|
|
1024
|
+
parent === undefined
|
|
1025
|
+
? []
|
|
1026
|
+
: all.filter((one) => one.index !== parent.index).map((one) => one.id),
|
|
1027
|
+
});
|
|
1028
|
+
const childPrefix = root ? "" : `${prefix}${last ? " " : "│ "}`;
|
|
1029
|
+
const below = children.get(index) ?? [];
|
|
1030
|
+
below.forEach((child, at) => walk(child, childPrefix, false, at + 1 === below.length));
|
|
1031
|
+
};
|
|
1032
|
+
|
|
1033
|
+
const roots = children.get(-1) ?? [];
|
|
1034
|
+
// Cancelled rows are the last group, so an active child never hangs from one.
|
|
1035
|
+
const active = roots.filter((index) => !cancelledRow(rows[index] as TuiRow));
|
|
1036
|
+
const cancelled = roots.filter((index) => cancelledRow(rows[index] as TuiRow));
|
|
1037
|
+
active.forEach((index, at) => walk(index, "", true, at + 1 === active.length));
|
|
1038
|
+
cancelled.forEach((index, at) => walk(index, "", true, at + 1 === cancelled.length));
|
|
1039
|
+
return out;
|
|
1040
|
+
}
|
|
1041
|
+
|
|
1042
|
+
/** The next level in the order the page draws it, for the chosen layout. */
|
|
1043
|
+
export function displayChildren(
|
|
1044
|
+
rows: readonly TuiRow[],
|
|
1045
|
+
layout: TuiChildLayout,
|
|
1046
|
+
): readonly TuiChildEntry[] {
|
|
1047
|
+
if (rows.length === 0) return [];
|
|
1048
|
+
return layout === "list" ? flatChildEntries(rows) : treeChildEntries(rows);
|
|
1049
|
+
}
|
|
1050
|
+
|
|
1051
|
+
/** The next level's bar cells and counts: cancelled children last and not counted. */
|
|
1052
|
+
export interface TuiChildProgress {
|
|
1053
|
+
readonly entries: readonly TuiChildEntry[];
|
|
1054
|
+
readonly done: number;
|
|
1055
|
+
readonly total: number;
|
|
1056
|
+
}
|
|
1057
|
+
|
|
1058
|
+
export function childProgress(
|
|
1059
|
+
rows: readonly TuiRow[],
|
|
1060
|
+
layout: TuiChildLayout,
|
|
1061
|
+
): TuiChildProgress {
|
|
1062
|
+
const shown = displayChildren(rows, layout);
|
|
1063
|
+
const active = shown.filter((entry) => !cancelledRow(entry.row));
|
|
1064
|
+
const cancelled = shown.filter((entry) => cancelledRow(entry.row));
|
|
1065
|
+
return {
|
|
1066
|
+
entries: [...active, ...cancelled],
|
|
1067
|
+
done: active.filter((entry) => doneRow(entry.row)).length,
|
|
1068
|
+
total: active.length,
|
|
1069
|
+
};
|
|
1070
|
+
}
|
|
1071
|
+
|
|
1072
|
+
function clampCursor(list: TuiList): number {
|
|
1073
|
+
if (list.rows.length === 0) return 0;
|
|
1074
|
+
return Math.min(Math.max(0, list.cursor), list.rows.length - 1);
|
|
1075
|
+
}
|
|
1076
|
+
|
|
1077
|
+
function withTab(state: TuiState, tabId: string, patch: (tab: TuiTab) => TuiTab): TuiState {
|
|
1078
|
+
const index = state.tabs.findIndex((tab) => tab.id === tabId);
|
|
1079
|
+
if (index < 0) return state;
|
|
1080
|
+
const tabs = state.tabs.map((tab, at) => (at === index ? patch(tab) : tab));
|
|
1081
|
+
return { ...state, tabs };
|
|
1082
|
+
}
|
|
1083
|
+
|
|
1084
|
+
function nearEnd(tab: TuiTab): boolean {
|
|
1085
|
+
const { list } = tab;
|
|
1086
|
+
if (list.rows.length === 0) return false;
|
|
1087
|
+
return list.cursor >= Math.max(0, list.rows.length - PAGE_AHEAD);
|
|
1088
|
+
}
|
|
1089
|
+
|
|
1090
|
+
/** Sets one page request for a tab, and never a second while one is loading. */
|
|
1091
|
+
function requestPage(state: TuiState, tab: TuiTab): TuiState {
|
|
1092
|
+
const { list } = tab;
|
|
1093
|
+
if (!nearEnd(tab) || list.next === undefined || list.loading) return state;
|
|
1094
|
+
if (state.pageRequest?.tabId === tab.id && state.pageRequest.cursor === list.next) {
|
|
1095
|
+
return state;
|
|
1096
|
+
}
|
|
1097
|
+
const updated = withTab(state, tab.id, (current) => ({
|
|
1098
|
+
...current,
|
|
1099
|
+
list: { ...current.list, loading: true },
|
|
1100
|
+
}));
|
|
1101
|
+
return {
|
|
1102
|
+
...updated,
|
|
1103
|
+
pageRequest: { tabId: tab.id, cursor: list.next, version: list.version },
|
|
1104
|
+
};
|
|
1105
|
+
}
|
|
1106
|
+
|
|
1107
|
+
function activeTabOf(state: TuiState): TuiTab | undefined {
|
|
1108
|
+
return state.tabs[state.active];
|
|
1109
|
+
}
|
|
1110
|
+
|
|
1111
|
+
/**
|
|
1112
|
+
* The rows a section's Markdown takes once wrapped to the pane, without its
|
|
1113
|
+
* trailing blank. `width` defaults to no wrapping for callers that only want
|
|
1114
|
+
* the renderer's own line count.
|
|
1115
|
+
*/
|
|
1116
|
+
export function markdownLineCount(
|
|
1117
|
+
markdown: string,
|
|
1118
|
+
width = Number.POSITIVE_INFINITY,
|
|
1119
|
+
): number {
|
|
1120
|
+
return layoutMarkdown(
|
|
1121
|
+
markdown,
|
|
1122
|
+
Number.isFinite(width) ? width : undefined,
|
|
1123
|
+
).length;
|
|
1124
|
+
}
|
|
1125
|
+
|
|
1126
|
+
/**
|
|
1127
|
+
* The rows of a section at the pane's width. They are laid out once for each
|
|
1128
|
+
* content version of each open object and width, and kept: scrolling and
|
|
1129
|
+
* drawing read the same rows without wrapping the text again.
|
|
1130
|
+
*/
|
|
1131
|
+
export function sectionLayout(
|
|
1132
|
+
state: TuiState,
|
|
1133
|
+
section: TuiSection,
|
|
1134
|
+
): readonly LayoutLine[] {
|
|
1135
|
+
const object = state.object;
|
|
1136
|
+
const key = `${object?.version ?? 0}\u0000${object?.id ?? ""}\u0000${section.id}`;
|
|
1137
|
+
return cachedLayout(
|
|
1138
|
+
key,
|
|
1139
|
+
section.version,
|
|
1140
|
+
section.markdown,
|
|
1141
|
+
sectionWidth(state),
|
|
1142
|
+
);
|
|
1143
|
+
}
|
|
1144
|
+
|
|
1145
|
+
/**
|
|
1146
|
+
* The rows of a section's changing tail, laid out as Markdown at the pane's
|
|
1147
|
+
* width. The view draws them weak and after the content.
|
|
1148
|
+
*/
|
|
1149
|
+
export function sectionTailLayout(
|
|
1150
|
+
state: TuiState,
|
|
1151
|
+
section: TuiSection,
|
|
1152
|
+
): readonly LayoutLine[] {
|
|
1153
|
+
if (section.tail === undefined || section.tail === "") return [];
|
|
1154
|
+
const object = state.object;
|
|
1155
|
+
const key = `${object?.version ?? 0}\u0000${object?.id ?? ""}\u0000${section.id}\u0000tail`;
|
|
1156
|
+
return cachedLayout(key, section.version, section.tail, sectionWidth(state));
|
|
1157
|
+
}
|
|
1158
|
+
|
|
1159
|
+
/** The rows a section takes on screen: its content, then its changing tail. */
|
|
1160
|
+
export function sectionLineCount(state: TuiState, section: TuiSection): number {
|
|
1161
|
+
return sectionLayout(state, section).length + sectionTailLayout(state, section).length;
|
|
1162
|
+
}
|
|
1163
|
+
|
|
1164
|
+
/** The layout's fixed columns (0-based): the list, its divider, the object pane. */
|
|
1165
|
+
export const LIST_X = 1;
|
|
1166
|
+
/** The list takes about 26% of the terminal, between these two widths, on every tab. */
|
|
1167
|
+
export const LIST_MIN_WIDTH = 24;
|
|
1168
|
+
export const LIST_MAX_WIDTH = 38;
|
|
1169
|
+
export const LIST_SHARE = 0.26;
|
|
1170
|
+
/** The object pane needs at least this many columns for the two panes to show. */
|
|
1171
|
+
export const MIN_OBJECT_WIDTH = 55;
|
|
1172
|
+
/** The lines above the first row of a list: a blank line and the dim title. */
|
|
1173
|
+
export const LIST_HEAD = 2;
|
|
1174
|
+
|
|
1175
|
+
/** The columns of the list's text for a terminal `width` wide. */
|
|
1176
|
+
export function listWidthFor(width: number): number {
|
|
1177
|
+
return Math.min(LIST_MAX_WIDTH, Math.max(LIST_MIN_WIDTH, Math.floor(width * LIST_SHARE)));
|
|
1178
|
+
}
|
|
1179
|
+
|
|
1180
|
+
/** The columns before a row's text: marker, glyph and their spaces. */
|
|
1181
|
+
export const ROW_INDENT = 4;
|
|
1182
|
+
|
|
1183
|
+
/** The row id without its type: `item:T-1-x` is `T-1-x`. */
|
|
1184
|
+
export function bareId(id: string): string {
|
|
1185
|
+
const at = id.indexOf(":");
|
|
1186
|
+
return at < 0 ? id : id.slice(at + 1);
|
|
1187
|
+
}
|
|
1188
|
+
|
|
1189
|
+
/** The short id and the slug of an id like `T-0031-tui-object`. */
|
|
1190
|
+
function splitId(id: string): { readonly short: string; readonly slug: string } {
|
|
1191
|
+
const match = /^([A-Za-z]+-\d+)-(.+)$/.exec(id);
|
|
1192
|
+
if (match === null) return { short: id, slug: "" };
|
|
1193
|
+
return { short: match[1] ?? id, slug: match[2] ?? "" };
|
|
1194
|
+
}
|
|
1195
|
+
|
|
1196
|
+
/** The short id and slug of a row, without its type or repository. */
|
|
1197
|
+
export function rowName(row: TuiRow): string {
|
|
1198
|
+
if (row.unnamed === true && row.title !== "") return sanitize(row.title);
|
|
1199
|
+
const bare = bareId(row.id);
|
|
1200
|
+
const { short, slug } = splitId(bare.slice(bare.lastIndexOf("/") + 1));
|
|
1201
|
+
if (row.error !== undefined || slug === "") return short;
|
|
1202
|
+
return `${short} ${slug}`;
|
|
1203
|
+
}
|
|
1204
|
+
|
|
1205
|
+
/** The parts of a row's dim line: state and badge, then the note, unless the row says. */
|
|
1206
|
+
export function rowDetail(row: TuiRow): string {
|
|
1207
|
+
const parts =
|
|
1208
|
+
row.detail ?? [`${row.state}${row.badge === undefined ? "" : ` ${row.badge}`}`, row.note ?? ""];
|
|
1209
|
+
return parts
|
|
1210
|
+
.map((part) => sanitize(part).replace(/\n/g, " ").trim())
|
|
1211
|
+
.filter((part) => part !== "")
|
|
1212
|
+
.join(" · ");
|
|
1213
|
+
}
|
|
1214
|
+
|
|
1215
|
+
/**
|
|
1216
|
+
* The columns of the list's text. Every tab uses one rule, so the divider stays
|
|
1217
|
+
* in the same column when the person switches tabs at the same terminal width.
|
|
1218
|
+
*/
|
|
1219
|
+
export function listWidth(state: TuiState): number {
|
|
1220
|
+
return listWidthFor(state.width);
|
|
1221
|
+
}
|
|
1222
|
+
|
|
1223
|
+
/** The divider's column: the list, then one column of padding. */
|
|
1224
|
+
export function dividerX(state: TuiState): number {
|
|
1225
|
+
return LIST_X + listWidth(state) + 1;
|
|
1226
|
+
}
|
|
1227
|
+
|
|
1228
|
+
/** The object pane's first column: one column of padding after the divider. */
|
|
1229
|
+
export function rightX(state: TuiState): number {
|
|
1230
|
+
return dividerX(state) + 2;
|
|
1231
|
+
}
|
|
1232
|
+
|
|
1233
|
+
/** The lines the body has: the header, its rule, the footer rule and the footer take four. */
|
|
1234
|
+
export function bodyHeight(state: TuiState): number {
|
|
1235
|
+
return Math.max(1, state.height - 4);
|
|
1236
|
+
}
|
|
1237
|
+
|
|
1238
|
+
/** The lines the page's framed header, next level and tabs take before its section. */
|
|
1239
|
+
export function objectHeaderLines(state: TuiState): number {
|
|
1240
|
+
const object = state.object;
|
|
1241
|
+
if (object === undefined) return 0;
|
|
1242
|
+
const width = Math.max(1, sectionWidth(state) - 2);
|
|
1243
|
+
const title = object.row.title === "" ? 0 : wrapText(object.row.title, width).length;
|
|
1244
|
+
const children = childLines(object);
|
|
1245
|
+
const status =
|
|
1246
|
+
object.error !== undefined ||
|
|
1247
|
+
object.row.error !== undefined ||
|
|
1248
|
+
object.unavailable !== undefined ||
|
|
1249
|
+
pagePosition(activeSection(state)) !== undefined
|
|
1250
|
+
? 1
|
|
1251
|
+
: 0;
|
|
1252
|
+
// top border, a blank rail, the title, a status line, bar/heading/rows, bottom,
|
|
1253
|
+
// a blank line, tabs and rule.
|
|
1254
|
+
return 1 + 1 + title + status + children + 1 + 1 + 2;
|
|
1255
|
+
}
|
|
1256
|
+
|
|
1257
|
+
/** How many Markdown lines fit under the object's frame and tabs. */
|
|
1258
|
+
export function sectionViewport(state: TuiState): number {
|
|
1259
|
+
// The hint line stays under the content.
|
|
1260
|
+
return Math.max(1, bodyHeight(state) - objectHeaderLines(state) - 1);
|
|
1261
|
+
}
|
|
1262
|
+
|
|
1263
|
+
/** The columns a section's Markdown has in the object pane. */
|
|
1264
|
+
export function sectionWidth(state: TuiState): number {
|
|
1265
|
+
const start = twoPane(state) ? rightX(state) : LIST_X;
|
|
1266
|
+
return Math.max(1, state.width - start - 1);
|
|
1267
|
+
}
|
|
1268
|
+
|
|
1269
|
+
/** The section the object pane shows, if any. */
|
|
1270
|
+
export function activeSection(state: TuiState): TuiSection | undefined {
|
|
1271
|
+
const object = state.object;
|
|
1272
|
+
return object === undefined ? undefined : object.sections[object.active];
|
|
1273
|
+
}
|
|
1274
|
+
|
|
1275
|
+
function makeSection(info: ViewSectionInfo, previous?: TuiSection): TuiSection {
|
|
1276
|
+
const live = info.live === true;
|
|
1277
|
+
if (previous !== undefined && previous.id === info.id) {
|
|
1278
|
+
return { ...previous, title: info.title, live };
|
|
1279
|
+
}
|
|
1280
|
+
return {
|
|
1281
|
+
id: info.id,
|
|
1282
|
+
title: info.title,
|
|
1283
|
+
live,
|
|
1284
|
+
markdown: "",
|
|
1285
|
+
loaded: false,
|
|
1286
|
+
loading: false,
|
|
1287
|
+
full: false,
|
|
1288
|
+
capped: false,
|
|
1289
|
+
pages: 0,
|
|
1290
|
+
pageStart: 0,
|
|
1291
|
+
pageMarks: [],
|
|
1292
|
+
scroll: 0,
|
|
1293
|
+
following: live,
|
|
1294
|
+
version: 0,
|
|
1295
|
+
};
|
|
1296
|
+
}
|
|
1297
|
+
|
|
1298
|
+
function withObject(state: TuiState, patch: (object: TuiObject) => TuiObject): TuiState {
|
|
1299
|
+
return state.object === undefined ? state : { ...state, object: patch(state.object) };
|
|
1300
|
+
}
|
|
1301
|
+
|
|
1302
|
+
/** Rebuilds one section, keeping all the others as they are. */
|
|
1303
|
+
function withSection(
|
|
1304
|
+
state: TuiState,
|
|
1305
|
+
sectionId: string,
|
|
1306
|
+
patch: (section: TuiSection) => TuiSection,
|
|
1307
|
+
): TuiState {
|
|
1308
|
+
return withObject(state, (object) => ({
|
|
1309
|
+
...object,
|
|
1310
|
+
sections: object.sections.map((section) =>
|
|
1311
|
+
section.id === sectionId ? patch(section) : section,
|
|
1312
|
+
),
|
|
1313
|
+
}));
|
|
1314
|
+
}
|
|
1315
|
+
|
|
1316
|
+
function bottomScroll(state: TuiState, section: TuiSection): number {
|
|
1317
|
+
return Math.max(0, sectionLineCount(state, section) - sectionViewport(state));
|
|
1318
|
+
}
|
|
1319
|
+
|
|
1320
|
+
/** Asks for a section's first page or its next one, once. */
|
|
1321
|
+
function requestSection(
|
|
1322
|
+
state: TuiState,
|
|
1323
|
+
index: number,
|
|
1324
|
+
options: {
|
|
1325
|
+
cursor?: string | undefined;
|
|
1326
|
+
jump?: number | undefined;
|
|
1327
|
+
full?: boolean | undefined;
|
|
1328
|
+
force?: boolean | undefined;
|
|
1329
|
+
} = {},
|
|
1330
|
+
): TuiState {
|
|
1331
|
+
const object = state.object;
|
|
1332
|
+
const section = object?.sections[index];
|
|
1333
|
+
if (object === undefined || section === undefined) return state;
|
|
1334
|
+
const cursor = options.cursor;
|
|
1335
|
+
const full = options.full === true;
|
|
1336
|
+
if (
|
|
1337
|
+
options.force !== true &&
|
|
1338
|
+
cursor === undefined &&
|
|
1339
|
+
!full &&
|
|
1340
|
+
section.loaded
|
|
1341
|
+
) {
|
|
1342
|
+
return state;
|
|
1343
|
+
}
|
|
1344
|
+
const current = state.sectionRequest;
|
|
1345
|
+
if (
|
|
1346
|
+
current !== undefined &&
|
|
1347
|
+
current.objectId === object.id &&
|
|
1348
|
+
current.sectionId === section.id &&
|
|
1349
|
+
current.cursor === cursor &&
|
|
1350
|
+
current.jump === options.jump &&
|
|
1351
|
+
current.full === full &&
|
|
1352
|
+
current.version === section.version &&
|
|
1353
|
+
current.objectVersion === object.version
|
|
1354
|
+
) {
|
|
1355
|
+
return state;
|
|
1356
|
+
}
|
|
1357
|
+
const updated = withSection(state, section.id, (value) => ({
|
|
1358
|
+
...value,
|
|
1359
|
+
loading: true,
|
|
1360
|
+
error: undefined,
|
|
1361
|
+
}));
|
|
1362
|
+
return {
|
|
1363
|
+
...updated,
|
|
1364
|
+
sectionRequest: {
|
|
1365
|
+
objectId: object.id,
|
|
1366
|
+
sectionId: section.id,
|
|
1367
|
+
...(cursor === undefined ? {} : { cursor }),
|
|
1368
|
+
...(options.jump === undefined ? {} : { jump: options.jump }),
|
|
1369
|
+
full,
|
|
1370
|
+
version: section.version,
|
|
1371
|
+
objectVersion: object.version,
|
|
1372
|
+
},
|
|
1373
|
+
};
|
|
1374
|
+
}
|
|
1375
|
+
|
|
1376
|
+
/** Selects a section tab and loads it if it has no content yet. */
|
|
1377
|
+
function selectSection(state: TuiState, index: number): TuiState {
|
|
1378
|
+
const object = state.object;
|
|
1379
|
+
if (object === undefined || index < 0 || index >= object.sections.length) {
|
|
1380
|
+
return state;
|
|
1381
|
+
}
|
|
1382
|
+
if (index === object.active) return state;
|
|
1383
|
+
const moved = withObject(state, (value) => ({ ...value, active: index }));
|
|
1384
|
+
const section = object.sections[index];
|
|
1385
|
+
if (section === undefined || section.loaded) return moved;
|
|
1386
|
+
return requestSection(moved, index);
|
|
1387
|
+
}
|
|
1388
|
+
|
|
1389
|
+
function moveSection(state: TuiState, delta: number): TuiState {
|
|
1390
|
+
const object = state.object;
|
|
1391
|
+
if (object === undefined || object.sections.length === 0) return state;
|
|
1392
|
+
const index = (object.active + delta + object.sections.length) % object.sections.length;
|
|
1393
|
+
return selectSection(state, index);
|
|
1394
|
+
}
|
|
1395
|
+
|
|
1396
|
+
/** Opens the row under the list cursor. */
|
|
1397
|
+
function openObject(state: TuiState): TuiState {
|
|
1398
|
+
const tab = activeTabOf(state);
|
|
1399
|
+
const row = tab?.list.rows[tab.list.cursor];
|
|
1400
|
+
if (row === undefined || row.openable === false) return state;
|
|
1401
|
+
const previous = state.object?.source === row.id ? state.object : undefined;
|
|
1402
|
+
const version = state.objectVersion + 1;
|
|
1403
|
+
// A row of a plugin tab knows its type, so the request names it: a bare id
|
|
1404
|
+
// can be ambiguous across types.
|
|
1405
|
+
const typeName = row.type;
|
|
1406
|
+
const requestId =
|
|
1407
|
+
typeName !== undefined && !row.id.startsWith(`${typeName}:`)
|
|
1408
|
+
? `${typeName}:${row.id}`
|
|
1409
|
+
: row.id;
|
|
1410
|
+
const object: TuiObject = {
|
|
1411
|
+
id: requestId,
|
|
1412
|
+
source: row.id,
|
|
1413
|
+
type:
|
|
1414
|
+
previous?.type ??
|
|
1415
|
+
typeName ??
|
|
1416
|
+
(row.id.includes(":") ? row.id.slice(0, row.id.indexOf(":")) : row.id),
|
|
1417
|
+
link: previous?.link ?? "",
|
|
1418
|
+
row,
|
|
1419
|
+
// A request for the sections died with the old open, so none is loading.
|
|
1420
|
+
sections: (previous?.sections ?? []).map((section) => ({
|
|
1421
|
+
...section,
|
|
1422
|
+
loading: false,
|
|
1423
|
+
})),
|
|
1424
|
+
active: previous?.active ?? 0,
|
|
1425
|
+
actions: previous?.actions ?? [],
|
|
1426
|
+
loading: true,
|
|
1427
|
+
version,
|
|
1428
|
+
};
|
|
1429
|
+
return {
|
|
1430
|
+
...state,
|
|
1431
|
+
object,
|
|
1432
|
+
objectVersion: version,
|
|
1433
|
+
objectRequest: { id: requestId, version },
|
|
1434
|
+
sectionRequest: undefined,
|
|
1435
|
+
};
|
|
1436
|
+
}
|
|
1437
|
+
|
|
1438
|
+
/** Opens the page of an object an action created, named "<type>:<id>". */
|
|
1439
|
+
function openNamed(state: TuiState, id: string): TuiState {
|
|
1440
|
+
const colon = id.indexOf(":");
|
|
1441
|
+
if (colon <= 0 || colon === id.length - 1) return state;
|
|
1442
|
+
const type = id.slice(0, colon);
|
|
1443
|
+
const version = state.objectVersion + 1;
|
|
1444
|
+
const object: TuiObject = {
|
|
1445
|
+
id,
|
|
1446
|
+
source: id,
|
|
1447
|
+
type,
|
|
1448
|
+
link: "",
|
|
1449
|
+
// A placeholder until the page answers; the page's own row replaces it.
|
|
1450
|
+
row: { id, title: id, state: "", waiting: false, running: false, type },
|
|
1451
|
+
sections: [],
|
|
1452
|
+
active: 0,
|
|
1453
|
+
actions: [],
|
|
1454
|
+
loading: true,
|
|
1455
|
+
version,
|
|
1456
|
+
};
|
|
1457
|
+
return {
|
|
1458
|
+
...state,
|
|
1459
|
+
object,
|
|
1460
|
+
objectVersion: version,
|
|
1461
|
+
objectRequest: { id, version },
|
|
1462
|
+
sectionRequest: undefined,
|
|
1463
|
+
followPending: false,
|
|
1464
|
+
};
|
|
1465
|
+
}
|
|
1466
|
+
|
|
1467
|
+
function closeObject(state: TuiState): TuiState {
|
|
1468
|
+
if (state.object === undefined) return state;
|
|
1469
|
+
return {
|
|
1470
|
+
...state,
|
|
1471
|
+
object: undefined,
|
|
1472
|
+
objectRequest: undefined,
|
|
1473
|
+
sectionRequest: undefined,
|
|
1474
|
+
};
|
|
1475
|
+
}
|
|
1476
|
+
|
|
1477
|
+
function applyObject(
|
|
1478
|
+
state: TuiState,
|
|
1479
|
+
view: ViewObject,
|
|
1480
|
+
version: number | undefined,
|
|
1481
|
+
): TuiState {
|
|
1482
|
+
const previous = state.object;
|
|
1483
|
+
// A pane that was closed stays closed: a late answer never reopens it.
|
|
1484
|
+
if (previous === undefined) return state;
|
|
1485
|
+
if (
|
|
1486
|
+
version !== undefined &&
|
|
1487
|
+
version !== previous.version
|
|
1488
|
+
) {
|
|
1489
|
+
return state;
|
|
1490
|
+
}
|
|
1491
|
+
const row = rowFromView(view.row);
|
|
1492
|
+
const oldSections = previous?.sections ?? [];
|
|
1493
|
+
const sections = view.sections.map((info) =>
|
|
1494
|
+
makeSection(
|
|
1495
|
+
{ ...info, title: sanitize(info.title) },
|
|
1496
|
+
oldSections.find((section) => section.id === info.id),
|
|
1497
|
+
),
|
|
1498
|
+
);
|
|
1499
|
+
const oldActive = previous?.sections[previous.active]?.id;
|
|
1500
|
+
const found =
|
|
1501
|
+
oldActive === undefined
|
|
1502
|
+
? -1
|
|
1503
|
+
: sections.findIndex((section) => section.id === oldActive);
|
|
1504
|
+
const active =
|
|
1505
|
+
found >= 0
|
|
1506
|
+
? found
|
|
1507
|
+
: Math.min(previous?.active ?? 0, Math.max(0, sections.length - 1));
|
|
1508
|
+
const object: TuiObject = {
|
|
1509
|
+
id: view.id,
|
|
1510
|
+
source: previous.source,
|
|
1511
|
+
type: view.type,
|
|
1512
|
+
link: view.link,
|
|
1513
|
+
row,
|
|
1514
|
+
sections,
|
|
1515
|
+
active,
|
|
1516
|
+
actions: view.actions,
|
|
1517
|
+
...(previous.children === undefined ? {} : { children: previous.children }),
|
|
1518
|
+
...(previous.back === undefined ? {} : { back: previous.back }),
|
|
1519
|
+
loading: false,
|
|
1520
|
+
...(view.unavailable === undefined
|
|
1521
|
+
? {}
|
|
1522
|
+
: { unavailable: sanitize(view.unavailable) }),
|
|
1523
|
+
version: previous?.version ?? version ?? 0,
|
|
1524
|
+
};
|
|
1525
|
+
let updated: TuiState = {
|
|
1526
|
+
...state,
|
|
1527
|
+
object,
|
|
1528
|
+
objectRequest:
|
|
1529
|
+
state.objectRequest !== undefined &&
|
|
1530
|
+
(state.objectRequest.version === version ||
|
|
1531
|
+
state.objectRequest.version === previous?.version)
|
|
1532
|
+
? undefined
|
|
1533
|
+
: state.objectRequest,
|
|
1534
|
+
sectionRequest: undefined,
|
|
1535
|
+
};
|
|
1536
|
+
const section = sections[active];
|
|
1537
|
+
if (section !== undefined && !section.loaded) {
|
|
1538
|
+
updated = requestSection(updated, active);
|
|
1539
|
+
}
|
|
1540
|
+
const creating = updated.creating;
|
|
1541
|
+
if (creating !== undefined && previous.id === creating.parent) {
|
|
1542
|
+
updated = { ...updated, creating: undefined };
|
|
1543
|
+
const make = view.actions.find((action) => action.creates === creating.type);
|
|
1544
|
+
return make === undefined
|
|
1545
|
+
? withNotice(updated, `${creating.parent} has no way to create a ${creating.type}`)
|
|
1546
|
+
: startAction(updated, make.name);
|
|
1547
|
+
}
|
|
1548
|
+
return updated;
|
|
1549
|
+
}
|
|
1550
|
+
|
|
1551
|
+
/** `c` on a tab of one object type: the palette, listing where a new one can be made. */
|
|
1552
|
+
function createHere(state: TuiState): TuiState {
|
|
1553
|
+
const info = state.tabs[state.active]?.types?.find((one) => one.parents !== undefined);
|
|
1554
|
+
if (info === undefined) return withNotice(state, "nothing is made from this tab");
|
|
1555
|
+
return {
|
|
1556
|
+
...state,
|
|
1557
|
+
input: { kind: "palette", query: `new ${info.type}`, cursor: 0 },
|
|
1558
|
+
settingsRequest: true,
|
|
1559
|
+
};
|
|
1560
|
+
}
|
|
1561
|
+
|
|
1562
|
+
function applyObjectError(
|
|
1563
|
+
state: TuiState,
|
|
1564
|
+
id: string,
|
|
1565
|
+
message: string,
|
|
1566
|
+
version: number | undefined,
|
|
1567
|
+
): TuiState {
|
|
1568
|
+
const object = state.object;
|
|
1569
|
+
if (object === undefined || object.id !== id) return state;
|
|
1570
|
+
if (version !== undefined && version !== object.version) return state;
|
|
1571
|
+
return {
|
|
1572
|
+
...state,
|
|
1573
|
+
object: { ...object, loading: false, error: sanitize(message) },
|
|
1574
|
+
objectRequest: undefined,
|
|
1575
|
+
sectionRequest: undefined,
|
|
1576
|
+
};
|
|
1577
|
+
}
|
|
1578
|
+
|
|
1579
|
+
/**
|
|
1580
|
+
* The line the server puts last when it cuts a section at its size cap
|
|
1581
|
+
* (`operad show <id> --section <section> --full`). Only that exact line, as the
|
|
1582
|
+
* last line, counts: the same words inside the text do not.
|
|
1583
|
+
*/
|
|
1584
|
+
const TRUNCATION_LINE =
|
|
1585
|
+
/^\[Truncated\. See all of it with `operad show .+ --section .+ --full`\.\]$/;
|
|
1586
|
+
|
|
1587
|
+
/** True when the text ends with the server's truncation line. */
|
|
1588
|
+
export function isTruncated(markdown: string): boolean {
|
|
1589
|
+
const lines = markdown.split("\n");
|
|
1590
|
+
while (lines.length > 0 && (lines[lines.length - 1] ?? "").trim() === "") lines.pop();
|
|
1591
|
+
return TRUNCATION_LINE.test(lines[lines.length - 1] ?? "");
|
|
1592
|
+
}
|
|
1593
|
+
|
|
1594
|
+
/** The offset in `joinMarkdown(before, after)` where `after` begins. */
|
|
1595
|
+
export function markPage(before: string, after: string): number {
|
|
1596
|
+
return joinMarkdown(before, after).length - after.length;
|
|
1597
|
+
}
|
|
1598
|
+
|
|
1599
|
+
/** Joins a page after the text before it. */
|
|
1600
|
+
export function joinMarkdown(before: string, after: string): string {
|
|
1601
|
+
if (before.length === 0) return after;
|
|
1602
|
+
if (after.length === 0) return before;
|
|
1603
|
+
return `${before.replace(/\n?$/, "\n")}${after}`;
|
|
1604
|
+
}
|
|
1605
|
+
|
|
1606
|
+
function applySection(
|
|
1607
|
+
state: TuiState,
|
|
1608
|
+
objectId: string,
|
|
1609
|
+
sectionId: string,
|
|
1610
|
+
content: ViewSectionContent,
|
|
1611
|
+
cursor: string | undefined,
|
|
1612
|
+
full: boolean,
|
|
1613
|
+
version: number | undefined,
|
|
1614
|
+
objectVersion: number | undefined,
|
|
1615
|
+
pages: number | undefined,
|
|
1616
|
+
jump: number | undefined,
|
|
1617
|
+
marks: readonly number[] | undefined,
|
|
1618
|
+
start: number | undefined,
|
|
1619
|
+
after: boolean,
|
|
1620
|
+
): TuiState {
|
|
1621
|
+
const object = state.object;
|
|
1622
|
+
if (object === undefined || object.id !== objectId) return state;
|
|
1623
|
+
const section = object.sections.find((candidate) => candidate.id === sectionId);
|
|
1624
|
+
if (section === undefined) return state;
|
|
1625
|
+
if (version !== undefined && version !== section.version) return state;
|
|
1626
|
+
// A page asked for before the object was closed and opened again.
|
|
1627
|
+
if (objectVersion !== undefined && objectVersion !== object.version) return state;
|
|
1628
|
+
// A page read appends one page; a read after the mark appends what follows it
|
|
1629
|
+
// only when the plugin says its answer is the delta. Anything else replaces.
|
|
1630
|
+
const pageAppend = cursor !== undefined && jump === undefined;
|
|
1631
|
+
const afterAppend = after && content.delta === true;
|
|
1632
|
+
const appended = pageAppend || afterAppend;
|
|
1633
|
+
const markdown = appended
|
|
1634
|
+
? joinMarkdown(section.markdown, content.markdown)
|
|
1635
|
+
: content.markdown;
|
|
1636
|
+
const capped = !full && isTruncated(markdown);
|
|
1637
|
+
const updated = withSection(state, sectionId, (value) => {
|
|
1638
|
+
const next: TuiSection = {
|
|
1639
|
+
...value,
|
|
1640
|
+
markdown,
|
|
1641
|
+
next: content.next,
|
|
1642
|
+
loaded: true,
|
|
1643
|
+
loading: false,
|
|
1644
|
+
full: full || value.full,
|
|
1645
|
+
capped,
|
|
1646
|
+
pages: pageAppend ? value.pages + 1 : appended ? value.pages : (pages ?? 1),
|
|
1647
|
+
pageStart: jump ?? (pageAppend ? value.pageStart : (start ?? 0)),
|
|
1648
|
+
pageMarks: pageAppend
|
|
1649
|
+
? [...value.pageMarks, markPage(section.markdown, content.markdown)]
|
|
1650
|
+
: appended
|
|
1651
|
+
? value.pageMarks
|
|
1652
|
+
: (marks ?? []),
|
|
1653
|
+
// The answer's mark is the section's position now; a read that gives none
|
|
1654
|
+
// makes the next refresh read the section whole, as before.
|
|
1655
|
+
mark: content.mark,
|
|
1656
|
+
// The answer's tail is the whole current tail; a page read that leaves it
|
|
1657
|
+
// out keeps the one shown.
|
|
1658
|
+
tail: pageAppend && content.tail === undefined ? value.tail : content.tail,
|
|
1659
|
+
...(content.pages === undefined ? {} : { toc: content.pages }),
|
|
1660
|
+
error: undefined,
|
|
1661
|
+
version: value.version + 1,
|
|
1662
|
+
};
|
|
1663
|
+
const bottom = bottomScroll(state, next);
|
|
1664
|
+
if (jump !== undefined) return { ...next, scroll: 0, following: false };
|
|
1665
|
+
return {
|
|
1666
|
+
...next,
|
|
1667
|
+
scroll: next.following ? bottom : Math.min(value.scroll, bottom),
|
|
1668
|
+
};
|
|
1669
|
+
});
|
|
1670
|
+
return {
|
|
1671
|
+
...updated,
|
|
1672
|
+
sectionRequest:
|
|
1673
|
+
state.sectionRequest?.sectionId === sectionId ? undefined : state.sectionRequest,
|
|
1674
|
+
};
|
|
1675
|
+
}
|
|
1676
|
+
|
|
1677
|
+
function applySectionError(
|
|
1678
|
+
state: TuiState,
|
|
1679
|
+
objectId: string,
|
|
1680
|
+
sectionId: string,
|
|
1681
|
+
message: string,
|
|
1682
|
+
version: number | undefined,
|
|
1683
|
+
objectVersion: number | undefined,
|
|
1684
|
+
): TuiState {
|
|
1685
|
+
const object = state.object;
|
|
1686
|
+
const section = object?.sections.find((candidate) => candidate.id === sectionId);
|
|
1687
|
+
if (object === undefined || object.id !== objectId || section === undefined) {
|
|
1688
|
+
return state;
|
|
1689
|
+
}
|
|
1690
|
+
if (version !== undefined && version !== section.version) return state;
|
|
1691
|
+
if (objectVersion !== undefined && objectVersion !== object.version) return state;
|
|
1692
|
+
const updated = withSection(state, sectionId, (value) => ({
|
|
1693
|
+
...value,
|
|
1694
|
+
loading: false,
|
|
1695
|
+
error: sanitize(message),
|
|
1696
|
+
}));
|
|
1697
|
+
return {
|
|
1698
|
+
...updated,
|
|
1699
|
+
sectionRequest:
|
|
1700
|
+
state.sectionRequest?.sectionId === sectionId ? undefined : state.sectionRequest,
|
|
1701
|
+
};
|
|
1702
|
+
}
|
|
1703
|
+
|
|
1704
|
+
/** Scrolls the open section; a page can ask for the section's next page. */
|
|
1705
|
+
function scrollSection(state: TuiState, delta: number): TuiState {
|
|
1706
|
+
const object = state.object;
|
|
1707
|
+
const section = activeSection(state);
|
|
1708
|
+
if (object === undefined || section === undefined) return state;
|
|
1709
|
+
const bottom = bottomScroll(state, section);
|
|
1710
|
+
const start = section.following ? bottom : section.scroll;
|
|
1711
|
+
const next = Math.min(bottom, Math.max(0, start + delta));
|
|
1712
|
+
const following = delta < 0 ? false : section.live && next >= bottom;
|
|
1713
|
+
const moved = withSection(state, section.id, (value) => ({
|
|
1714
|
+
...value,
|
|
1715
|
+
scroll: next,
|
|
1716
|
+
following,
|
|
1717
|
+
}));
|
|
1718
|
+
if (
|
|
1719
|
+
delta > 0 &&
|
|
1720
|
+
next >= bottom &&
|
|
1721
|
+
section.next !== undefined &&
|
|
1722
|
+
!section.loading
|
|
1723
|
+
) {
|
|
1724
|
+
return requestSection(moved, object.active, { cursor: section.next });
|
|
1725
|
+
}
|
|
1726
|
+
return moved;
|
|
1727
|
+
}
|
|
1728
|
+
|
|
1729
|
+
/** The layout line each loaded page starts at, for the width in force. */
|
|
1730
|
+
let startsMemo: { key: string; starts: readonly number[] } | undefined;
|
|
1731
|
+
function pageStarts(state: TuiState, section: TuiSection): readonly number[] {
|
|
1732
|
+
const width = sectionWidth(state);
|
|
1733
|
+
const key = `${width}\u0000${section.id}\u0000${section.version}\u0000${section.markdown.length}`;
|
|
1734
|
+
if (startsMemo?.key === key) return startsMemo.starts;
|
|
1735
|
+
const starts = section.pageMarks.map(
|
|
1736
|
+
(offset) => layoutMarkdown(section.markdown.slice(0, offset), width).length,
|
|
1737
|
+
);
|
|
1738
|
+
startsMemo = { key, starts };
|
|
1739
|
+
return starts;
|
|
1740
|
+
}
|
|
1741
|
+
|
|
1742
|
+
/**
|
|
1743
|
+
* The page of the table of contents under the top of the pane. Without a state
|
|
1744
|
+
* it is the first page the text holds.
|
|
1745
|
+
*/
|
|
1746
|
+
export function shownPage(section: TuiSection, state?: TuiState): number {
|
|
1747
|
+
if (state === undefined) return section.pageStart;
|
|
1748
|
+
const passed = pageStarts(state, section).filter((line) => line <= section.scroll).length;
|
|
1749
|
+
return section.pageStart + passed;
|
|
1750
|
+
}
|
|
1751
|
+
|
|
1752
|
+
/** "page 3/27 · docs/manual.md" for a section with a table of contents. */
|
|
1753
|
+
export function pagePosition(
|
|
1754
|
+
section: TuiSection | undefined,
|
|
1755
|
+
state?: TuiState,
|
|
1756
|
+
): string | undefined {
|
|
1757
|
+
const toc = section?.toc;
|
|
1758
|
+
if (section === undefined || toc === undefined || toc.length === 0 || !section.loaded) {
|
|
1759
|
+
return undefined;
|
|
1760
|
+
}
|
|
1761
|
+
const at = Math.min(shownPage(section, state), toc.length - 1);
|
|
1762
|
+
return `page ${at + 1}/${toc.length} · ${toc[at]?.label ?? ""}`;
|
|
1763
|
+
}
|
|
1764
|
+
|
|
1765
|
+
/** Loads page `index` of the open section's table of contents and shows it at the top. */
|
|
1766
|
+
function loadPage(state: TuiState, index: number): TuiState {
|
|
1767
|
+
const object = state.object;
|
|
1768
|
+
const section = activeSection(state);
|
|
1769
|
+
const page = section?.toc?.[index];
|
|
1770
|
+
if (object === undefined || page === undefined) return state;
|
|
1771
|
+
return requestSection(state, object.active, {
|
|
1772
|
+
jump: index,
|
|
1773
|
+
force: true,
|
|
1774
|
+
...(page.cursor === undefined ? {} : { cursor: page.cursor }),
|
|
1775
|
+
});
|
|
1776
|
+
}
|
|
1777
|
+
|
|
1778
|
+
/** `n` and `N`: the next or previous page; the ends stay where they are. */
|
|
1779
|
+
function stepPage(state: TuiState, delta: number): TuiState {
|
|
1780
|
+
const section = activeSection(state);
|
|
1781
|
+
const toc = section?.toc;
|
|
1782
|
+
if (section === undefined || toc === undefined || toc.length === 0 || section.loading) {
|
|
1783
|
+
return state;
|
|
1784
|
+
}
|
|
1785
|
+
const target = shownPage(section, state) + delta;
|
|
1786
|
+
if (target < 0) return { ...state, notice: "first page" };
|
|
1787
|
+
if (target >= toc.length) return { ...state, notice: "last page" };
|
|
1788
|
+
return loadPage(state, target);
|
|
1789
|
+
}
|
|
1790
|
+
|
|
1791
|
+
/** `p`: the page picker, when the open section has pages; otherwise nothing. */
|
|
1792
|
+
function openPagePicker(state: TuiState): TuiState {
|
|
1793
|
+
const toc = activeSection(state)?.toc;
|
|
1794
|
+
if (toc === undefined || toc.length === 0 || state.keys) return state;
|
|
1795
|
+
const section = activeSection(state);
|
|
1796
|
+
const shown = section === undefined ? 0 : Math.min(shownPage(section, state), toc.length - 1);
|
|
1797
|
+
// With no query the entries are in order, so the page shown is at its index.
|
|
1798
|
+
return { ...state, input: { kind: "pages", query: "", cursor: shown } };
|
|
1799
|
+
}
|
|
1800
|
+
|
|
1801
|
+
/** One line of the page picker: a page of the table of contents. */
|
|
1802
|
+
export interface TuiPageEntry {
|
|
1803
|
+
readonly label: string;
|
|
1804
|
+
/** Its index in the table of contents. */
|
|
1805
|
+
readonly index: number;
|
|
1806
|
+
}
|
|
1807
|
+
|
|
1808
|
+
/** The picker's lines for the typed query, best match first. */
|
|
1809
|
+
export function pageEntries(state: TuiState): readonly TuiPageEntry[] {
|
|
1810
|
+
const toc = activeSection(state)?.toc ?? [];
|
|
1811
|
+
const query = state.input?.kind === "pages" ? state.input.query : "";
|
|
1812
|
+
return toc
|
|
1813
|
+
.map((page, index) => ({ label: page.label, index, score: fuzzyScore(query, page.label) }))
|
|
1814
|
+
.filter((item) => item.score >= 0)
|
|
1815
|
+
.sort((a, b) => a.score - b.score || a.index - b.index)
|
|
1816
|
+
.map(({ label, index }) => ({ label, index }));
|
|
1817
|
+
}
|
|
1818
|
+
|
|
1819
|
+
/** The theme list: moving shows the theme under the cursor, Enter keeps it, Esc puts the old one back. */
|
|
1820
|
+
function themesKey(
|
|
1821
|
+
state: TuiState,
|
|
1822
|
+
input: Extract<TuiInput, { readonly kind: "themes" }>,
|
|
1823
|
+
event: { key: string; ctrl?: boolean | undefined },
|
|
1824
|
+
): TuiState {
|
|
1825
|
+
const key = event.key.toLowerCase();
|
|
1826
|
+
if (key === "escape" || key === "esc") {
|
|
1827
|
+
return { ...state, input: undefined, themePreview: undefined };
|
|
1828
|
+
}
|
|
1829
|
+
const names = state.themeNames;
|
|
1830
|
+
const down = key === "down" || key === "arrowdown" || (event.ctrl === true && (key === "n" || key === "j"));
|
|
1831
|
+
const up = key === "up" || key === "arrowup" || (event.ctrl === true && (key === "p" || key === "k"));
|
|
1832
|
+
if (down || up) {
|
|
1833
|
+
const cursor = Math.min(Math.max(0, input.cursor + (down ? 1 : -1)), Math.max(0, names.length - 1));
|
|
1834
|
+
return { ...state, input: { ...input, cursor }, themePreview: names[cursor] };
|
|
1835
|
+
}
|
|
1836
|
+
if (key === "enter" || key === "return") {
|
|
1837
|
+
const chosen = names[input.cursor];
|
|
1838
|
+
if (chosen === undefined) return state;
|
|
1839
|
+
return {
|
|
1840
|
+
...state,
|
|
1841
|
+
input: undefined,
|
|
1842
|
+
themePreview: undefined,
|
|
1843
|
+
themeRequest: chosen,
|
|
1844
|
+
themeCurrent: chosen,
|
|
1845
|
+
};
|
|
1846
|
+
}
|
|
1847
|
+
return state;
|
|
1848
|
+
}
|
|
1849
|
+
|
|
1850
|
+
/** A key arrives while the page picker is open. */
|
|
1851
|
+
function pagesKey(
|
|
1852
|
+
state: TuiState,
|
|
1853
|
+
input: Extract<TuiInput, { readonly kind: "pages" }>,
|
|
1854
|
+
event: { key: string; text?: string | undefined; ctrl?: boolean | undefined; meta?: boolean | undefined },
|
|
1855
|
+
): TuiState {
|
|
1856
|
+
const key = event.key.toLowerCase();
|
|
1857
|
+
if (key === "escape" || key === "esc") return { ...state, input: undefined };
|
|
1858
|
+
const entries = pageEntries(state);
|
|
1859
|
+
const down = key === "down" || key === "arrowdown" || (event.ctrl === true && (key === "n" || key === "j"));
|
|
1860
|
+
const up = key === "up" || key === "arrowup" || (event.ctrl === true && (key === "p" || key === "k"));
|
|
1861
|
+
if (down || up) {
|
|
1862
|
+
const cursor = Math.min(
|
|
1863
|
+
Math.max(0, input.cursor + (down ? 1 : -1)),
|
|
1864
|
+
Math.max(0, entries.length - 1),
|
|
1865
|
+
);
|
|
1866
|
+
return { ...state, input: { ...input, cursor } };
|
|
1867
|
+
}
|
|
1868
|
+
if (key === "enter" || key === "return") {
|
|
1869
|
+
const chosen = entries[Math.min(input.cursor, entries.length - 1)];
|
|
1870
|
+
if (chosen === undefined) return state;
|
|
1871
|
+
return loadPage({ ...state, input: undefined }, chosen.index);
|
|
1872
|
+
}
|
|
1873
|
+
if (key === "backspace") {
|
|
1874
|
+
return { ...state, input: { ...input, query: dropLast(input.query), cursor: 0 } };
|
|
1875
|
+
}
|
|
1876
|
+
const text = event.ctrl === true || event.meta === true ? undefined : typed(event);
|
|
1877
|
+
return text === undefined
|
|
1878
|
+
? state
|
|
1879
|
+
: { ...state, input: { ...input, query: input.query + text, cursor: 0 } };
|
|
1880
|
+
}
|
|
1881
|
+
|
|
1882
|
+
function jumpSection(state: TuiState, to: "top" | "bottom"): TuiState {
|
|
1883
|
+
const object = state.object;
|
|
1884
|
+
const section = activeSection(state);
|
|
1885
|
+
if (object === undefined || section === undefined) return state;
|
|
1886
|
+
const bottom = bottomScroll(state, section);
|
|
1887
|
+
const moved = withSection(state, section.id, (value) => ({
|
|
1888
|
+
...value,
|
|
1889
|
+
scroll: to === "top" ? 0 : bottom,
|
|
1890
|
+
following: to === "bottom" && value.live,
|
|
1891
|
+
}));
|
|
1892
|
+
if (
|
|
1893
|
+
to === "bottom" &&
|
|
1894
|
+
section.next !== undefined &&
|
|
1895
|
+
!section.loading
|
|
1896
|
+
) {
|
|
1897
|
+
return requestSection(moved, object.active, { cursor: section.next });
|
|
1898
|
+
}
|
|
1899
|
+
return moved;
|
|
1900
|
+
}
|
|
1901
|
+
|
|
1902
|
+
|
|
1903
|
+
function move(state: TuiState, delta: number): TuiState {
|
|
1904
|
+
const tab = activeTabOf(state);
|
|
1905
|
+
if (tab === undefined || tab.list.rows.length === 0) return state;
|
|
1906
|
+
const next = clampCursor({
|
|
1907
|
+
...tab.list,
|
|
1908
|
+
cursor: tab.list.cursor + delta,
|
|
1909
|
+
});
|
|
1910
|
+
if (next === tab.list.cursor) {
|
|
1911
|
+
// At the last row a further Down still asks for the page the previous
|
|
1912
|
+
// failed attempt or the end of the loaded rows left unread.
|
|
1913
|
+
return delta > 0 ? requestPage(state, tab) : state;
|
|
1914
|
+
}
|
|
1915
|
+
const moved = moveCursor(state, tab.id, next);
|
|
1916
|
+
const updated = activeTabOf(moved);
|
|
1917
|
+
return updated === undefined ? moved : requestPage(moved, updated);
|
|
1918
|
+
}
|
|
1919
|
+
|
|
1920
|
+
/** Moves a tab's cursor; the object pane follows it after the shell's debounce. */
|
|
1921
|
+
function moveCursor(state: TuiState, tabId: string, cursor: number): TuiState {
|
|
1922
|
+
const moved = withTab(state, tabId, (current) => ({
|
|
1923
|
+
...current,
|
|
1924
|
+
list: { ...current.list, cursor },
|
|
1925
|
+
}));
|
|
1926
|
+
return { ...moved, moveSeq: state.moveSeq + 1, followPending: true };
|
|
1927
|
+
}
|
|
1928
|
+
|
|
1929
|
+
function jump(state: TuiState, to: "top" | "bottom"): TuiState {
|
|
1930
|
+
const tab = activeTabOf(state);
|
|
1931
|
+
if (tab === undefined) return state;
|
|
1932
|
+
const cursor = to === "top" ? 0 : Math.max(0, tab.list.rows.length - 1);
|
|
1933
|
+
if (cursor === tab.list.cursor) return state;
|
|
1934
|
+
const moved = moveCursor(state, tab.id, cursor);
|
|
1935
|
+
const updated = activeTabOf(moved);
|
|
1936
|
+
return updated === undefined ? moved : requestPage(moved, updated);
|
|
1937
|
+
}
|
|
1938
|
+
|
|
1939
|
+
function activate(state: TuiState, index: number): TuiState {
|
|
1940
|
+
if (index < 0 || index >= state.tabs.length || index === state.active) return state;
|
|
1941
|
+
// Another tab is another list: the object of the old one closes.
|
|
1942
|
+
return followLoaded(
|
|
1943
|
+
closeObject({ ...state, active: index, followPending: false }),
|
|
1944
|
+
state.tabs[index]?.id ?? "",
|
|
1945
|
+
);
|
|
1946
|
+
}
|
|
1947
|
+
|
|
1948
|
+
/** The row the active list's cursor is on, if it can be opened. */
|
|
1949
|
+
function selectedRow(state: TuiState): TuiRow | undefined {
|
|
1950
|
+
const tab = activeTabOf(state);
|
|
1951
|
+
const row = tab?.list.rows[tab.list.cursor];
|
|
1952
|
+
return row === undefined || row.openable === false ? undefined : row;
|
|
1953
|
+
}
|
|
1954
|
+
|
|
1955
|
+
/** How many rows of the next level the page shows at most. */
|
|
1956
|
+
export const CHILD_ROWS = 8;
|
|
1957
|
+
|
|
1958
|
+
/** The lines the next level takes in the page: the bar, its heading and its rows. */
|
|
1959
|
+
export function childLines(object: TuiObject | undefined): number {
|
|
1960
|
+
const children = object?.children;
|
|
1961
|
+
if (children === undefined || children.rows.length === 0) return 0;
|
|
1962
|
+
return Math.min(children.rows.length, CHILD_ROWS) + 2;
|
|
1963
|
+
}
|
|
1964
|
+
|
|
1965
|
+
function withChildren(state: TuiState, patch: (children: TuiChildren) => TuiChildren): TuiState {
|
|
1966
|
+
return withObject(state, (object) =>
|
|
1967
|
+
object.children === undefined ? object : { ...object, children: patch(object.children) },
|
|
1968
|
+
);
|
|
1969
|
+
}
|
|
1970
|
+
|
|
1971
|
+
/** Opens the page of the child under the page's cursor; the plan's page waits behind it. */
|
|
1972
|
+
function openChild(state: TuiState): TuiState {
|
|
1973
|
+
const parent = state.object;
|
|
1974
|
+
const children = parent?.children;
|
|
1975
|
+
const shown = children === undefined ? [] : displayChildren(children.rows, state.childLayout);
|
|
1976
|
+
const row = shown[children?.cursor ?? 0]?.row;
|
|
1977
|
+
if (parent === undefined || row === undefined) return state;
|
|
1978
|
+
const type = row.type ?? (row.id.includes(":") ? row.id.slice(0, row.id.indexOf(":")) : "");
|
|
1979
|
+
const requestId = type === "" || row.id.startsWith(`${type}:`) ? row.id : `${type}:${row.id}`;
|
|
1980
|
+
const version = state.objectVersion + 1;
|
|
1981
|
+
const object: TuiObject = {
|
|
1982
|
+
id: requestId,
|
|
1983
|
+
// The left row still owns the pane: a refresh of the list does not replace it.
|
|
1984
|
+
source: parent.source,
|
|
1985
|
+
type,
|
|
1986
|
+
link: "",
|
|
1987
|
+
row,
|
|
1988
|
+
sections: [],
|
|
1989
|
+
active: 0,
|
|
1990
|
+
actions: [],
|
|
1991
|
+
loading: true,
|
|
1992
|
+
version,
|
|
1993
|
+
back: parent,
|
|
1994
|
+
};
|
|
1995
|
+
return {
|
|
1996
|
+
...state,
|
|
1997
|
+
object,
|
|
1998
|
+
objectVersion: version,
|
|
1999
|
+
objectRequest: { id: requestId, version },
|
|
2000
|
+
sectionRequest: undefined,
|
|
2001
|
+
};
|
|
2002
|
+
}
|
|
2003
|
+
|
|
2004
|
+
/** Back from a child's page to the page it was opened from; false when it has none. */
|
|
2005
|
+
function goBack(state: TuiState): TuiState | undefined {
|
|
2006
|
+
const back = state.object?.back;
|
|
2007
|
+
if (back === undefined) return undefined;
|
|
2008
|
+
const version = state.objectVersion + 1;
|
|
2009
|
+
return {
|
|
2010
|
+
...state,
|
|
2011
|
+
object: { ...back, version },
|
|
2012
|
+
objectVersion: version,
|
|
2013
|
+
objectRequest: { id: back.id, version },
|
|
2014
|
+
sectionRequest: undefined,
|
|
2015
|
+
};
|
|
2016
|
+
}
|
|
2017
|
+
|
|
2018
|
+
/** The keys of a focused next-level list; undefined when the key is not one of them. */
|
|
2019
|
+
function childKey(state: TuiState, key: string): TuiState | undefined {
|
|
2020
|
+
const children = state.object?.children;
|
|
2021
|
+
if (children === undefined || !children.focused) return undefined;
|
|
2022
|
+
if (isKey(key, "up", "arrowup", "k")) {
|
|
2023
|
+
return withChildren(state, (c) => ({ ...c, cursor: Math.max(0, c.cursor - 1) }));
|
|
2024
|
+
}
|
|
2025
|
+
if (isKey(key, "down", "arrowdown", "j")) {
|
|
2026
|
+
const count = displayChildren(children.rows, state.childLayout).length;
|
|
2027
|
+
return withChildren(state, (c) => ({
|
|
2028
|
+
...c,
|
|
2029
|
+
cursor: Math.min(count - 1, c.cursor + 1),
|
|
2030
|
+
}));
|
|
2031
|
+
}
|
|
2032
|
+
if (isKey(key, "g")) return withChildren(state, (c) => ({ ...c, cursor: 0 }));
|
|
2033
|
+
if (key === "G") {
|
|
2034
|
+
const count = displayChildren(children.rows, state.childLayout).length;
|
|
2035
|
+
return withChildren(state, (c) => ({ ...c, cursor: count - 1 }));
|
|
2036
|
+
}
|
|
2037
|
+
if (isKey(key, "enter", "return")) return openChild({ ...state, followPending: false });
|
|
2038
|
+
if (isKey(key, "escape", "esc")) return withChildren(state, (c) => ({ ...c, focused: false }));
|
|
2039
|
+
return undefined;
|
|
2040
|
+
}
|
|
2041
|
+
|
|
2042
|
+
/** Enter: opens the selected row; on the row that is already open, its next level. */
|
|
2043
|
+
function enter(state: TuiState): TuiState {
|
|
2044
|
+
const row = selectedRow(state);
|
|
2045
|
+
if (row === undefined) return state;
|
|
2046
|
+
const open = state.object;
|
|
2047
|
+
if (open !== undefined && open.source === row.id && open.error === undefined) {
|
|
2048
|
+
// A child's page is open: the list is not where Enter acts.
|
|
2049
|
+
if (open.back !== undefined) return { ...state, followPending: false };
|
|
2050
|
+
return withChildren({ ...state, followPending: false }, (c) => ({ ...c, focused: true }));
|
|
2051
|
+
}
|
|
2052
|
+
return openObject({ ...state, followPending: false });
|
|
2053
|
+
}
|
|
2054
|
+
|
|
2055
|
+
/**
|
|
2056
|
+
* The debounce after a cursor move ended: with the object pane beside the list,
|
|
2057
|
+
* the pane shows the row the cursor is on. A narrow screen opens nothing by
|
|
2058
|
+
* itself.
|
|
2059
|
+
*/
|
|
2060
|
+
function follow(state: TuiState): TuiState {
|
|
2061
|
+
if (!state.followPending) return state;
|
|
2062
|
+
const cleared = { ...state, followPending: false };
|
|
2063
|
+
if (!twoPane(cleared)) return cleared;
|
|
2064
|
+
const row = selectedRow(cleared);
|
|
2065
|
+
if (row === undefined) return cleared;
|
|
2066
|
+
if (cleared.object?.source === row.id) return cleared;
|
|
2067
|
+
return openObject(cleared);
|
|
2068
|
+
}
|
|
2069
|
+
|
|
2070
|
+
function cycle(state: TuiState, delta: number): TuiState {
|
|
2071
|
+
if (state.tabs.length === 0) return state;
|
|
2072
|
+
const index = (state.active + delta + state.tabs.length) % state.tabs.length;
|
|
2073
|
+
return activate(state, index);
|
|
2074
|
+
}
|
|
2075
|
+
|
|
2076
|
+
function isKey(key: string, ...names: string[]): boolean {
|
|
2077
|
+
const lowered = key.toLowerCase();
|
|
2078
|
+
return names.some((name) => lowered === name.toLowerCase());
|
|
2079
|
+
}
|
|
2080
|
+
|
|
2081
|
+
/** The name keys.json uses for a key: `enter` and `escape`, however the terminal names them. */
|
|
2082
|
+
function bindingLabel(label: string): string {
|
|
2083
|
+
const lowered = label.toLowerCase();
|
|
2084
|
+
if (lowered === "return") return "enter";
|
|
2085
|
+
if (lowered === "esc") return "escape";
|
|
2086
|
+
return label;
|
|
2087
|
+
}
|
|
2088
|
+
|
|
2089
|
+
/** A palette command does what its key does. */
|
|
2090
|
+
function runCommand(state: TuiState, command: TuiCommand): TuiState {
|
|
2091
|
+
switch (command.kind) {
|
|
2092
|
+
case "help":
|
|
2093
|
+
return handleKey(state, { key: "?", text: "?" });
|
|
2094
|
+
case "refresh":
|
|
2095
|
+
return handleKey(state, { key: "r", text: "r" });
|
|
2096
|
+
case "back":
|
|
2097
|
+
return handleKey(state, { key: "escape" });
|
|
2098
|
+
case "quit":
|
|
2099
|
+
return handleKey(state, { key: "q", text: "q" });
|
|
2100
|
+
case "themes": {
|
|
2101
|
+
const at = state.themeNames.indexOf(state.themeCurrent);
|
|
2102
|
+
const cursor = Math.max(0, at);
|
|
2103
|
+
return {
|
|
2104
|
+
...state,
|
|
2105
|
+
input: { kind: "themes", cursor },
|
|
2106
|
+
themePreview: state.themeNames[cursor],
|
|
2107
|
+
};
|
|
2108
|
+
}
|
|
2109
|
+
case "create": {
|
|
2110
|
+
const opened = openNamed({ ...state, followPending: false }, command.parent);
|
|
2111
|
+
return opened === state
|
|
2112
|
+
? state
|
|
2113
|
+
: { ...opened, creating: { parent: command.parent, type: command.type } };
|
|
2114
|
+
}
|
|
2115
|
+
case "go":
|
|
2116
|
+
return activate(state, state.tabs.findIndex((tab) => tab.id === command.tabId));
|
|
2117
|
+
case "open": {
|
|
2118
|
+
const id = command.id.includes(":") ? command.id : `${command.type}:${command.id}`;
|
|
2119
|
+
return openNamed({ ...state, followPending: false }, id);
|
|
2120
|
+
}
|
|
2121
|
+
}
|
|
2122
|
+
}
|
|
2123
|
+
|
|
2124
|
+
function handleKey(
|
|
2125
|
+
state: TuiState,
|
|
2126
|
+
event: {
|
|
2127
|
+
key: string;
|
|
2128
|
+
text?: string | undefined;
|
|
2129
|
+
shift?: boolean | undefined;
|
|
2130
|
+
ctrl?: boolean | undefined;
|
|
2131
|
+
meta?: boolean | undefined;
|
|
2132
|
+
},
|
|
2133
|
+
): TuiState {
|
|
2134
|
+
const typing = state.input;
|
|
2135
|
+
const calm = state.notice === undefined ? state : { ...state, notice: undefined };
|
|
2136
|
+
if (typing?.kind === "pages") return pagesKey(calm, typing, event);
|
|
2137
|
+
if (typing?.kind === "themes") return themesKey(calm, typing, event);
|
|
2138
|
+
if (typing?.kind === "palette" && isKey(event.key, "enter", "return")) {
|
|
2139
|
+
const chosen = chosenEntry(calm);
|
|
2140
|
+
if (chosen?.command !== undefined) {
|
|
2141
|
+
return runCommand(remember({ ...calm, input: undefined }, chosen), chosen.command);
|
|
2142
|
+
}
|
|
2143
|
+
}
|
|
2144
|
+
if (typing !== undefined) return inputKey(calm, typing, event);
|
|
2145
|
+
// Ctrl-C is the renderer's exit; no other chord is ours.
|
|
2146
|
+
if (event.ctrl === true || event.meta === true) return calm;
|
|
2147
|
+
state = calm;
|
|
2148
|
+
const key = event.key;
|
|
2149
|
+
const shift = event.shift === true;
|
|
2150
|
+
const label = keyLabel(key, shift);
|
|
2151
|
+
// Reserved keys were dropped from the bindings; the rest, a user binding
|
|
2152
|
+
// first, run their action.
|
|
2153
|
+
const own = isReserved(label)
|
|
2154
|
+
? undefined
|
|
2155
|
+
: bindingFor(state.bindings, bindingLabel(label));
|
|
2156
|
+
// The key list closes first, so the action acts on what the person sees.
|
|
2157
|
+
if (own !== undefined) return startAction({ ...state, keys: false }, own.action);
|
|
2158
|
+
if (key === ":" && !state.keys) {
|
|
2159
|
+
return { ...state, input: { kind: "palette", query: "", cursor: 0 }, settingsRequest: true };
|
|
2160
|
+
}
|
|
2161
|
+
if (key === "?" || (key === "/" && shift)) {
|
|
2162
|
+
return { ...state, keys: !state.keys, helpScroll: 0 };
|
|
2163
|
+
}
|
|
2164
|
+
if (isKey(key, "q")) return { ...state, quit: true };
|
|
2165
|
+
if (state.keys) {
|
|
2166
|
+
// The list behind the key list is hidden, so no other key acts on it.
|
|
2167
|
+
if (isKey(key, "escape", "esc")) return { ...state, keys: false, helpScroll: 0 };
|
|
2168
|
+
const page = helpViewport(state);
|
|
2169
|
+
const bottom = Math.max(0, HELP_LINES - page);
|
|
2170
|
+
const at = Math.min(Math.max(0, state.helpScroll), bottom);
|
|
2171
|
+
const to = (row: number): TuiState => ({
|
|
2172
|
+
...state,
|
|
2173
|
+
helpScroll: Math.min(bottom, Math.max(0, row)),
|
|
2174
|
+
});
|
|
2175
|
+
if (isKey(key, "up", "arrowup", "k")) return to(at - 1);
|
|
2176
|
+
if (isKey(key, "down", "arrowdown", "j")) return to(at + 1);
|
|
2177
|
+
if (isKey(key, "pageup", "page-up", "prior", "b")) return to(at - page);
|
|
2178
|
+
if (isKey(key, "pagedown", "page-down", "next", "space")) return to(at + page);
|
|
2179
|
+
if (isKey(key, "home")) return to(0);
|
|
2180
|
+
if (isKey(key, "end")) return to(bottom);
|
|
2181
|
+
return state;
|
|
2182
|
+
}
|
|
2183
|
+
if (isKey(key, "t") && (state.object?.children?.rows.length ?? 0) > 0) {
|
|
2184
|
+
const childLayout: TuiChildLayout = state.childLayout === "tree" ? "list" : "tree";
|
|
2185
|
+
return { ...state, childLayout, layoutRequest: childLayout, followPending: false };
|
|
2186
|
+
}
|
|
2187
|
+
const inChildren = childKey(state, key);
|
|
2188
|
+
if (inChildren !== undefined) return inChildren;
|
|
2189
|
+
if (isKey(key, "enter", "return")) return enter(state);
|
|
2190
|
+
if (isKey(key, "escape", "esc", "backspace")) {
|
|
2191
|
+
const back = goBack(state);
|
|
2192
|
+
if (back !== undefined) return back;
|
|
2193
|
+
if (isKey(key, "backspace")) return state;
|
|
2194
|
+
return closeObject({ ...state, followPending: false });
|
|
2195
|
+
}
|
|
2196
|
+
if (isKey(key, "[", "left", "arrowleft")) return moveSection(state, -1);
|
|
2197
|
+
if (isKey(key, "]", "right", "arrowright")) return moveSection(state, 1);
|
|
2198
|
+
const object = state.object;
|
|
2199
|
+
if (object !== undefined) {
|
|
2200
|
+
// The pane scrolls by page in both layouts; only on a narrow screen, where
|
|
2201
|
+
// the object fills the screen, do the list keys scroll it by line.
|
|
2202
|
+
const page = Math.max(1, sectionViewport(state) - 1);
|
|
2203
|
+
if (isKey(key, "pageup", "page-up", "prior", "b")) return scrollSection(state, -page);
|
|
2204
|
+
if (isKey(key, "pagedown", "page-down", "next", "space", " ")) {
|
|
2205
|
+
return scrollSection(state, page);
|
|
2206
|
+
}
|
|
2207
|
+
if (isKey(key, "home")) return jumpSection(state, "top");
|
|
2208
|
+
if (isKey(key, "end")) return jumpSection(state, "bottom");
|
|
2209
|
+
if (isKey(key, "p")) return openPagePicker(state);
|
|
2210
|
+
if (isKey(key, "n")) return stepPage(state, shift ? -1 : 1);
|
|
2211
|
+
if (isKey(key, "f")) {
|
|
2212
|
+
const section = activeSection(state);
|
|
2213
|
+
if (section !== undefined && section.capped && !section.full) {
|
|
2214
|
+
const at = section.pageStart;
|
|
2215
|
+
const cursor = section.toc?.[at]?.cursor;
|
|
2216
|
+
return requestSection(
|
|
2217
|
+
state,
|
|
2218
|
+
object.active,
|
|
2219
|
+
section.toc === undefined || at === 0
|
|
2220
|
+
? { full: true }
|
|
2221
|
+
: { full: true, jump: at, force: true, ...(cursor === undefined ? {} : { cursor }) },
|
|
2222
|
+
);
|
|
2223
|
+
}
|
|
2224
|
+
return filterKeyApplies(state) ? cycleFilter(state) : state;
|
|
2225
|
+
}
|
|
2226
|
+
if (!twoPane(state)) {
|
|
2227
|
+
if (isKey(key, "up", "arrowup", "k")) return scrollSection(state, -1);
|
|
2228
|
+
if (isKey(key, "down", "arrowdown", "j")) return scrollSection(state, 1);
|
|
2229
|
+
if (key === "G" || (isKey(key, "g") && shift)) return jumpSection(state, "bottom");
|
|
2230
|
+
if (isKey(key, "g")) return jumpSection(state, "top");
|
|
2231
|
+
}
|
|
2232
|
+
}
|
|
2233
|
+
if (isKey(key, "c") && !shift) return createHere(state);
|
|
2234
|
+
if (isKey(key, "f")) return cycleFilter(state);
|
|
2235
|
+
// A rotated token is not retried: the person runs the TUI again.
|
|
2236
|
+
if (isKey(key, "r") && state.connection === "unauthorized") return state;
|
|
2237
|
+
if (isKey(key, "r")) {
|
|
2238
|
+
const retry = { ...state, refreshRequested: true };
|
|
2239
|
+
const object = state.object;
|
|
2240
|
+
const section = object?.sections[object.active];
|
|
2241
|
+
// A section that failed to load asks again; the others refresh with the lists.
|
|
2242
|
+
return object !== undefined && section?.error !== undefined && !section.loading
|
|
2243
|
+
? requestSection(retry, object.active, { force: true })
|
|
2244
|
+
: retry;
|
|
2245
|
+
}
|
|
2246
|
+
if (isKey(key, "tab")) return cycle(state, shift ? -1 : 1);
|
|
2247
|
+
if (isKey(key, "up", "arrowup", "k")) return move(state, -1);
|
|
2248
|
+
if (isKey(key, "down", "arrowdown", "j")) return move(state, 1);
|
|
2249
|
+
if (key === "G" || (isKey(key, "g") && shift)) return jump(state, "bottom");
|
|
2250
|
+
if (isKey(key, "g")) return jump(state, "top");
|
|
2251
|
+
if (/^[1-9]$/.test(key)) return activate(state, Number(key) - 1);
|
|
2252
|
+
const answer = selectedAnswer(state);
|
|
2253
|
+
if (answer !== undefined && label === MAIN_KEY) return startAnswer(state, answer);
|
|
2254
|
+
return runKey(state, label) ?? state;
|
|
2255
|
+
}
|
|
2256
|
+
|
|
2257
|
+
/** The filters are shown and `f` cycles them: the list is on screen. */
|
|
2258
|
+
function filterKeyApplies(state: TuiState): boolean {
|
|
2259
|
+
const tab = activeTabOf(state);
|
|
2260
|
+
if (tab?.filters === undefined) return false;
|
|
2261
|
+
return state.object === undefined || twoPane(state);
|
|
2262
|
+
}
|
|
2263
|
+
|
|
2264
|
+
/** `f`: the next filter of the type tab, its first page fetched again. */
|
|
2265
|
+
function cycleFilter(state: TuiState): TuiState {
|
|
2266
|
+
const tab = activeTabOf(state);
|
|
2267
|
+
const filters = tab?.filters;
|
|
2268
|
+
if (tab === undefined || filters === undefined || filters.length === 0) return state;
|
|
2269
|
+
const at = filters.findIndex((f) => f.name === tab.filter);
|
|
2270
|
+
const next = filters[(at + 1) % filters.length]?.name;
|
|
2271
|
+
if (next === undefined || next === tab.filter) return state;
|
|
2272
|
+
const changed = withTab(state, tab.id, (current) => ({
|
|
2273
|
+
...current,
|
|
2274
|
+
filter: next,
|
|
2275
|
+
filterSeq: (current.filterSeq ?? 0) + 1,
|
|
2276
|
+
hidden: undefined,
|
|
2277
|
+
// The rows stay until the new ones arrive; a page of the old filter is
|
|
2278
|
+
// dropped by its version, and its paging cursor is no longer valid.
|
|
2279
|
+
list: {
|
|
2280
|
+
...current.list,
|
|
2281
|
+
next: undefined,
|
|
2282
|
+
loading: false,
|
|
2283
|
+
switching: true,
|
|
2284
|
+
version: current.list.version + 1,
|
|
2285
|
+
},
|
|
2286
|
+
}));
|
|
2287
|
+
const quiet =
|
|
2288
|
+
changed.pageRequest?.tabId === tab.id ? { ...changed, pageRequest: undefined } : changed;
|
|
2289
|
+
return {
|
|
2290
|
+
...quiet,
|
|
2291
|
+
filterRequest: { tabId: tab.id, filter: next, seq: (tab.filterSeq ?? 0) + 1 },
|
|
2292
|
+
};
|
|
2293
|
+
}
|
|
2294
|
+
|
|
2295
|
+
/** What the header adds after the current plugin tab: the filter and what it hides. */
|
|
2296
|
+
export function filterHeader(
|
|
2297
|
+
tab: TuiTab | undefined,
|
|
2298
|
+
): { readonly filter: string; readonly hidden: string } | undefined {
|
|
2299
|
+
if (tab?.kind !== "plugin" || tab.filters === undefined || tab.filter === undefined) {
|
|
2300
|
+
return undefined;
|
|
2301
|
+
}
|
|
2302
|
+
const hidden =
|
|
2303
|
+
tab.filter !== "all" && (tab.hidden ?? 0) > 0 ? ` · ${tab.hidden} hidden` : "";
|
|
2304
|
+
return { filter: ` · ${tab.filter}`, hidden };
|
|
2305
|
+
}
|
|
2306
|
+
|
|
2307
|
+
function typeTitle(type: TuiTypeInfo | undefined): string {
|
|
2308
|
+
if (type === undefined) return "";
|
|
2309
|
+
return type.title.length === 0 ? type.type : type.title;
|
|
2310
|
+
}
|
|
2311
|
+
|
|
2312
|
+
/** The plugin a type belongs to; a server that does not say gives each type its own tab. */
|
|
2313
|
+
function pluginOf(type: TuiTypeInfo): string {
|
|
2314
|
+
return type.plugin === undefined || type.plugin === "" ? type.type : type.plugin;
|
|
2315
|
+
}
|
|
2316
|
+
|
|
2317
|
+
function applyTypes(state: TuiState, types: readonly TuiTypeInfo[]): TuiState {
|
|
2318
|
+
const fixed = state.tabs.filter((tab) => tab.kind !== "plugin");
|
|
2319
|
+
const existing = new Map(state.tabs.map((tab) => [tab.id, tab]));
|
|
2320
|
+
const byPlugin = new Map<string, TuiTypeInfo[]>();
|
|
2321
|
+
for (const info of types) {
|
|
2322
|
+
const name = pluginOf(info);
|
|
2323
|
+
const group = byPlugin.get(name) ?? [];
|
|
2324
|
+
if (!group.some((seen) => seen.type === info.type)) group.push(info);
|
|
2325
|
+
byPlugin.set(name, group);
|
|
2326
|
+
}
|
|
2327
|
+
const pluginTabs: TuiTab[] = [];
|
|
2328
|
+
const settingsTypes: TuiTypeInfo[] = [];
|
|
2329
|
+
for (const [name, group] of byPlugin) {
|
|
2330
|
+
// A plugin whose objects belong under settings gets no tab; all its types
|
|
2331
|
+
// are listed in the settings view, one group per type.
|
|
2332
|
+
if (group.some((info) => info.settings === true)) {
|
|
2333
|
+
for (const info of group) {
|
|
2334
|
+
if (!settingsTypes.some((seen) => seen.type === info.type)) settingsTypes.push(info);
|
|
2335
|
+
}
|
|
2336
|
+
continue;
|
|
2337
|
+
}
|
|
2338
|
+
const id = `plugin:${name}`;
|
|
2339
|
+
// A server that names no plugin gives each type its own tab, titled as the type.
|
|
2340
|
+
const first = group[0];
|
|
2341
|
+
const title = first?.plugin === undefined || first.plugin === "" ? typeTitle(first) : name;
|
|
2342
|
+
const fresh = { ...makeTab(id, "plugin", title, group), plugin: name };
|
|
2343
|
+
const old = existing.get(id);
|
|
2344
|
+
if (old === undefined) {
|
|
2345
|
+
pluginTabs.push(fresh);
|
|
2346
|
+
continue;
|
|
2347
|
+
}
|
|
2348
|
+
// A known tab keeps its chosen filter while the plugin still declares it.
|
|
2349
|
+
const kept = fresh.filters?.some((f) => f.name === old.filter) === true;
|
|
2350
|
+
pluginTabs.push({
|
|
2351
|
+
...old,
|
|
2352
|
+
types: group,
|
|
2353
|
+
filters: fresh.filters,
|
|
2354
|
+
filter: kept ? old.filter : fresh.filter,
|
|
2355
|
+
});
|
|
2356
|
+
}
|
|
2357
|
+
const settingsTab = settingsTabFor(state.settingsTab, settingsTypes);
|
|
2358
|
+
const tabs = [...fixed, ...pluginTabs];
|
|
2359
|
+
const activeId = state.tabs[state.active]?.id;
|
|
2360
|
+
const found = tabs.findIndex((tab) => tab.id === activeId);
|
|
2361
|
+
return {
|
|
2362
|
+
...state,
|
|
2363
|
+
tabs,
|
|
2364
|
+
active: found < 0 ? Math.min(state.active, Math.max(0, tabs.length - 1)) : found,
|
|
2365
|
+
settingsTab,
|
|
2366
|
+
};
|
|
2367
|
+
}
|
|
2368
|
+
|
|
2369
|
+
/** The settings list for the types under it, keeping its cursor and filter when it can. */
|
|
2370
|
+
function settingsTabFor(
|
|
2371
|
+
old: TuiTab | undefined,
|
|
2372
|
+
types: readonly TuiTypeInfo[],
|
|
2373
|
+
): TuiTab | undefined {
|
|
2374
|
+
if (types.length === 0) return undefined;
|
|
2375
|
+
const fresh = makeTab("settings-objects", "settings", "Settings", types);
|
|
2376
|
+
if (old === undefined) return fresh;
|
|
2377
|
+
const kept = fresh.filters?.some((f) => f.name === old.filter) === true;
|
|
2378
|
+
return {
|
|
2379
|
+
...old,
|
|
2380
|
+
types,
|
|
2381
|
+
filters: fresh.filters,
|
|
2382
|
+
filter: kept ? old.filter : fresh.filter,
|
|
2383
|
+
};
|
|
2384
|
+
}
|
|
2385
|
+
|
|
2386
|
+
/** Keeps the cursor on the same object when the rows are replaced. */
|
|
2387
|
+
function cursorAfterReplace(old: TuiList, rows: readonly TuiRow[]): number {
|
|
2388
|
+
const selected = old.rows[old.cursor]?.id;
|
|
2389
|
+
if (selected !== undefined) {
|
|
2390
|
+
const found = rows.findIndex((row) => row.id === selected);
|
|
2391
|
+
if (found >= 0) return found;
|
|
2392
|
+
}
|
|
2393
|
+
// A new filter that hides the selection starts at the top; a live refresh
|
|
2394
|
+
// keeps the place, which is the next row.
|
|
2395
|
+
if (old.switching === true) return 0;
|
|
2396
|
+
return clampCursor({ ...old, rows });
|
|
2397
|
+
}
|
|
2398
|
+
|
|
2399
|
+
/** True when two ids name one object, with or without scope or type. */
|
|
2400
|
+
export function sameObject(a: string, b: string): boolean {
|
|
2401
|
+
if (a === b) return true;
|
|
2402
|
+
const tail = (id: string): string => id.slice(Math.max(id.lastIndexOf(":"), id.lastIndexOf("/")) + 1);
|
|
2403
|
+
return tail(a) !== "" && tail(a) === tail(b);
|
|
2404
|
+
}
|
|
2405
|
+
|
|
2406
|
+
/** The rows without the one at `at`; its group's heading moves to the next row of the group. */
|
|
2407
|
+
function dropRow(rows: readonly TuiRow[], at: number): TuiRow[] {
|
|
2408
|
+
const gone = rows[at];
|
|
2409
|
+
const out = rows.filter((_, i) => i !== at);
|
|
2410
|
+
const after = out[at];
|
|
2411
|
+
if (
|
|
2412
|
+
gone?.group !== undefined &&
|
|
2413
|
+
after !== undefined &&
|
|
2414
|
+
after.group === undefined &&
|
|
2415
|
+
after.parent === gone.parent
|
|
2416
|
+
) {
|
|
2417
|
+
out[at] = { ...after, group: gone.group };
|
|
2418
|
+
}
|
|
2419
|
+
return out;
|
|
2420
|
+
}
|
|
2421
|
+
|
|
2422
|
+
/** Puts a fresh row in place of the rows of the same object; the cursor stays. */
|
|
2423
|
+
function replaceRow(state: TuiState, row: TuiRow): TuiState {
|
|
2424
|
+
return replacePageRow(replaceListRow(state, row), row);
|
|
2425
|
+
}
|
|
2426
|
+
|
|
2427
|
+
/** Puts a fresh row in place of the same object's row on the open page's next level. */
|
|
2428
|
+
function replacePageRow(state: TuiState, row: TuiRow): TuiState {
|
|
2429
|
+
const object = state.object;
|
|
2430
|
+
const children = object?.children;
|
|
2431
|
+
if (object === undefined || children === undefined) return state;
|
|
2432
|
+
const at = children.rows.findIndex((old) => sameObject(old.id, row.id));
|
|
2433
|
+
if (at < 0) return state;
|
|
2434
|
+
const old = children.rows[at] as TuiRow;
|
|
2435
|
+
const next = {
|
|
2436
|
+
...row,
|
|
2437
|
+
...(old.type === undefined ? {} : { type: old.type }),
|
|
2438
|
+
...(old.parent === undefined ? {} : { parent: old.parent }),
|
|
2439
|
+
...(old.group === undefined ? {} : { group: old.group }),
|
|
2440
|
+
...(old.bare === true ? { bare: true } : {}),
|
|
2441
|
+
};
|
|
2442
|
+
// The page's next level follows the tab's filter, so a child that no longer
|
|
2443
|
+
// matches leaves the list like the left one does.
|
|
2444
|
+
const tab = pluginTabOf(state, object.type);
|
|
2445
|
+
const states = tab?.types
|
|
2446
|
+
?.find((info) => info.type === (old.type ?? next.type))
|
|
2447
|
+
?.filters?.find((filter) => filter.name === tab.filter)?.states;
|
|
2448
|
+
if (states === undefined || next.state === "error" || states.includes(next.state)) {
|
|
2449
|
+
return withChildren(state, (c) => ({
|
|
2450
|
+
...c,
|
|
2451
|
+
rows: c.rows.map((candidate) => (sameObject(candidate.id, row.id) ? next : candidate)),
|
|
2452
|
+
}));
|
|
2453
|
+
}
|
|
2454
|
+
const selected = displayChildren(children.rows, state.childLayout)[children.cursor]?.row.id;
|
|
2455
|
+
const rows = dropRow(children.rows, at);
|
|
2456
|
+
const shown = displayChildren(rows, state.childLayout);
|
|
2457
|
+
const found = selected === undefined ? -1 : shown.findIndex((entry) => entry.row.id === selected);
|
|
2458
|
+
return withObject(state, (value) =>
|
|
2459
|
+
value.children === undefined
|
|
2460
|
+
? value
|
|
2461
|
+
: {
|
|
2462
|
+
...value,
|
|
2463
|
+
children: {
|
|
2464
|
+
...children,
|
|
2465
|
+
rows,
|
|
2466
|
+
cursor: found >= 0 ? found : Math.min(children.cursor, Math.max(0, shown.length - 1)),
|
|
2467
|
+
focused: rows.length > 0 && children.focused,
|
|
2468
|
+
hidden: (children.hidden ?? 0) + 1,
|
|
2469
|
+
...(doneRow(next) ? { hiddenDone: (children.hiddenDone ?? 0) + 1 } : {}),
|
|
2470
|
+
...(cancelledRow(next)
|
|
2471
|
+
? { hiddenCancelled: (children.hiddenCancelled ?? 0) + 1 }
|
|
2472
|
+
: {}),
|
|
2473
|
+
},
|
|
2474
|
+
},
|
|
2475
|
+
);
|
|
2476
|
+
}
|
|
2477
|
+
|
|
2478
|
+
function replaceListRow(state: TuiState, row: TuiRow): TuiState {
|
|
2479
|
+
let changed = false;
|
|
2480
|
+
let selectedLeft = false;
|
|
2481
|
+
const update = (tab: TuiTab, active: boolean): TuiTab => {
|
|
2482
|
+
const at = tab.list.rows.findIndex((old) => sameObject(old.id, row.id));
|
|
2483
|
+
const shown = at < 0 ? undefined : tab.list.rows[at];
|
|
2484
|
+
const info = tab.types?.find((candidate) => candidate.type === shown?.type);
|
|
2485
|
+
const states = info?.filters?.find((f) => f.name === tab.filter)?.states;
|
|
2486
|
+
if (
|
|
2487
|
+
at >= 0 &&
|
|
2488
|
+
states !== undefined &&
|
|
2489
|
+
row.state !== "error" &&
|
|
2490
|
+
!states.includes(row.state)
|
|
2491
|
+
) {
|
|
2492
|
+
// The object no longer matches the filter: it leaves the list, and the
|
|
2493
|
+
// cursor keeps its place, which is now the next row.
|
|
2494
|
+
changed = true;
|
|
2495
|
+
if (active && at === tab.list.cursor) selectedLeft = true;
|
|
2496
|
+
const rows = dropRow(tab.list.rows, at);
|
|
2497
|
+
const cursor = Math.min(tab.list.cursor, Math.max(0, rows.length - 1));
|
|
2498
|
+
return {
|
|
2499
|
+
...tab,
|
|
2500
|
+
hidden: (tab.hidden ?? 0) + 1,
|
|
2501
|
+
list: { ...tab.list, rows, cursor },
|
|
2502
|
+
};
|
|
2503
|
+
}
|
|
2504
|
+
const rows = tab.list.rows.map((old) => {
|
|
2505
|
+
if (!sameObject(old.id, row.id)) return old;
|
|
2506
|
+
changed = true;
|
|
2507
|
+
return {
|
|
2508
|
+
...row,
|
|
2509
|
+
...(old.type === undefined ? {} : { type: old.type }),
|
|
2510
|
+
...(old.parent === undefined ? {} : { parent: old.parent }),
|
|
2511
|
+
...(old.group === undefined ? {} : { group: old.group }),
|
|
2512
|
+
...(old.bare === true ? { bare: true } : {}),
|
|
2513
|
+
...(old.heading === true ? { heading: true } : {}),
|
|
2514
|
+
...(old.grouped === true ? { grouped: true } : {}),
|
|
2515
|
+
};
|
|
2516
|
+
});
|
|
2517
|
+
return rows.some((r, i) => r !== tab.list.rows[i])
|
|
2518
|
+
? { ...tab, list: { ...tab.list, rows } }
|
|
2519
|
+
: tab;
|
|
2520
|
+
};
|
|
2521
|
+
const tabs = state.tabs.map((tab, index) =>
|
|
2522
|
+
tab.kind === "plugin" ? update(tab, index === state.active) : tab,
|
|
2523
|
+
);
|
|
2524
|
+
const settingsTab =
|
|
2525
|
+
state.settingsTab === undefined ? undefined : update(state.settingsTab, false);
|
|
2526
|
+
if (!changed) return state;
|
|
2527
|
+
const next = {
|
|
2528
|
+
...state,
|
|
2529
|
+
tabs,
|
|
2530
|
+
...(settingsTab === undefined ? {} : { settingsTab }),
|
|
2531
|
+
};
|
|
2532
|
+
return selectedLeft
|
|
2533
|
+
? { ...next, moveSeq: next.moveSeq + 1, followPending: true }
|
|
2534
|
+
: next;
|
|
2535
|
+
}
|
|
2536
|
+
|
|
2537
|
+
/** Drops the row of an object that is gone from the server. */
|
|
2538
|
+
function removeRow(state: TuiState, id: string): TuiState {
|
|
2539
|
+
let changed = false;
|
|
2540
|
+
const update = (tab: TuiTab): TuiTab => {
|
|
2541
|
+
const at = tab.list.rows.findIndex((row) => sameObject(row.id, id));
|
|
2542
|
+
if (at < 0) return tab;
|
|
2543
|
+
changed = true;
|
|
2544
|
+
const rows = dropRow(tab.list.rows, at);
|
|
2545
|
+
const cursor = Math.min(tab.list.cursor, Math.max(0, rows.length - 1));
|
|
2546
|
+
return { ...tab, list: { ...tab.list, rows, cursor } };
|
|
2547
|
+
};
|
|
2548
|
+
const tabs = state.tabs.map((tab) => (tab.kind === "plugin" ? update(tab) : tab));
|
|
2549
|
+
const settingsTab =
|
|
2550
|
+
state.settingsTab === undefined ? undefined : update(state.settingsTab);
|
|
2551
|
+
return changed
|
|
2552
|
+
? {
|
|
2553
|
+
...state,
|
|
2554
|
+
tabs,
|
|
2555
|
+
...(settingsTab === undefined ? {} : { settingsTab }),
|
|
2556
|
+
}
|
|
2557
|
+
: state;
|
|
2558
|
+
}
|
|
2559
|
+
|
|
2560
|
+
function replaceRows(
|
|
2561
|
+
tab: TuiTab,
|
|
2562
|
+
rows: readonly TuiRow[],
|
|
2563
|
+
next: string | undefined,
|
|
2564
|
+
): TuiTab {
|
|
2565
|
+
const cursor = cursorAfterReplace(tab.list, rows);
|
|
2566
|
+
return {
|
|
2567
|
+
...tab,
|
|
2568
|
+
list: {
|
|
2569
|
+
...tab.list,
|
|
2570
|
+
rows,
|
|
2571
|
+
cursor,
|
|
2572
|
+
next,
|
|
2573
|
+
loaded: true,
|
|
2574
|
+
// The page that was in flight for the old rows is dropped when it lands.
|
|
2575
|
+
loading: false,
|
|
2576
|
+
error: undefined,
|
|
2577
|
+
switching: undefined,
|
|
2578
|
+
version: tab.list.version + 1,
|
|
2579
|
+
},
|
|
2580
|
+
};
|
|
2581
|
+
}
|
|
2582
|
+
|
|
2583
|
+
function clearRequest(before: TuiState, after: TuiState, tabId: string): TuiState {
|
|
2584
|
+
return before.pageRequest?.tabId === tabId
|
|
2585
|
+
? { ...after, pageRequest: undefined }
|
|
2586
|
+
: after;
|
|
2587
|
+
}
|
|
2588
|
+
|
|
2589
|
+
/**
|
|
2590
|
+
* After a list loads or another tab is shown, the pane beside it shows the row
|
|
2591
|
+
* the cursor rests on, unless it already does.
|
|
2592
|
+
*/
|
|
2593
|
+
function followLoaded(state: TuiState, tabId: string): TuiState {
|
|
2594
|
+
const tab = activeTabOf(state);
|
|
2595
|
+
if (tab === undefined || tab.id !== tabId) return state;
|
|
2596
|
+
const row = tab.list.rows[tab.list.cursor];
|
|
2597
|
+
// An empty list leaves no row for the pane beside it to follow; a lone object page is kept.
|
|
2598
|
+
if (row === undefined) return twoPane(state) ? closeObject({ ...state, followPending: false }) : state;
|
|
2599
|
+
if (row.openable === false || state.object?.source === row.id) return state;
|
|
2600
|
+
// A follow already waiting keeps its debounce: restarting it on every refresh could starve it.
|
|
2601
|
+
if (state.followPending) return state;
|
|
2602
|
+
return { ...state, moveSeq: state.moveSeq + 1, followPending: true };
|
|
2603
|
+
}
|
|
2604
|
+
|
|
2605
|
+
/** The answerable question on the selected pending row, if any. */
|
|
2606
|
+
function selectedAnswer(state: TuiState): TuiAnswer | undefined {
|
|
2607
|
+
const tab = activeTabOf(state);
|
|
2608
|
+
if (tab?.kind !== "pending") return undefined;
|
|
2609
|
+
return tab.list.rows[tab.list.cursor]?.answer;
|
|
2610
|
+
}
|
|
2611
|
+
|
|
2612
|
+
function applyPending(state: TuiState, items: readonly PendingItem[]): TuiState {
|
|
2613
|
+
const rows = items.map(rowFromPending);
|
|
2614
|
+
return followLoaded(
|
|
2615
|
+
withTab(state, "pending", (tab) => replaceRows(tab, rows, undefined)),
|
|
2616
|
+
"pending",
|
|
2617
|
+
);
|
|
2618
|
+
}
|
|
2619
|
+
|
|
2620
|
+
function applyRunning(state: TuiState, items: readonly RunningItem[]): TuiState {
|
|
2621
|
+
const rows = items.map(rowFromRunning);
|
|
2622
|
+
return followLoaded(
|
|
2623
|
+
withTab(state, "running", (tab) => replaceRows(tab, rows, undefined)),
|
|
2624
|
+
"running",
|
|
2625
|
+
);
|
|
2626
|
+
}
|
|
2627
|
+
|
|
2628
|
+
function appendPage(
|
|
2629
|
+
state: TuiState,
|
|
2630
|
+
tabId: string,
|
|
2631
|
+
rows: readonly TuiRow[],
|
|
2632
|
+
next: string | undefined,
|
|
2633
|
+
replace: boolean,
|
|
2634
|
+
version: number | undefined,
|
|
2635
|
+
filter?: string | undefined,
|
|
2636
|
+
hidden?: number | undefined,
|
|
2637
|
+
filterSeq?: number | undefined,
|
|
2638
|
+
): TuiState {
|
|
2639
|
+
const tab = state.tabs.find((candidate) => candidate.id === tabId);
|
|
2640
|
+
if (tab === undefined) return state;
|
|
2641
|
+
if (filterSeq !== undefined && filterSeq !== (tab.filterSeq ?? 0)) return state;
|
|
2642
|
+
if (!replace && version !== undefined && version !== tab.list.version) {
|
|
2643
|
+
return state;
|
|
2644
|
+
}
|
|
2645
|
+
// An answer to a filter that is no longer the chosen one is never shown.
|
|
2646
|
+
if (filter !== undefined && tab.filter !== undefined && filter !== tab.filter) {
|
|
2647
|
+
return state;
|
|
2648
|
+
}
|
|
2649
|
+
const wasSwitching = replace && tab.list.switching === true;
|
|
2650
|
+
const updated = withTab(state, tabId, (current) => {
|
|
2651
|
+
if (replace) return { ...replaceRows(current, rows, next), hidden };
|
|
2652
|
+
const list: TuiList = {
|
|
2653
|
+
...current.list,
|
|
2654
|
+
rows: [...current.list.rows, ...rows],
|
|
2655
|
+
loaded: true,
|
|
2656
|
+
loading: false,
|
|
2657
|
+
next,
|
|
2658
|
+
error: undefined,
|
|
2659
|
+
};
|
|
2660
|
+
return {
|
|
2661
|
+
...current,
|
|
2662
|
+
...(hidden === undefined ? {} : { hidden }),
|
|
2663
|
+
list: { ...list, cursor: clampCursor(list) },
|
|
2664
|
+
};
|
|
2665
|
+
});
|
|
2666
|
+
const cleared = clearRequest(state, updated, tabId);
|
|
2667
|
+
// `f` keeps the open object even when the new filter hides its row or leaves
|
|
2668
|
+
// no row at all; its next level follows the new filter.
|
|
2669
|
+
if (wasSwitching || !replace) return cleared;
|
|
2670
|
+
return followLoaded(cleared, tabId);
|
|
2671
|
+
}
|
|
2672
|
+
|
|
2673
|
+
/** The type a page of a tab's rows is of: the tab's first. */
|
|
2674
|
+
function firstType(state: TuiState, tabId: string): string | undefined {
|
|
2675
|
+
return state.tabs.find((tab) => tab.id === tabId)?.types?.[0]?.type;
|
|
2676
|
+
}
|
|
2677
|
+
|
|
2678
|
+
function rowOfEntry(entry: TuiTreeEntry): TuiRow {
|
|
2679
|
+
return {
|
|
2680
|
+
...rowFromView(entry.row, entry.type),
|
|
2681
|
+
...(entry.bare === true ? { bare: true } : {}),
|
|
2682
|
+
...(entry.parent === undefined ? {} : { parent: sanitize(entry.parent) }),
|
|
2683
|
+
...(entry.heading === undefined
|
|
2684
|
+
? {}
|
|
2685
|
+
: {
|
|
2686
|
+
group: {
|
|
2687
|
+
...rowFromView(entry.heading.row, entry.heading.type),
|
|
2688
|
+
...(entry.heading.label === undefined
|
|
2689
|
+
? {}
|
|
2690
|
+
: { unnamed: true, title: sanitize(entry.heading.label) }),
|
|
2691
|
+
},
|
|
2692
|
+
}),
|
|
2693
|
+
};
|
|
2694
|
+
}
|
|
2695
|
+
|
|
2696
|
+
/**
|
|
2697
|
+
* The rows one entry makes. A group heading that names a settings object is a
|
|
2698
|
+
* row of its own, so Enter on it opens that object; its rows are indented under
|
|
2699
|
+
* it. Other group headings stay metadata on the first row.
|
|
2700
|
+
*/
|
|
2701
|
+
function rowsOfEntry(state: TuiState, entry: TuiTreeEntry): readonly TuiRow[] {
|
|
2702
|
+
const row = rowOfEntry(entry);
|
|
2703
|
+
const heading = entry.heading;
|
|
2704
|
+
if (
|
|
2705
|
+
heading === undefined ||
|
|
2706
|
+
!(state.settingsTab?.types?.some((info) => info.type === heading.type) === true)
|
|
2707
|
+
) {
|
|
2708
|
+
return [row];
|
|
2709
|
+
}
|
|
2710
|
+
const named = heading.label ?? heading.row.title;
|
|
2711
|
+
const headingRow: TuiRow = {
|
|
2712
|
+
...rowFromView(heading.row, heading.type),
|
|
2713
|
+
title: sanitize(named),
|
|
2714
|
+
unnamed: true,
|
|
2715
|
+
heading: true,
|
|
2716
|
+
};
|
|
2717
|
+
return [headingRow, { ...row, group: undefined, grouped: true }];
|
|
2718
|
+
}
|
|
2719
|
+
|
|
2720
|
+
function applyChildren(
|
|
2721
|
+
state: TuiState,
|
|
2722
|
+
objectId: string,
|
|
2723
|
+
version: number | undefined,
|
|
2724
|
+
entries: readonly TuiTreeEntry[],
|
|
2725
|
+
hidden?: number | undefined,
|
|
2726
|
+
hiddenDone?: number | undefined,
|
|
2727
|
+
hiddenCancelled?: number | undefined,
|
|
2728
|
+
filter?: string | undefined,
|
|
2729
|
+
filterSeq?: number | undefined,
|
|
2730
|
+
): TuiState {
|
|
2731
|
+
const object = state.object;
|
|
2732
|
+
if (object === undefined || object.id !== objectId) return state;
|
|
2733
|
+
if (version !== undefined && version !== object.version) return state;
|
|
2734
|
+
const tab = pluginTabOf(state, object.type);
|
|
2735
|
+
if (filterSeq !== undefined && filterSeq !== (tab?.filterSeq ?? 0)) return state;
|
|
2736
|
+
if (filter !== undefined && tab?.filter !== undefined && filter !== tab.filter) return state;
|
|
2737
|
+
const rows = entries.map(rowOfEntry);
|
|
2738
|
+
const old = object.children;
|
|
2739
|
+
const selected =
|
|
2740
|
+
old === undefined
|
|
2741
|
+
? undefined
|
|
2742
|
+
: displayChildren(old.rows, state.childLayout)[old.cursor]?.row.id;
|
|
2743
|
+
const shown = displayChildren(rows, state.childLayout);
|
|
2744
|
+
const found = selected === undefined ? -1 : shown.findIndex((entry) => entry.row.id === selected);
|
|
2745
|
+
const cursor = found >= 0 ? found : Math.min(old?.cursor ?? 0, Math.max(0, shown.length - 1));
|
|
2746
|
+
const children: TuiChildren = {
|
|
2747
|
+
rows,
|
|
2748
|
+
cursor,
|
|
2749
|
+
focused: rows.length > 0 && old?.focused === true,
|
|
2750
|
+
...(hidden === undefined || hidden === 0 ? {} : { hidden }),
|
|
2751
|
+
...(hiddenDone === undefined ? {} : { hiddenDone }),
|
|
2752
|
+
...(hiddenCancelled === undefined || hiddenCancelled === 0 ? {} : { hiddenCancelled }),
|
|
2753
|
+
};
|
|
2754
|
+
return withObject(state, (value) => ({
|
|
2755
|
+
...value,
|
|
2756
|
+
children: rows.length === 0 && (hidden ?? 0) === 0 ? undefined : children,
|
|
2757
|
+
}));
|
|
2758
|
+
}
|
|
2759
|
+
|
|
2760
|
+
function applyPageError(
|
|
2761
|
+
state: TuiState,
|
|
2762
|
+
tabId: string,
|
|
2763
|
+
message: string,
|
|
2764
|
+
version: number | undefined,
|
|
2765
|
+
): TuiState {
|
|
2766
|
+
const current = state.tabs.find((candidate) => candidate.id === tabId);
|
|
2767
|
+
if (current === undefined) return state;
|
|
2768
|
+
if (version !== undefined && version !== current.list.version) return state;
|
|
2769
|
+
const updated = withTab(state, tabId, (tab) => ({
|
|
2770
|
+
...tab,
|
|
2771
|
+
list: { ...tab.list, loading: false, error: sanitize(message) },
|
|
2772
|
+
}));
|
|
2773
|
+
if (updated === state) return state;
|
|
2774
|
+
if (state.pageRequest?.tabId === tabId) {
|
|
2775
|
+
return { ...updated, pageRequest: undefined };
|
|
2776
|
+
}
|
|
2777
|
+
return updated;
|
|
2778
|
+
}
|
|
2779
|
+
|
|
2780
|
+
/** An answer to a refresh that is no longer the current one. */
|
|
2781
|
+
function stale(state: TuiState, generation: number | undefined): boolean {
|
|
2782
|
+
return generation !== undefined && generation !== state.generation;
|
|
2783
|
+
}
|
|
2784
|
+
|
|
2785
|
+
/** The pure reducer: the same state and event always give the same state. */
|
|
2786
|
+
export function reduce(state: TuiState, event: TuiStateEvent): TuiState {
|
|
2787
|
+
switch (event.type) {
|
|
2788
|
+
case "types":
|
|
2789
|
+
return stale(state, event.generation)
|
|
2790
|
+
? state
|
|
2791
|
+
: applyTypes(state, event.types);
|
|
2792
|
+
case "pending":
|
|
2793
|
+
return stale(state, event.generation)
|
|
2794
|
+
? state
|
|
2795
|
+
: applyPending(state, event.items);
|
|
2796
|
+
case "running":
|
|
2797
|
+
return stale(state, event.generation)
|
|
2798
|
+
? state
|
|
2799
|
+
: applyRunning(state, event.items);
|
|
2800
|
+
case "page":
|
|
2801
|
+
if (event.replace === true && stale(state, event.generation)) return state;
|
|
2802
|
+
return appendPage(
|
|
2803
|
+
state,
|
|
2804
|
+
event.tabId,
|
|
2805
|
+
event.page.rows.map((row) => rowFromView(row, firstType(state, event.tabId))),
|
|
2806
|
+
event.page.next,
|
|
2807
|
+
event.replace === true,
|
|
2808
|
+
event.version,
|
|
2809
|
+
event.filter,
|
|
2810
|
+
event.page.hidden,
|
|
2811
|
+
event.filterSeq,
|
|
2812
|
+
);
|
|
2813
|
+
case "tree": {
|
|
2814
|
+
if (stale(state, event.generation)) return state;
|
|
2815
|
+
const rows = event.entries.flatMap((entry) => rowsOfEntry(state, entry));
|
|
2816
|
+
const next = appendPage(
|
|
2817
|
+
state,
|
|
2818
|
+
event.tabId,
|
|
2819
|
+
rows,
|
|
2820
|
+
undefined,
|
|
2821
|
+
true,
|
|
2822
|
+
undefined,
|
|
2823
|
+
event.filter,
|
|
2824
|
+
event.hidden,
|
|
2825
|
+
event.filterSeq,
|
|
2826
|
+
);
|
|
2827
|
+
if (next === state) return state;
|
|
2828
|
+
return withTab(next, event.tabId, (tab) => ({ ...tab, level: event.level }));
|
|
2829
|
+
}
|
|
2830
|
+
case "children":
|
|
2831
|
+
return applyChildren(
|
|
2832
|
+
state,
|
|
2833
|
+
event.objectId,
|
|
2834
|
+
event.version,
|
|
2835
|
+
event.entries,
|
|
2836
|
+
event.hidden,
|
|
2837
|
+
event.hiddenDone,
|
|
2838
|
+
event.hiddenCancelled,
|
|
2839
|
+
event.filter,
|
|
2840
|
+
event.filterSeq,
|
|
2841
|
+
);
|
|
2842
|
+
case "filter-launched":
|
|
2843
|
+
return state.filterRequest === undefined
|
|
2844
|
+
? state
|
|
2845
|
+
: { ...state, filterRequest: undefined };
|
|
2846
|
+
case "rows":
|
|
2847
|
+
return appendPage(
|
|
2848
|
+
state,
|
|
2849
|
+
event.tabId,
|
|
2850
|
+
event.rows.map((row) => rowFromView(row, firstType(state, event.tabId))),
|
|
2851
|
+
event.next,
|
|
2852
|
+
event.replace === true,
|
|
2853
|
+
undefined,
|
|
2854
|
+
);
|
|
2855
|
+
case "page-error":
|
|
2856
|
+
return applyPageError(state, event.tabId, event.message, event.version);
|
|
2857
|
+
case "object":
|
|
2858
|
+
return applyObject(state, event.object, event.version);
|
|
2859
|
+
case "object-error":
|
|
2860
|
+
return applyObjectError(state, event.id, event.message, event.version);
|
|
2861
|
+
case "section":
|
|
2862
|
+
return applySection(
|
|
2863
|
+
state,
|
|
2864
|
+
event.objectId,
|
|
2865
|
+
event.sectionId,
|
|
2866
|
+
event.content,
|
|
2867
|
+
event.cursor,
|
|
2868
|
+
event.full === true,
|
|
2869
|
+
event.version,
|
|
2870
|
+
event.objectVersion,
|
|
2871
|
+
event.pages,
|
|
2872
|
+
event.jump,
|
|
2873
|
+
event.marks,
|
|
2874
|
+
event.start,
|
|
2875
|
+
event.after === true,
|
|
2876
|
+
);
|
|
2877
|
+
case "section-error":
|
|
2878
|
+
return applySectionError(
|
|
2879
|
+
state,
|
|
2880
|
+
event.objectId,
|
|
2881
|
+
event.sectionId,
|
|
2882
|
+
event.message,
|
|
2883
|
+
event.version,
|
|
2884
|
+
event.objectVersion,
|
|
2885
|
+
);
|
|
2886
|
+
case "follow":
|
|
2887
|
+
return follow(state);
|
|
2888
|
+
case "refresh-start":
|
|
2889
|
+
return { ...state, generation: state.generation + 1 };
|
|
2890
|
+
case "page-launched":
|
|
2891
|
+
return state.pageRequest === undefined
|
|
2892
|
+
? state
|
|
2893
|
+
: { ...state, pageRequest: undefined };
|
|
2894
|
+
case "object-launched":
|
|
2895
|
+
return state.objectRequest === undefined
|
|
2896
|
+
? state
|
|
2897
|
+
: { ...state, objectRequest: undefined };
|
|
2898
|
+
case "section-launched":
|
|
2899
|
+
return state.sectionRequest === undefined
|
|
2900
|
+
? state
|
|
2901
|
+
: { ...state, sectionRequest: undefined };
|
|
2902
|
+
case "status":
|
|
2903
|
+
if (stale(state, event.generation)) return state;
|
|
2904
|
+
return {
|
|
2905
|
+
...state,
|
|
2906
|
+
status: event.message === undefined ? undefined : sanitize(event.message),
|
|
2907
|
+
};
|
|
2908
|
+
case "key":
|
|
2909
|
+
return handleKey(state, event);
|
|
2910
|
+
case "resize":
|
|
2911
|
+
return {
|
|
2912
|
+
...state,
|
|
2913
|
+
width: Math.max(1, Math.trunc(event.width)),
|
|
2914
|
+
height: Math.max(1, Math.trunc(event.height)),
|
|
2915
|
+
...(event.theme === undefined ? {} : { theme: event.theme }),
|
|
2916
|
+
};
|
|
2917
|
+
case "theme":
|
|
2918
|
+
return { ...state, theme: event.theme };
|
|
2919
|
+
case "keys":
|
|
2920
|
+
{
|
|
2921
|
+
if (stale(state, event.generation)) return state;
|
|
2922
|
+
const resolved = resolveBindings(event.bindings);
|
|
2923
|
+
return {
|
|
2924
|
+
...state,
|
|
2925
|
+
bindings: resolved.bindings,
|
|
2926
|
+
...(resolved.notice === undefined ? {} : { notice: resolved.notice }),
|
|
2927
|
+
};
|
|
2928
|
+
}
|
|
2929
|
+
case "theme-names":
|
|
2930
|
+
return {
|
|
2931
|
+
...state,
|
|
2932
|
+
themeNames: event.names,
|
|
2933
|
+
themeCurrent: event.current,
|
|
2934
|
+
// A theme file removed while the list is open must not leave the cursor past the end.
|
|
2935
|
+
...(state.input?.kind === "themes"
|
|
2936
|
+
? { input: { ...state.input, cursor: Math.min(state.input.cursor, Math.max(0, event.names.length - 1)) } }
|
|
2937
|
+
: {}),
|
|
2938
|
+
};
|
|
2939
|
+
case "theme-launched":
|
|
2940
|
+
return { ...state, themeRequest: undefined };
|
|
2941
|
+
case "layout-launched":
|
|
2942
|
+
return state.layoutRequest === undefined
|
|
2943
|
+
? state
|
|
2944
|
+
: { ...state, layoutRequest: undefined };
|
|
2945
|
+
case "action-launched":
|
|
2946
|
+
return state.actionRequest === undefined
|
|
2947
|
+
? state
|
|
2948
|
+
: { ...state, actionRequest: undefined };
|
|
2949
|
+
case "answer-launched":
|
|
2950
|
+
return state.answerRequest === undefined
|
|
2951
|
+
? state
|
|
2952
|
+
: { ...state, answerRequest: undefined };
|
|
2953
|
+
case "setting-launched":
|
|
2954
|
+
return { ...state, settingRequest: undefined, settingsRequest: false };
|
|
2955
|
+
case "settings":
|
|
2956
|
+
return { ...state, settings: event.settings };
|
|
2957
|
+
case "settings-objects": {
|
|
2958
|
+
if (stale(state, event.generation)) return state;
|
|
2959
|
+
const tab = state.settingsTab;
|
|
2960
|
+
if (tab === undefined) return state;
|
|
2961
|
+
return {
|
|
2962
|
+
...state,
|
|
2963
|
+
settingsTab: replaceRows(tab, event.entries.map(rowOfEntry), undefined),
|
|
2964
|
+
};
|
|
2965
|
+
}
|
|
2966
|
+
case "action-done": {
|
|
2967
|
+
const done: TuiState = {
|
|
2968
|
+
...(event.notice === undefined ? state : withNotice(state, event.notice)),
|
|
2969
|
+
acting: false,
|
|
2970
|
+
actionRequest: undefined,
|
|
2971
|
+
answerRequest: undefined,
|
|
2972
|
+
};
|
|
2973
|
+
return event.opens === undefined ? done : openNamed(done, event.opens);
|
|
2974
|
+
}
|
|
2975
|
+
case "action-error":
|
|
2976
|
+
return actionFailed(state, event.message);
|
|
2977
|
+
case "connection":
|
|
2978
|
+
return state.connection === event.connection
|
|
2979
|
+
? state
|
|
2980
|
+
: { ...state, connection: event.connection };
|
|
2981
|
+
case "row-gone":
|
|
2982
|
+
return removeRow(state, event.id);
|
|
2983
|
+
case "row":
|
|
2984
|
+
return replaceRow(state, rowFromView(event.row));
|
|
2985
|
+
case "refreshed":
|
|
2986
|
+
return state.refreshRequested
|
|
2987
|
+
? { ...state, refreshRequested: false }
|
|
2988
|
+
: state;
|
|
2989
|
+
}
|
|
2990
|
+
}
|
|
2991
|
+
|
|
2992
|
+
/** True when the terminal is wide enough for the list and the object pane. */
|
|
2993
|
+
export function twoPane(state: TuiState): boolean {
|
|
2994
|
+
// The list's width decides: the narrow column leaves room for the object
|
|
2995
|
+
// pane sooner than the terminal's former wider list did.
|
|
2996
|
+
const right = LIST_X + listWidth(state) + 3;
|
|
2997
|
+
return state.width - right - 1 >= MIN_OBJECT_WIDTH;
|
|
2998
|
+
}
|
|
2999
|
+
|
|
3000
|
+
/** The active tab, if any. */
|
|
3001
|
+
export function activeTab(state: TuiState): TuiTab | undefined {
|
|
3002
|
+
return state.tabs[state.active];
|
|
3003
|
+
}
|
|
3004
|
+
|
|
3005
|
+
/** The tab whose cursor the keys move. */
|
|
3006
|
+
export function activeRows(state: TuiState): readonly TuiRow[] {
|
|
3007
|
+
return activeTab(state)?.list.rows ?? [];
|
|
3008
|
+
}
|
|
3009
|
+
|
|
3010
|
+
/** The label a tab shows, with Pending's badge count. */
|
|
3011
|
+
export function tabLabel(tab: TuiTab): string {
|
|
3012
|
+
if (tab.kind === "pending" && tab.list.rows.length > 0) {
|
|
3013
|
+
return `${tab.title} ${tab.list.rows.length}`;
|
|
3014
|
+
}
|
|
3015
|
+
return tab.title;
|
|
3016
|
+
}
|
|
3017
|
+
|
|
3018
|
+
/** The header line, exactly as the design names it. */
|
|
3019
|
+
export function headerText(state: TuiState): string {
|
|
3020
|
+
const pending = state.tabs.find((tab) => tab.kind === "pending");
|
|
3021
|
+
const running = state.tabs.find((tab) => tab.kind === "running");
|
|
3022
|
+
const types = state.tabs
|
|
3023
|
+
.filter((tab) => tab.kind === "plugin")
|
|
3024
|
+
.map((tab) => tab.title);
|
|
3025
|
+
return `operad ─ ${[
|
|
3026
|
+
`Pending ${pending?.list.rows.length ?? 0}`,
|
|
3027
|
+
`Running ${running?.list.rows.length ?? 0}`,
|
|
3028
|
+
...types,
|
|
3029
|
+
].join(" · ")}`;
|
|
3030
|
+
}
|
|
3031
|
+
|
|
3032
|
+
/** One footer hint: its key, when it has one, and what the key does. */
|
|
3033
|
+
export interface TuiFooterSegment {
|
|
3034
|
+
readonly key?: string | undefined;
|
|
3035
|
+
readonly hint: string;
|
|
3036
|
+
}
|
|
3037
|
+
|
|
3038
|
+
/** The footer's hints in reading order; the keys are drawn in the text colour and the rest weak. */
|
|
3039
|
+
export function footerSegments(state: TuiState): readonly TuiFooterSegment[] {
|
|
3040
|
+
const segments: TuiFooterSegment[] = [];
|
|
3041
|
+
const add = (key: string | undefined, hint: string): void => {
|
|
3042
|
+
segments.push(key === undefined ? { hint } : { key, hint });
|
|
3043
|
+
};
|
|
3044
|
+
const object = state.object;
|
|
3045
|
+
const main = mainAction(state);
|
|
3046
|
+
if (object !== undefined && main !== undefined) {
|
|
3047
|
+
add(mainKey(state), main.name);
|
|
3048
|
+
if (pageActions(object.actions).length > 1) add(":", "more");
|
|
3049
|
+
}
|
|
3050
|
+
const filtering = filterKeyApplies(state);
|
|
3051
|
+
if (object === undefined) {
|
|
3052
|
+
add("j/k", "move");
|
|
3053
|
+
add("enter", "open");
|
|
3054
|
+
add("1-9", "tab");
|
|
3055
|
+
add("g/G", "ends");
|
|
3056
|
+
if (filtering) add("f", "filter");
|
|
3057
|
+
add("r", "refresh");
|
|
3058
|
+
add("?", "keys");
|
|
3059
|
+
add("q", "quit");
|
|
3060
|
+
} else if (twoPane(state)) {
|
|
3061
|
+
const section = activeSection(state);
|
|
3062
|
+
const capped = section !== undefined && section.capped && !section.full;
|
|
3063
|
+
add("j/k", "row");
|
|
3064
|
+
add("space/b", "scroll");
|
|
3065
|
+
add("[ ]", "section");
|
|
3066
|
+
if (pagePosition(section) !== undefined) {
|
|
3067
|
+
add("p", "pages");
|
|
3068
|
+
add("n/N", "page");
|
|
3069
|
+
}
|
|
3070
|
+
add("f", filtering && !capped ? "filter" : "full");
|
|
3071
|
+
if (object.children !== undefined) add("t", state.childLayout === "tree" ? "list" : "tree");
|
|
3072
|
+
add("esc", "close");
|
|
3073
|
+
add("?", "keys");
|
|
3074
|
+
add("q", "quit");
|
|
3075
|
+
} else {
|
|
3076
|
+
add("j/k", "scroll");
|
|
3077
|
+
add("space/b", "page");
|
|
3078
|
+
add("g/G", "ends");
|
|
3079
|
+
add("[ ]", "section");
|
|
3080
|
+
if (pagePosition(activeSection(state)) !== undefined) {
|
|
3081
|
+
add("p", "pages");
|
|
3082
|
+
add("n/N", "page");
|
|
3083
|
+
}
|
|
3084
|
+
add("f", "full");
|
|
3085
|
+
if (object.children !== undefined) add("t", state.childLayout === "tree" ? "list" : "tree");
|
|
3086
|
+
add("esc", "back");
|
|
3087
|
+
add("?", "keys");
|
|
3088
|
+
add("q", "quit");
|
|
3089
|
+
}
|
|
3090
|
+
if (state.keys) add("?", "closes the key list");
|
|
3091
|
+
return segments;
|
|
3092
|
+
}
|
|
3093
|
+
|
|
3094
|
+
/** The footer for the current screen. */
|
|
3095
|
+
export function footerText(state: TuiState): string {
|
|
3096
|
+
return footerSegments(state)
|
|
3097
|
+
.map((segment) => (segment.key === undefined ? segment.hint : `${segment.key} ${segment.hint}`))
|
|
3098
|
+
.join(" · ");
|
|
3099
|
+
}
|
|
3100
|
+
|
|
3101
|
+
/** Groups of keys, shown with a blank line between them. */
|
|
3102
|
+
export const KEY_GROUPS: readonly (readonly (readonly [string, string])[])[] = [
|
|
3103
|
+
[
|
|
3104
|
+
["1–9", "open that tab (closes the open object)"],
|
|
3105
|
+
["tab / shift-tab", "next or previous tab"],
|
|
3106
|
+
],
|
|
3107
|
+
[
|
|
3108
|
+
["↑↓ / j k", "move the row; on a narrow screen with an object open, scroll it"],
|
|
3109
|
+
["g / G", "first or last row; on a narrow screen with an object open, its top or bottom"],
|
|
3110
|
+
["enter", "open the selected row (on a narrow screen); on the open row, move into its list; there, open the row"],
|
|
3111
|
+
],
|
|
3112
|
+
[
|
|
3113
|
+
["PgUp PgDn / b space", "scroll the open section a page"],
|
|
3114
|
+
["Home / End", "top of the open section, or its end (follows a live one)"],
|
|
3115
|
+
["[ ] / ← →", "the previous or next section"],
|
|
3116
|
+
],
|
|
3117
|
+
[
|
|
3118
|
+
["p", "pick a page of the open section (when it has pages)"],
|
|
3119
|
+
["n / N", "the next or previous page of the open section"],
|
|
3120
|
+
["f", "load the whole open section when it is capped; otherwise, on a tab with filters, the next filter"],
|
|
3121
|
+
["t", "switch the child list between tree and list"],
|
|
3122
|
+
],
|
|
3123
|
+
[
|
|
3124
|
+
["c", "on a tab of chats: start a new one; the palette lists where (a repository or a bot)"],
|
|
3125
|
+
["r", "fetch the lists again"],
|
|
3126
|
+
["escape / backspace", "close the key list, leave a list inside the open object, return from a row opened there, or close the object"],
|
|
3127
|
+
["?", "show or hide this key list"],
|
|
3128
|
+
["q", "quit"],
|
|
3129
|
+
],
|
|
3130
|
+
];
|
|
3131
|
+
|
|
3132
|
+
/** Every key the TUI reads, in the order the key list shows them. */
|
|
3133
|
+
export const KEY_LIST: readonly (readonly [string, string])[] = KEY_GROUPS.flat();
|
|
3134
|
+
|
|
3135
|
+
/** The rows the full key list takes: a heading, its keys, and a blank line between groups. */
|
|
3136
|
+
export const HELP_LINES = KEY_LIST.length + KEY_GROUPS.length;
|
|
3137
|
+
|
|
3138
|
+
/** The key list rows the terminal shows at once; a short terminal scrolls the rest. */
|
|
3139
|
+
export function helpViewport(state: TuiState): number {
|
|
3140
|
+
return Math.max(1, Math.min(HELP_LINES + 4, state.height) - 4);
|
|
3141
|
+
}
|
|
3142
|
+
|
|
3143
|
+
/** The lines one object takes in a list: two, and the blank line after them. */
|
|
3144
|
+
export const ROW_LINES = 3;
|
|
3145
|
+
|
|
3146
|
+
/** The lines one row takes; an error row shows its text elsewhere. */
|
|
3147
|
+
export function rowLines(row: TuiRow): number {
|
|
3148
|
+
return ROW_LINES * (row.group === undefined ? 1 : 2);
|
|
3149
|
+
}
|
|
3150
|
+
|
|
3151
|
+
/**
|
|
3152
|
+
* The rows `[start, end)` that fit in `available` lines around the cursor. The
|
|
3153
|
+
* lines after the rows (`loading more…` and a page error) are kept free.
|
|
3154
|
+
*/
|
|
3155
|
+
export function listWindow(
|
|
3156
|
+
list: TuiList,
|
|
3157
|
+
available: number,
|
|
3158
|
+
): { readonly start: number; readonly end: number } {
|
|
3159
|
+
const count = list.rows.length;
|
|
3160
|
+
if (count === 0) return { start: 0, end: 0 };
|
|
3161
|
+
const tail = (list.loading ? 1 : 0) + (list.error === undefined ? 0 : 1);
|
|
3162
|
+
// The blank line after the last object is not drawn, so it needs no room.
|
|
3163
|
+
const room = Math.max(1, Math.trunc(available) - tail + 1);
|
|
3164
|
+
const cursor = Math.min(Math.max(0, list.cursor), count - 1);
|
|
3165
|
+
let start = cursor;
|
|
3166
|
+
let end = cursor + 1;
|
|
3167
|
+
let used = rowLines(list.rows[cursor] as TuiRow);
|
|
3168
|
+
for (;;) {
|
|
3169
|
+
let grew = false;
|
|
3170
|
+
const after = list.rows[end];
|
|
3171
|
+
if (after !== undefined && used + rowLines(after) <= room) {
|
|
3172
|
+
used += rowLines(after);
|
|
3173
|
+
end += 1;
|
|
3174
|
+
grew = true;
|
|
3175
|
+
}
|
|
3176
|
+
const before = list.rows[start - 1];
|
|
3177
|
+
if (before !== undefined && used + rowLines(before) <= room) {
|
|
3178
|
+
used += rowLines(before);
|
|
3179
|
+
start -= 1;
|
|
3180
|
+
grew = true;
|
|
3181
|
+
}
|
|
3182
|
+
if (!grew) return { start, end };
|
|
3183
|
+
}
|
|
3184
|
+
}
|
|
3185
|
+
|
|
3186
|
+
/** The red footer line an action leaves: a confirmation, or a refusal. */
|
|
3187
|
+
export function noticeText(state: TuiState): string | undefined {
|
|
3188
|
+
return confirmText(state) ?? state.notice;
|
|
3189
|
+
}
|
|
3190
|
+
|
|
3191
|
+
/** The footer when a failure is showing: the reason, then the way out. */
|
|
3192
|
+
export function statusText(state: TuiState): string | undefined {
|
|
3193
|
+
if (state.connection === "unreachable") {
|
|
3194
|
+
return `no server at ${state.url ?? "the address"}: is \`operad serve\` running? retrying q quit`;
|
|
3195
|
+
}
|
|
3196
|
+
if (state.connection === "unauthorized") {
|
|
3197
|
+
return "the token was rotated: run the TUI again or pass --token q quit";
|
|
3198
|
+
}
|
|
3199
|
+
return state.status === undefined ? undefined : `${state.status} q quit`;
|
|
3200
|
+
}
|