summaryrefslogtreecommitdiff
path: root/plugins/claude-security/scripts/views/term/canvas.mjs
blob: 6180c40f8e5552831e754c4a57abfa673cb4d999 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import { mix, PAPER } from './palette.mjs';
import { cell, REST, SPACE, times } from './text.mjs';
const PAPER_UNIT = 2 ** 24;
export const BOLD = 2 ** 48;
export const ITALIC = 2 * BOLD;
export const CHIP = 4 * BOLD;
export const on = (colour, paper) => colour + paper * PAPER_UNIT;
export const ink = (colour) => on(colour, PAPER);
export const colourOf = (pen) => pen & 0xffffff;
export const paperOf = (pen) => (pen / PAPER_UNIT) & 0xffffff;
export const styled = (pen, style) => ((pen / style) & 1) === 1;
export const RULE_CELL = cell('─');
const ELLIPSIS = cell('…');
export class Rect {
	x;
	y;
	w;
	h;
	constructor(x, y, w, h) {
		this.x = x;
		this.y = y;
		this.w = w;
		this.h = h;
	}
	get end() {
		return this.x + this.w;
	}
	inset(across, down) {
		return new Rect(this.x + across, this.y + down, this.w - 2 * across, this.h - 2 * down);
	}
	below(rows) {
		return new Rect(this.x, this.y + rows, this.w, this.h - rows);
	}
	high(h) {
		return new Rect(this.x, this.y, this.w, h);
	}
}
export class Grid {
	cols;
	rows;
	text;
	inks;
	constructor(cols, rows) {
		this.cols = cols;
		this.rows = rows;
		this.text = new Int32Array(cols * rows).fill(SPACE);
		this.inks = new Float64Array(cols * rows).fill(ink(PAPER));
	}
	take(other, from, to, count) {
		for (let n = 0; n < count; n++) {
			this.text[to + n] = other.text[from + n];
			this.inks[to + n] = other.inks[from + n];
		}
	}
	recolour(change) {
		const { inks } = this;
		const changed = new Map();
		let last = NaN;
		let pen = NaN;
		for (let at = 0; at < inks.length; at++) {
			if (inks[at] !== last) {
				last = inks[at];
				pen = changed.get(last) ?? change(last);
				changed.set(last, pen);
			}
			inks[at] = pen;
		}
	}
}
export class Canvas {
	grid;
	nerd;
	widths;
	constructor(grid, nerd, widths) {
		this.grid = grid;
		this.nerd = nerd;
		this.widths = widths;
	}
	cells(text) {
		return typeof text === 'string' ? this.widths.cells(text) : Array.from(this.nerd ? text.nerd : text.plain, cell);
	}
	measure(line) {
		let cells = 0;
		for (const [text] of line)
			cells += typeof text === 'string' ? this.widths.width(text) : this.cells(text).length;
		return cells;
	}
	spans(x, y, line, end) {
		let at = x;
		for (const [text, pen] of line)
			at = this.put(at, y, text, pen, end);
		return at;
	}
	put(x, y, text, pen, end = this.grid.cols) {
		const { cols, rows, text: cells, inks } = this.grid;
		const room = Math.min(end, cols) - x;
		if (y < 0 || y >= rows || x < 0 || room <= 0)
			return x;
		const found = this.cells(text);
		if (found.length === 0)
			return x;
		if (found.length > room) {
			const lost = found.some((held, n) => n >= room - 1 && held !== SPACE && held !== RULE_CELL);
			if (found[room - 1] === REST)
				found[room - 2] = SPACE;
			found[room - 1] = lost ? ELLIPSIS : found[found.length - 1];
			found.length = room;
		}
		const start = y * cols + x;
		const after = start + found.length;
		if (cells[start] === REST && x > 0)
			cells[start - 1] = SPACE;
		if (x + found.length < cols && cells[after] === REST)
			cells[after] = SPACE;
		for (let at = start; at < after; at++) {
			cells[at] = found[at - start];
			inks[at] = pen;
		}
		return x + found.length;
	}
	lines(area, rows) {
		rows.slice(0, Math.max(area.h, 0)).forEach((row, n) => this.spans(area.x, area.y + n, row, area.end));
	}
	whole(start, y, end, parts, gap = []) {
		return parts.reduce((x, part) => {
			const line = x > start ? [...gap, ...part] : part;
			return x + this.measure(line) <= end ? this.spans(x, y, line, end) : x;
		}, start);
	}
	frame(area, pen) {
		const line = times('─', area.w - 2);
		const gap = times(' ', area.w - 2);
		for (let y = area.y + 1; y < area.y + area.h - 1; y++)
			this.put(area.x, y, `│${gap}│`, pen);
		this.put(area.x, area.y, `╭${line}╮`, pen);
		this.put(area.x, area.y + area.h - 1, `╰${line}╯`, pen);
	}
	fade(share) {
		this.grid.recolour(pen => on(mix(colourOf(pen), PAPER, share), mix(paperOf(pen), PAPER, share)));
	}
}