termita-tui 0.0.0-stage → 0.1.1
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/LICENSE +21 -0
- package/README.md +170 -2
- package/lib/canvas/index.js +335 -0
- package/lib/colors/index.js +43 -0
- package/lib/element/index.js +178 -0
- package/lib/elements/box.js +147 -0
- package/lib/elements/input.js +84 -0
- package/lib/elements/list.js +192 -0
- package/lib/elements/text.js +140 -0
- package/lib/matrix/index.js +131 -0
- package/lib/screen/index.js +174 -0
- package/lib/types/index.d.js +117 -0
- package/package.json +37 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Gabriel Antonio Farias Ravanal <galileo.fandango@gmail.com>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,171 @@
|
|
|
1
|
-
#
|
|
1
|
+
# termita
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
I named it **termita** because it is for the **term**inal, and because it is a tiny,
|
|
4
|
+
annoying package that gets in your way in the best possible way. I built it for my
|
|
5
|
+
personal projects to avoid installing a giant TUI library just to put a box with text
|
|
6
|
+
in a terminal.
|
|
7
|
+
|
|
8
|
+
- Zero runtime dependencies
|
|
9
|
+
- CommonJS, works with plain `require()`
|
|
10
|
+
- No async, no build step, no react-style reconciler — you mutate, it renders
|
|
11
|
+
|
|
12
|
+
```sh
|
|
13
|
+
npm i termita-tui
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Quick start
|
|
17
|
+
|
|
18
|
+
```js
|
|
19
|
+
const screen = require("termita-tui"); // the screen singleton (also "termita/screen")
|
|
20
|
+
const Text = require("termita-tui/text");
|
|
21
|
+
|
|
22
|
+
screen.append(new Text("hello world", "green"), 2, 1);
|
|
23
|
+
screen.start(); // hides the cursor, renders, keeps terminal clean
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
`Ctrl+C` restores the terminal (clear screen, show cursor) on the way out.
|
|
27
|
+
|
|
28
|
+
The screen is a singleton per process — one root of the element tree, one keyboard.
|
|
29
|
+
Every element hangs from it with `screen.append(element, x, y)`.
|
|
30
|
+
|
|
31
|
+
## Elements
|
|
32
|
+
|
|
33
|
+
```js
|
|
34
|
+
const Text = require("termita-tui/text");
|
|
35
|
+
const Box = require("termita-tui/box");
|
|
36
|
+
const List = require("termita-tui/list");
|
|
37
|
+
const Input = require("termita-tui/input");
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### `Text`
|
|
41
|
+
|
|
42
|
+
```js
|
|
43
|
+
new Text("value", "red");
|
|
44
|
+
new Text("value", { color: "yellow", backgroundColor: "bgBlue", fontWeight: "bold" });
|
|
45
|
+
new Text("long paragraph...", { width: 20, textWrap: "wrap" }); // never overflows its width
|
|
46
|
+
new Text("long line...", { width: 20, textOverflow: "ellipsis" }); // cut with "…"
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
`t.set(newText)` replaces the content and re-renders.
|
|
50
|
+
|
|
51
|
+
### `Box`
|
|
52
|
+
|
|
53
|
+
The only element that draws a frame, and the only one that accepts `border`:
|
|
54
|
+
|
|
55
|
+
```js
|
|
56
|
+
const panel = new Box(24, 6, { border: "solid", backgroundColor: "bgBlack" });
|
|
57
|
+
panel.fill(); // paint the interior so the background covers the whole rect
|
|
58
|
+
panel.fillRow(0); // paint one full row (for height-1 boxes)
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Borders: `"solid" | "double" | "rounded" | "heavy" | "ascii"` — or omitted for an invisible div.
|
|
62
|
+
A `Box` **clips its children** to its rect: overflow is hidden by design.
|
|
63
|
+
|
|
64
|
+
### `List`
|
|
65
|
+
|
|
66
|
+
A menu of strings. `use()` captures the keyboard (arrows/j/k move, enter picks,
|
|
67
|
+
escape returns `null`) and resolves with the chosen text:
|
|
68
|
+
|
|
69
|
+
```js
|
|
70
|
+
const menu = new List(20, ["compile", "deploy", "quit"], { selectedBgColor: "bgBlue", selectedChar: ">" });
|
|
71
|
+
screen.append(menu, 2, 2);
|
|
72
|
+
|
|
73
|
+
const choice = await menu.use();
|
|
74
|
+
if (choice === "quit") screen.stop();
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### `Input`
|
|
78
|
+
|
|
79
|
+
```js
|
|
80
|
+
const input = new Input("username: ");
|
|
81
|
+
screen.append(input, 2, 5);
|
|
82
|
+
|
|
83
|
+
const value = await input.use(); // returns a Promise<string>
|
|
84
|
+
const secret = await input.use(true); // masks with *
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## Nothing is mandatory
|
|
88
|
+
|
|
89
|
+
Every bundled element is just an `Element`. If you dislike a style, want a different
|
|
90
|
+
selection color, or need a widget that does not exist — **extend `Element` and put it
|
|
91
|
+
on the screen**. The compositor does not care about your classes: it only reads each
|
|
92
|
+
element's character grid, its `x/y`, its `styles` and its children.
|
|
93
|
+
|
|
94
|
+
```js
|
|
95
|
+
const Element = require("termita/element");
|
|
96
|
+
|
|
97
|
+
class ProgressBar extends Element
|
|
98
|
+
{
|
|
99
|
+
setValue(value)
|
|
100
|
+
{
|
|
101
|
+
const cells = Math.round(value * 10);
|
|
102
|
+
this.matrix.setString("[" + "#".repeat(cells) + "-".repeat(10 - cells) + "]");
|
|
103
|
+
this.refresh();
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const bar = new ProgressBar();
|
|
108
|
+
screen.append(bar, 4, 3);
|
|
109
|
+
bar.setValue(0.6);
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
`screen.append(panel, 0, 0)`, `panel.append(new Text("x", "red"), 2, 1)` and
|
|
113
|
+
`child.move(4, 2)` work for anything — including your own classes.
|
|
114
|
+
|
|
115
|
+
## Styles
|
|
116
|
+
|
|
117
|
+
Every element has a `styles` object (`color`, `backgroundColor`, `fontWeight`,
|
|
118
|
+
`fontStyle`, `textDecoration`) and **styles cascade**: children inherit what the
|
|
119
|
+
parent declares and override what they declare themselves.
|
|
120
|
+
|
|
121
|
+
```js
|
|
122
|
+
panel.styles = { backgroundColor: "bgYellow" }; // children sit on yellow
|
|
123
|
+
label.styles = { color: "red" }; // ...with red text, yellow bg kept
|
|
124
|
+
label.styles = { backgroundColor: undefined }; // undefined removes a key: inherits again
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
`Box` adds `border` (its own type, `BoxStyles`). `Text` adds `width` + `textWrap` /
|
|
128
|
+
`textOverflow` together (`TextOptions`); using them without `width` is a type error.
|
|
129
|
+
`List` adds the `selected*` options.
|
|
130
|
+
|
|
131
|
+
## Scrolling
|
|
132
|
+
|
|
133
|
+
Any element can scroll **its children** (its own frame/background stays still):
|
|
134
|
+
|
|
135
|
+
```js
|
|
136
|
+
panel.scrollTo(0, 3); // x, y — negative and decimal values are allowed
|
|
137
|
+
panel.view_point; // { x, y }, read only
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
The library imposes no limits; clamp (or don't) in your own code.
|
|
141
|
+
|
|
142
|
+
## Keyboard
|
|
143
|
+
|
|
144
|
+
```js
|
|
145
|
+
screen.onKey("enter", (info) => console.log(info.key, info.raw));
|
|
146
|
+
screen.onKey(["j", "k", "down", "up"], moveSelection);
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
Named keys: `enter`, `escape`, `space`, `tab`, `backspace`, `delete`,
|
|
150
|
+
`up | down | left | right`, `ctrl+c` — anything else is the lowercase character.
|
|
151
|
+
By default `ctrl+c` ends the program (cleanly); subscribe to it to take control.
|
|
152
|
+
|
|
153
|
+
Modal capture — while active, **global handlers go silent** (that's how `Input.use()`
|
|
154
|
+
and `List.use()` coexist with `onKey`):
|
|
155
|
+
|
|
156
|
+
```js
|
|
157
|
+
screen.captureKeys((info) => { /* only I see the keys */ });
|
|
158
|
+
screen.releaseKeys();
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
## Screens
|
|
162
|
+
|
|
163
|
+
```js
|
|
164
|
+
screen.start();
|
|
165
|
+
screen.stop(); // restore cursor + clear
|
|
166
|
+
screen.refresh(); // re-render on demand (structure changes auto-render)
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
## License
|
|
170
|
+
|
|
171
|
+
MIT — Gabriel Antonio Farias Ravanal
|
|
@@ -0,0 +1,335 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file
|
|
3
|
+
* @description Canvas to global calcs. Compone el arbol de elementos en filas de tramos (pieces)
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @import {IScreen, IElement, Styles} from "#lib/types/index.d.js"
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
const COLORS = require("#lib/colors/index.js");
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @typedef {{
|
|
14
|
+
* element_source: IElement;
|
|
15
|
+
* start: number;
|
|
16
|
+
* length: number;
|
|
17
|
+
* element_origin: { x: number; y: number };
|
|
18
|
+
* inherited: Styles;
|
|
19
|
+
* }} ICanvasPiece
|
|
20
|
+
* @typedef {{
|
|
21
|
+
* readonly pieces: Array<ICanvasPiece>;
|
|
22
|
+
* insert(x: number, piece: ICanvasPiece): void;
|
|
23
|
+
* reset(): void;
|
|
24
|
+
* }} ICanvasRow
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* @param {Styles} styles
|
|
29
|
+
* @returns {string}
|
|
30
|
+
*/
|
|
31
|
+
const stylesToAnsi = function(styles)
|
|
32
|
+
{
|
|
33
|
+
const parts = [];
|
|
34
|
+
if(styles.fontWeight === "bold") parts.push(COLORS.bold);
|
|
35
|
+
if(styles.fontWeight === "dim") parts.push(COLORS.dim);
|
|
36
|
+
if(styles.fontStyle === "italic") parts.push(COLORS.italic);
|
|
37
|
+
if(styles.textDecoration === "underline") parts.push(COLORS.underline);
|
|
38
|
+
if(styles.textDecoration === "line-through") parts.push(COLORS.strikethrough);
|
|
39
|
+
if(styles.color) parts.push(COLORS[styles.color]);
|
|
40
|
+
if(styles.backgroundColor) parts.push(COLORS[styles.backgroundColor]);
|
|
41
|
+
return parts.join("");
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Interseccion de dos rects de clip (el clip de un hijo nunca amplía al del padre).
|
|
46
|
+
* @param {{ x0: number; y0: number; x1: number; y1: number }} a
|
|
47
|
+
* @param {{ x0: number; y0: number; x1: number; y1: number }} b
|
|
48
|
+
* @returns {{ x0: number; y0: number; x1: number; y1: number }}
|
|
49
|
+
*/
|
|
50
|
+
const clipIntersection = function(a, b)
|
|
51
|
+
{
|
|
52
|
+
return {
|
|
53
|
+
x0: Math.max(a.x0, b.x0),
|
|
54
|
+
y0: Math.max(a.y0, b.y0),
|
|
55
|
+
x1: Math.min(a.x1, b.x1),
|
|
56
|
+
y1: Math.min(a.y1, b.y1),
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* @class
|
|
62
|
+
* @implements {ICanvasPiece}
|
|
63
|
+
*/
|
|
64
|
+
const CanvasPiece = class
|
|
65
|
+
{
|
|
66
|
+
/**
|
|
67
|
+
* @param {IElement} element_source
|
|
68
|
+
* @param {number} start columna absoluta donde comienza el tramo en esta fila
|
|
69
|
+
* @param {number} length
|
|
70
|
+
* @param {{ x: number; y: number }} element_origin origen absoluto de la matrix del elemento
|
|
71
|
+
* @param {Styles} inherited estilos heredados del padre
|
|
72
|
+
*/
|
|
73
|
+
constructor(element_source, start, length, element_origin, inherited)
|
|
74
|
+
{
|
|
75
|
+
this.element_source = element_source;
|
|
76
|
+
this.start = start;
|
|
77
|
+
this.length = length;
|
|
78
|
+
this.element_origin = element_origin;
|
|
79
|
+
this.inherited = inherited;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* @class
|
|
85
|
+
* @implements {ICanvasRow}
|
|
86
|
+
*/
|
|
87
|
+
const CanvasRow = class
|
|
88
|
+
{
|
|
89
|
+
/**@type {number}*/
|
|
90
|
+
#width;
|
|
91
|
+
/**@type {Array<ICanvasPiece|null>}*/
|
|
92
|
+
#painted;
|
|
93
|
+
/**@type {Array<ICanvasPiece>|null}*/
|
|
94
|
+
#pieces = null;
|
|
95
|
+
/**
|
|
96
|
+
* @param {number} width
|
|
97
|
+
*/
|
|
98
|
+
constructor(width)
|
|
99
|
+
{
|
|
100
|
+
this.#width = width;
|
|
101
|
+
this.#painted = new Array(width).fill(null);
|
|
102
|
+
}
|
|
103
|
+
/**@returns {Array<ICanvasPiece>}*/
|
|
104
|
+
get pieces()
|
|
105
|
+
{
|
|
106
|
+
if(!this.#pieces) this.#pieces = this.#compress();
|
|
107
|
+
return this.#pieces;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Pinta el piece sobre [x, x + length). Lo ultimo pintado gana: el z-order se controla por orden de pintado.
|
|
111
|
+
* @param {number} x
|
|
112
|
+
* @param {ICanvasPiece} piece
|
|
113
|
+
* @returns {void}
|
|
114
|
+
*/
|
|
115
|
+
insert(x, piece)
|
|
116
|
+
{
|
|
117
|
+
const start = Math.max(0, x);
|
|
118
|
+
const end = Math.min(this.#width, x + piece.length);
|
|
119
|
+
for(let i = start; i < end; i++)
|
|
120
|
+
{
|
|
121
|
+
this.#painted[i] = piece;
|
|
122
|
+
}
|
|
123
|
+
this.#pieces = null;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* @returns {void}
|
|
127
|
+
*/
|
|
128
|
+
reset()
|
|
129
|
+
{
|
|
130
|
+
this.#painted.fill(null);
|
|
131
|
+
this.#pieces = null;
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Run length encoding del array pintado. Dos celdas se fusionan solo si apuntan al mismo objeto piece.
|
|
135
|
+
* El offset de lectura se calcula contra element_origin, asi que el start memorizado aca es la
|
|
136
|
+
* columna real de pantalla (incluso para tramos cortados por el borde izquierdo).
|
|
137
|
+
* @returns {Array<ICanvasPiece>}
|
|
138
|
+
*/
|
|
139
|
+
#compress()
|
|
140
|
+
{
|
|
141
|
+
/**@type {ICanvasPiece[]}*/
|
|
142
|
+
const pieces = [];
|
|
143
|
+
/**@type {ICanvasPiece|null}*/
|
|
144
|
+
let source = null;
|
|
145
|
+
/**@type {ICanvasPiece|null}*/
|
|
146
|
+
let current = null;
|
|
147
|
+
for(let x = 0; x < this.#width; x++)
|
|
148
|
+
{
|
|
149
|
+
const ref = this.#painted[x];
|
|
150
|
+
if(!ref || ref !== source)
|
|
151
|
+
{
|
|
152
|
+
source = ref;
|
|
153
|
+
current = ref ? new CanvasPiece(ref.element_source, x, 1, ref.element_origin, ref.inherited) : null;
|
|
154
|
+
if(current) pieces.push(current);
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
/**@type {ICanvasPiece}*/(current).length++;
|
|
158
|
+
}
|
|
159
|
+
return pieces;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* @class
|
|
165
|
+
*/
|
|
166
|
+
const Canvas = class
|
|
167
|
+
{
|
|
168
|
+
/**@type {IScreen}*/
|
|
169
|
+
#screen;
|
|
170
|
+
/**@type {CanvasRow[]}*/
|
|
171
|
+
#rows = [];
|
|
172
|
+
/**@type {number}*/
|
|
173
|
+
#width = 0;
|
|
174
|
+
/**@type {number}*/
|
|
175
|
+
#height = 0;
|
|
176
|
+
/**@type {boolean}*/
|
|
177
|
+
#dirty = true;
|
|
178
|
+
/**
|
|
179
|
+
* @param {IScreen} screen
|
|
180
|
+
*/
|
|
181
|
+
constructor(screen)
|
|
182
|
+
{
|
|
183
|
+
this.#screen = screen;
|
|
184
|
+
this.resize();
|
|
185
|
+
}
|
|
186
|
+
get width(){ return this.#width }
|
|
187
|
+
get height(){ return this.#height }
|
|
188
|
+
/**
|
|
189
|
+
* Reconstruye las filas al tamano de la viewport. Llamar en resize() de la terminal.
|
|
190
|
+
* @returns {void}
|
|
191
|
+
*/
|
|
192
|
+
resize()
|
|
193
|
+
{
|
|
194
|
+
this.#width = process.stdout.columns || 80;
|
|
195
|
+
this.#height = process.stdout.rows || 24;
|
|
196
|
+
this.#rows = Array.from({ length: this.#height }, () => new CanvasRow(this.#width));
|
|
197
|
+
this.#dirty = true;
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Marca estructura cambiada (move/append/removeChild) o contenido que agrando la matrix.
|
|
201
|
+
* El recalc real es perezoso en getString().
|
|
202
|
+
* @returns {void}
|
|
203
|
+
*/
|
|
204
|
+
mark()
|
|
205
|
+
{
|
|
206
|
+
this.#dirty = true;
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* @returns {void}
|
|
210
|
+
*/
|
|
211
|
+
recalc()
|
|
212
|
+
{
|
|
213
|
+
this.#rows.forEach(row => row.reset());
|
|
214
|
+
this.#paint(this.#screen, { x: 0, y: 0 }, {});
|
|
215
|
+
this.#dirty = false;
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Composicion final: string de pantalla listo para escribir en stdout.
|
|
219
|
+
* Si hay cambios estructurales marca la diferencia: recalc corre solo cuando hace falta.
|
|
220
|
+
* @returns {string}
|
|
221
|
+
*/
|
|
222
|
+
getString()
|
|
223
|
+
{
|
|
224
|
+
if(this.#dirty) this.recalc();
|
|
225
|
+
const lines = [];
|
|
226
|
+
/**@type {string}*/
|
|
227
|
+
let active = "";
|
|
228
|
+
for(let y = 0; y < this.#height; y++)
|
|
229
|
+
{
|
|
230
|
+
let line = "";
|
|
231
|
+
let cursor = 0;
|
|
232
|
+
for(const piece of this.#rows[y].pieces)
|
|
233
|
+
{
|
|
234
|
+
if(piece.start > cursor)
|
|
235
|
+
{
|
|
236
|
+
if(active)
|
|
237
|
+
{
|
|
238
|
+
line += COLORS.reset;
|
|
239
|
+
active = "";
|
|
240
|
+
}
|
|
241
|
+
line += " ".repeat(piece.start - cursor);
|
|
242
|
+
}
|
|
243
|
+
const ansi = stylesToAnsi({ ...piece.inherited, ...piece.element_source.styles });
|
|
244
|
+
if(ansi !== active)
|
|
245
|
+
{
|
|
246
|
+
line += COLORS.reset + ansi;
|
|
247
|
+
active = ansi;
|
|
248
|
+
}
|
|
249
|
+
const matrix_y = y - piece.element_origin.y;
|
|
250
|
+
const matrix_x = piece.start - piece.element_origin.x;
|
|
251
|
+
for(let i = 0; i < piece.length; i++)
|
|
252
|
+
{
|
|
253
|
+
line += piece.element_source.matrix.get(matrix_x + i, matrix_y) || " ";
|
|
254
|
+
}
|
|
255
|
+
cursor = piece.start + piece.length;
|
|
256
|
+
}
|
|
257
|
+
if(cursor < this.#width)
|
|
258
|
+
{
|
|
259
|
+
if(active)
|
|
260
|
+
{
|
|
261
|
+
line += COLORS.reset;
|
|
262
|
+
active = "";
|
|
263
|
+
}
|
|
264
|
+
line += " ".repeat(this.#width - cursor);
|
|
265
|
+
}
|
|
266
|
+
if(active)
|
|
267
|
+
{
|
|
268
|
+
line += COLORS.reset;
|
|
269
|
+
active = "";
|
|
270
|
+
}
|
|
271
|
+
lines.push(line);
|
|
272
|
+
}
|
|
273
|
+
return lines.join("\n");
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Pinta las runs no vacias de un elemento y luego desciende a sus hijos.
|
|
277
|
+
* Padre antes que hijos, hermanos en orden de llegada (el ultimo queda arriba).
|
|
278
|
+
* @param {IElement} element
|
|
279
|
+
* @param {{ x: number; y: number }} origin
|
|
280
|
+
* @param {Styles} inherited
|
|
281
|
+
* @param {{ x0: number; y0: number; x1: number; y1: number }|null} [clip]
|
|
282
|
+
*/
|
|
283
|
+
#paint(element, origin, inherited, clip = null)
|
|
284
|
+
{
|
|
285
|
+
const computed = { ...inherited, ...element.styles };
|
|
286
|
+
const matrix = element.matrix;
|
|
287
|
+
const x0 = clip ? Math.max(0, clip.x0) : 0;
|
|
288
|
+
const x1 = clip ? Math.min(this.#width, clip.x1) : this.#width;
|
|
289
|
+
for(let y = 0; y < matrix.height; y++)
|
|
290
|
+
{
|
|
291
|
+
const abs_y = origin.y + y;
|
|
292
|
+
if(abs_y < 0 || abs_y >= this.#height) continue;
|
|
293
|
+
if(clip && (abs_y < clip.y0 || abs_y >= clip.y1)) continue;
|
|
294
|
+
const row = this.#rows[abs_y];
|
|
295
|
+
|
|
296
|
+
/**@type {ICanvasPiece|null}*/
|
|
297
|
+
let run = null;
|
|
298
|
+
for(let x = 0; x <= matrix.width; x++)
|
|
299
|
+
{
|
|
300
|
+
const filled = x < matrix.width && matrix.get(x, y) !== null;
|
|
301
|
+
if(filled)
|
|
302
|
+
{
|
|
303
|
+
if(run) run.length++;
|
|
304
|
+
else run = new CanvasPiece(element, origin.x + x, 1, origin, inherited);
|
|
305
|
+
}
|
|
306
|
+
else if(run)
|
|
307
|
+
{
|
|
308
|
+
const start = Math.max(run.start, x0);
|
|
309
|
+
const end = Math.min(run.start + run.length, x1);
|
|
310
|
+
if(start < end)
|
|
311
|
+
{
|
|
312
|
+
run = new CanvasPiece(element, start, end - start, origin, inherited);
|
|
313
|
+
row.insert(start, run);
|
|
314
|
+
}
|
|
315
|
+
run = null;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
const own = element.getClipRect(origin);
|
|
320
|
+
const inner = own ? (clip ? clipIntersection(clip, own) : own) : clip;
|
|
321
|
+
element.children.forEach(child =>
|
|
322
|
+
{
|
|
323
|
+
this.#paint(child,
|
|
324
|
+
{
|
|
325
|
+
// el view_point admite fracciones y negativos: la grilla se cuantiza aca, no en scrollTo
|
|
326
|
+
x: Math.floor(origin.x + child.position.x - element.view_point.x),
|
|
327
|
+
y: Math.floor(origin.y + child.position.y - element.view_point.y),
|
|
328
|
+
},
|
|
329
|
+
computed,
|
|
330
|
+
inner);
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
module.exports = Canvas;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
reset: '\x1b[0m',
|
|
3
|
+
bold: '\x1b[1m',
|
|
4
|
+
dim: '\x1b[2m',
|
|
5
|
+
italic: '\x1b[3m',
|
|
6
|
+
underline: '\x1b[4m',
|
|
7
|
+
blink: '\x1b[5m',
|
|
8
|
+
reverse: '\x1b[7m',
|
|
9
|
+
hidden: '\x1b[8m',
|
|
10
|
+
strikethrough: '\x1b[9m',
|
|
11
|
+
black: '\x1b[30m',
|
|
12
|
+
red: '\x1b[31m',
|
|
13
|
+
green: '\x1b[32m',
|
|
14
|
+
yellow: '\x1b[33m',
|
|
15
|
+
blue: '\x1b[34m',
|
|
16
|
+
magenta: '\x1b[35m',
|
|
17
|
+
cyan: '\x1b[36m',
|
|
18
|
+
white: '\x1b[37m',
|
|
19
|
+
brightBlack: '\x1b[90m',
|
|
20
|
+
brightRed: '\x1b[91m',
|
|
21
|
+
brightGreen: '\x1b[92m',
|
|
22
|
+
brightYellow: '\x1b[93m',
|
|
23
|
+
brightBlue: '\x1b[94m',
|
|
24
|
+
brightMagenta: '\x1b[95m',
|
|
25
|
+
brightCyan: '\x1b[96m',
|
|
26
|
+
brightWhite: '\x1b[97m',
|
|
27
|
+
bgBlack: '\x1b[40m',
|
|
28
|
+
bgRed: '\x1b[41m',
|
|
29
|
+
bgGreen: '\x1b[42m',
|
|
30
|
+
bgYellow: '\x1b[43m',
|
|
31
|
+
bgBlue: '\x1b[44m',
|
|
32
|
+
bgMagenta: '\x1b[45m',
|
|
33
|
+
bgCyan: '\x1b[46m',
|
|
34
|
+
bgWhite: '\x1b[47m',
|
|
35
|
+
bgBrightBlack: '\x1b[100m',
|
|
36
|
+
bgBrightRed: '\x1b[101m',
|
|
37
|
+
bgBrightGreen: '\x1b[102m',
|
|
38
|
+
bgBrightYellow: '\x1b[103m',
|
|
39
|
+
bgBrightBlue: '\x1b[104m',
|
|
40
|
+
bgBrightMagenta: '\x1b[105m',
|
|
41
|
+
bgBrightCyan: '\x1b[106m',
|
|
42
|
+
bgBrightWhite: '\x1b[107m',
|
|
43
|
+
};
|