@diagc/cli 1.5.0 → 1.6.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/assets/guide/index.md +1 -1
- package/assets/skill/SKILL.md +1 -1
- package/dist/cli/io.d.ts +5 -0
- package/dist/cli/io.js +4 -0
- package/dist/cli/parse.d.ts +7 -0
- package/dist/cli/parse.js +25 -0
- package/dist/cli/run.js +4 -0
- package/dist/cli.js +23 -1
- package/dist/commands/shared.d.ts +4 -0
- package/dist/commands/shared.js +13 -0
- package/dist/commands/show.d.ts +5 -0
- package/dist/commands/show.js +115 -0
- package/dist/commands/tui.d.ts +3 -0
- package/dist/commands/tui.js +36 -0
- package/dist/init.d.ts +1 -1
- package/dist/init.js +1 -1
- package/dist/text/accents.d.ts +9 -0
- package/dist/text/accents.js +83 -0
- package/dist/text/ansi.d.ts +28 -0
- package/dist/text/ansi.js +111 -0
- package/dist/text/braille.d.ts +28 -0
- package/dist/text/braille.js +58 -0
- package/dist/text/canvas.d.ts +65 -0
- package/dist/text/canvas.js +155 -0
- package/dist/text/content.d.ts +32 -0
- package/dist/text/content.js +90 -0
- package/dist/text/fish.d.ts +27 -0
- package/dist/text/fish.js +209 -0
- package/dist/text/folds.d.ts +14 -0
- package/dist/text/folds.js +45 -0
- package/dist/text/glyphs.d.ts +31 -0
- package/dist/text/glyphs.js +84 -0
- package/dist/text/layout.d.ts +61 -0
- package/dist/text/layout.js +334 -0
- package/dist/text/legend.d.ts +44 -0
- package/dist/text/legend.js +252 -0
- package/dist/text/paint.d.ts +25 -0
- package/dist/text/paint.js +208 -0
- package/dist/text/render.d.ts +11 -0
- package/dist/text/render.js +28 -0
- package/dist/text/scene.d.ts +40 -0
- package/dist/text/scene.js +50 -0
- package/dist/text/vocabulary.d.ts +37 -0
- package/dist/text/vocabulary.js +211 -0
- package/dist/text/width.d.ts +15 -0
- package/dist/text/width.js +181 -0
- package/dist/tui/actions.d.ts +10 -0
- package/dist/tui/actions.js +297 -0
- package/dist/tui/app.d.ts +31 -0
- package/dist/tui/app.js +389 -0
- package/dist/tui/details.d.ts +3 -0
- package/dist/tui/details.js +54 -0
- package/dist/tui/keys.d.ts +33 -0
- package/dist/tui/keys.js +113 -0
- package/dist/tui/kitty.d.ts +36 -0
- package/dist/tui/kitty.js +65 -0
- package/dist/tui/legend.d.ts +14 -0
- package/dist/tui/legend.js +18 -0
- package/dist/tui/nav.d.ts +25 -0
- package/dist/tui/nav.js +79 -0
- package/dist/tui/overview.d.ts +46 -0
- package/dist/tui/overview.js +115 -0
- package/dist/tui/pixels.d.ts +28 -0
- package/dist/tui/pixels.js +80 -0
- package/dist/tui/screen.d.ts +44 -0
- package/dist/tui/screen.js +335 -0
- package/dist/tui/state.d.ts +100 -0
- package/dist/tui/state.js +72 -0
- package/dist/tui/terminal.d.ts +21 -0
- package/dist/tui/terminal.js +53 -0
- package/dist/watch.d.ts +2 -0
- package/dist/watch.js +1 -0
- package/package.json +3 -2
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { deflateSync } from 'node:zlib';
|
|
2
|
+
// Pictures on the terminal through kitty's graphics protocol
|
|
3
|
+
// (https://sw.kovidgoyal.net/kitty/graphics-protocol/), which kitty, Ghostty and
|
|
4
|
+
// WezTerm speak: as much of it as the overview needs. One image, placed over cells the
|
|
5
|
+
// screen leaves blank, sent again only when it changes. A terminal that never says it
|
|
6
|
+
// shows images keeps the braille overview, and so does tmux, which keeps the query to
|
|
7
|
+
// itself.
|
|
8
|
+
/** What the TUI asks the terminal as it starts: its text colour (OSC 10), its cell's
|
|
9
|
+
* size in pixels (CSI 16 t), and whether it shows images, by the query the protocol
|
|
10
|
+
* keeps for that, which a terminal without it ignores. Terminals answer in order, so
|
|
11
|
+
* by the time the last answer comes the others have. */
|
|
12
|
+
export const TERMINAL_QUERY = '\x1b]10;?\x1b\\\x1b[16t\x1b_Gi=31,s=1,v=1,a=q,t=d,f=24;AAAA\x1b\\';
|
|
13
|
+
/** The cell's size again, after a resize: a font size change is one. */
|
|
14
|
+
export const CELL_SIZE_QUERY = '\x1b[16t';
|
|
15
|
+
/** The overview's image id: any number, since the TUI's screen is its own. */
|
|
16
|
+
export const OVERVIEW_IMAGE = 0x64696167;
|
|
17
|
+
/** The protocol's limit on one command's base64 payload. */
|
|
18
|
+
const CHUNK = 4096;
|
|
19
|
+
/** A cell's size where the terminal does not say, about a usual one: the terminal scales
|
|
20
|
+
* the image to its cells either way, so a wrong guess only blurs it. */
|
|
21
|
+
const CELL_STAND_IN = { width: 10, height: 20 };
|
|
22
|
+
/** Text colour where the terminal does not say: as readable on a light ground as a dark one. */
|
|
23
|
+
const INK_STAND_IN = [128, 128, 128];
|
|
24
|
+
export function hear(heard, reply) {
|
|
25
|
+
if (reply.kind === 'graphics')
|
|
26
|
+
return { ...heard, images: reply.ok };
|
|
27
|
+
if (reply.kind === 'text-color')
|
|
28
|
+
return { ...heard, ink: reply.rgb };
|
|
29
|
+
// 0 is a terminal that does not know
|
|
30
|
+
const { width, height } = reply;
|
|
31
|
+
return width > 0 && height > 0 ? { ...heard, cell: { width, height } } : heard;
|
|
32
|
+
}
|
|
33
|
+
export function graphicsOf(heard) {
|
|
34
|
+
return heard.images === true ? { cell: heard.cell ?? CELL_STAND_IN, ink: heard.ink ?? INK_STAND_IN } : undefined;
|
|
35
|
+
}
|
|
36
|
+
const command = (keys, payload) => `\x1b_G${keys}${payload === undefined ? '' : `;${payload}`}\x1b\\`;
|
|
37
|
+
/** The image, sent and shown at its place in one go, zlib-compressed. The cursor goes to
|
|
38
|
+
* the place's corner, and stays there: the frame positions its own. Sending an image under
|
|
39
|
+
* an id already in use replaces it. */
|
|
40
|
+
function showImage({ place, width, height, rgba }) {
|
|
41
|
+
const data = deflateSync(rgba).toString('base64');
|
|
42
|
+
const chunks = data.match(new RegExp(`.{1,${CHUNK}}`, 'g')) ?? [''];
|
|
43
|
+
const keys = `a=T,f=32,o=z,s=${width},v=${height},c=${place.width},r=${place.height},i=${OVERVIEW_IMAGE},p=1,C=1,q=2`;
|
|
44
|
+
const sent = chunks.map((chunk, i) => {
|
|
45
|
+
const more = `m=${i < chunks.length - 1 ? 1 : 0}`;
|
|
46
|
+
return command(i === 0 ? `${keys},${more}` : `q=2,${more}`, chunk);
|
|
47
|
+
});
|
|
48
|
+
return `\x1b[${place.y + 1};${place.x + 1}H${sent.join('')}`;
|
|
49
|
+
}
|
|
50
|
+
/** The image off the screen, and out of the terminal's memory. */
|
|
51
|
+
const removeImage = () => command(`a=d,d=I,i=${OVERVIEW_IMAGE},q=2`);
|
|
52
|
+
const sameImage = (a, b) => a.place.x === b.place.x &&
|
|
53
|
+
a.place.y === b.place.y &&
|
|
54
|
+
a.place.width === b.place.width &&
|
|
55
|
+
a.place.height === b.place.height &&
|
|
56
|
+
a.width === b.width &&
|
|
57
|
+
a.height === b.height &&
|
|
58
|
+
Buffer.from(a.rgba.buffer, a.rgba.byteOffset, a.rgba.byteLength).equals(b.rgba);
|
|
59
|
+
/** What turns the image `shown` into `next` on the screen: nothing when they are the same,
|
|
60
|
+
* which is most frames, since every frame rewrites the text but an image stays put. */
|
|
61
|
+
export function imageChange(shown, next) {
|
|
62
|
+
if (next === undefined)
|
|
63
|
+
return shown === undefined ? '' : removeImage();
|
|
64
|
+
return shown !== undefined && sameImage(shown, next) ? '' : showImage(next);
|
|
65
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { LegendPosition } from '@diagc/core/internal';
|
|
2
|
+
import type { CellPoint, CellRect } from '../text/canvas.js';
|
|
3
|
+
interface Extent {
|
|
4
|
+
width: number;
|
|
5
|
+
height: number;
|
|
6
|
+
}
|
|
7
|
+
/** Where a legend of `size` goes in `view`, the canvas, kept off `overview` (its place
|
|
8
|
+
* with its border), or nowhere without the room. */
|
|
9
|
+
export declare function legendPlace(size: Extent, position: LegendPosition, view: Extent & {
|
|
10
|
+
top: number;
|
|
11
|
+
}, overview?: CellRect): CellRect | undefined;
|
|
12
|
+
/** Whether the screen cell `at` is on the legend, its border included. */
|
|
13
|
+
export declare function onLegend(place: CellRect, at: CellPoint): boolean;
|
|
14
|
+
export {};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/** Less room than this and it is left out: its title and two of its lines, with border. */
|
|
2
|
+
const MIN = { width: 12, height: 4 };
|
|
3
|
+
/** Where a legend of `size` goes in `view`, the canvas, kept off `overview` (its place
|
|
4
|
+
* with its border), or nowhere without the room. */
|
|
5
|
+
export function legendPlace(size, position, view, overview) {
|
|
6
|
+
const width = Math.min(size.width, view.width);
|
|
7
|
+
const x = position.endsWith('right') ? view.width - width : 0;
|
|
8
|
+
const shares = overview !== undefined && x < overview.x + overview.width && overview.x < x + width;
|
|
9
|
+
const bottom = shares ? overview.y : view.top + view.height;
|
|
10
|
+
const height = Math.min(size.height, bottom - view.top);
|
|
11
|
+
if (width < MIN.width || height < MIN.height)
|
|
12
|
+
return undefined;
|
|
13
|
+
return { x, y: position.startsWith('top') ? view.top : bottom - height, width, height };
|
|
14
|
+
}
|
|
15
|
+
/** Whether the screen cell `at` is on the legend, its border included. */
|
|
16
|
+
export function onLegend(place, at) {
|
|
17
|
+
return at.x >= place.x && at.x < place.x + place.width && at.y >= place.y && at.y < place.y + place.height;
|
|
18
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { CellPoint } from '../text/canvas.js';
|
|
2
|
+
import type { BoxGeometry } from '../text/layout.js';
|
|
3
|
+
export type Direction = 'up' | 'down' | 'left' | 'right';
|
|
4
|
+
/** The box to go to from `from` in `dir`: of those whose centre lies that way, the
|
|
5
|
+
* nearest, where being off to the side counts double. */
|
|
6
|
+
export declare function nearest(boxes: ReadonlyMap<string, BoxGeometry>, from: string, dir: Direction): string | undefined;
|
|
7
|
+
/** Every box, top to bottom, then left to right: the order Tab steps through. */
|
|
8
|
+
export declare function readingOrder(boxes: ReadonlyMap<string, BoxGeometry>): string[];
|
|
9
|
+
/** The box a click at (x, y) means: the smallest that holds the cell, so a click
|
|
10
|
+
* inside a child picks the child, not the container around it. */
|
|
11
|
+
export declare function hitTest(boxes: ReadonlyMap<string, BoxGeometry>, x: number, y: number): string | undefined;
|
|
12
|
+
/** The scroll that brings `box` into a view of `view`'s size, moving as little as it can. */
|
|
13
|
+
export declare function follow(scroll: CellPoint, box: BoxGeometry, view: {
|
|
14
|
+
width: number;
|
|
15
|
+
height: number;
|
|
16
|
+
}): CellPoint;
|
|
17
|
+
/** `scroll` kept where the view still shows content: never before the start, never so
|
|
18
|
+
* far that the view is past the end. */
|
|
19
|
+
export declare function clampScroll(scroll: CellPoint, content: {
|
|
20
|
+
width: number;
|
|
21
|
+
height: number;
|
|
22
|
+
}, view: {
|
|
23
|
+
width: number;
|
|
24
|
+
height: number;
|
|
25
|
+
}): CellPoint;
|
package/dist/tui/nav.js
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/** Room kept around a box scrolled into view, in cells. */
|
|
2
|
+
const MARGIN = 2;
|
|
3
|
+
/** A box's centre, its rows counted double: a cell is about twice as tall as it is
|
|
4
|
+
* wide, so a box two rows down is about as far as one four columns across. */
|
|
5
|
+
const centre = (b) => ({ x: b.x + b.width / 2, y: (b.y + b.height / 2) * 2 });
|
|
6
|
+
/** How far `d` is along `dir` from `c`, and how far off to the side. */
|
|
7
|
+
function along(c, d, dir) {
|
|
8
|
+
const dx = d.x - c.x;
|
|
9
|
+
const dy = d.y - c.y;
|
|
10
|
+
if (dir === 'up')
|
|
11
|
+
return [-dy, dx];
|
|
12
|
+
if (dir === 'down')
|
|
13
|
+
return [dy, dx];
|
|
14
|
+
if (dir === 'left')
|
|
15
|
+
return [-dx, dy];
|
|
16
|
+
return [dx, dy];
|
|
17
|
+
}
|
|
18
|
+
/** The box to go to from `from` in `dir`: of those whose centre lies that way, the
|
|
19
|
+
* nearest, where being off to the side counts double. */
|
|
20
|
+
export function nearest(boxes, from, dir) {
|
|
21
|
+
const start = boxes.get(from);
|
|
22
|
+
if (start === undefined)
|
|
23
|
+
return undefined;
|
|
24
|
+
const c = centre(start);
|
|
25
|
+
let best;
|
|
26
|
+
let bestScore = Infinity;
|
|
27
|
+
for (const [id, box] of boxes) {
|
|
28
|
+
if (id === from)
|
|
29
|
+
continue;
|
|
30
|
+
const [primary, secondary] = along(c, centre(box), dir);
|
|
31
|
+
if (primary <= 0)
|
|
32
|
+
continue;
|
|
33
|
+
const score = primary + 2 * Math.abs(secondary);
|
|
34
|
+
if (score < bestScore)
|
|
35
|
+
[best, bestScore] = [id, score];
|
|
36
|
+
}
|
|
37
|
+
return best;
|
|
38
|
+
}
|
|
39
|
+
/** Every box, top to bottom, then left to right: the order Tab steps through. */
|
|
40
|
+
export function readingOrder(boxes) {
|
|
41
|
+
return [...boxes].sort(([, a], [, b]) => a.y - b.y || a.x - b.x).map(([id]) => id);
|
|
42
|
+
}
|
|
43
|
+
/** The box a click at (x, y) means: the smallest that holds the cell, so a click
|
|
44
|
+
* inside a child picks the child, not the container around it. */
|
|
45
|
+
export function hitTest(boxes, x, y) {
|
|
46
|
+
let hit;
|
|
47
|
+
let area = Infinity;
|
|
48
|
+
for (const [id, b] of boxes) {
|
|
49
|
+
if (x < b.x || x >= b.x + b.width || y < b.y || y >= b.y + b.height)
|
|
50
|
+
continue;
|
|
51
|
+
if (b.width * b.height < area)
|
|
52
|
+
[hit, area] = [id, b.width * b.height];
|
|
53
|
+
}
|
|
54
|
+
return hit;
|
|
55
|
+
}
|
|
56
|
+
/** One axis of `follow`: the least move that shows [start, start + size) and a margin
|
|
57
|
+
* within a window of `span` at `at`; the start, for something bigger than the window. */
|
|
58
|
+
function followAxis(at, start, size, span) {
|
|
59
|
+
const low = start - MARGIN;
|
|
60
|
+
const high = start + size + MARGIN;
|
|
61
|
+
if (high - low > span || low < at)
|
|
62
|
+
return Math.max(0, low);
|
|
63
|
+
if (high > at + span)
|
|
64
|
+
return high - span;
|
|
65
|
+
return at;
|
|
66
|
+
}
|
|
67
|
+
/** The scroll that brings `box` into a view of `view`'s size, moving as little as it can. */
|
|
68
|
+
export function follow(scroll, box, view) {
|
|
69
|
+
return {
|
|
70
|
+
x: followAxis(scroll.x, box.x, box.width, view.width),
|
|
71
|
+
y: followAxis(scroll.y, box.y, box.height, view.height),
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
/** `scroll` kept where the view still shows content: never before the start, never so
|
|
75
|
+
* far that the view is past the end. */
|
|
76
|
+
export function clampScroll(scroll, content, view) {
|
|
77
|
+
const clamp = (v, max) => Math.max(0, Math.min(v, Math.max(0, max)));
|
|
78
|
+
return { x: clamp(scroll.x, content.width - view.width), y: clamp(scroll.y, content.height - view.height) };
|
|
79
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { Cell, CellPoint, CellRect } from '../text/canvas.js';
|
|
2
|
+
import type { TextLayout } from '../text/layout.js';
|
|
3
|
+
import { Pixels } from './pixels.js';
|
|
4
|
+
/** Where the overview's dots go on the screen, inside a border a cell wide. */
|
|
5
|
+
export interface OverviewPlace extends CellRect {
|
|
6
|
+
/** overview cells per picture cell, the same across and down, so the shape is kept */
|
|
7
|
+
scale: number;
|
|
8
|
+
}
|
|
9
|
+
/** The part of the picture on screen. */
|
|
10
|
+
export interface PictureWindow {
|
|
11
|
+
scroll: CellPoint;
|
|
12
|
+
width: number;
|
|
13
|
+
height: number;
|
|
14
|
+
}
|
|
15
|
+
interface Extent {
|
|
16
|
+
width: number;
|
|
17
|
+
height: number;
|
|
18
|
+
}
|
|
19
|
+
/** Where the overview of `picture` goes in `view`, the canvas, or nowhere: with the
|
|
20
|
+
* whole picture on screen there is nothing to find, and on a small screen no room. */
|
|
21
|
+
export declare function overviewPlace(picture: Extent, view: Extent & {
|
|
22
|
+
top: number;
|
|
23
|
+
}): OverviewPlace | undefined;
|
|
24
|
+
/** A cell's size in pixels, as the terminal says. */
|
|
25
|
+
export interface CellPixels {
|
|
26
|
+
width: number;
|
|
27
|
+
height: number;
|
|
28
|
+
}
|
|
29
|
+
/** Where the overview goes, and the pixels each of its cells holds. */
|
|
30
|
+
export type PixelPlace = OverviewPlace & {
|
|
31
|
+
cell: CellPixels;
|
|
32
|
+
};
|
|
33
|
+
/** The overview's cells, `place`'s size: each box's outline and the window's, what is
|
|
34
|
+
* off screen dim and the selected box inverse, as it is on the canvas. */
|
|
35
|
+
export declare function overviewCells(layout: TextLayout, place: OverviewPlace, window: PictureWindow, selected?: string): Cell[][];
|
|
36
|
+
/** The overview in pixels, `place`'s cells' worth: each box's outline and the lines
|
|
37
|
+
* between them, what is off screen dimmed, the part on screen outlined and faintly
|
|
38
|
+
* tinted, and the selected box filled in, as inverse fills it on the canvas. */
|
|
39
|
+
export declare function overviewPixels(layout: TextLayout, place: PixelPlace, window: PictureWindow, selected?: string): Pixels;
|
|
40
|
+
/** Whether the screen cell `at` is on the overview, its border included. */
|
|
41
|
+
export declare function onOverview(place: OverviewPlace, at: CellPoint): boolean;
|
|
42
|
+
/** The scroll that centres a window of `window`'s size on the part of the picture
|
|
43
|
+
* under the screen cell `at`. A cell off the overview — its border, or a drag that
|
|
44
|
+
* left it — counts as the nearest cell on it. */
|
|
45
|
+
export declare function overviewJump(place: OverviewPlace, at: CellPoint, window: Extent, picture: Extent): CellPoint;
|
|
46
|
+
export {};
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { Dots } from '../text/braille.js';
|
|
2
|
+
import { clampScroll } from './nav.js';
|
|
3
|
+
import { Pixels } from './pixels.js';
|
|
4
|
+
/** The most of the canvas the overview takes, its border included: a third across and
|
|
5
|
+
* half down, about square on a usual terminal, where a row is twice a column's height. */
|
|
6
|
+
const SHARE = { width: 1 / 3, height: 1 / 2 };
|
|
7
|
+
/** Less room than this inside the border and it is left out: too small to tell boxes apart. */
|
|
8
|
+
const MIN = { width: 6, height: 2 };
|
|
9
|
+
const clamp = (v, low, high) => Math.max(low, Math.min(v, high));
|
|
10
|
+
/** Where the overview of `picture` goes in `view`, the canvas, or nowhere: with the
|
|
11
|
+
* whole picture on screen there is nothing to find, and on a small screen no room. */
|
|
12
|
+
export function overviewPlace(picture, view) {
|
|
13
|
+
if (picture.width <= view.width && picture.height <= view.height)
|
|
14
|
+
return undefined;
|
|
15
|
+
const room = {
|
|
16
|
+
width: Math.floor(view.width * SHARE.width) - 2,
|
|
17
|
+
height: Math.floor(view.height * SHARE.height) - 2,
|
|
18
|
+
};
|
|
19
|
+
if (room.width < MIN.width || room.height < MIN.height)
|
|
20
|
+
return undefined;
|
|
21
|
+
const scale = Math.min(room.width / picture.width, room.height / picture.height);
|
|
22
|
+
// min: the product can land a hair above the room, and ceil would take a cell more
|
|
23
|
+
const width = Math.min(room.width, Math.max(1, Math.ceil(picture.width * scale)));
|
|
24
|
+
const height = Math.min(room.height, Math.max(1, Math.ceil(picture.height * scale)));
|
|
25
|
+
return { x: view.width - 1 - width, y: view.top + view.height - 1 - height, width, height, scale };
|
|
26
|
+
}
|
|
27
|
+
/** A braille cell's dots. */
|
|
28
|
+
const BRAILLE = { width: 2, height: 4 };
|
|
29
|
+
/** A rectangle of the picture as the overview's dots, `unit` to a cell: every dot it
|
|
30
|
+
* touches, so a box too small for one still shows as one. */
|
|
31
|
+
function toDots(r, scale, unit = BRAILLE) {
|
|
32
|
+
const x = Math.floor(r.x * scale * unit.width);
|
|
33
|
+
const y = Math.floor(r.y * scale * unit.height);
|
|
34
|
+
const right = Math.max(x, Math.ceil((r.x + r.width) * scale * unit.width) - 1);
|
|
35
|
+
const bottom = Math.max(y, Math.ceil((r.y + r.height) * scale * unit.height) - 1);
|
|
36
|
+
return { x, y, width: right - x + 1, height: bottom - y + 1 };
|
|
37
|
+
}
|
|
38
|
+
const overlaps = (a, b) => a.x < b.x + b.width && b.x < a.x + a.width && a.y < b.y + b.height && b.y < a.y + a.height;
|
|
39
|
+
/** The window cut to the picture: past the picture's edge its outline would be lost. */
|
|
40
|
+
function onPicture(window, picture) {
|
|
41
|
+
const x = clamp(window.scroll.x, 0, picture.width);
|
|
42
|
+
const y = clamp(window.scroll.y, 0, picture.height);
|
|
43
|
+
return {
|
|
44
|
+
x,
|
|
45
|
+
y,
|
|
46
|
+
width: clamp(window.scroll.x + window.width, x, picture.width) - x,
|
|
47
|
+
height: clamp(window.scroll.y + window.height, y, picture.height) - y,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
/** The overview's cells, `place`'s size: each box's outline and the window's, what is
|
|
51
|
+
* off screen dim and the selected box inverse, as it is on the canvas. */
|
|
52
|
+
export function overviewCells(layout, place, window, selected) {
|
|
53
|
+
const dots = new Dots(place.width, place.height);
|
|
54
|
+
for (const box of layout.boxes.values())
|
|
55
|
+
dots.outline(toDots(box, place.scale));
|
|
56
|
+
const screen = toDots(onPicture(window, layout), place.scale);
|
|
57
|
+
dots.outline(screen);
|
|
58
|
+
const box = selected === undefined ? undefined : layout.boxes.get(selected);
|
|
59
|
+
const chosen = box === undefined ? undefined : toDots(box, place.scale);
|
|
60
|
+
return Array.from({ length: place.height }, (_, row) => Array.from({ length: place.width }, (_, column) => {
|
|
61
|
+
const cell = { x: column * 2, y: row * 4, width: 2, height: 4 };
|
|
62
|
+
const style = chosen !== undefined && overlaps(cell, chosen)
|
|
63
|
+
? { inverse: true }
|
|
64
|
+
: overlaps(cell, screen)
|
|
65
|
+
? {}
|
|
66
|
+
: { dim: true };
|
|
67
|
+
return { ch: dots.char(column, row), style };
|
|
68
|
+
}));
|
|
69
|
+
}
|
|
70
|
+
/** How much of the terminal's text colour each part of the pixel overview takes: what
|
|
71
|
+
* is off screen gets `OFF_SCREEN` of its share, as dim text is about half as strong. */
|
|
72
|
+
const SHARE_OF = { box: 1, line: 0.5, screen: 1, screenTint: 0.2, selected: 1 };
|
|
73
|
+
const OFF_SCREEN = 0.4;
|
|
74
|
+
/** The overview in pixels, `place`'s cells' worth: each box's outline and the lines
|
|
75
|
+
* between them, what is off screen dimmed, the part on screen outlined and faintly
|
|
76
|
+
* tinted, and the selected box filled in, as inverse fills it on the canvas. */
|
|
77
|
+
export function overviewPixels(layout, place, window, selected) {
|
|
78
|
+
const { cell, scale } = place;
|
|
79
|
+
const pixels = new Pixels(place.width * cell.width, place.height * cell.height);
|
|
80
|
+
// a route runs through the middles of its cells
|
|
81
|
+
const centre = (p) => ({
|
|
82
|
+
x: Math.floor((p.x + 0.5) * scale * cell.width),
|
|
83
|
+
y: Math.floor((p.y + 0.5) * scale * cell.height),
|
|
84
|
+
});
|
|
85
|
+
for (const route of layout.routes.values()) {
|
|
86
|
+
route.points.slice(1).forEach((p, i) => pixels.line(centre(route.points[i]), centre(p), SHARE_OF.line));
|
|
87
|
+
}
|
|
88
|
+
// a fish's spine is drawn heavy, a bone's to the boxes' thin lines
|
|
89
|
+
const spine = layout.fish?.spine;
|
|
90
|
+
if (spine !== undefined)
|
|
91
|
+
pixels.line(centre({ x: spine.from, y: spine.y }), centre({ x: spine.to, y: spine.y }), SHARE_OF.box);
|
|
92
|
+
for (const box of layout.boxes.values())
|
|
93
|
+
pixels.outline(toDots(box, scale, cell), SHARE_OF.box);
|
|
94
|
+
const screen = toDots(onPicture(window, layout), scale, cell);
|
|
95
|
+
pixels.dimOutside(screen, OFF_SCREEN);
|
|
96
|
+
pixels.fill(screen, SHARE_OF.screenTint);
|
|
97
|
+
pixels.outline(screen, SHARE_OF.screen);
|
|
98
|
+
const box = selected === undefined ? undefined : layout.boxes.get(selected);
|
|
99
|
+
if (box !== undefined)
|
|
100
|
+
pixels.fill(toDots(box, scale, cell), SHARE_OF.selected);
|
|
101
|
+
return pixels;
|
|
102
|
+
}
|
|
103
|
+
/** Whether the screen cell `at` is on the overview, its border included. */
|
|
104
|
+
export function onOverview(place, at) {
|
|
105
|
+
return at.x >= place.x - 1 && at.x <= place.x + place.width && at.y >= place.y - 1 && at.y <= place.y + place.height;
|
|
106
|
+
}
|
|
107
|
+
/** The scroll that centres a window of `window`'s size on the part of the picture
|
|
108
|
+
* under the screen cell `at`. A cell off the overview — its border, or a drag that
|
|
109
|
+
* left it — counts as the nearest cell on it. */
|
|
110
|
+
export function overviewJump(place, at, window, picture) {
|
|
111
|
+
const column = clamp(at.x - place.x, 0, place.width - 1);
|
|
112
|
+
const row = clamp(at.y - place.y, 0, place.height - 1);
|
|
113
|
+
const centre = { x: (column + 0.5) / place.scale, y: (row + 0.5) / place.scale };
|
|
114
|
+
return clampScroll({ x: Math.round(centre.x - window.width / 2), y: Math.round(centre.y - window.height / 2) }, picture, window);
|
|
115
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { DotRect } from '../text/braille.js';
|
|
2
|
+
import type { CellPoint } from '../text/canvas.js';
|
|
3
|
+
/**
|
|
4
|
+
* A picture in one colour, for a terminal that shows images: each pixel holds how much
|
|
5
|
+
* of the colour it takes, 0 to 1, which becomes its alpha. Drawn in the terminal's own
|
|
6
|
+
* text colour over a transparent ground, a share is what `dim` is on text — the
|
|
7
|
+
* colour blended into whatever background the terminal has. A pixel keeps the most
|
|
8
|
+
* anything drew it, so the order things are drawn in does not matter. Anything drawn
|
|
9
|
+
* outside is dropped, as on the canvas.
|
|
10
|
+
*/
|
|
11
|
+
export declare class Pixels {
|
|
12
|
+
readonly width: number;
|
|
13
|
+
readonly height: number;
|
|
14
|
+
private readonly shares;
|
|
15
|
+
constructor(width: number, height: number);
|
|
16
|
+
at(x: number, y: number): number;
|
|
17
|
+
dab(x: number, y: number, share: number): void;
|
|
18
|
+
fill({ x, y, width, height }: DotRect, share: number): void;
|
|
19
|
+
/** The rectangle's border, a pixel thick. */
|
|
20
|
+
outline({ x, y, width, height }: DotRect, share: number): void;
|
|
21
|
+
/** Bresenham's line, both ends included. The ends are rounded first: off whole
|
|
22
|
+
* pixels the walk would never land on the far end. */
|
|
23
|
+
line(from: CellPoint, to: CellPoint, share: number): void;
|
|
24
|
+
/** Every pixel outside `keep` at `factor` of its share. */
|
|
25
|
+
dimOutside(keep: DotRect, factor: number): void;
|
|
26
|
+
/** The picture as RGBA bytes, row by row: `rgb` everywhere, its share the alpha. */
|
|
27
|
+
rgba(rgb: readonly [number, number, number]): Uint8Array;
|
|
28
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A picture in one colour, for a terminal that shows images: each pixel holds how much
|
|
3
|
+
* of the colour it takes, 0 to 1, which becomes its alpha. Drawn in the terminal's own
|
|
4
|
+
* text colour over a transparent ground, a share is what `dim` is on text — the
|
|
5
|
+
* colour blended into whatever background the terminal has. A pixel keeps the most
|
|
6
|
+
* anything drew it, so the order things are drawn in does not matter. Anything drawn
|
|
7
|
+
* outside is dropped, as on the canvas.
|
|
8
|
+
*/
|
|
9
|
+
export class Pixels {
|
|
10
|
+
width;
|
|
11
|
+
height;
|
|
12
|
+
shares;
|
|
13
|
+
constructor(width, height) {
|
|
14
|
+
this.width = width;
|
|
15
|
+
this.height = height;
|
|
16
|
+
this.shares = new Float32Array(Math.max(0, width) * Math.max(0, height));
|
|
17
|
+
}
|
|
18
|
+
at(x, y) {
|
|
19
|
+
return this.shares[y * this.width + x] ?? 0;
|
|
20
|
+
}
|
|
21
|
+
dab(x, y, share) {
|
|
22
|
+
if (x < 0 || y < 0 || x >= this.width || y >= this.height)
|
|
23
|
+
return;
|
|
24
|
+
const i = y * this.width + x;
|
|
25
|
+
this.shares[i] = Math.max(this.shares[i], share);
|
|
26
|
+
}
|
|
27
|
+
fill({ x, y, width, height }, share) {
|
|
28
|
+
for (let j = y; j < y + height; j++)
|
|
29
|
+
for (let i = x; i < x + width; i++)
|
|
30
|
+
this.dab(i, j, share);
|
|
31
|
+
}
|
|
32
|
+
/** The rectangle's border, a pixel thick. */
|
|
33
|
+
outline({ x, y, width, height }, share) {
|
|
34
|
+
if (width <= 0 || height <= 0)
|
|
35
|
+
return;
|
|
36
|
+
const [right, bottom] = [x + width - 1, y + height - 1];
|
|
37
|
+
this.line({ x, y }, { x: right, y }, share);
|
|
38
|
+
this.line({ x, y: bottom }, { x: right, y: bottom }, share);
|
|
39
|
+
this.line({ x, y }, { x, y: bottom }, share);
|
|
40
|
+
this.line({ x: right, y }, { x: right, y: bottom }, share);
|
|
41
|
+
}
|
|
42
|
+
/** Bresenham's line, both ends included. The ends are rounded first: off whole
|
|
43
|
+
* pixels the walk would never land on the far end. */
|
|
44
|
+
line(from, to, share) {
|
|
45
|
+
const end = { x: Math.round(to.x), y: Math.round(to.y) };
|
|
46
|
+
let [x, y] = [Math.round(from.x), Math.round(from.y)];
|
|
47
|
+
const [dx, dy] = [Math.abs(end.x - x), -Math.abs(end.y - y)];
|
|
48
|
+
const [sx, sy] = [Math.sign(end.x - x), Math.sign(end.y - y)];
|
|
49
|
+
let err = dx + dy;
|
|
50
|
+
for (;;) {
|
|
51
|
+
this.dab(x, y, share);
|
|
52
|
+
if (x === end.x && y === end.y)
|
|
53
|
+
return;
|
|
54
|
+
const e2 = 2 * err;
|
|
55
|
+
if (e2 >= dy)
|
|
56
|
+
[err, x] = [err + dy, x + sx];
|
|
57
|
+
if (e2 <= dx)
|
|
58
|
+
[err, y] = [err + dx, y + sy];
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
/** Every pixel outside `keep` at `factor` of its share. */
|
|
62
|
+
dimOutside(keep, factor) {
|
|
63
|
+
for (let y = 0; y < this.height; y++) {
|
|
64
|
+
for (let x = 0; x < this.width; x++) {
|
|
65
|
+
const inside = x >= keep.x && x < keep.x + keep.width && y >= keep.y && y < keep.y + keep.height;
|
|
66
|
+
if (!inside)
|
|
67
|
+
this.shares[y * this.width + x] *= factor;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
/** The picture as RGBA bytes, row by row: `rgb` everywhere, its share the alpha. */
|
|
72
|
+
rgba(rgb) {
|
|
73
|
+
const out = new Uint8Array(this.shares.length * 4);
|
|
74
|
+
this.shares.forEach((share, i) => {
|
|
75
|
+
out.set(rgb, i * 4);
|
|
76
|
+
out[i * 4 + 3] = Math.round(share * 255);
|
|
77
|
+
});
|
|
78
|
+
return out;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { DiagramModel } from '@diagc/core/internal';
|
|
2
|
+
import { type ColorMode } from '../text/ansi.js';
|
|
3
|
+
import type { Cell, CellRect } from '../text/canvas.js';
|
|
4
|
+
import type { Scene } from '../text/scene.js';
|
|
5
|
+
import type { FrameImage, Graphics } from './kitty.js';
|
|
6
|
+
import { type OverviewPlace } from './overview.js';
|
|
7
|
+
import type { TuiState } from './state.js';
|
|
8
|
+
/** The details panel's columns, its separator included. */
|
|
9
|
+
export declare const PANEL_WIDTH = 40;
|
|
10
|
+
export interface Size {
|
|
11
|
+
columns: number;
|
|
12
|
+
rows: number;
|
|
13
|
+
}
|
|
14
|
+
export interface FrameInput {
|
|
15
|
+
model?: DiagramModel;
|
|
16
|
+
scene?: Scene;
|
|
17
|
+
/** the painted picture */
|
|
18
|
+
canvas?: Cell[][];
|
|
19
|
+
/** what to say where there is nothing to show yet */
|
|
20
|
+
notice?: string;
|
|
21
|
+
/** the terminal shows images: the overview goes as one */
|
|
22
|
+
graphics?: Graphics;
|
|
23
|
+
}
|
|
24
|
+
/** The canvas area the picture is drawn in: the rows between the title and the status,
|
|
25
|
+
* less the panel's columns when it sits beside the picture. `reduce` reads the same. */
|
|
26
|
+
export declare function canvasSize(state: TuiState, size: Size): {
|
|
27
|
+
top: number;
|
|
28
|
+
width: number;
|
|
29
|
+
height: number;
|
|
30
|
+
};
|
|
31
|
+
/** Where the overview goes on this screen, whether or not it is on. `reduce` reads the
|
|
32
|
+
* same, for clicks. */
|
|
33
|
+
export declare function overviewPlaceOf(state: TuiState, scene: Scene | undefined, size: Size): OverviewPlace | undefined;
|
|
34
|
+
/** Where the legend goes on this screen, its border included, whether or not it is on:
|
|
35
|
+
* kept off the overview while that is on. `reduce` reads the same, for clicks. */
|
|
36
|
+
export declare function legendPlaceOf(state: TuiState, scene: Scene | undefined, size: Size): CellRect | undefined;
|
|
37
|
+
/** The overview as an image, where it is drawn as one on this frame. */
|
|
38
|
+
export declare function overviewImage(state: TuiState, input: FrameInput, size: Size): FrameImage | undefined;
|
|
39
|
+
/** The frame for `state`: `size.rows` lines of `size.columns` cells each. */
|
|
40
|
+
export declare function composeFrame(state: TuiState, input: FrameInput, size: Size, mode: ColorMode): string[];
|
|
41
|
+
/** The escapes that put `lines` on the screen in one go: each line at its row, then
|
|
42
|
+
* `images`' commands, inside a synchronized update, so a terminal that has them never
|
|
43
|
+
* shows a frame half drawn. */
|
|
44
|
+
export declare function frameText(lines: readonly string[], images?: string): string;
|