@yaks/tui 0.0.0-stage → 0.2.36
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/Frame.js +52 -0
- package/Frame.js.map +1 -0
- package/Frame.ts +92 -0
- package/Image.js +10 -0
- package/Image.js.map +1 -0
- package/Image.ts +22 -0
- package/LICENSE +202 -0
- package/README.md +524 -2
- package/RenderedCursor.js +103 -0
- package/RenderedCursor.js.map +1 -0
- package/RenderedCursor.ts +127 -0
- package/Scroll.js +88 -0
- package/Scroll.js.map +1 -0
- package/Scroll.ts +155 -0
- package/Textarea.js +186 -0
- package/Textarea.js.map +1 -0
- package/Textarea.ts +234 -0
- package/VirtualList.js +729 -0
- package/VirtualList.js.map +1 -0
- package/VirtualList.ts +838 -0
- package/_dist/Frame.d.ts +25 -0
- package/_dist/Frame.d.ts.map +1 -0
- package/_dist/Image.d.ts +13 -0
- package/_dist/Image.d.ts.map +1 -0
- package/_dist/RenderedCursor.d.ts +9 -0
- package/_dist/RenderedCursor.d.ts.map +1 -0
- package/_dist/Scroll.d.ts +30 -0
- package/_dist/Scroll.d.ts.map +1 -0
- package/_dist/Textarea.d.ts +26 -0
- package/_dist/Textarea.d.ts.map +1 -0
- package/_dist/VirtualList.d.ts +103 -0
- package/_dist/VirtualList.d.ts.map +1 -0
- package/_dist/guide.d.ts +21 -0
- package/_dist/guide.d.ts.map +1 -0
- package/_dist/history.d.ts +12 -0
- package/_dist/history.d.ts.map +1 -0
- package/_dist/input.d.ts +47 -0
- package/_dist/input.d.ts.map +1 -0
- package/_dist/interaction.d.ts +15 -0
- package/_dist/interaction.d.ts.map +1 -0
- package/_dist/keymap.d.ts +4 -0
- package/_dist/keymap.d.ts.map +1 -0
- package/_dist/mod.d.ts +42 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/mouse.d.ts +15 -0
- package/_dist/mouse.d.ts.map +1 -0
- package/_dist/paint.d.ts +86 -0
- package/_dist/paint.d.ts.map +1 -0
- package/_dist/print.d.ts +20 -0
- package/_dist/print.d.ts.map +1 -0
- package/_dist/run.d.ts +19 -0
- package/_dist/run.d.ts.map +1 -0
- package/_dist/screen.d.ts +22 -0
- package/_dist/screen.d.ts.map +1 -0
- package/_dist/scrollbar.d.ts +7 -0
- package/_dist/scrollbar.d.ts.map +1 -0
- package/_dist/testing.d.ts +19 -0
- package/_dist/testing.d.ts.map +1 -0
- package/_dist/theme.d.ts +11 -0
- package/_dist/theme.d.ts.map +1 -0
- package/_dist/visual.d.ts +28 -0
- package/_dist/visual.d.ts.map +1 -0
- package/deno.json +1 -0
- package/graphics.js +157 -0
- package/graphics.js.map +1 -0
- package/graphics.ts +191 -0
- package/grid.js +38 -0
- package/grid.js.map +1 -0
- package/grid.ts +46 -0
- package/guide.js +144 -0
- package/guide.js.map +1 -0
- package/guide.ts +169 -0
- package/history.js +58 -0
- package/history.js.map +1 -0
- package/history.ts +59 -0
- package/input.js +356 -0
- package/input.js.map +1 -0
- package/input.ts +324 -0
- package/interaction.js +180 -0
- package/interaction.js.map +1 -0
- package/interaction.ts +185 -0
- package/keymap.js +20 -0
- package/keymap.js.map +1 -0
- package/keymap.ts +23 -0
- package/mod.js +39 -0
- package/mod.js.map +1 -0
- package/mod.ts +92 -0
- package/mouse.js +65 -0
- package/mouse.js.map +1 -0
- package/mouse.ts +98 -0
- package/package.json +49 -4
- package/paint.js +1010 -0
- package/paint.js.map +1 -0
- package/paint.ts +1063 -0
- package/print.js +31 -0
- package/print.js.map +1 -0
- package/print.ts +40 -0
- package/run.js +220 -0
- package/run.js.map +1 -0
- package/run.ts +237 -0
- package/screen.js +97 -0
- package/screen.js.map +1 -0
- package/screen.ts +139 -0
- package/scrollbar.js +30 -0
- package/scrollbar.js.map +1 -0
- package/scrollbar.ts +33 -0
- package/table.js +134 -0
- package/table.js.map +1 -0
- package/table.ts +152 -0
- package/testing.js +86 -0
- package/testing.js.map +1 -0
- package/testing.ts +95 -0
- package/textRows.js +38 -0
- package/textRows.js.map +1 -0
- package/textRows.ts +31 -0
- package/theme.js +50 -0
- package/theme.js.map +1 -0
- package/theme.ts +39 -0
- package/visual.js +195 -0
- package/visual.js.map +1 -0
- package/visual.ts +209 -0
package/paint.js
ADDED
|
@@ -0,0 +1,1010 @@
|
|
|
1
|
+
import * as system from "@yaks/process/system";
|
|
2
|
+
import { visualLines } from "./visual.js";
|
|
3
|
+
import { scrollbar } from "./scrollbar.js";
|
|
4
|
+
/**
|
|
5
|
+
* The ANSI backend: a tree of fake-DOM nodes becomes lines, and lines become
|
|
6
|
+
* bytes on a terminal.
|
|
7
|
+
*
|
|
8
|
+
* The seam. A `Backend` is `{size, start, draw, reset, stop}` — five calls,
|
|
9
|
+
* the whole contract `run()` knows. `draw(root)` is handed the rendered tree
|
|
10
|
+
* and returns `{written, metrics}`: how many screen lines it actually wrote
|
|
11
|
+
* (diagnostics, and what the snappiness test asserts on) and what it measured
|
|
12
|
+
* while laying out (`{total, height}` per `id`, which is how a scroll region
|
|
13
|
+
* learns how much content it has). Swapping this file for another renderer —
|
|
14
|
+
* OpenTUI's renderable tree, say — means implementing those five, not touching
|
|
15
|
+
* the widgets.
|
|
16
|
+
*
|
|
17
|
+
* Layout is structural attributes, because only the painter knows how wide
|
|
18
|
+
* and tall the terminal is: `row` lays element children side by side
|
|
19
|
+
* (`width` fixed, `grow` takes the rest), `col` stacks them (`grow` takes the
|
|
20
|
+
* leftover rows), `height` fixes a box, and `scroll` windows a box's content
|
|
21
|
+
* from that offset. A class's style in the sheet can say `row`, `col`,
|
|
22
|
+
* `width`, `grow`, `wrap` and `border` as well, and `grid`, which gives every
|
|
23
|
+
* row under it the same columns (./grid.ts); `ellipsis` and `align` say how
|
|
24
|
+
* an element sits in the width it is cut or padded to, as CSS's
|
|
25
|
+
* `text-overflow` and `text-align` do. Everything else flows: block elements
|
|
26
|
+
* stack as lines, inline elements run into them, class names look up the
|
|
27
|
+
* sheet. A form field paints what a browser shows of it: a text field
|
|
28
|
+
* (`input`, `textarea`) its `value`, or its `placeholder` while empty, with a
|
|
29
|
+
* painted cursor at `data-caret`; a checkbox or a radio its mark; a `select`
|
|
30
|
+
* its chosen option.
|
|
31
|
+
*
|
|
32
|
+
* The boundary. Every text node and every href loses the C0/DEL/C1 class
|
|
33
|
+
* before anything is painted (`@yaks/text`'s `safe`, with `\n` kept because a
|
|
34
|
+
* newline inside a text node means a line break here). Every escape the
|
|
35
|
+
* terminal sees is emitted by `ansi()` or by `draw()` — never by content.
|
|
36
|
+
*
|
|
37
|
+
* @module
|
|
38
|
+
*/ import { safe as strip, safeHref } from "@yaks/text";
|
|
39
|
+
import { doc, touch, TText } from "@yaks/dom";
|
|
40
|
+
import { table } from "./table.js";
|
|
41
|
+
import { tracks } from "./grid.js";
|
|
42
|
+
import { graphics } from "./graphics.js";
|
|
43
|
+
import { base, compose } from "./theme.js";
|
|
44
|
+
// What an element paints in place of its children, kept here by the element
|
|
45
|
+
// rather than on it: the DOM is every painter's, and these are the
|
|
46
|
+
// terminal's.
|
|
47
|
+
/** The image an element (`Image`) paints. */ export let images = new WeakMap();
|
|
48
|
+
/** The window a virtual list's element paints. */ export let viewports = new WeakMap();
|
|
49
|
+
// A newline inside a text node is a line break the painter honours, so the
|
|
50
|
+
// text host's boundary is applied per line rather than to the whole string.
|
|
51
|
+
let safe = (text)=>text.replaceAll('\t', ' ').split('\n').map(strip).join('\n');
|
|
52
|
+
// Semantic HTML emitted by shared Preact renderers. ANSI stays in this backend.
|
|
53
|
+
// A bare anchor wears the sheet's `Link`; one with a class is a part that
|
|
54
|
+
// says its own look there.
|
|
55
|
+
let semantic = (el, sheet)=>{
|
|
56
|
+
let tag = el.localName;
|
|
57
|
+
if (tag == 'a' && !el.className) return {
|
|
58
|
+
...sheet.Link
|
|
59
|
+
};
|
|
60
|
+
if (tag == 'strong' || tag == 'b' || /^h[1-6]$/.test(tag) || tag == 'th') {
|
|
61
|
+
return {
|
|
62
|
+
bold: true
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
if (tag == 'em' || tag == 'i') return {
|
|
66
|
+
italic: true
|
|
67
|
+
};
|
|
68
|
+
if (tag == 'del') return {
|
|
69
|
+
strike: true
|
|
70
|
+
};
|
|
71
|
+
if (tag == 'code' || tag == 'pre') return {
|
|
72
|
+
...sheet.Code
|
|
73
|
+
};
|
|
74
|
+
if (tag == 'blockquote') return {
|
|
75
|
+
indent: 2,
|
|
76
|
+
...sheet.Quote
|
|
77
|
+
};
|
|
78
|
+
if (tag == 'hr') return {
|
|
79
|
+
glyph: '────────',
|
|
80
|
+
dim: true
|
|
81
|
+
};
|
|
82
|
+
return {};
|
|
83
|
+
};
|
|
84
|
+
// A link's href is part of its style, however it is laid out: inline, or as
|
|
85
|
+
// a block its sheet makes of it. An href is content too, and it is emitted
|
|
86
|
+
// inside an OSC 8 sequence, where a single BEL byte ends the sequence and lets
|
|
87
|
+
// the rest of the URL be interpreted as terminal input.
|
|
88
|
+
let own = (el, sheet)=>{
|
|
89
|
+
let out = semantic(el, sheet);
|
|
90
|
+
if (el.className) {
|
|
91
|
+
for (let name of el.className.split(/\s+/)){
|
|
92
|
+
if (name && sheet[name]) Object.assign(out, sheet[name]);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
// The cursor (@yaks/ux/keys) and the focus show as a browser rings them;
|
|
96
|
+
// never a box focused only to hear keys (tabindex -1, a dialog).
|
|
97
|
+
let focused = doc.activeElement == el && el.attr('tabindex') != '-1';
|
|
98
|
+
if (focused || el.attr('data-cursor') != null) out.inverse = true;
|
|
99
|
+
let href = el.localName == 'a' && el.attr('href');
|
|
100
|
+
if (href) out.href = safeHref(href);
|
|
101
|
+
return out;
|
|
102
|
+
};
|
|
103
|
+
// Inherit text style down the tree; glyph/indent/gap act only where set.
|
|
104
|
+
let inherit = (parent, node)=>({
|
|
105
|
+
fg: node.fg ?? parent.fg,
|
|
106
|
+
bg: node.bg ?? parent.bg,
|
|
107
|
+
bold: node.bold ?? parent.bold,
|
|
108
|
+
dim: node.dim ?? parent.dim,
|
|
109
|
+
italic: node.italic ?? parent.italic,
|
|
110
|
+
underline: node.underline ?? parent.underline,
|
|
111
|
+
strike: node.strike ?? parent.strike,
|
|
112
|
+
inverse: node.inverse ?? parent.inverse,
|
|
113
|
+
href: node.href ?? parent.href
|
|
114
|
+
});
|
|
115
|
+
let INLINE = new Set([
|
|
116
|
+
'span',
|
|
117
|
+
'br',
|
|
118
|
+
'b',
|
|
119
|
+
'i',
|
|
120
|
+
'strong',
|
|
121
|
+
'em',
|
|
122
|
+
'del',
|
|
123
|
+
'mark',
|
|
124
|
+
'code',
|
|
125
|
+
'a',
|
|
126
|
+
'time',
|
|
127
|
+
'button',
|
|
128
|
+
'label',
|
|
129
|
+
'input',
|
|
130
|
+
'textarea',
|
|
131
|
+
'select'
|
|
132
|
+
]);
|
|
133
|
+
// Whether a node runs into its line: text does, and an inline tag does unless
|
|
134
|
+
// its sheet says `block`.
|
|
135
|
+
let runs = (n, sheet)=>n instanceof TText || INLINE.has(n.localName) && !n.className.split(/\s+/).some((k)=>sheet[k]?.block);
|
|
136
|
+
// What a closed `details` holds but its summary, which a browser does not
|
|
137
|
+
// show until it is opened.
|
|
138
|
+
let shut = (el)=>{
|
|
139
|
+
let up = el.parentNode;
|
|
140
|
+
return up?.localName == 'details' && !up.hasAttribute('open') && el.localName != 'summary';
|
|
141
|
+
};
|
|
142
|
+
// Whether an element shows; one its sheet hides, or a closed `details`, is
|
|
143
|
+
// noted as hidden.
|
|
144
|
+
let shows = (el, o, c)=>{
|
|
145
|
+
let hid = o.hidden || shut(el);
|
|
146
|
+
if (hid) c.hidden?.add(el);
|
|
147
|
+
return !hid;
|
|
148
|
+
};
|
|
149
|
+
// Whether a spaced layout puts a space between what a line holds and the run
|
|
150
|
+
// that follows it: when neither side brings its own.
|
|
151
|
+
let apart = (cur, next)=>!!cur.length && !!next.length && !/\s$/.test(cur[cur.length - 1].text) && !/^\s/.test(next[0].text);
|
|
152
|
+
let inline = (n, st, c)=>{
|
|
153
|
+
if (n instanceof TText) {
|
|
154
|
+
let text = safe(n.data);
|
|
155
|
+
return text ? [
|
|
156
|
+
{
|
|
157
|
+
text,
|
|
158
|
+
style: st,
|
|
159
|
+
owner: n.parentNode ?? undefined
|
|
160
|
+
}
|
|
161
|
+
] : [];
|
|
162
|
+
}
|
|
163
|
+
let el = n;
|
|
164
|
+
if (el.localName == 'br') return [
|
|
165
|
+
{
|
|
166
|
+
text: '\n',
|
|
167
|
+
style: st,
|
|
168
|
+
owner: el
|
|
169
|
+
}
|
|
170
|
+
];
|
|
171
|
+
let o = own(el, c.sheet);
|
|
172
|
+
if (!shows(el, o, c)) return [];
|
|
173
|
+
let s = inherit(st, o);
|
|
174
|
+
if (o.glyph) return [
|
|
175
|
+
{
|
|
176
|
+
text: o.glyph,
|
|
177
|
+
style: s,
|
|
178
|
+
owner: el
|
|
179
|
+
}
|
|
180
|
+
];
|
|
181
|
+
let typed = el.attr('data-caret') != null && el.isContentEditable;
|
|
182
|
+
if (el.localName == 'input' || el.localName == 'textarea' || typed) {
|
|
183
|
+
return field(el, s, c);
|
|
184
|
+
}
|
|
185
|
+
if (el.localName == 'select') {
|
|
186
|
+
return [
|
|
187
|
+
{
|
|
188
|
+
text: `${chosen(el)} ▾`,
|
|
189
|
+
style: s,
|
|
190
|
+
owner: el
|
|
191
|
+
}
|
|
192
|
+
];
|
|
193
|
+
}
|
|
194
|
+
// A block the sheet makes of an element keeps its runs apart, as it would
|
|
195
|
+
// on a line of its own, even where it is painted inline (a `dd`); so does
|
|
196
|
+
// one its sheet spaces, wherever it is painted.
|
|
197
|
+
let spaced = c.spaced && o.block || o.spaced;
|
|
198
|
+
let out = [];
|
|
199
|
+
for (let k of el.childNodes){
|
|
200
|
+
let segs = inline(k, s, c);
|
|
201
|
+
if (spaced && apart(out, segs)) out.push({
|
|
202
|
+
text: ' ',
|
|
203
|
+
style: s
|
|
204
|
+
});
|
|
205
|
+
out.push(...segs);
|
|
206
|
+
}
|
|
207
|
+
return out;
|
|
208
|
+
};
|
|
209
|
+
// What a checkbox and a radio show, unchecked and checked.
|
|
210
|
+
let MARKS = {
|
|
211
|
+
checkbox: [
|
|
212
|
+
'☐',
|
|
213
|
+
'☑'
|
|
214
|
+
],
|
|
215
|
+
radio: [
|
|
216
|
+
'○',
|
|
217
|
+
'◉'
|
|
218
|
+
]
|
|
219
|
+
};
|
|
220
|
+
// A field paints what a browser shows in it: a checkbox or a radio its mark,
|
|
221
|
+
// a password its dots, and any other its value (text typed over in place, its
|
|
222
|
+
// text), its placeholder (dimmed, as the text entry's hint is) while empty,
|
|
223
|
+
// and a painted cursor where `data-caret` puts the caret, since a terminal has
|
|
224
|
+
// no focus to show it by.
|
|
225
|
+
let field = (el, s, c)=>{
|
|
226
|
+
let type = el.attr('type') ?? 'text';
|
|
227
|
+
let seg = (text, style)=>text ? [
|
|
228
|
+
{
|
|
229
|
+
text,
|
|
230
|
+
style,
|
|
231
|
+
owner: el
|
|
232
|
+
}
|
|
233
|
+
] : [];
|
|
234
|
+
let mark = MARKS[type];
|
|
235
|
+
if (mark) return seg(mark[el.attr('checked') == null ? 0 : 1], s);
|
|
236
|
+
if (!shows(el, {
|
|
237
|
+
hidden: type == 'hidden'
|
|
238
|
+
}, c)) return [];
|
|
239
|
+
let value = safe(el.isContentEditable ? el.textContent : el.attr('value') ?? '');
|
|
240
|
+
if (type == 'password') value = '•'.repeat(value.length);
|
|
241
|
+
let hint = value ? [] : seg(safe(el.attr('placeholder') ?? ''), inherit(s, c.sheet.Entry_Hint ?? base.Entry_Hint));
|
|
242
|
+
let at = num(el, 'data-caret');
|
|
243
|
+
if (at == null) return value ? seg(value, s) : hint;
|
|
244
|
+
at = Math.min(Math.max(0, at), value.length);
|
|
245
|
+
// At the end of the value, or of one of its lines, the cursor is a cell of
|
|
246
|
+
// its own.
|
|
247
|
+
let under = value[at] && value[at] != '\n' ? value[at] : '';
|
|
248
|
+
return [
|
|
249
|
+
...seg(value.slice(0, at), s),
|
|
250
|
+
...seg(under || ' ', inherit(s, c.sheet.Cursor ?? base.Cursor)),
|
|
251
|
+
...seg(value.slice(at + under.length), s),
|
|
252
|
+
...hint
|
|
253
|
+
];
|
|
254
|
+
};
|
|
255
|
+
// A node's text, sanitized by the same function — a text node's data is never
|
|
256
|
+
// painted raw, whichever branch reaches it.
|
|
257
|
+
let text = (n)=>n instanceof TText ? safe(n.data) : n.childNodes.map(text).join('');
|
|
258
|
+
// A select's chosen option, as it shows closed: the one its value names, or
|
|
259
|
+
// the one marked selected, or the first.
|
|
260
|
+
let chosen = (el)=>{
|
|
261
|
+
let options = kids(el).flatMap((k)=>k.localName == 'optgroup' ? kids(k) : [
|
|
262
|
+
k
|
|
263
|
+
]).filter((k)=>k.localName == 'option');
|
|
264
|
+
let value = el.attr('value');
|
|
265
|
+
let pick = options.find((o)=>(o.attr('value') ?? text(o)) == value) ?? options.find((o)=>o.attr('selected') != null) ?? options[0];
|
|
266
|
+
return pick ? text(pick) : '';
|
|
267
|
+
};
|
|
268
|
+
let kids = (el)=>el.childNodes.filter((n)=>!(n instanceof TText));
|
|
269
|
+
let num = (el, k)=>{
|
|
270
|
+
let v = el.attr(k);
|
|
271
|
+
return v == null || v === '' ? null : +v;
|
|
272
|
+
};
|
|
273
|
+
// How an element is boxed: by its attribute, or by its classes in the sheet,
|
|
274
|
+
// so a part styled for a browser and a terminal alike says its layout where
|
|
275
|
+
// it says its colours.
|
|
276
|
+
let grows = (el, sheet)=>el.attr('grow') != null || !!own(el, sheet).grow;
|
|
277
|
+
let wide = (el, sheet)=>num(el, 'width') ?? own(el, sheet).width ?? null;
|
|
278
|
+
// Block flow: text and inline children run into a line, element children stack.
|
|
279
|
+
// A lone element child inherits the box: a wrapper div is not a layout, so the
|
|
280
|
+
// screen's height reaches the `col` or `row` an app returns from its root.
|
|
281
|
+
let flow = (el, s, w, h, c, o = own(el, c.sheet))=>{
|
|
282
|
+
let wrapper = kids(el).length == 1 && !el.childNodes.some((n)=>runs(n, c.sheet));
|
|
283
|
+
let lines = [];
|
|
284
|
+
let cur = [];
|
|
285
|
+
let flush = ()=>{
|
|
286
|
+
if (cur.length) lines.push(cur);
|
|
287
|
+
cur = [];
|
|
288
|
+
};
|
|
289
|
+
if (el.localName == 'tr') {
|
|
290
|
+
for (let [i, cell] of kids(el).entries()){
|
|
291
|
+
if (i) cur.push({
|
|
292
|
+
text: ' | ',
|
|
293
|
+
style: s
|
|
294
|
+
});
|
|
295
|
+
cur.push(...inline(cell, s, c));
|
|
296
|
+
}
|
|
297
|
+
flush();
|
|
298
|
+
return lines;
|
|
299
|
+
}
|
|
300
|
+
if (el.localName == 'hr') return [
|
|
301
|
+
[
|
|
302
|
+
{
|
|
303
|
+
text: '────────',
|
|
304
|
+
style: s
|
|
305
|
+
}
|
|
306
|
+
]
|
|
307
|
+
];
|
|
308
|
+
if (el.localName == 'dl') {
|
|
309
|
+
// A term and the description after it share a line, `term: description`,
|
|
310
|
+
// as @yaks/text prints them.
|
|
311
|
+
for (let k of kids(el)){
|
|
312
|
+
if (k.localName == 'dd' && cur.length) {
|
|
313
|
+
cur.push({
|
|
314
|
+
text: ' ',
|
|
315
|
+
style: s
|
|
316
|
+
}, ...inline(k, s, c));
|
|
317
|
+
flush();
|
|
318
|
+
} else if (k.localName == 'dt') {
|
|
319
|
+
flush();
|
|
320
|
+
cur.push(...inline(k, s, c), {
|
|
321
|
+
text: ':',
|
|
322
|
+
style: s
|
|
323
|
+
});
|
|
324
|
+
} else {
|
|
325
|
+
flush();
|
|
326
|
+
lines.push(...lay(k, s, w, null, c));
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
flush();
|
|
330
|
+
return lines;
|
|
331
|
+
}
|
|
332
|
+
if (el.localName == 'pre') {
|
|
333
|
+
// A preformatted block keeps its newlines and the styles of what it holds
|
|
334
|
+
// (a highlighter's spans), and paints its allocated width, including
|
|
335
|
+
// blank rows. Keep long lines intact so the enclosing layout retains its
|
|
336
|
+
// wrap policy.
|
|
337
|
+
let rows = [
|
|
338
|
+
[]
|
|
339
|
+
];
|
|
340
|
+
for (let seg of el.childNodes.flatMap((k)=>inline(k, s, c))){
|
|
341
|
+
seg.text.split('\n').forEach((t, i)=>{
|
|
342
|
+
if (i) rows.push([]);
|
|
343
|
+
if (t) rows.at(-1).push({
|
|
344
|
+
...seg,
|
|
345
|
+
text: t
|
|
346
|
+
});
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
for (let row of rows){
|
|
350
|
+
let used = row.reduce((n, seg)=>n + seg.text.length, 0);
|
|
351
|
+
let pad = ' '.repeat(Math.max(0, w - used));
|
|
352
|
+
let last = row.at(-1);
|
|
353
|
+
lines.push(last && alike(last.style, s) ? [
|
|
354
|
+
...row.slice(0, -1),
|
|
355
|
+
{
|
|
356
|
+
...last,
|
|
357
|
+
text: last.text + pad
|
|
358
|
+
}
|
|
359
|
+
] : pad || !row.length ? [
|
|
360
|
+
...row,
|
|
361
|
+
{
|
|
362
|
+
text: pad,
|
|
363
|
+
style: s
|
|
364
|
+
}
|
|
365
|
+
] : row);
|
|
366
|
+
}
|
|
367
|
+
return lines;
|
|
368
|
+
}
|
|
369
|
+
let previousParagraph = false;
|
|
370
|
+
let spaced = (c.spaced || o.spaced) && !el.className.split(/\s+/).includes('Md_Code');
|
|
371
|
+
for (let n of el.childNodes){
|
|
372
|
+
// White space opening a line says nothing, as a browser collapses it: the
|
|
373
|
+
// space a tree puts between two blocks for a plain-text reader is no line.
|
|
374
|
+
if (n instanceof TText && !cur.length && !n.data.trim()) continue;
|
|
375
|
+
if (runs(n, c.sheet)) {
|
|
376
|
+
let segs = inline(n, s, c);
|
|
377
|
+
if (!segs.length) continue;
|
|
378
|
+
previousParagraph = false;
|
|
379
|
+
if (spaced && apart(cur, segs)) cur.push({
|
|
380
|
+
text: ' ',
|
|
381
|
+
style: s
|
|
382
|
+
});
|
|
383
|
+
for (let seg of segs){
|
|
384
|
+
// Newlines inside a text node are line breaks.
|
|
385
|
+
seg.text.split('\n').forEach((part, i)=>{
|
|
386
|
+
if (i) {
|
|
387
|
+
lines.push(cur); // even empty — a blank line is content here
|
|
388
|
+
cur = [];
|
|
389
|
+
}
|
|
390
|
+
if (part) cur.push({
|
|
391
|
+
...seg,
|
|
392
|
+
text: part
|
|
393
|
+
});
|
|
394
|
+
});
|
|
395
|
+
}
|
|
396
|
+
} else {
|
|
397
|
+
flush();
|
|
398
|
+
// HTML paragraphs have a visual boundary even without literal newlines.
|
|
399
|
+
// Keep it between paragraphs, not after every paragraph/list item.
|
|
400
|
+
let paragraph = n.localName == 'p';
|
|
401
|
+
if (paragraph && previousParagraph) lines.push([]);
|
|
402
|
+
previousParagraph = paragraph;
|
|
403
|
+
lines.push(...lay(n, s, w, wrapper ? h : null, c));
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
flush();
|
|
407
|
+
return lines;
|
|
408
|
+
};
|
|
409
|
+
// Stack element children; `grow` children share whatever rows are left.
|
|
410
|
+
let col = (el, s, w, h, c)=>{
|
|
411
|
+
let all = kids(el);
|
|
412
|
+
let fixed = new Map();
|
|
413
|
+
for (let k of all){
|
|
414
|
+
if (!grows(k, c.sheet)) fixed.set(k, lay(k, s, w, null, c));
|
|
415
|
+
}
|
|
416
|
+
let used = [
|
|
417
|
+
...fixed.values()
|
|
418
|
+
].reduce((n, l)=>n + l.length, 0);
|
|
419
|
+
let growers = all.filter((k)=>grows(k, c.sheet));
|
|
420
|
+
let left = h == null ? null : Math.max(0, h - used);
|
|
421
|
+
let out = [];
|
|
422
|
+
if (left != null) {
|
|
423
|
+
let shares = new Map();
|
|
424
|
+
let fitters = growers.filter((k)=>k.attr('grow-fit') != null);
|
|
425
|
+
let fair = Math.floor(left / Math.max(1, growers.length));
|
|
426
|
+
for (let k of fitters){
|
|
427
|
+
let natural = lay(k, s, w, null, c).length;
|
|
428
|
+
let share = Math.min(natural, fair);
|
|
429
|
+
shares.set(k, share);
|
|
430
|
+
left -= share;
|
|
431
|
+
}
|
|
432
|
+
let expanding = growers.filter((k)=>!shares.has(k));
|
|
433
|
+
expanding.forEach((k, i)=>{
|
|
434
|
+
let share = Math.floor(left / (expanding.length - i));
|
|
435
|
+
shares.set(k, share);
|
|
436
|
+
left -= share;
|
|
437
|
+
});
|
|
438
|
+
for (let k of growers)fixed.set(k, lay(k, s, w, shares.get(k), c));
|
|
439
|
+
}
|
|
440
|
+
for (let k of all)out.push(...fixed.get(k) ?? lay(k, s, w, null, c));
|
|
441
|
+
return out;
|
|
442
|
+
};
|
|
443
|
+
// Lay element children side by side; `width` is fixed, `grow` takes the rest.
|
|
444
|
+
// Under a grid, the grid's columns say how wide each is instead (`cells`).
|
|
445
|
+
let row = (el, s, w, h, c)=>{
|
|
446
|
+
if (c.columns) return cells(el, s, w, h, c);
|
|
447
|
+
let all = kids(el).filter((k)=>shows(k, own(k, c.sheet), c));
|
|
448
|
+
let fixed = all.map((k)=>wide(k, c.sheet));
|
|
449
|
+
let spare = w - fixed.reduce((n, v)=>n + (v ?? 0), 0);
|
|
450
|
+
let growers = fixed.filter((v)=>v == null).length;
|
|
451
|
+
let taken = 0;
|
|
452
|
+
let widths = fixed.map((v, i)=>{
|
|
453
|
+
if (v != null) return v;
|
|
454
|
+
let last = fixed.lastIndexOf(null) == i;
|
|
455
|
+
let share = last ? spare - taken : Math.floor(spare / growers);
|
|
456
|
+
taken += share;
|
|
457
|
+
return Math.max(0, share);
|
|
458
|
+
});
|
|
459
|
+
let cols = all.map((k, i)=>lay(k, s, widths[i], h, c));
|
|
460
|
+
let rows = h ?? Math.max(0, ...cols.map((l)=>l.length));
|
|
461
|
+
return Array.from({
|
|
462
|
+
length: rows
|
|
463
|
+
}, (_, y)=>all.flatMap((k, i)=>{
|
|
464
|
+
let o = own(k, c.sheet);
|
|
465
|
+
let line = clip(cols[i][y] ?? [], widths[i], o.ellipsis);
|
|
466
|
+
// The last column is not padded: the screen's own erase ends the line.
|
|
467
|
+
return i == all.length - 1 ? line : pad(line, widths[i], o.align);
|
|
468
|
+
}));
|
|
469
|
+
};
|
|
470
|
+
// How far apart a grid's columns stand.
|
|
471
|
+
let GAP = 2;
|
|
472
|
+
// How wide a grid measures a cell's content at: wider than any terminal.
|
|
473
|
+
let MEASURE = 1000;
|
|
474
|
+
// The rows under a grid, through whatever holds them (a head, a body), but
|
|
475
|
+
// not into a row, or a grid of its own.
|
|
476
|
+
let rowsUnder = (el, sheet)=>kids(el).flatMap((k)=>k.attr('row') != null || own(k, sheet).row ? [
|
|
477
|
+
k
|
|
478
|
+
] : own(k, sheet).grid ? [] : rowsUnder(k, sheet));
|
|
479
|
+
// Lay a grid out: measure every row's cells, column by column, give each
|
|
480
|
+
// column its width (./grid.ts), then lay the grid's children out as they
|
|
481
|
+
// would be, every row under it in those columns.
|
|
482
|
+
let grid = (el, s, w, h, c)=>{
|
|
483
|
+
let free = {
|
|
484
|
+
...c,
|
|
485
|
+
columns: undefined
|
|
486
|
+
};
|
|
487
|
+
let cols = [];
|
|
488
|
+
for (let r of rowsUnder(el, c.sheet)){
|
|
489
|
+
kids(r).forEach((k, i)=>{
|
|
490
|
+
let t = cols[i] ??= {
|
|
491
|
+
content: 0
|
|
492
|
+
};
|
|
493
|
+
let lines = layout(k, s, MEASURE, null, free);
|
|
494
|
+
t.content = Math.max(t.content, ...lines.map((l)=>l.map((seg)=>seg.text).join('').trimEnd().length));
|
|
495
|
+
t.grow ||= grows(k, c.sheet);
|
|
496
|
+
t.width ??= wide(k, c.sheet);
|
|
497
|
+
});
|
|
498
|
+
}
|
|
499
|
+
return flow(el, s, w, h, {
|
|
500
|
+
...c,
|
|
501
|
+
columns: tracks(cols, w, GAP)
|
|
502
|
+
});
|
|
503
|
+
};
|
|
504
|
+
// A row under a grid: its cells in the grid's columns, a gap apart, each cut
|
|
505
|
+
// to its column and set to its side of it, and the row's own colours under
|
|
506
|
+
// the whole line, as a subgrid row's background runs under its gaps.
|
|
507
|
+
let cells = (el, s, w, h, c)=>{
|
|
508
|
+
let widths = c.columns;
|
|
509
|
+
let free = {
|
|
510
|
+
...c,
|
|
511
|
+
columns: undefined
|
|
512
|
+
};
|
|
513
|
+
let all = kids(el);
|
|
514
|
+
let laid = all.map((k, i)=>owned(k, layout(k, s, widths[i] ?? 0, h, free)));
|
|
515
|
+
let rows = h ?? Math.max(0, ...laid.map((l)=>l.length));
|
|
516
|
+
let gap = {
|
|
517
|
+
text: ' '.repeat(GAP),
|
|
518
|
+
style: s,
|
|
519
|
+
decorative: true
|
|
520
|
+
};
|
|
521
|
+
return Array.from({
|
|
522
|
+
length: rows
|
|
523
|
+
}, (_, y)=>pad(all.flatMap((k, i)=>{
|
|
524
|
+
let o = own(k, c.sheet);
|
|
525
|
+
let n = widths[i] ?? 0;
|
|
526
|
+
let cell = pad(clip(laid[i][y] ?? [], n, o.ellipsis), n, o.align, s);
|
|
527
|
+
return i ? [
|
|
528
|
+
gap,
|
|
529
|
+
...cell
|
|
530
|
+
] : cell;
|
|
531
|
+
}), w, 'left', s));
|
|
532
|
+
};
|
|
533
|
+
// The first element under `el` marked `reveal`, and everything under it.
|
|
534
|
+
let mark = (el)=>{
|
|
535
|
+
if (el.attr('reveal') != null) {
|
|
536
|
+
let all = [];
|
|
537
|
+
let add = (n)=>(all.push(n), kids(n).forEach(add));
|
|
538
|
+
add(el);
|
|
539
|
+
return all;
|
|
540
|
+
}
|
|
541
|
+
for (let k of kids(el)){
|
|
542
|
+
let found = mark(k);
|
|
543
|
+
if (found) return found;
|
|
544
|
+
}
|
|
545
|
+
};
|
|
546
|
+
// The first of a box's lines that an element marked `reveal` inside it paints,
|
|
547
|
+
// or -1.
|
|
548
|
+
let marked = (box, lines)=>{
|
|
549
|
+
let own = new Set(kids(box).flatMap((k)=>mark(k) ?? []));
|
|
550
|
+
return own.size ? lines.findIndex((l)=>l.some((seg)=>own.has(seg.owner))) : -1;
|
|
551
|
+
};
|
|
552
|
+
// Window a box's content from its scroll offset, recording what it measured.
|
|
553
|
+
let windowed = (el, lines, h, c, width)=>{
|
|
554
|
+
let height = h ?? lines.length;
|
|
555
|
+
let id = el.attr('id');
|
|
556
|
+
if (id) {
|
|
557
|
+
let reveal = marked(el, lines);
|
|
558
|
+
c.metrics[id] = {
|
|
559
|
+
total: lines.length,
|
|
560
|
+
height,
|
|
561
|
+
width,
|
|
562
|
+
...reveal < 0 ? {} : {
|
|
563
|
+
reveal
|
|
564
|
+
}
|
|
565
|
+
};
|
|
566
|
+
}
|
|
567
|
+
let top = Math.min(Math.max(0, num(el, 'scroll') ?? 0), Math.max(0, lines.length - height));
|
|
568
|
+
return lines.slice(top, top + height);
|
|
569
|
+
};
|
|
570
|
+
/** The lines an element becomes, given a content width and allotted rows. */ let layout = (el, st, w, h, c, o = own(el, c.sheet))=>{
|
|
571
|
+
let image = images.get(el);
|
|
572
|
+
if (image) {
|
|
573
|
+
let rows = Math.max(1, Math.min(16, Math.floor(image.rows) || 8));
|
|
574
|
+
return Array.from({
|
|
575
|
+
length: rows
|
|
576
|
+
}, (_, row)=>[
|
|
577
|
+
{
|
|
578
|
+
text: row == 0 ? safe(image.alt).replaceAll('\n', ' ').slice(0, w).padEnd(w) : ' '.repeat(w),
|
|
579
|
+
style: st,
|
|
580
|
+
owner: el,
|
|
581
|
+
image: {
|
|
582
|
+
source: image,
|
|
583
|
+
row,
|
|
584
|
+
rows,
|
|
585
|
+
width: w
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
]);
|
|
589
|
+
}
|
|
590
|
+
let selected = visualLines(el.attr('id') ?? '', w, h ?? 6);
|
|
591
|
+
if (selected) return selected;
|
|
592
|
+
let viewport = viewports.get(el);
|
|
593
|
+
if (viewport) return viewport(w, h ?? 0, st, c.sheet);
|
|
594
|
+
let s = inherit(st, o);
|
|
595
|
+
if (el.localName == 'svg') {
|
|
596
|
+
return o.glyph ? [
|
|
597
|
+
[
|
|
598
|
+
{
|
|
599
|
+
text: o.glyph,
|
|
600
|
+
style: s,
|
|
601
|
+
owner: el
|
|
602
|
+
}
|
|
603
|
+
]
|
|
604
|
+
] : [];
|
|
605
|
+
}
|
|
606
|
+
let wraps = el.attr('wrap') != null || !!o.wrap;
|
|
607
|
+
let outer = num(el, 'height') ?? h;
|
|
608
|
+
// Borders consume real layout space; descendants measure the inner width.
|
|
609
|
+
let border = el.attr('border') ?? o.border;
|
|
610
|
+
let framed = border != null && w >= 3;
|
|
611
|
+
let box = outer == null ? null : Math.max(0, outer - (framed ? 2 : 0));
|
|
612
|
+
let bar = el.attr('scrollbar') != null && el.attr('scroll') != null && w - (o.indent ?? 0) - (framed ? 2 : 0) >= 2;
|
|
613
|
+
let contentWidth = Math.max(0, w - (o.indent ?? 0) - (framed ? 2 : 0) - (bar ? 1 : 0));
|
|
614
|
+
let lines = el.matches('input,textarea,select') ? [
|
|
615
|
+
inline(el, st, c)
|
|
616
|
+
] : el.localName == 'table' ? table(el, contentWidth, s, c.sheet, (cell, width)=>{
|
|
617
|
+
let ancestors = [];
|
|
618
|
+
for(let node = cell.parentNode; node && node !== el; node = node.parentNode){
|
|
619
|
+
ancestors.unshift(node);
|
|
620
|
+
}
|
|
621
|
+
let inherited = ancestors.reduce((style, node)=>inherit(style, own(node, c.sheet)), s);
|
|
622
|
+
return lay(cell, inherited, width, null, c);
|
|
623
|
+
}, wrap) : o.grid ? grid(el, s, contentWidth, box, c) : el.attr('row') != null || o.row ? row(el, s, contentWidth, box, c) : el.attr('col') != null || o.col ? col(el, s, contentWidth, box, c) : flow(el, s, contentWidth, wraps || el.attr('scroll') != null ? null : box, c, o);
|
|
624
|
+
if (el.localName == 'summary' && lines.length) {
|
|
625
|
+
// The mark a browser puts before a summary: whether it is open.
|
|
626
|
+
let open = el.parentNode?.hasAttribute('open');
|
|
627
|
+
lines = [
|
|
628
|
+
[
|
|
629
|
+
{
|
|
630
|
+
text: open ? '▾ ' : '▸ ',
|
|
631
|
+
style: s
|
|
632
|
+
},
|
|
633
|
+
...lines[0]
|
|
634
|
+
],
|
|
635
|
+
...lines.slice(1)
|
|
636
|
+
];
|
|
637
|
+
}
|
|
638
|
+
if (el.localName == 'li') {
|
|
639
|
+
let marker = safe(el.attr('data-marker') ?? '• ');
|
|
640
|
+
lines = lines.map((line, i)=>[
|
|
641
|
+
{
|
|
642
|
+
text: i ? ' '.repeat(marker.length) : marker,
|
|
643
|
+
style: s
|
|
644
|
+
},
|
|
645
|
+
...line
|
|
646
|
+
]);
|
|
647
|
+
}
|
|
648
|
+
if (wraps) {
|
|
649
|
+
lines = lines.flatMap((line)=>wrap(line, contentWidth));
|
|
650
|
+
}
|
|
651
|
+
const maximum = num(el, 'max-height');
|
|
652
|
+
if (maximum != null && maximum >= 0 && lines.length > maximum) {
|
|
653
|
+
lines = lines.slice(0, maximum);
|
|
654
|
+
const overflow = el.attr('overflow-text');
|
|
655
|
+
if (overflow) lines.push([
|
|
656
|
+
{
|
|
657
|
+
text: safe(overflow),
|
|
658
|
+
style: s
|
|
659
|
+
}
|
|
660
|
+
]);
|
|
661
|
+
}
|
|
662
|
+
if (el.attr('scroll') != null) {
|
|
663
|
+
let total = lines.length;
|
|
664
|
+
lines = windowed(el, lines, box, c, contentWidth);
|
|
665
|
+
if (bar) {
|
|
666
|
+
lines = scrollbar(lines, contentWidth + 1, {
|
|
667
|
+
total,
|
|
668
|
+
height: box ?? total,
|
|
669
|
+
top: Math.min(Math.max(0, total - (box ?? total)), Math.max(0, num(el, 'scroll') ?? 0)),
|
|
670
|
+
bottom: el.attr('scroll-snapped') != null
|
|
671
|
+
}, c.sheet);
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
if (o.indent) {
|
|
675
|
+
lines = lines.map((l)=>[
|
|
676
|
+
{
|
|
677
|
+
text: ' '.repeat(o.indent),
|
|
678
|
+
style: s,
|
|
679
|
+
decorative: true
|
|
680
|
+
},
|
|
681
|
+
...l
|
|
682
|
+
]);
|
|
683
|
+
}
|
|
684
|
+
if (o.gap && lines.length) lines.push([]);
|
|
685
|
+
if (box != null) lines = fit(lines, box);
|
|
686
|
+
if (framed) {
|
|
687
|
+
let edge = c.sheet[border] ?? {};
|
|
688
|
+
let rule = (left, right)=>[
|
|
689
|
+
{
|
|
690
|
+
text: left + '─'.repeat(Math.max(0, w - 2)) + right,
|
|
691
|
+
style: edge,
|
|
692
|
+
decorative: true
|
|
693
|
+
}
|
|
694
|
+
];
|
|
695
|
+
lines = [
|
|
696
|
+
rule('╭', '╮'),
|
|
697
|
+
...lines.map((line)=>[
|
|
698
|
+
{
|
|
699
|
+
text: '│',
|
|
700
|
+
style: edge,
|
|
701
|
+
decorative: true
|
|
702
|
+
},
|
|
703
|
+
...pad(clip(line, w - 2), w - 2),
|
|
704
|
+
{
|
|
705
|
+
text: '│',
|
|
706
|
+
style: edge,
|
|
707
|
+
decorative: true
|
|
708
|
+
}
|
|
709
|
+
]),
|
|
710
|
+
rule('╰', '╯')
|
|
711
|
+
];
|
|
712
|
+
}
|
|
713
|
+
if (el.attr('fill') != null) {
|
|
714
|
+
lines = lines.map((line)=>{
|
|
715
|
+
let clipped = clip(line, w);
|
|
716
|
+
let remaining = Math.max(0, w - clipped.reduce((n, part)=>n + part.text.length, 0));
|
|
717
|
+
return [
|
|
718
|
+
...clipped,
|
|
719
|
+
{
|
|
720
|
+
text: ' '.repeat(remaining),
|
|
721
|
+
style: s,
|
|
722
|
+
decorative: true
|
|
723
|
+
}
|
|
724
|
+
];
|
|
725
|
+
});
|
|
726
|
+
}
|
|
727
|
+
return outer == null ? lines : fit(lines, outer);
|
|
728
|
+
};
|
|
729
|
+
/** Ownership travels with the painted cells through windowing, wrapping and clipping. */ export let lay = (el, st, w, h, c)=>{
|
|
730
|
+
let o = own(el, c.sheet);
|
|
731
|
+
if (!shows(el, o, c)) return [];
|
|
732
|
+
let boxed = viewports.has(el) || el.attr('scroll') != null || num(el, 'width') != null || o.width != null || el.attr('grow') != null || o.grow;
|
|
733
|
+
let lines = layout(el, st, w, h, c, o);
|
|
734
|
+
return owned(el, boxed ? lines.map((l)=>pad(clip(l, w, o.ellipsis), w, o.align)) : lines);
|
|
735
|
+
};
|
|
736
|
+
// Every cell an element painted is its own, unless something under it
|
|
737
|
+
// claimed it first; a viewport's are all its own.
|
|
738
|
+
let owned = (el, lines)=>lines.map((line)=>line.map((seg)=>!viewports.has(el) && seg.owner ? seg : {
|
|
739
|
+
...seg,
|
|
740
|
+
owner: el
|
|
741
|
+
}));
|
|
742
|
+
let fit = (lines, h)=>lines.length >= h ? lines.slice(0, h) : [
|
|
743
|
+
...lines,
|
|
744
|
+
...Array.from({
|
|
745
|
+
length: h - lines.length
|
|
746
|
+
}, ()=>[])
|
|
747
|
+
];
|
|
748
|
+
let width = (l)=>l.reduce((n, s)=>n + s.text.length, 0);
|
|
749
|
+
// Fill a line out to `w` columns in `style`, the line set to `align`'s side.
|
|
750
|
+
let pad = (l, w, align = 'left', style = {})=>{
|
|
751
|
+
let n = w - width(l);
|
|
752
|
+
if (n <= 0) return l;
|
|
753
|
+
let before = align == 'right' ? n : align == 'center' ? n >> 1 : 0;
|
|
754
|
+
let fill = (k)=>k ? [
|
|
755
|
+
{
|
|
756
|
+
text: ' '.repeat(k),
|
|
757
|
+
style,
|
|
758
|
+
decorative: true
|
|
759
|
+
}
|
|
760
|
+
] : [];
|
|
761
|
+
return [
|
|
762
|
+
...fill(before),
|
|
763
|
+
...l,
|
|
764
|
+
...fill(n - before)
|
|
765
|
+
];
|
|
766
|
+
};
|
|
767
|
+
/// let texts = (l: Line) => l.map((s) => s.text)
|
|
768
|
+
/// let line = [{ text: 'hello ', style: {} }, { text: 'world', style: {} }]
|
|
769
|
+
/// texts(clip(line, 8)) -> ['hello ', 'wo']
|
|
770
|
+
/// texts(clip(line, 8, true)) -> ['hello ', 'w', '…']
|
|
771
|
+
/// texts(clip(line, 11, true)) -> ['hello ', 'world']
|
|
772
|
+
/** Cut a line to a column count, keeping whole segments where it can; with
|
|
773
|
+
* `ellipsis`, a line that is cut ends in `…`. */ export let clip = (line, w, ellipsis = false)=>{
|
|
774
|
+
if (ellipsis && w > 0 && width(line) > w) {
|
|
775
|
+
let out = clip(line, w - 1);
|
|
776
|
+
let { style, owner } = out.at(-1) ?? line[0];
|
|
777
|
+
return [
|
|
778
|
+
...out,
|
|
779
|
+
{
|
|
780
|
+
text: '…',
|
|
781
|
+
style,
|
|
782
|
+
owner
|
|
783
|
+
}
|
|
784
|
+
];
|
|
785
|
+
}
|
|
786
|
+
let out = [];
|
|
787
|
+
let len = 0;
|
|
788
|
+
for (let s of line){
|
|
789
|
+
if (len + s.text.length <= w) {
|
|
790
|
+
out.push(s);
|
|
791
|
+
len += s.text.length;
|
|
792
|
+
} else {
|
|
793
|
+
if (w > len) out.push({
|
|
794
|
+
...s,
|
|
795
|
+
text: s.text.slice(0, w - len)
|
|
796
|
+
});
|
|
797
|
+
break;
|
|
798
|
+
}
|
|
799
|
+
}
|
|
800
|
+
return out;
|
|
801
|
+
};
|
|
802
|
+
/** Fold at word boundaries (hard-fold long words), preserving every styled
|
|
803
|
+
* character. Uses the same column units as clip; runs before scroll measures. */ export let wrap = (line, columns)=>{
|
|
804
|
+
let w = Math.floor(columns);
|
|
805
|
+
if (w <= 0) return [
|
|
806
|
+
[]
|
|
807
|
+
];
|
|
808
|
+
let text = line.map((s)=>s.text).join('');
|
|
809
|
+
if (text.length <= w) return [
|
|
810
|
+
line
|
|
811
|
+
];
|
|
812
|
+
let out = [];
|
|
813
|
+
let start = 0, seg = 0, offset = 0;
|
|
814
|
+
while(start < text.length){
|
|
815
|
+
let end = Math.min(start + w, text.length);
|
|
816
|
+
if (end < text.length) {
|
|
817
|
+
let space = text.slice(start, end).lastIndexOf(' ');
|
|
818
|
+
if (space >= 0) end = start + space + 1;
|
|
819
|
+
}
|
|
820
|
+
let row = [];
|
|
821
|
+
let left = end - start;
|
|
822
|
+
while(left > 0 && seg < line.length){
|
|
823
|
+
let s = line[seg];
|
|
824
|
+
let n = Math.min(left, s.text.length - offset);
|
|
825
|
+
if (n) {
|
|
826
|
+
row.push({
|
|
827
|
+
...s,
|
|
828
|
+
text: s.text.slice(offset, offset + n)
|
|
829
|
+
});
|
|
830
|
+
}
|
|
831
|
+
left -= n;
|
|
832
|
+
offset += n;
|
|
833
|
+
if (offset == s.text.length) {
|
|
834
|
+
seg++;
|
|
835
|
+
offset = 0;
|
|
836
|
+
}
|
|
837
|
+
}
|
|
838
|
+
if (end < text.length && row.length) {
|
|
839
|
+
row[row.length - 1] = {
|
|
840
|
+
...row.at(-1),
|
|
841
|
+
softBreak: true
|
|
842
|
+
};
|
|
843
|
+
}
|
|
844
|
+
out.push(row);
|
|
845
|
+
start = end;
|
|
846
|
+
}
|
|
847
|
+
return out;
|
|
848
|
+
};
|
|
849
|
+
let rgb = (hex)=>[
|
|
850
|
+
hex.slice(1, 3),
|
|
851
|
+
hex.slice(3, 5),
|
|
852
|
+
hex.slice(5, 7)
|
|
853
|
+
].map((h)=>parseInt(h, 16));
|
|
854
|
+
// Whether two styles paint alike, so a run in one can carry on in the other.
|
|
855
|
+
let alike = (a, b)=>ansi([
|
|
856
|
+
{
|
|
857
|
+
text: '',
|
|
858
|
+
style: a
|
|
859
|
+
}
|
|
860
|
+
]) == ansi([
|
|
861
|
+
{
|
|
862
|
+
text: '',
|
|
863
|
+
style: b
|
|
864
|
+
}
|
|
865
|
+
]);
|
|
866
|
+
/** The bytes a line becomes — every escape the terminal sees is emitted here. */ export let ansi = (line)=>line.map((s)=>{
|
|
867
|
+
let codes = [];
|
|
868
|
+
if (s.style.fg) codes.push(`38;2;${rgb(s.style.fg).join(';')}`);
|
|
869
|
+
if (s.style.bg) codes.push(`48;2;` + rgb(s.style.bg).join(';'));
|
|
870
|
+
if (s.style.bold) codes.push('1');
|
|
871
|
+
if (s.style.dim) codes.push('2');
|
|
872
|
+
if (s.style.italic) codes.push('3');
|
|
873
|
+
if (s.style.underline) codes.push('4');
|
|
874
|
+
if (s.style.inverse) codes.push('7');
|
|
875
|
+
if (s.style.strike) codes.push('9');
|
|
876
|
+
let t = codes.length ? `\x1b[${codes.join(';')}m${s.text}\x1b[0m` : s.text;
|
|
877
|
+
return s.style.href // OSC 8: the terminal makes it clickable
|
|
878
|
+
? `\x1b]8;;${s.style.href}\x07${t}\x1b]8;;\x07` : t;
|
|
879
|
+
}).join('');
|
|
880
|
+
/** The whole screen a tree produces at a given size — the function the tests
|
|
881
|
+
* assert against. */ export let screenful = (root, columns, rows, sheet = {})=>{
|
|
882
|
+
let c = {
|
|
883
|
+
sheet: compose(sheet),
|
|
884
|
+
metrics: {},
|
|
885
|
+
hidden: new Set()
|
|
886
|
+
};
|
|
887
|
+
let lines = lay(root, {}, columns, rows, c);
|
|
888
|
+
return {
|
|
889
|
+
lines,
|
|
890
|
+
metrics: c.metrics,
|
|
891
|
+
hidden: c.hidden
|
|
892
|
+
};
|
|
893
|
+
};
|
|
894
|
+
/** A tree painted once, as tall as it is, for a command that prints and exits:
|
|
895
|
+
* the layout and escapes a held screen gets, folded at word boundaries to the
|
|
896
|
+
* terminal's width, with no screen kept. `sheet` extends {@link base}, as it
|
|
897
|
+
* does for {@link ansiBackend}. */ export let printout = (root, columns, sheet = {})=>{
|
|
898
|
+
let lines = lay(root, {}, columns, null, {
|
|
899
|
+
sheet: compose(sheet),
|
|
900
|
+
metrics: {}
|
|
901
|
+
}).flatMap((line)=>wrap(line, columns));
|
|
902
|
+
while(lines.length && !width(lines.at(-1)))lines.pop();
|
|
903
|
+
return lines.map((line)=>ansi(line).trimEnd()).join('\n');
|
|
904
|
+
};
|
|
905
|
+
let enc = new TextEncoder();
|
|
906
|
+
/**
|
|
907
|
+
* OSC 52: hand text to the clipboard through the terminal — the escape travels
|
|
908
|
+
* the tty like any output, so it works across ssh (tmux needs set-clipboard on).
|
|
909
|
+
*/ export let clipboard = (text)=>`\x1b]52;c;${btoa(Array.from(enc.encode(text), (b)=>String.fromCharCode(b)).join(''))}\x07`;
|
|
910
|
+
// A DEC private mode the terminal may already have set (a parent app's mouse,
|
|
911
|
+
// its focus reports): saved before it is set, and on the way out reset and
|
|
912
|
+
// then restored. A terminal that keeps saved modes gets back what it had; one
|
|
913
|
+
// that ignores save and restore (tmux among them) is left with it off, never
|
|
914
|
+
// still on.
|
|
915
|
+
let saved = (n)=>[
|
|
916
|
+
`\x1b[?${n}s\x1b[?${n}h`,
|
|
917
|
+
`\x1b[?${n}l\x1b[?${n}r`
|
|
918
|
+
];
|
|
919
|
+
/**
|
|
920
|
+
* Every mode the ANSI backend turns on, as `[on, off]`: `start` writes each
|
|
921
|
+
* `on` in order and `stop` each `off` in reverse, so the terminal is given
|
|
922
|
+
* back as it was found.
|
|
923
|
+
*/ export let MODES = [
|
|
924
|
+
[
|
|
925
|
+
'\x1b[?1049h',
|
|
926
|
+
'\x1b[?1049l'
|
|
927
|
+
],
|
|
928
|
+
[
|
|
929
|
+
'\x1b[?25l',
|
|
930
|
+
'\x1b[?25h'
|
|
931
|
+
],
|
|
932
|
+
[
|
|
933
|
+
'\x1b[?2004h',
|
|
934
|
+
'\x1b[?2004l'
|
|
935
|
+
],
|
|
936
|
+
[
|
|
937
|
+
'\x1b[?1007h',
|
|
938
|
+
'\x1b[?1007l'
|
|
939
|
+
],
|
|
940
|
+
saved(1004),
|
|
941
|
+
saved(1000),
|
|
942
|
+
saved(1006),
|
|
943
|
+
[
|
|
944
|
+
'\x1b[>4;2m',
|
|
945
|
+
'\x1b[>4;0m'
|
|
946
|
+
],
|
|
947
|
+
// kitty disambiguate: without it the terminal collapses Shift+Enter to CR
|
|
948
|
+
[
|
|
949
|
+
'\x1b[>1u',
|
|
950
|
+
'\x1b[<u'
|
|
951
|
+
]
|
|
952
|
+
];
|
|
953
|
+
/**
|
|
954
|
+
* The ANSI backend. Paints only the lines that changed since the last frame:
|
|
955
|
+
* a keystroke moves one line, so a keystroke writes one line. `size` and
|
|
956
|
+
* `write` are injected so a test can drive it without a terminal. A `sheet`
|
|
957
|
+
* given as a function is read at every paint, so the theme can change while
|
|
958
|
+
* the app runs.
|
|
959
|
+
*/ export let ansiBackend = (opts = {})=>{
|
|
960
|
+
let pictures = graphics({
|
|
961
|
+
enabled: opts.graphics == 'kitty',
|
|
962
|
+
tmux: opts.tmux,
|
|
963
|
+
changed: touch
|
|
964
|
+
});
|
|
965
|
+
let sheet = ()=>typeof opts.sheet == 'function' ? opts.sheet() : opts.sheet;
|
|
966
|
+
let size = opts.size ?? (()=>system.consoleSize());
|
|
967
|
+
let write = opts.write ?? ((s)=>void system.stdout.writeSync(enc.encode(s)));
|
|
968
|
+
let last = [];
|
|
969
|
+
return {
|
|
970
|
+
size,
|
|
971
|
+
copy: (text)=>write(osc52(text)),
|
|
972
|
+
control: pictures.reply,
|
|
973
|
+
start: ()=>write(MODES.map(([on])=>on).join('')),
|
|
974
|
+
stop: ()=>write(pictures.close() + MODES.map(([, off])=>off).reverse().join('')),
|
|
975
|
+
reset: ()=>{
|
|
976
|
+
last = [];
|
|
977
|
+
pictures.reset();
|
|
978
|
+
},
|
|
979
|
+
draw: (root)=>{
|
|
980
|
+
let { columns, rows } = size();
|
|
981
|
+
let { lines, metrics, hidden } = screenful(root, columns, rows, sheet());
|
|
982
|
+
let annotated = pictures.annotate(lines);
|
|
983
|
+
let out = '';
|
|
984
|
+
let written = 0;
|
|
985
|
+
for(let y = 0; y < rows; y++){
|
|
986
|
+
let s = ansi(clip(annotated[y] ?? [], columns));
|
|
987
|
+
if (s === last[y]) continue;
|
|
988
|
+
last[y] = s;
|
|
989
|
+
out += `\x1b[${y + 1};1H${s}\x1b[K`;
|
|
990
|
+
written++;
|
|
991
|
+
}
|
|
992
|
+
last.length = rows;
|
|
993
|
+
out += pictures.draw(lines.slice(0, rows).map((line)=>clip(line, columns)), !!out);
|
|
994
|
+
if (out) write(out);
|
|
995
|
+
return {
|
|
996
|
+
written,
|
|
997
|
+
metrics,
|
|
998
|
+
hidden,
|
|
999
|
+
lines: lines.slice(0, rows).map((line)=>clip(line, columns))
|
|
1000
|
+
};
|
|
1001
|
+
}
|
|
1002
|
+
};
|
|
1003
|
+
};
|
|
1004
|
+
/** Clipboard bytes are base64, never interpolated as terminal controls. */ export let osc52 = (text)=>{
|
|
1005
|
+
let bytes = new TextEncoder().encode(text);
|
|
1006
|
+
let binary = '';
|
|
1007
|
+
for (let b of bytes)binary += String.fromCharCode(b);
|
|
1008
|
+
return '\x1b]52;c;' + btoa(binary) + '\x07';
|
|
1009
|
+
};
|
|
1010
|
+
//# sourceMappingURL=paint.js.map
|