@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/screen.ts
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { interceptKey } from './keymap.js'
|
|
2
|
+
/**
|
|
3
|
+
* What a widget knows about the screen it is on, and how a key reaches it.
|
|
4
|
+
* Three signals in one place: the terminal's size (a widget that collapses
|
|
5
|
+
* needs it), the measurements the last paint took (a scroll region learns its
|
|
6
|
+
* content height from the painter, since only the painter does layout), and
|
|
7
|
+
* the focus stack — `useKeys` puts a handler on it while mounted, and `press`
|
|
8
|
+
* offers a key to the topmost handler first, walking down until one takes it.
|
|
9
|
+
* Last mounted has focus; a handler that returns anything falsy passes.
|
|
10
|
+
*
|
|
11
|
+
* @module
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { visualKey } from './visual.js'
|
|
15
|
+
import { computed, type Signal, signal } from '@preact/signals-core'
|
|
16
|
+
/* @ts-types="npm:@types/react@19.3.0" */ import {
|
|
17
|
+
useCallback,
|
|
18
|
+
useLayoutEffect,
|
|
19
|
+
useMemo,
|
|
20
|
+
useRef,
|
|
21
|
+
useSyncExternalStore,
|
|
22
|
+
} from 'react'
|
|
23
|
+
import type { Key } from './input.ts'
|
|
24
|
+
import type { Metrics } from './paint.ts'
|
|
25
|
+
|
|
26
|
+
/** The terminal's size in cells, kept current by `run`. */
|
|
27
|
+
export let size: Signal<{ columns: number; rows: number }> = signal({
|
|
28
|
+
columns: 80,
|
|
29
|
+
rows: 24,
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
/** Terminal focus reporting is independent of application/widget focus.
|
|
33
|
+
* Defaults to focused when the terminal does not send focus reports. */
|
|
34
|
+
export let terminalFocused: Signal<boolean> = signal(true)
|
|
35
|
+
|
|
36
|
+
/** What the last paint measured, per element id. */
|
|
37
|
+
export let metrics: Signal<Metrics> = signal<Metrics>({})
|
|
38
|
+
|
|
39
|
+
/** What a key handler returns: true when it consumed the key. */
|
|
40
|
+
export type Keys = (key: Key) => boolean | void
|
|
41
|
+
|
|
42
|
+
let stack: Keys[] = []
|
|
43
|
+
let targets = new Map<string, Keys>()
|
|
44
|
+
|
|
45
|
+
/** Deliver a command to a named widget without changing keyboard focus. */
|
|
46
|
+
export let pressTo = (id: string, key: Key): boolean => !!targets.get(id)?.(key)
|
|
47
|
+
|
|
48
|
+
/** Offer a key to the focus stack, topmost first. */
|
|
49
|
+
export let press = (key: Key): boolean => {
|
|
50
|
+
if (key.name == 'focusin' || key.name == 'focusout') {
|
|
51
|
+
terminalFocused.value = key.name == 'focusin'
|
|
52
|
+
return true
|
|
53
|
+
}
|
|
54
|
+
if (interceptKey(key)) return true
|
|
55
|
+
if (visualKey(key)) return true
|
|
56
|
+
return pressFocused(key)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Forward an already-routed key to the focused widget stack. */
|
|
60
|
+
export let pressFocused = (key: Key): boolean => {
|
|
61
|
+
for (let i = stack.length - 1; i >= 0; i--) if (stack[i](key)) return true
|
|
62
|
+
return false
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Take keys while this component is mounted; the newest mount has focus. */
|
|
66
|
+
export let useKeys = (fn: Keys, id?: string): void => {
|
|
67
|
+
// The handler closes over this render's state, so the stack holds a stable
|
|
68
|
+
// shim and the shim reads the newest closure — registering the closure
|
|
69
|
+
// itself would reorder focus on every render.
|
|
70
|
+
let ref = useRef(fn)
|
|
71
|
+
ref.current = fn
|
|
72
|
+
// A layout effect, not an effect: outside a browser Preact defers effects
|
|
73
|
+
// to a frame that may be 100ms away, and a key pressed before then would
|
|
74
|
+
// reach nobody.
|
|
75
|
+
useLayoutEffect(() => {
|
|
76
|
+
let shim: Keys = (k) => ref.current(k)
|
|
77
|
+
stack.push(shim)
|
|
78
|
+
return () => {
|
|
79
|
+
stack.splice(stack.indexOf(shim), 1)
|
|
80
|
+
}
|
|
81
|
+
}, [])
|
|
82
|
+
useLayoutEffect(() => {
|
|
83
|
+
if (!id) return
|
|
84
|
+
let shim: Keys = (k) => ref.current(k)
|
|
85
|
+
targets.set(id, shim)
|
|
86
|
+
return () => {
|
|
87
|
+
if (targets.get(id) === shim) targets.delete(id)
|
|
88
|
+
}
|
|
89
|
+
}, [id])
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
type Metric = Metrics[string]
|
|
93
|
+
let empty: Metric = { total: 0, height: 0, width: 0 }
|
|
94
|
+
|
|
95
|
+
/** Read presentation state and repaint when it changes. */
|
|
96
|
+
export let useScreen = <T>(state: Signal<T>): T => {
|
|
97
|
+
let subscribe = useCallback((wake: () => void) => state.subscribe(wake), [
|
|
98
|
+
state,
|
|
99
|
+
])
|
|
100
|
+
return useSyncExternalStore(subscribe, () => state.peek())
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** What the painter measured for an element id on the last paint, or one of
|
|
104
|
+
* its measures. */
|
|
105
|
+
export function useMetric(id: string): Metric
|
|
106
|
+
export function useMetric<K extends keyof Metric>(id: string, key: K): Metric[K]
|
|
107
|
+
export function useMetric<K extends keyof Metric>(
|
|
108
|
+
id: string,
|
|
109
|
+
key?: K,
|
|
110
|
+
): Metric | Metric[K] {
|
|
111
|
+
// Read through a signal of its own, so a component re-renders only when what
|
|
112
|
+
// it reads changed, and a fresh one when the id it measures changes.
|
|
113
|
+
let measure = useMemo(
|
|
114
|
+
() =>
|
|
115
|
+
computed(() => {
|
|
116
|
+
let value = metrics.value[id] ?? empty
|
|
117
|
+
return key == null ? value : value[key]
|
|
118
|
+
}),
|
|
119
|
+
[id, key],
|
|
120
|
+
)
|
|
121
|
+
return useScreen(measure)
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** Publish a paint's measurements; unchanged measurements re-render nothing. */
|
|
125
|
+
export let measured = (next: Metrics): void => {
|
|
126
|
+
let now = metrics.value
|
|
127
|
+
let same = Object.keys(next).length == Object.keys(now).length &&
|
|
128
|
+
Object.entries(next).every(([k, v]) =>
|
|
129
|
+
now[k]?.total == v.total && now[k]?.height == v.height &&
|
|
130
|
+
now[k]?.width == v.width && now[k]?.reveal == v.reveal
|
|
131
|
+
)
|
|
132
|
+
if (!same) metrics.value = next
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** Forget the focus stack — for a test that mounts more than one app. */
|
|
136
|
+
export let clear = (): void => {
|
|
137
|
+
stack.length = 0
|
|
138
|
+
terminalFocused.value = true
|
|
139
|
+
}
|
package/scrollbar.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/** A fixed three-cell thumb over an estimated (or exact) scroll range. */ export let scrollbar = (lines, width, position, sheet)=>{
|
|
2
|
+
if (width < 2 || position.height <= 0) return lines;
|
|
3
|
+
let { height, total, top, bottom } = position;
|
|
4
|
+
let thumb = Math.min(3, height);
|
|
5
|
+
let range = Math.max(0, total - height);
|
|
6
|
+
let progress = bottom ? 1 : range ? Math.max(0, Math.min(1, top / range)) : 0;
|
|
7
|
+
let start = Math.round(progress * (height - thumb));
|
|
8
|
+
let style = {
|
|
9
|
+
...sheet.Scrollbar,
|
|
10
|
+
...bottom ? sheet.Scrollbar_Snapped : {}
|
|
11
|
+
};
|
|
12
|
+
return Array.from({
|
|
13
|
+
length: height
|
|
14
|
+
}, (_, i)=>{
|
|
15
|
+
let line = lines[i] ?? [];
|
|
16
|
+
let used = line.reduce((n, s)=>n + s.text.length, 0);
|
|
17
|
+
return [
|
|
18
|
+
...line,
|
|
19
|
+
{
|
|
20
|
+
text: ' '.repeat(Math.max(0, width - 1 - used)),
|
|
21
|
+
style: {}
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
text: i >= start && i < start + thumb ? '█' : '│',
|
|
25
|
+
style
|
|
26
|
+
}
|
|
27
|
+
];
|
|
28
|
+
});
|
|
29
|
+
};
|
|
30
|
+
//# sourceMappingURL=scrollbar.js.map
|
package/scrollbar.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"scrollbar.js","sources":["./scrollbar.ts"],"names":[],"mappings":"AAAA,wEAAwE,GAWxE,OAAO,IAAI,YAAY,CACrB,OACA,OACA,UACA;EAEA,IAAI,QAAQ,KAAK,SAAS,MAAM,IAAI,GAAG,OAAO;EAC9C,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG;EACrC,IAAI,QAAQ,KAAK,GAAG,CAAC,GAAG;EACxB,IAAI,QAAQ,KAAK,GAAG,CAAC,GAAG,QAAQ;EAChC,IAAI,WAAW,SAAS,IAAI,QAAQ,KAAK,GAAG,CAAC,GAAG,KAAK,GAAG,CAAC,GAAG,MAAM,UAAU;EAC5E,IAAI,QAAQ,KAAK,KAAK,CAAC,WAAW,CAAC,SAAS,KAAK;EACjD,IAAI,QAAQ;IAAE,GAAG,MAAM,SAAS;IAAE,GAAI,SAAS,MAAM,iBAAiB,GAAG,CAAC,CAAC;EAAE;EAC7E,OAAO,MAAM,IAAI,CAAC;IAAE,QAAQ;EAAO,GAAG,CAAC,GAAG;IACxC,IAAI,OAAO,KAAK,CAAC,EAAE,IAAI,EAAE;IACzB,IAAI,OAAO,KAAK,MAAM,CAAC,CAAC,GAAG,IAAM,IAAI,EAAE,IAAI,CAAC,MAAM,EAAE;IACpD,OAAO;SAAI;MAAM;QACf,MAAM,IAAI,MAAM,CAAC,KAAK,GAAG,CAAC,GAAG,QAAQ,IAAI;QACzC,OAAO,CAAC;MACV;MAAG;QAAE,MAAM,KAAK,SAAS,IAAI,QAAQ,QAAQ,MAAM;QAAK;MAAM;KAAE;EAClE;AACF,EAAC"}
|
package/scrollbar.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/** A fixed three-cell thumb over an estimated (or exact) scroll range. */
|
|
2
|
+
import type { Line } from './paint.ts'
|
|
3
|
+
import type { Sheet } from 'jsr:@yaks/ui@0.2.36/sheet'
|
|
4
|
+
|
|
5
|
+
export type ScrollPosition = {
|
|
6
|
+
total: number
|
|
7
|
+
top: number
|
|
8
|
+
height: number
|
|
9
|
+
bottom: boolean
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export let scrollbar = (
|
|
13
|
+
lines: Line[],
|
|
14
|
+
width: number,
|
|
15
|
+
position: ScrollPosition,
|
|
16
|
+
sheet: Sheet,
|
|
17
|
+
): Line[] => {
|
|
18
|
+
if (width < 2 || position.height <= 0) return lines
|
|
19
|
+
let { height, total, top, bottom } = position
|
|
20
|
+
let thumb = Math.min(3, height)
|
|
21
|
+
let range = Math.max(0, total - height)
|
|
22
|
+
let progress = bottom ? 1 : range ? Math.max(0, Math.min(1, top / range)) : 0
|
|
23
|
+
let start = Math.round(progress * (height - thumb))
|
|
24
|
+
let style = { ...sheet.Scrollbar, ...(bottom ? sheet.Scrollbar_Snapped : {}) }
|
|
25
|
+
return Array.from({ length: height }, (_, i) => {
|
|
26
|
+
let line = lines[i] ?? []
|
|
27
|
+
let used = line.reduce((n, s) => n + s.text.length, 0)
|
|
28
|
+
return [...line, {
|
|
29
|
+
text: ' '.repeat(Math.max(0, width - 1 - used)),
|
|
30
|
+
style: {},
|
|
31
|
+
}, { text: i >= start && i < start + thumb ? '█' : '│', style }]
|
|
32
|
+
})
|
|
33
|
+
}
|
package/table.js
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
/** Semantic table layout. Intrinsic sizing samples bounded text; cells wrap at
|
|
2
|
+
* their allocated widths. This module does not depend on Markdown or graphs. */ import { TElement, TText } from "@yaks/dom";
|
|
3
|
+
let elements = (el)=>el.childNodes.filter((n)=>n instanceof TElement);
|
|
4
|
+
let rowsOf = (el)=>elements(el).flatMap((n)=>n.localName == 'tr' ? [
|
|
5
|
+
n
|
|
6
|
+
] : [
|
|
7
|
+
'thead',
|
|
8
|
+
'tbody',
|
|
9
|
+
'tfoot'
|
|
10
|
+
].includes(n.localName) ? elements(n).filter((r)=>r.localName == 'tr') : []);
|
|
11
|
+
let cellsOf = (el)=>elements(el).filter((n)=>[
|
|
12
|
+
'td',
|
|
13
|
+
'th'
|
|
14
|
+
].includes(n.localName));
|
|
15
|
+
let width = (line)=>line.reduce((n, s)=>n + s.text.length, 0);
|
|
16
|
+
// Do not scan every large cell just to determine column widths. The actual
|
|
17
|
+
// layout still reads all displayed cells; virtualization operates per item.
|
|
18
|
+
let sample = (cell)=>{
|
|
19
|
+
let text = '', visited = 0;
|
|
20
|
+
let walk = (node)=>{
|
|
21
|
+
if (++visited > 128 || text.length >= 128) return;
|
|
22
|
+
if (node instanceof TText) text += node.data.slice(0, 128 - text.length);
|
|
23
|
+
else if (node.localName == 'br') text += '\n';
|
|
24
|
+
else {
|
|
25
|
+
for (let child of node.childNodes){
|
|
26
|
+
if (child instanceof TElement || child instanceof TText) walk(child);
|
|
27
|
+
if (visited > 128 || text.length >= 128) break;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
if (cell) walk(cell);
|
|
32
|
+
return text;
|
|
33
|
+
};
|
|
34
|
+
export let table = (el, available, style, sheet, layout, wrap)=>{
|
|
35
|
+
let rows = rowsOf(el).map(cellsOf);
|
|
36
|
+
let count = rows.reduce((n, r)=>Math.max(n, r.length), 0);
|
|
37
|
+
if (!count || available < 1) return [];
|
|
38
|
+
let edge = {
|
|
39
|
+
...style,
|
|
40
|
+
...sheet.Table_Border
|
|
41
|
+
};
|
|
42
|
+
let seg = (text, st = style)=>({
|
|
43
|
+
text,
|
|
44
|
+
style: st,
|
|
45
|
+
decorative: st === edge
|
|
46
|
+
});
|
|
47
|
+
let cellLines = (cell, w)=>{
|
|
48
|
+
if (!cell) return [
|
|
49
|
+
[]
|
|
50
|
+
];
|
|
51
|
+
let result = layout(cell, w).flatMap((line)=>wrap(line, w));
|
|
52
|
+
if (cell.localName == 'th') {
|
|
53
|
+
result = result.map((line)=>line.map((s)=>({
|
|
54
|
+
...s,
|
|
55
|
+
style: {
|
|
56
|
+
...s.style,
|
|
57
|
+
...sheet.Table_Header
|
|
58
|
+
}
|
|
59
|
+
})));
|
|
60
|
+
}
|
|
61
|
+
return result.length ? result : [
|
|
62
|
+
[]
|
|
63
|
+
];
|
|
64
|
+
};
|
|
65
|
+
// A vertical record layout is more useful than one-character columns.
|
|
66
|
+
if (available < count * 6 + 1) {
|
|
67
|
+
let header = rows[0].some((c)=>c.localName == 'th') ? rows[0] : undefined;
|
|
68
|
+
let records = header && rows.length > 1 ? rows.slice(1) : rows;
|
|
69
|
+
return records.flatMap((row, index)=>{
|
|
70
|
+
let lines = index ? [
|
|
71
|
+
[
|
|
72
|
+
seg('─'.repeat(available), edge)
|
|
73
|
+
]
|
|
74
|
+
] : [];
|
|
75
|
+
row.forEach((cell, col)=>{
|
|
76
|
+
if (header && records !== rows) {
|
|
77
|
+
lines.push(...cellLines(header[col], available));
|
|
78
|
+
}
|
|
79
|
+
lines.push(...cellLines(cell, available));
|
|
80
|
+
});
|
|
81
|
+
return lines;
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
let budget = available - (count * 3 + 1);
|
|
85
|
+
let desired = Array.from({
|
|
86
|
+
length: count
|
|
87
|
+
}, (_, col)=>Math.max(3, ...rows.slice(0, 32).map((row)=>Math.min(40, Math.max(...sample(row[col]).split('\n').map((s)=>s.length))))));
|
|
88
|
+
let widths = Array(count).fill(3);
|
|
89
|
+
let remaining = budget - count * 3;
|
|
90
|
+
while(remaining > 0){
|
|
91
|
+
let grew = false;
|
|
92
|
+
for(let i = 0; i < count && remaining > 0; i++){
|
|
93
|
+
if (widths[i] < desired[i]) {
|
|
94
|
+
widths[i]++;
|
|
95
|
+
remaining--;
|
|
96
|
+
grew = true;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
if (!grew) break;
|
|
100
|
+
}
|
|
101
|
+
// Intrinsic widths guide the first allocation; share all remaining space.
|
|
102
|
+
let extra = Math.floor(remaining / count);
|
|
103
|
+
let remainder = remaining % count;
|
|
104
|
+
widths = widths.map((w, i)=>w + extra + (i < remainder ? 1 : 0));
|
|
105
|
+
let rule = (left, middle, right)=>[
|
|
106
|
+
seg(left + widths.map((w)=>'─'.repeat(w + 2)).join(middle) + right, edge)
|
|
107
|
+
];
|
|
108
|
+
let out = [
|
|
109
|
+
rule('┌', '┬', '┐')
|
|
110
|
+
];
|
|
111
|
+
rows.forEach((row, index)=>{
|
|
112
|
+
let columns = widths.map((w, i)=>cellLines(row[i], w));
|
|
113
|
+
let height = Math.max(...columns.map((c)=>c.length));
|
|
114
|
+
for(let y = 0; y < height; y++){
|
|
115
|
+
let line = [
|
|
116
|
+
seg('│', edge)
|
|
117
|
+
];
|
|
118
|
+
widths.forEach((w, col)=>{
|
|
119
|
+
let content = columns[col][y] ?? [];
|
|
120
|
+
let space = Math.max(0, w - width(content));
|
|
121
|
+
let align = row[col]?.attr('align') ?? /text-align\s*:\s*(left|right|center)/.exec(row[col]?.attr('style') ?? '')?.[1];
|
|
122
|
+
let before = align == 'right' ? space : align == 'center' ? Math.floor(space / 2) : 0;
|
|
123
|
+
line.push(seg(' ' + ' '.repeat(before)), ...content, seg(' '.repeat(space - before) + ' '), seg('│', edge));
|
|
124
|
+
});
|
|
125
|
+
out.push(line);
|
|
126
|
+
}
|
|
127
|
+
if (index < rows.length - 1) {
|
|
128
|
+
out.push(rule('├', '┼', '┤'));
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
out.push(rule('└', '┴', '┘'));
|
|
132
|
+
return out;
|
|
133
|
+
};
|
|
134
|
+
//# sourceMappingURL=table.js.map
|
package/table.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"table.js","sources":["./table.ts"],"names":[],"mappings":"AAAA;8EAC8E,GAC9E,SAAS,QAAQ,EAAE,KAAK,yBAA8B;AAKtD,IAAI,WAAW,CAAC,KACd,GAAG,UAAU,CAAC,MAAM,CAAC,CAAC,IAAqB,aAAa;AAC1D,IAAI,SAAS,CAAC,KACZ,SAAS,IAAI,OAAO,C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AE,IAAI,EAAE;QACnC,IAAI,QAAQ,KAAK,GAAG,CAAC,GAAG,IAAI,MAAM;QAClC,IAAI,QAAQ,GAAG,CAAC,IAAI,EAAE,KAAK,YACzB,uCAAuC,IAAI,CACzC,GAAG,CAAC,IAAI,EAAE,KAAK,YAAY,KAC1B,CAAC,EAAE;QACR,IAAI,SAAS,SAAS,UAClB,QACA,SAAS,WACT,KAAK,KAAK,CAAC,QAAQ,KACnB;QACJ,KAAK,IAAI,CACP,IAAI,MAAM,IAAI,MAAM,CAAC,aAClB,SACH,IAAI,IAAI,MAAM,CAAC,QAAQ,UAAU,MACjC,IAAI,KAAK;MAEb;MACA,IAAI,IAAI,CAAC;IACX;IACA,IAAI,QAAQ,KAAK,MAAM,GAAG,GAAG;MAC3B,IAAI,IAAI,CAAC,KAAK,KAAK,KAAK;IAC1B;EACF;EACA,IAAI,IAAI,CAAC,KAAK,KAAK,KAAK;EACxB,OAAO;AACT,EAAC"}
|
package/table.ts
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/** Semantic table layout. Intrinsic sizing samples bounded text; cells wrap at
|
|
2
|
+
* their allocated widths. This module does not depend on Markdown or graphs. */
|
|
3
|
+
import { TElement, TText } from '@yaks/dom'
|
|
4
|
+
import type { Line } from './paint.ts'
|
|
5
|
+
import type { Sheet, Style } from 'jsr:@yaks/ui@0.2.36/sheet'
|
|
6
|
+
|
|
7
|
+
type Cell = TElement | undefined
|
|
8
|
+
let elements = (el: TElement) =>
|
|
9
|
+
el.childNodes.filter((n): n is TElement => n instanceof TElement)
|
|
10
|
+
let rowsOf = (el: TElement): TElement[] =>
|
|
11
|
+
elements(el).flatMap((n) =>
|
|
12
|
+
n.localName == 'tr'
|
|
13
|
+
? [n]
|
|
14
|
+
: ['thead', 'tbody', 'tfoot'].includes(n.localName)
|
|
15
|
+
? elements(n).filter((r) => r.localName == 'tr')
|
|
16
|
+
: []
|
|
17
|
+
)
|
|
18
|
+
let cellsOf = (el: TElement) =>
|
|
19
|
+
elements(el).filter((n) => ['td', 'th'].includes(n.localName))
|
|
20
|
+
let width = (line: Line) => line.reduce((n, s) => n + s.text.length, 0)
|
|
21
|
+
|
|
22
|
+
// Do not scan every large cell just to determine column widths. The actual
|
|
23
|
+
// layout still reads all displayed cells; virtualization operates per item.
|
|
24
|
+
let sample = (cell: Cell): string => {
|
|
25
|
+
let text = '', visited = 0
|
|
26
|
+
let walk = (node: TElement | TText) => {
|
|
27
|
+
if (++visited > 128 || text.length >= 128) return
|
|
28
|
+
if (node instanceof TText) text += node.data.slice(0, 128 - text.length)
|
|
29
|
+
else if (node.localName == 'br') text += '\n'
|
|
30
|
+
else {for (let child of node.childNodes) {
|
|
31
|
+
if (child instanceof TElement || child instanceof TText) walk(child)
|
|
32
|
+
if (visited > 128 || text.length >= 128) break
|
|
33
|
+
}}
|
|
34
|
+
}
|
|
35
|
+
if (cell) walk(cell)
|
|
36
|
+
return text
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export let table = (
|
|
40
|
+
el: TElement,
|
|
41
|
+
available: number,
|
|
42
|
+
style: Style,
|
|
43
|
+
sheet: Sheet,
|
|
44
|
+
layout: (cell: TElement, width: number) => Line[],
|
|
45
|
+
wrap: (line: Line, width: number) => Line[],
|
|
46
|
+
): Line[] => {
|
|
47
|
+
let rows = rowsOf(el).map(cellsOf)
|
|
48
|
+
let count = rows.reduce((n, r) => Math.max(n, r.length), 0)
|
|
49
|
+
if (!count || available < 1) return []
|
|
50
|
+
let edge = { ...style, ...sheet.Table_Border }
|
|
51
|
+
let seg = (text: string, st = style) => ({
|
|
52
|
+
text,
|
|
53
|
+
style: st,
|
|
54
|
+
decorative: st === edge,
|
|
55
|
+
})
|
|
56
|
+
let cellLines = (cell: Cell, w: number): Line[] => {
|
|
57
|
+
if (!cell) return [[]]
|
|
58
|
+
let result = layout(cell, w).flatMap((line) => wrap(line, w))
|
|
59
|
+
if (cell.localName == 'th') {
|
|
60
|
+
result = result.map((line) =>
|
|
61
|
+
line.map((s) => ({
|
|
62
|
+
...s,
|
|
63
|
+
style: { ...s.style, ...sheet.Table_Header },
|
|
64
|
+
}))
|
|
65
|
+
)
|
|
66
|
+
}
|
|
67
|
+
return result.length ? result : [[]]
|
|
68
|
+
}
|
|
69
|
+
// A vertical record layout is more useful than one-character columns.
|
|
70
|
+
if (available < count * 6 + 1) {
|
|
71
|
+
let header = rows[0].some((c) => c.localName == 'th') ? rows[0] : undefined
|
|
72
|
+
let records = header && rows.length > 1 ? rows.slice(1) : rows
|
|
73
|
+
return records.flatMap((row, index) => {
|
|
74
|
+
let lines: Line[] = index ? [[seg('─'.repeat(available), edge)]] : []
|
|
75
|
+
row.forEach((cell, col) => {
|
|
76
|
+
if (header && records !== rows) {
|
|
77
|
+
lines.push(...cellLines(header[col], available))
|
|
78
|
+
}
|
|
79
|
+
lines.push(...cellLines(cell, available))
|
|
80
|
+
})
|
|
81
|
+
return lines
|
|
82
|
+
})
|
|
83
|
+
}
|
|
84
|
+
let budget = available - (count * 3 + 1)
|
|
85
|
+
let desired = Array.from(
|
|
86
|
+
{ length: count },
|
|
87
|
+
(_, col) =>
|
|
88
|
+
Math.max(
|
|
89
|
+
3,
|
|
90
|
+
...rows.slice(0, 32).map((row) =>
|
|
91
|
+
Math.min(
|
|
92
|
+
40,
|
|
93
|
+
Math.max(...sample(row[col]).split('\n').map((s) => s.length)),
|
|
94
|
+
)
|
|
95
|
+
),
|
|
96
|
+
),
|
|
97
|
+
)
|
|
98
|
+
let widths = Array(count).fill(3) as number[]
|
|
99
|
+
let remaining = budget - count * 3
|
|
100
|
+
while (remaining > 0) {
|
|
101
|
+
let grew = false
|
|
102
|
+
for (let i = 0; i < count && remaining > 0; i++) {
|
|
103
|
+
if (widths[i] < desired[i]) {
|
|
104
|
+
widths[i]++
|
|
105
|
+
remaining--
|
|
106
|
+
grew = true
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
if (!grew) break
|
|
110
|
+
}
|
|
111
|
+
// Intrinsic widths guide the first allocation; share all remaining space.
|
|
112
|
+
let extra = Math.floor(remaining / count)
|
|
113
|
+
let remainder = remaining % count
|
|
114
|
+
widths = widths.map((w, i) => w + extra + (i < remainder ? 1 : 0))
|
|
115
|
+
let rule = (left: string, middle: string, right: string): Line => [seg(
|
|
116
|
+
left + widths.map((w) => '─'.repeat(w + 2)).join(middle) + right,
|
|
117
|
+
edge,
|
|
118
|
+
)]
|
|
119
|
+
let out: Line[] = [rule('┌', '┬', '┐')]
|
|
120
|
+
rows.forEach((row, index) => {
|
|
121
|
+
let columns = widths.map((w, i) => cellLines(row[i], w))
|
|
122
|
+
let height = Math.max(...columns.map((c) => c.length))
|
|
123
|
+
for (let y = 0; y < height; y++) {
|
|
124
|
+
let line: Line = [seg('│', edge)]
|
|
125
|
+
widths.forEach((w, col) => {
|
|
126
|
+
let content = columns[col][y] ?? []
|
|
127
|
+
let space = Math.max(0, w - width(content))
|
|
128
|
+
let align = row[col]?.attr('align') ??
|
|
129
|
+
/text-align\s*:\s*(left|right|center)/.exec(
|
|
130
|
+
row[col]?.attr('style') ?? '',
|
|
131
|
+
)?.[1]
|
|
132
|
+
let before = align == 'right'
|
|
133
|
+
? space
|
|
134
|
+
: align == 'center'
|
|
135
|
+
? Math.floor(space / 2)
|
|
136
|
+
: 0
|
|
137
|
+
line.push(
|
|
138
|
+
seg(' ' + ' '.repeat(before)),
|
|
139
|
+
...content,
|
|
140
|
+
seg(' '.repeat(space - before) + ' '),
|
|
141
|
+
seg('│', edge),
|
|
142
|
+
)
|
|
143
|
+
})
|
|
144
|
+
out.push(line)
|
|
145
|
+
}
|
|
146
|
+
if (index < rows.length - 1) {
|
|
147
|
+
out.push(rule('├', '┼', '┤'))
|
|
148
|
+
}
|
|
149
|
+
})
|
|
150
|
+
out.push(rule('└', '┴', '┘'))
|
|
151
|
+
return out
|
|
152
|
+
}
|
package/testing.js
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A mounted app for a test: a fake terminal of a fixed size, the ANSI backend
|
|
3
|
+
* writing into an array instead of a tty, and keys delivered the way the real
|
|
4
|
+
* loop delivers them — raw bytes through `decode` into the focus stack. `send`
|
|
5
|
+
* returns the number of screen lines that keystroke repainted, which is what
|
|
6
|
+
* lets a test assert that a keystroke is cheap rather than just hoping it is.
|
|
7
|
+
*
|
|
8
|
+
* @module
|
|
9
|
+
*/ /* @ts-types="npm:@types/react@19.3.0" */ import { createElement as h } from "react";
|
|
10
|
+
import { act } from "@yaks/react/testing";
|
|
11
|
+
import { draw as show } from "@yaks/react/draw";
|
|
12
|
+
import { install, onPaint, place } from "@yaks/dom";
|
|
13
|
+
import { decode } from "./input.js";
|
|
14
|
+
import { clearMouse, routeMouse } from "./mouse.js";
|
|
15
|
+
import { ansiBackend, screenful } from "./paint.js";
|
|
16
|
+
import { clear, measured, press, size } from "./screen.js";
|
|
17
|
+
/** Mount an app on a fake terminal, its classes dressed by `sheet`; `free()`
|
|
18
|
+
* restores the document. */ export let mount = async (App, columns = 60, rows = 12, sheet = {})=>{
|
|
19
|
+
let screen = install();
|
|
20
|
+
let out = [];
|
|
21
|
+
let wrote = 0;
|
|
22
|
+
let lines = [];
|
|
23
|
+
let backend = ansiBackend({
|
|
24
|
+
sheet,
|
|
25
|
+
size: ()=>({
|
|
26
|
+
columns,
|
|
27
|
+
rows
|
|
28
|
+
}),
|
|
29
|
+
write: (s)=>void out.push(s)
|
|
30
|
+
});
|
|
31
|
+
size.value = {
|
|
32
|
+
columns,
|
|
33
|
+
rows
|
|
34
|
+
};
|
|
35
|
+
let draw = ()=>{
|
|
36
|
+
let r = backend.draw(screen.root);
|
|
37
|
+
wrote += r.written;
|
|
38
|
+
lines = r.lines ?? [];
|
|
39
|
+
measured(r.metrics);
|
|
40
|
+
place(lines, r.hidden);
|
|
41
|
+
};
|
|
42
|
+
onPaint(draw);
|
|
43
|
+
let hide = show(/*#__PURE__*/ h(App, {}), screen.root);
|
|
44
|
+
// Preact renders, runs effects and the tree repaints on microtasks; a few
|
|
45
|
+
// turns settle the measure/re-render loop a scroll region makes on its
|
|
46
|
+
// first paint.
|
|
47
|
+
let settle = async ()=>{
|
|
48
|
+
for(let i = 0; i < 8; i++)await Promise.resolve();
|
|
49
|
+
};
|
|
50
|
+
await settle();
|
|
51
|
+
return {
|
|
52
|
+
out,
|
|
53
|
+
// The whole layout, unclipped: a test reads what the tree says even where
|
|
54
|
+
// the terminal is too narrow to show all of it.
|
|
55
|
+
text: ()=>screenful(screen.root, columns, rows, sheet).lines.map((l)=>l.map((s)=>s.text).join('').trimEnd()).join('\n'),
|
|
56
|
+
send: async (bytes)=>{
|
|
57
|
+
wrote = 0;
|
|
58
|
+
for (let key of decode(bytes)){
|
|
59
|
+
await act(()=>{
|
|
60
|
+
if (key.name == 'mouse') routeMouse(key, lines);
|
|
61
|
+
else press(key);
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
await settle();
|
|
65
|
+
return wrote;
|
|
66
|
+
},
|
|
67
|
+
resize: async (width, height)=>{
|
|
68
|
+
columns = width;
|
|
69
|
+
rows = height;
|
|
70
|
+
size.value = {
|
|
71
|
+
columns,
|
|
72
|
+
rows
|
|
73
|
+
};
|
|
74
|
+
draw();
|
|
75
|
+
await settle();
|
|
76
|
+
},
|
|
77
|
+
free: ()=>{
|
|
78
|
+
hide();
|
|
79
|
+
onPaint(()=>{});
|
|
80
|
+
clearMouse();
|
|
81
|
+
clear();
|
|
82
|
+
screen.free();
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
};
|
|
86
|
+
//# sourceMappingURL=testing.js.map
|
package/testing.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"testing.js","sources":["./testing.ts"],"names":[],"mappings":"AAAA;;;;;;;;CAQC,GAED,uCAAuC,GAAG,SAA6B,iBAAiB,CAAC,gBAA0B;AACnH,SAAS,GAAG,mCAAwC;AACpD,SAAS,QAAQ,IAAI,gCAAqC;AAC1D,SAAS,OAAO,EA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MACR,KAAK,IAAI,OAAO,OAAO,OAAQ;QAC7B,MAAM,IAAI;UACR,IAAI,IAAI,IAAI,IAAI,SAAS,WAAW,KAAK;eACpC,MAAM;QACb;MACF;MACA,MAAM;MACN,OAAO;IACT;IACA,QAAQ,OAAO,OAAO;MACpB,UAAU;MACV,OAAO;MACP,KAAK,KAAK,GAAG;QAAE;QAAS;MAAK;MAC7B;MACA,MAAM;IACR;IACA,MAAM;MACJ;MACA,QAAQ,KAAO;MACf;MACA;MACA,OAAO,IAAI;IACb;EACF;AACF,EAAC"}
|
package/testing.ts
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A mounted app for a test: a fake terminal of a fixed size, the ANSI backend
|
|
3
|
+
* writing into an array instead of a tty, and keys delivered the way the real
|
|
4
|
+
* loop delivers them — raw bytes through `decode` into the focus stack. `send`
|
|
5
|
+
* returns the number of screen lines that keystroke repainted, which is what
|
|
6
|
+
* lets a test assert that a keystroke is cheap rather than just hoping it is.
|
|
7
|
+
*
|
|
8
|
+
* @module
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/* @ts-types="npm:@types/react@19.3.0" */ import { type ComponentType, createElement as h } from 'react'
|
|
12
|
+
import { act } from '@yaks/react/testing'
|
|
13
|
+
import { draw as show } from '@yaks/react/draw'
|
|
14
|
+
import { install, onPaint, place } from '@yaks/dom'
|
|
15
|
+
import { decode } from './input.js'
|
|
16
|
+
import { clearMouse, routeMouse } from './mouse.js'
|
|
17
|
+
import { ansiBackend, type Line, screenful } from './paint.js'
|
|
18
|
+
import type { Sheet } from 'jsr:@yaks/ui@0.2.36/sheet'
|
|
19
|
+
import { clear, measured, press, size } from './screen.js'
|
|
20
|
+
|
|
21
|
+
/** Mount an app on a fake terminal, its classes dressed by `sheet`; `free()`
|
|
22
|
+
* restores the document. */
|
|
23
|
+
export let mount = async (
|
|
24
|
+
App: ComponentType,
|
|
25
|
+
columns = 60,
|
|
26
|
+
rows = 12,
|
|
27
|
+
sheet: Sheet = {},
|
|
28
|
+
): Promise<{
|
|
29
|
+
out: string[]
|
|
30
|
+
text: () => string
|
|
31
|
+
send: (bytes: string) => Promise<number>
|
|
32
|
+
resize: (width: number, height: number) => Promise<void>
|
|
33
|
+
free: () => void
|
|
34
|
+
}> => {
|
|
35
|
+
let screen = install()
|
|
36
|
+
let out: string[] = []
|
|
37
|
+
let wrote = 0
|
|
38
|
+
let lines: Line[] = []
|
|
39
|
+
let backend = ansiBackend({
|
|
40
|
+
sheet,
|
|
41
|
+
size: () => ({ columns, rows }),
|
|
42
|
+
write: (s) => void out.push(s),
|
|
43
|
+
})
|
|
44
|
+
size.value = { columns, rows }
|
|
45
|
+
let draw = () => {
|
|
46
|
+
let r = backend.draw(screen.root)
|
|
47
|
+
wrote += r.written
|
|
48
|
+
lines = r.lines ?? []
|
|
49
|
+
measured(r.metrics)
|
|
50
|
+
place(lines, r.hidden)
|
|
51
|
+
}
|
|
52
|
+
onPaint(draw)
|
|
53
|
+
let hide = show(h(App, {}), screen.root as unknown as Element)
|
|
54
|
+
// Preact renders, runs effects and the tree repaints on microtasks; a few
|
|
55
|
+
// turns settle the measure/re-render loop a scroll region makes on its
|
|
56
|
+
// first paint.
|
|
57
|
+
let settle = async () => {
|
|
58
|
+
for (let i = 0; i < 8; i++) await Promise.resolve()
|
|
59
|
+
}
|
|
60
|
+
await settle()
|
|
61
|
+
return {
|
|
62
|
+
out,
|
|
63
|
+
// The whole layout, unclipped: a test reads what the tree says even where
|
|
64
|
+
// the terminal is too narrow to show all of it.
|
|
65
|
+
text: () =>
|
|
66
|
+
screenful(screen.root, columns, rows, sheet).lines
|
|
67
|
+
.map((l) => l.map((s) => s.text).join('').trimEnd())
|
|
68
|
+
.join('\n'),
|
|
69
|
+
send: async (bytes: string) => {
|
|
70
|
+
wrote = 0
|
|
71
|
+
for (let key of decode(bytes)) {
|
|
72
|
+
await act(() => {
|
|
73
|
+
if (key.name == 'mouse') routeMouse(key, lines)
|
|
74
|
+
else press(key)
|
|
75
|
+
})
|
|
76
|
+
}
|
|
77
|
+
await settle()
|
|
78
|
+
return wrote
|
|
79
|
+
},
|
|
80
|
+
resize: async (width, height) => {
|
|
81
|
+
columns = width
|
|
82
|
+
rows = height
|
|
83
|
+
size.value = { columns, rows }
|
|
84
|
+
draw()
|
|
85
|
+
await settle()
|
|
86
|
+
},
|
|
87
|
+
free: () => {
|
|
88
|
+
hide()
|
|
89
|
+
onPaint(() => {})
|
|
90
|
+
clearMouse()
|
|
91
|
+
clear()
|
|
92
|
+
screen.free()
|
|
93
|
+
},
|
|
94
|
+
}
|
|
95
|
+
}
|