ltms-reader 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +136 -0
- package/LICENSE +21 -0
- package/LICENSE-UNICODE +49 -0
- package/README.md +284 -2
- package/bin/ltms-reader.mjs +16 -0
- package/package.json +32 -4
- package/src/app.mjs +415 -0
- package/src/bidi-table.mjs +354 -0
- package/src/bidi.mjs +251 -0
- package/src/engine.mjs +505 -0
- package/src/guard.mjs +116 -0
- package/src/helper.mjs +127 -0
- package/src/layout.mjs +181 -0
- package/src/lines.mjs +18 -0
- package/src/main.mjs +133 -0
- package/src/reading.mjs +165 -0
- package/src/screen-process.mjs +6 -0
- package/src/screen.mjs +127 -0
- package/src/sources.mjs +134 -0
- package/src/start.mjs +123 -0
- package/src/text.mjs +712 -0
- package/src/view.mjs +337 -0
- package/src/views.mjs +518 -0
- package/src/width-table.mjs +101 -0
package/src/app.mjs
ADDED
|
@@ -0,0 +1,415 @@
|
|
|
1
|
+
// src/app.mjs — the reader's state, and what each key does to it. No terminal in it: the screen (src/screen.mjs) passes
|
|
2
|
+
// each key in and draws render(state); the tests drive it the same way.
|
|
3
|
+
//
|
|
4
|
+
// The state is a stack of views: the bottom one is Projects (a folder of projects) or the session (one transcript);
|
|
5
|
+
// Enter pushes the row's view, Esc pops it. A search or an address pushes what it opens on top of where it was, so Esc
|
|
6
|
+
// goes back there; r reads the list on screen again, Projects or a project's Sessions (Y2, round 6). Lists come from the
|
|
7
|
+
// source (src/sources.mjs): the projects at once, a project's sessions by ltms index and a search by ltms search, each
|
|
8
|
+
// while the screen says what is being read; a session is read in this process, after a frame that says which file is
|
|
9
|
+
// being read.
|
|
10
|
+
import { recordTooLarge } from './reading.mjs';
|
|
11
|
+
import { clusters } from './text.mjs';
|
|
12
|
+
import { recordIndexAt, recordView, recordViewBytes } from './view.mjs';
|
|
13
|
+
import { bodyHeight, coverageText, headOf, recordLines, render, top, topOf } from './views.mjs';
|
|
14
|
+
|
|
15
|
+
/** An address as ltms prints it: a prefix (8 characters of a session's file name, or agent- and 8 of an agent's id)
|
|
16
|
+
* and a line (L) or a turn (T). */
|
|
17
|
+
export const ADDRESS = /^(agent-[0-9A-Za-z._-]{1,8}|[0-9A-Za-z._-]{1,8}):([LT])(\d{1,9})$/;
|
|
18
|
+
const MAX_INPUT = 2000;
|
|
19
|
+
/** The turns a search shows at a time. */
|
|
20
|
+
export const SEARCH_MAX = 100;
|
|
21
|
+
/** What the screen says of an error: a refusal's own words (they name paths and sizes), a system error's code and path,
|
|
22
|
+
* else only its kind: its message could quote a transcript. */
|
|
23
|
+
const message = (e) => {
|
|
24
|
+
if (e && typeof e.message === 'string' && e.kind) return e.message;
|
|
25
|
+
if (typeof e?.code === 'string' && typeof e?.syscall === 'string') return `${typeof e.path === 'string' ? `${e.path}: ` : ''}${e.code} (${e.syscall})`;
|
|
26
|
+
return `${e?.name ?? 'Error'} while reading (an internal error; its message is not shown, since it could quote a transcript)`;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/** The rows of a session view: each turn, and after it each compaction in it (with the summary that follows it); the
|
|
30
|
+
* compactions before the first prompt first. */
|
|
31
|
+
export function sessionRows(view) {
|
|
32
|
+
const rows = [];
|
|
33
|
+
const pairs = (items, lead) => {
|
|
34
|
+
const out = [];
|
|
35
|
+
items.forEach((x, i) => {
|
|
36
|
+
if (x.kind === 'compaction') {
|
|
37
|
+
const next = items.slice(i + 1).find((y) => y.kind === 'compaction' || y.kind === 'summary');
|
|
38
|
+
const s = next?.kind === 'summary' ? next : null;
|
|
39
|
+
out.push({ kind: 'compaction', a: x.a, line: x.line, summary: s?.a ?? null, sline: s?.line ?? null, trigger: x.trigger, pre: x.pre, post: x.post, time: x.time, by: x.by?.ta ?? null, lead });
|
|
40
|
+
} else if (x.kind === 'summary' && !(items[i - 1]?.kind === 'compaction' || out.some((r) => r.summary === x.a))) {
|
|
41
|
+
out.push({ kind: 'summary', a: x.a, line: x.line, text: x.text, more: x.more ?? 0, time: x.time, lead });
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
return out;
|
|
45
|
+
};
|
|
46
|
+
rows.push(...pairs(view.lead, true));
|
|
47
|
+
view.turns.forEach((t, i) => {
|
|
48
|
+
rows.push({ kind: 'turn', index: i, ta: t.ta, a: t.a, line: t.line, ch: t.ch, calls: t.calls, failed: t.failed, prompt: t.prompt.text, promptMore: t.prompt.more, command: t.command, n: t.n, time: t.time, from: t.from, to: t.to, records: t.records });
|
|
49
|
+
rows.push(...pairs(t.items, false));
|
|
50
|
+
});
|
|
51
|
+
return rows;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** The rows of a Sessions view: one per session `ltms index` lists (src/engine.mjs index), newest first. */
|
|
55
|
+
export const sessionsRows = (d) => d.sessions.map((s) => ({ kind: 'session', p: s.p, first: s.first, last: s.last, turns: s.turns, calls: s.calls, failures: s.failures, compactions: Array.isArray(s.compactions) ? s.compactions.length : 0, agents: s.agents, file: s.file, firstPrompt: s.firstPrompt, lastReply: s.lastReply, copies: s.copies?.length ?? 0 }));
|
|
56
|
+
|
|
57
|
+
/** Where the selection goes in a list read again: the row `same` finds, when there is one; else the place it was at,
|
|
58
|
+
* within the list (Y2, round 6). */
|
|
59
|
+
const keptAt = (rows, same, at) => { const i = rows.findIndex(same); return i >= 0 ? i : Math.max(0, Math.min(at, rows.length - 1)); };
|
|
60
|
+
|
|
61
|
+
/** The rows of a turn view: its prompt, then its items in the order they were written; a call's result right after it. */
|
|
62
|
+
export function turnRows(t) {
|
|
63
|
+
const rows = [{ kind: 'prompt', a: t.a, line: t.line, ch: t.ch, n: t.n, text: t.prompt.text, more: t.prompt.more, command: t.command, time: t.time }];
|
|
64
|
+
t.items.forEach((x, i) => {
|
|
65
|
+
if (x.kind === 'call') {
|
|
66
|
+
rows.push({ kind: 'call', item: x, a: x.a, line: x.line, tool: x.tool, ok: x.ok, inputKey: x.input.key, inputText: x.input.text, inputMore: x.input.more, r: x.r, time: x.time });
|
|
67
|
+
if (x.r) rows.push({ kind: 'result', call: x, a: x.r, line: x.rline, n: x.n, text: x.result?.text ?? '', more: x.result?.more ?? 0, of: x.a, ok: x.ok });
|
|
68
|
+
} else if (x.kind === 'compaction') {
|
|
69
|
+
const next = t.items.slice(i + 1).find((y) => y.kind === 'compaction' || y.kind === 'summary');
|
|
70
|
+
rows.push({ kind: 'compaction', a: x.a, line: x.line, trigger: x.trigger, pre: x.pre, post: x.post, summary: next?.kind === 'summary' ? next.a : null, time: x.time, by: x.by?.ta ?? null });
|
|
71
|
+
} else {
|
|
72
|
+
rows.push({ kind: x.kind, a: x.a, line: x.line, text: x.text, more: x.more ?? 0, sub: x.sub, command: x.command ?? null, time: x.time });
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
return rows;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** The rows of a search's page: its coverage and notices, then each file with a hit and its matching turns. */
|
|
79
|
+
export function searchRows(result) {
|
|
80
|
+
const rows = [{ kind: 'info', text: coverageText(result.coverage) }];
|
|
81
|
+
const notices = String(result.stderr ?? '').split('\n').map((l) => l.trim()).filter((l) => l !== '' && !/^ltms search: \d+ matching turns? in /.test(l));
|
|
82
|
+
for (const n of notices) rows.push({ kind: 'info', text: n });
|
|
83
|
+
const byF = new Map(result.files.map((f) => [f.f, f]));
|
|
84
|
+
let last = null;
|
|
85
|
+
for (const h of result.hits) {
|
|
86
|
+
const f = byF.get(h.f);
|
|
87
|
+
if (!f) continue;
|
|
88
|
+
if (f !== last) { rows.push({ kind: 'file', f: f.f, p: f.p, project: f.project, file: f.file, last: f.last, copies: f.copies?.length ?? 0 }); last = f; }
|
|
89
|
+
rows.push({ kind: 'hit', ta: h.ta, ch: h.ch, at: Array.isArray(h.at) ? h.at : [], project: f.project, file: f.file });
|
|
90
|
+
}
|
|
91
|
+
const c = result.coverage, shown = c?.shown?.turns ?? result.hits.length, left = c?.left ?? 0;
|
|
92
|
+
if (left > 0) rows.push({ kind: 'more', skip: result.skip + shown, next: Math.min(left, result.max), left });
|
|
93
|
+
return rows;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export class App {
|
|
97
|
+
/**
|
|
98
|
+
* source: a RootSource or a FileSource. cols, rows: the screen's size. timeZone: for the times shown (this machine's
|
|
99
|
+
* when undefined). onChange(): the state changed, draw it. onExit(code): a key or a command asked to quit.
|
|
100
|
+
*/
|
|
101
|
+
constructor({ source, cols = 80, rows = 24, timeZone, now = Date.now, onChange = () => {}, onExit = () => {} }) {
|
|
102
|
+
Object.assign(this, { source, now, onChange, onExit });
|
|
103
|
+
this.state = { cols, rows, stack: [], input: null, help: false, note: null, timeZone, now: now() };
|
|
104
|
+
this.pending = new Set();
|
|
105
|
+
this.tick = null;
|
|
106
|
+
this.doc = null; // the transcript read last: { key, doc }
|
|
107
|
+
this.quitting = null;
|
|
108
|
+
this.pasting = false; // between a paste's start and its end
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** The first views: Projects (and, when one project was given, its sessions), or the one transcript's session. */
|
|
112
|
+
start({ project = null } = {}) {
|
|
113
|
+
if (this.source.kind === 'file') this.openSession(null, this.source.name);
|
|
114
|
+
else {
|
|
115
|
+
this.pushProjects();
|
|
116
|
+
if (project !== null) this.pushSessions(project);
|
|
117
|
+
}
|
|
118
|
+
this.changed();
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
lines() { return render(this.state, this.state.cols, this.state.rows); }
|
|
122
|
+
resize(cols, rows) { this.state.cols = cols; this.state.rows = rows; this.changed(); }
|
|
123
|
+
changed() { this.state.now = this.now(); this.onChange(); }
|
|
124
|
+
|
|
125
|
+
/** Resolves when nothing is being read. */
|
|
126
|
+
async idle() { while (this.pending.size) await Promise.allSettled([...this.pending]); }
|
|
127
|
+
|
|
128
|
+
track(p) {
|
|
129
|
+
const done = p.finally(() => {
|
|
130
|
+
this.pending.delete(done);
|
|
131
|
+
if (!this.pending.size && this.tick) { clearInterval(this.tick); this.tick = null; }
|
|
132
|
+
this.changed();
|
|
133
|
+
});
|
|
134
|
+
this.pending.add(done);
|
|
135
|
+
if (!this.tick) { this.tick = setInterval(() => this.changed(), 1000); this.tick.unref?.(); }
|
|
136
|
+
return done;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
dispose() { if (this.tick) clearInterval(this.tick); this.tick = null; }
|
|
140
|
+
|
|
141
|
+
// ---- views ----------------------------------------------------------------------------------------------------------
|
|
142
|
+
push(v) { this.state.stack.push({ sel: 0, top: 0, ...v }); return top(this.state); }
|
|
143
|
+
|
|
144
|
+
pushProjects() {
|
|
145
|
+
let data;
|
|
146
|
+
try { data = this.source.projects(); } catch (e) { this.push({ kind: 'projects', data: null, error: message(e), rootShown: this.source.rootShown, rows: [] }); return; }
|
|
147
|
+
this.push({ kind: 'projects', data, rows: data.projects.map((p) => ({ kind: 'project', ...p })) });
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
pushSessions(project) {
|
|
151
|
+
const v = this.push({ kind: 'sessions', project, data: null, rows: [], since: this.now() });
|
|
152
|
+
let p;
|
|
153
|
+
try { p = Promise.resolve(this.source.sessions(project)); } catch (e) { p = Promise.reject(e); }
|
|
154
|
+
this.track(p.then((d) => { v.data = d; v.rows = sessionsRows(d); }, (e) => { v.error = message(e); }));
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* r (Y2, round 6, his F2: a list was read once, and a project whose files changed showed the list as it was read until
|
|
159
|
+
* the reader started again): the list on screen read again, Projects by the projects folder's walk, a project's
|
|
160
|
+
* Sessions by `ltms index` run afresh (a list kept from an earlier reading too), its view saying what is being read
|
|
161
|
+
* meanwhile, as when it was opened; the selection on the same row when it is still there (a project by its name, a
|
|
162
|
+
* session by its file), else at the place it was. On another view, or while a Sessions list is being read, a note.
|
|
163
|
+
*/
|
|
164
|
+
reread(v) {
|
|
165
|
+
if (v.kind === 'projects') {
|
|
166
|
+
const name = v.rows[v.sel]?.name;
|
|
167
|
+
let data;
|
|
168
|
+
try { data = this.source.projects(); } catch (e) { Object.assign(v, { data: null, error: message(e), rootShown: this.source.rootShown, rows: [], sel: 0 }); return; }
|
|
169
|
+
const rows = data.projects.map((p) => ({ kind: 'project', ...p }));
|
|
170
|
+
Object.assign(v, { data, error: null, rows, sel: keptAt(rows, (x) => x.name === name, v.sel) });
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
if (v.kind === 'sessions') {
|
|
174
|
+
if (!v.data && !v.error) { this.note(`the sessions of ${v.project} are being read`); return; }
|
|
175
|
+
const file = v.rows[v.sel]?.file, at = v.sel;
|
|
176
|
+
Object.assign(v, { data: null, error: null, rows: [], since: this.now() });
|
|
177
|
+
let p;
|
|
178
|
+
try { p = Promise.resolve(this.source.sessions(v.project, { fresh: true })); } catch (e) { p = Promise.reject(e); }
|
|
179
|
+
this.track(p.then((d) => { v.data = d; v.rows = sessionsRows(d); v.sel = keptAt(v.rows, (x) => x.file === file, at); }, (e) => { v.error = message(e); }));
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
this.note('r reads a list again: Projects, or a project\'s Sessions');
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/** The transcript, read in this process: the one read last when it is the same file, unchanged. */
|
|
186
|
+
readDoc(project, file) {
|
|
187
|
+
const key = `${project}\0${file}`;
|
|
188
|
+
const d = this.doc?.key === key ? this.doc.doc : null;
|
|
189
|
+
if (d && this.source.unchanged?.(d)) return d;
|
|
190
|
+
const doc = this.source.open(project, file);
|
|
191
|
+
this.doc = { key, doc };
|
|
192
|
+
if (this.source.errorClasses) {
|
|
193
|
+
this.track(this.source.errorClasses(doc).then((ek) => { doc.ek = ek; }, () => { doc.ekError = true; }));
|
|
194
|
+
}
|
|
195
|
+
return doc;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/** Push a session's view; it is read after a frame that says so. `then` opens a turn in it once read ({ ta }, with the
|
|
199
|
+
* row on line `at` selected) or a record ({ line }, above the turn that holds it). On the session's own view, `then`
|
|
200
|
+
* opens there, without a second copy of the view. */
|
|
201
|
+
openSession(project, file, then = null) {
|
|
202
|
+
const here = top(this.state);
|
|
203
|
+
if (then && here?.kind === 'session' && here.project === project && here.file === file && here.doc) { this.then(here, then); this.changed(); return here; }
|
|
204
|
+
const v = this.push({ kind: 'session', project, file, doc: null, rows: [], since: this.now() });
|
|
205
|
+
this.track(new Promise((resolve) => setImmediate(resolve)).then(() => {
|
|
206
|
+
const doc = this.readDoc(project, file);
|
|
207
|
+
v.doc = doc;
|
|
208
|
+
v.rows = sessionRows(doc.view);
|
|
209
|
+
if (then && top(this.state) === v) this.then(v, then);
|
|
210
|
+
}).catch((e) => { v.error = message(e); }));
|
|
211
|
+
this.changed();
|
|
212
|
+
return v;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/** In a session read: open the turn named, or the record on a line (in the turn that holds it, when one does). */
|
|
216
|
+
then(sv, { ta = null, line = null, at = null }) {
|
|
217
|
+
const doc = sv.doc, turns = doc.view.turns;
|
|
218
|
+
const holds = (t, n) => t.line === n || t.items.some((x) => x.line === n || x.rline === n);
|
|
219
|
+
let ti = ta !== null ? turns.findIndex((t) => t.ta === ta) : turns.findIndex((t) => holds(t, line));
|
|
220
|
+
if (ta !== null && ti < 0) { this.note(`${ta} is not a turn of ${doc.name} (it has ${turns.length})`, true); return; }
|
|
221
|
+
if (ti >= 0) {
|
|
222
|
+
sv.sel = Math.max(0, sv.rows.findIndex((r) => r.kind === 'turn' && r.index === ti));
|
|
223
|
+
const tv = this.pushTurn(sv, ti);
|
|
224
|
+
const n = line ?? at;
|
|
225
|
+
if (n !== null) { const k = tv.rows.findIndex((r) => r.line === n); if (k >= 0) tv.sel = k; }
|
|
226
|
+
}
|
|
227
|
+
if (line !== null) this.pushRecord(sv.project, sv.file, doc, line);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
pushTurn(sv, i) {
|
|
231
|
+
const t = sv.doc.view.turns[i];
|
|
232
|
+
return this.push({ kind: 'turn', project: sv.project, file: sv.file, doc: sv.doc, turn: t, rows: turnRows(t) });
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/** Push the record on a line: refused, before anything of it is laid out, when its view would not fit in this
|
|
236
|
+
* process's memory beside the transcript's parse (src/reading.mjs recordTooLarge), by its line's size, then by the
|
|
237
|
+
* JSON its view would write, measured by walking the record (view.mjs recordViewBytes, N3; walked only when its line
|
|
238
|
+
* passes): the view says so, with the NODE_OPTIONS that would let it be shown. */
|
|
239
|
+
pushRecord(project, file, doc, line) {
|
|
240
|
+
const i = recordIndexAt(doc.t, line);
|
|
241
|
+
if (i === undefined) { this.note(`no record was parsed on line ${line} of ${doc.name}: the line is blank, not a JSON object, or past the end of the file`, true); return null; }
|
|
242
|
+
const a = `${doc.prefix}:L${line}`;
|
|
243
|
+
const { heapLimit, again } = this.source.reading ?? {}, bytes = doc.t.positions[i].bytes;
|
|
244
|
+
const why = recordTooLarge(bytes, doc.size, heapLimit, again) ?? recordTooLarge(bytes, doc.size, heapLimit, again, recordViewBytes(doc.t, line));
|
|
245
|
+
if (why) return this.push({ kind: 'record', project, file, doc, line, a, rec: null, json: false, error: `${a} is not shown: ${why}` });
|
|
246
|
+
const rec = recordView(doc.t, line, { name: doc.name, whole: true });
|
|
247
|
+
return this.push({ kind: 'record', project, file, doc, line, a, rec, json: false });
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
pushSearch(q) {
|
|
251
|
+
const where = this.scope();
|
|
252
|
+
const v = this.push({ kind: 'search', q, project: where.project, scopeShown: where.shown, skip: 0, max: SEARCH_MAX, result: null, rows: [], since: this.now() });
|
|
253
|
+
this.runSearch(v);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
runSearch(v) {
|
|
257
|
+
v.result = null; v.error = null; v.rows = []; v.since = this.now();
|
|
258
|
+
let p;
|
|
259
|
+
try { p = Promise.resolve(this.source.search({ project: v.project, q: v.q, skip: v.skip, max: v.max })); } catch (e) { p = Promise.reject(e); }
|
|
260
|
+
this.track(p.then((r) => {
|
|
261
|
+
v.result = r; v.rows = searchRows(r);
|
|
262
|
+
// the first matching turn selected, so that Enter opens it at its first match (R.F7: the session's row above it
|
|
263
|
+
// was selected, and Enter opened the session)
|
|
264
|
+
const hit = v.rows.findIndex((x) => x.kind === 'hit');
|
|
265
|
+
v.sel = hit >= 0 ? hit : Math.max(0, v.rows.findIndex((x) => x.kind !== 'info')); v.top = 0;
|
|
266
|
+
}, (e) => { v.error = message(e); }));
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/** What a search covers from here: the project of the view on top (every project from Projects), or the one file. */
|
|
270
|
+
scope() {
|
|
271
|
+
if (this.source.kind === 'file') return { project: null, shown: `in ${this.source.name}` };
|
|
272
|
+
const v = top(this.state);
|
|
273
|
+
const project = v.kind === 'projects' ? null : v.project ?? null;
|
|
274
|
+
return { project, shown: project === null ? 'in every project' : `in ${project}` };
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
note(text, bad = false) { this.state.note = { text, bad }; }
|
|
278
|
+
|
|
279
|
+
// ---- keys -----------------------------------------------------------------------------------------------------------
|
|
280
|
+
/** One keypress, as readline's emitKeypressEvents gives it: the string, and { name, ctrl, meta, shift, sequence }. A
|
|
281
|
+
* paste (bracketed by the terminal: readline's paste-start and paste-end) is text: into the line being typed, its new
|
|
282
|
+
* lines and tabs as spaces, never submitting it; with no line being typed, nothing (R.F8). Ctrl-C quits even then. */
|
|
283
|
+
key(str, k = {}) {
|
|
284
|
+
if (this.quitting !== null) return;
|
|
285
|
+
if (k.ctrl && k.name === 'c') return this.exit(130);
|
|
286
|
+
if (k.name === 'paste-start' || k.name === 'paste-end') { this.pasting = k.name === 'paste-start'; return this.changed(); }
|
|
287
|
+
if (this.pasting) {
|
|
288
|
+
if (this.state.input && typeof str === 'string' && !k.ctrl && !k.meta) this.inputKey(str.replace(/[\r\n\t]/g, ' '), {});
|
|
289
|
+
return this.changed();
|
|
290
|
+
}
|
|
291
|
+
this.state.note = null;
|
|
292
|
+
if (this.state.input) { this.inputKey(str, k); return this.changed(); }
|
|
293
|
+
// the keys screen: q quits there too (R.F11), any other key goes back
|
|
294
|
+
if (this.state.help) { if (k.name === 'q' && !k.ctrl && !k.meta) return this.exit(0); this.state.help = false; return this.changed(); }
|
|
295
|
+
const v = top(this.state);
|
|
296
|
+
const name = k.name;
|
|
297
|
+
// a key with Ctrl does nothing here (but Ctrl-C, above); with Alt (meta), a named key is the key itself: two Esc
|
|
298
|
+
// pressed at once arrive as one Esc with meta
|
|
299
|
+
if (k.ctrl) return this.changed();
|
|
300
|
+
if (name === 'up') this.move(v, -1);
|
|
301
|
+
else if (name === 'down') this.move(v, 1);
|
|
302
|
+
else if (name === 'pageup') this.move(v, -this.page(v));
|
|
303
|
+
else if (name === 'pagedown') this.move(v, this.page(v));
|
|
304
|
+
else if (name === 'home') this.move(v, -Infinity);
|
|
305
|
+
else if (name === 'end') this.move(v, Infinity);
|
|
306
|
+
else if (name === 'return' || name === 'enter' || name === 'right') this.open(v);
|
|
307
|
+
else if (name === 'escape' || name === 'left') this.back();
|
|
308
|
+
else if (k.meta) { /* Alt and a character: nothing */ }
|
|
309
|
+
else if (name === 'q') return this.exit(0);
|
|
310
|
+
else if (str === '/') this.state.input = { kind: 'search', text: '', scopeShown: this.scope().shown };
|
|
311
|
+
else if (str === ':') this.state.input = { kind: 'address', text: '' };
|
|
312
|
+
else if (str === '?') this.state.help = true;
|
|
313
|
+
else if (str === 'j' && v.kind === 'record' && v.rec) { v.json = !v.json; v.top = 0; }
|
|
314
|
+
else if (str === 'r') this.reread(v);
|
|
315
|
+
this.changed();
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/** A page: the rows of the list in sight, less one (the lines above a list are more than one in a Sessions view). */
|
|
319
|
+
page(v) { return Math.max(1, bodyHeight(this.state.rows, headed(v), headOf(this.state, v)) - 1); }
|
|
320
|
+
|
|
321
|
+
move(v, delta) {
|
|
322
|
+
if (v.kind === 'record') {
|
|
323
|
+
if (!v.rec) return;
|
|
324
|
+
const n = recordLines(v.rec, v.json, this.state.cols).length, h = bodyHeight(this.state.rows, false);
|
|
325
|
+
v.top = Math.max(0, Math.min(Math.max(0, n - h), delta === -Infinity ? 0 : delta === Infinity ? n : (v.top ?? 0) + delta));
|
|
326
|
+
return;
|
|
327
|
+
}
|
|
328
|
+
const rows = v.rows ?? [];
|
|
329
|
+
if (!rows.length) return;
|
|
330
|
+
const ok = (i) => rows[i] && rows[i].kind !== 'info';
|
|
331
|
+
let i = delta === -Infinity ? 0 : delta === Infinity ? rows.length - 1 : Math.max(0, Math.min(rows.length - 1, v.sel + delta));
|
|
332
|
+
const dir = delta < 0 ? -1 : 1;
|
|
333
|
+
while (rows[i] && !ok(i)) i += dir;
|
|
334
|
+
if (!ok(i)) { i = Math.max(0, Math.min(rows.length - 1, i)); while (rows[i] && !ok(i)) i -= dir; }
|
|
335
|
+
if (!ok(i)) return;
|
|
336
|
+
v.sel = i;
|
|
337
|
+
v.top = topOf(v, rows.length, bodyHeight(this.state.rows, headed(v), headOf(this.state, v)));
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
open(v) {
|
|
341
|
+
const r = v.rows?.[v.sel];
|
|
342
|
+
if (!r) return;
|
|
343
|
+
try {
|
|
344
|
+
if (v.kind === 'projects') { if (!r.unreadable) this.pushSessions(r.name); else this.note(`${r.name} cannot be listed (${r.unreadable})`, true); }
|
|
345
|
+
else if (v.kind === 'sessions') this.openSession(v.project, r.file);
|
|
346
|
+
else if (v.kind === 'session') {
|
|
347
|
+
if (!v.doc) return;
|
|
348
|
+
if (r.kind === 'turn') this.pushTurn(v, r.index);
|
|
349
|
+
else this.pushRecord(v.project, v.file, v.doc, r.kind === 'compaction' ? r.sline ?? r.line : r.line);
|
|
350
|
+
} else if (v.kind === 'turn') this.pushRecord(v.project, v.file, v.doc, r.line);
|
|
351
|
+
else if (v.kind === 'search') {
|
|
352
|
+
if (r.kind === 'file') this.openSession(r.project, r.file);
|
|
353
|
+
else if (r.kind === 'hit') this.openSession(r.project, r.file, { ta: r.ta, at: lineOf(r.at[0]?.a) });
|
|
354
|
+
else if (r.kind === 'more') { v.skip = r.skip; this.runSearch(v); }
|
|
355
|
+
}
|
|
356
|
+
} catch (e) { this.note(message(e), true); }
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
back() {
|
|
360
|
+
if (this.state.stack.length > 1) { this.state.stack.pop(); return; }
|
|
361
|
+
this.note('this is the first view: q quits');
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
inputKey(str, k) {
|
|
365
|
+
const inp = this.state.input;
|
|
366
|
+
if (k.name === 'escape') { this.state.input = null; return; }
|
|
367
|
+
if (k.name === 'return' || k.name === 'enter') {
|
|
368
|
+
const text = inp.text.trim();
|
|
369
|
+
this.state.input = null;
|
|
370
|
+
if (text === '') return;
|
|
371
|
+
if (inp.kind === 'search') this.pushSearch(text);
|
|
372
|
+
else this.openAddress(text);
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
if (k.name === 'backspace') { const cs = [...clusters(inp.text)]; cs.pop(); inp.text = cs.join(''); return; }
|
|
376
|
+
if (k.ctrl && k.name === 'u') { inp.text = ''; return; }
|
|
377
|
+
if (k.ctrl || k.meta || typeof str !== 'string' || str === '') return;
|
|
378
|
+
if (/[\u0000-\u001f\u007f-\u009f]/.test(str)) return;
|
|
379
|
+
if (inp.text.length + str.length <= MAX_INPUT) inp.text += str;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
/** Open an address as ltms prints it: the transcript its prefix names (in the project on screen first), then the
|
|
383
|
+
* turn or the record. */
|
|
384
|
+
openAddress(text) {
|
|
385
|
+
const m = ADDRESS.exec(text);
|
|
386
|
+
if (!m) { this.note(`not an address: ${text} (an address is <prefix>:L<line> or <prefix>:T<turn>, as ltms prints it)`, true); return; }
|
|
387
|
+
const [, prefix, kind, n] = m;
|
|
388
|
+
const then = kind === 'L' ? { line: Number(n) } : { ta: `${prefix}:T${String(Number(n)).padStart(3, '0')}` };
|
|
389
|
+
const here = this.scope().project;
|
|
390
|
+
const known = this.state.stack.findLast?.((v) => v.kind === 'sessions' && v.project === here)?.rows.find((r) => r.p === prefix);
|
|
391
|
+
if (known) { this.openSession(here, known.file, then); return; }
|
|
392
|
+
let p;
|
|
393
|
+
try { p = Promise.resolve(this.source.resolve(prefix, { prefer: here })); } catch (e) { p = Promise.reject(e); }
|
|
394
|
+
this.note(`looking for ${prefix} ...`);
|
|
395
|
+
this.track(p.then((found) => {
|
|
396
|
+
this.state.note = null;
|
|
397
|
+
if (!found.length) { this.note(this.source.kind === 'file' ? `${prefix} is not this transcript's prefix (${this.source.prefix})` : `no transcript has the address prefix ${prefix} in any project`, true); return; }
|
|
398
|
+
if (found.length > 1) this.note(`${found.length} transcripts have the prefix ${prefix}; this is the first, ${found[0].project}/${found[0].file}`, true);
|
|
399
|
+
this.openSession(found[0].project, found[0].file, then);
|
|
400
|
+
}, (e) => { this.note(message(e), true); }));
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
exit(code) {
|
|
404
|
+
if (this.quitting !== null) return;
|
|
405
|
+
this.quitting = code;
|
|
406
|
+
this.dispose();
|
|
407
|
+
this.onExit(code);
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
/** Whether a view's list has a line of column headings above it (as views.mjs draws it). */
|
|
412
|
+
const headed = (v) => v.kind !== 'record' && v.kind !== 'search';
|
|
413
|
+
|
|
414
|
+
/** The line of an address <prefix>:L<line>, or null. */
|
|
415
|
+
export const lineOf = (a) => { const m = /:L(\d+)$/.exec(String(a ?? '')); return m ? Number(m[1]) : null; };
|