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
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file
|
|
3
|
+
* @description Class to extends to screen elements
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
const Matrix = require("#lib/matrix/index.js");
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @import {IScreen, IElement, IMatrix, Styles} from "#lib/types/index.d.js"
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @class
|
|
14
|
+
* @implements {IElement}
|
|
15
|
+
*/
|
|
16
|
+
const Element = class
|
|
17
|
+
{
|
|
18
|
+
/**@type {IScreen|null}*/
|
|
19
|
+
#screen;
|
|
20
|
+
/**@type {{ x: number; y: number }}*/
|
|
21
|
+
#position;
|
|
22
|
+
/**@type {IElement | null}*/
|
|
23
|
+
#parent;
|
|
24
|
+
/**@type {Set<IElement>}*/
|
|
25
|
+
#children;
|
|
26
|
+
/**@type {IMatrix}*/
|
|
27
|
+
#matrix;
|
|
28
|
+
/**@type {Styles}*/
|
|
29
|
+
#styles;
|
|
30
|
+
/**@type {{ x: number; y: number }}*/
|
|
31
|
+
#view_point = { x: 0, y: 0 };
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* @param {Styles} [styles]
|
|
35
|
+
*/
|
|
36
|
+
constructor(styles = {})
|
|
37
|
+
{
|
|
38
|
+
this.#screen = null;
|
|
39
|
+
this.#parent = null;
|
|
40
|
+
this.#position = { x: 0, y: 0 };
|
|
41
|
+
this.#children = new Set();
|
|
42
|
+
this.#styles = { ...styles };
|
|
43
|
+
|
|
44
|
+
this.#matrix = new Matrix();
|
|
45
|
+
}
|
|
46
|
+
/**@returns {IScreen}*/
|
|
47
|
+
get screen()
|
|
48
|
+
{
|
|
49
|
+
if(!this.#screen) throw new Error("[TERMITA ERROR]: Element has no screen attached. Add it with screen.append(element, x, y) before accessing .screen.");
|
|
50
|
+
return this.#screen;
|
|
51
|
+
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
get parent()
|
|
55
|
+
{
|
|
56
|
+
if(!this.#parent) throw new Error("[TERMITA ERROR]: Element has no parent. Root elements (added directly to the screen) have no parent; to attach children use parent.append(element, x, y).");
|
|
57
|
+
return this.#parent;
|
|
58
|
+
};
|
|
59
|
+
get position()
|
|
60
|
+
{
|
|
61
|
+
const _this = this;
|
|
62
|
+
return {
|
|
63
|
+
get x() {
|
|
64
|
+
return _this.#position.x
|
|
65
|
+
},
|
|
66
|
+
get y() {
|
|
67
|
+
return _this.#position.y
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
get children(){ return this.#children };
|
|
72
|
+
get matrix(){ return this.#matrix };
|
|
73
|
+
/**@returns {Styles}*/
|
|
74
|
+
get styles(){ return this.#styles }
|
|
75
|
+
/**
|
|
76
|
+
* Ajusta estilos parciales (se mezclan con los existentes).
|
|
77
|
+
* @param {Styles} styles
|
|
78
|
+
*/
|
|
79
|
+
set styles(styles)
|
|
80
|
+
{
|
|
81
|
+
const merged = { ...this.#styles, ...styles };
|
|
82
|
+
for(const key in merged) if(merged[key] === undefined) delete merged[key];
|
|
83
|
+
this.#styles = /**@type {Styles}*/(merged);
|
|
84
|
+
this.__invalidate();
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* @param {number} x
|
|
88
|
+
* @param {number} y
|
|
89
|
+
*/
|
|
90
|
+
move(x, y)
|
|
91
|
+
{
|
|
92
|
+
this.#position.x = x;
|
|
93
|
+
this.#position.y = y;
|
|
94
|
+
this.__invalidate();
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* @param {IElement} element
|
|
98
|
+
* @param {number} x
|
|
99
|
+
* @param {number} y
|
|
100
|
+
*/
|
|
101
|
+
append(element, x, y)
|
|
102
|
+
{
|
|
103
|
+
if(element.#parent && element.#parent !== this) element.#parent.removeChild(element);
|
|
104
|
+
if(this.#screen) element.__attachScreen(this.#screen);
|
|
105
|
+
element.__attachParent(this);
|
|
106
|
+
element.move(x, y);
|
|
107
|
+
this.#children.add(element);
|
|
108
|
+
this.__invalidate();
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* @param {IElement} element
|
|
112
|
+
* @returns {void}
|
|
113
|
+
*/
|
|
114
|
+
removeChild(element)
|
|
115
|
+
{
|
|
116
|
+
this.#children.delete(element);
|
|
117
|
+
element.__attachScreen(null);
|
|
118
|
+
element.__attachParent(null);
|
|
119
|
+
this.__invalidate();
|
|
120
|
+
}
|
|
121
|
+
remove()
|
|
122
|
+
{
|
|
123
|
+
this.parent.removeChild(this);
|
|
124
|
+
}
|
|
125
|
+
refresh()
|
|
126
|
+
{
|
|
127
|
+
this.__invalidate();
|
|
128
|
+
}
|
|
129
|
+
/**@returns {{ x: number; y: number }}*/
|
|
130
|
+
get view_point(){ return this.#view_point };
|
|
131
|
+
/**
|
|
132
|
+
* Mueve el punto de vista del elemento: sus hijos se desplazan, su propio contenido no.
|
|
133
|
+
* @param {number} x
|
|
134
|
+
* @param {number} y
|
|
135
|
+
* @returns {void}
|
|
136
|
+
*/
|
|
137
|
+
scrollTo(x, y)
|
|
138
|
+
{
|
|
139
|
+
this.#view_point = { x, y };
|
|
140
|
+
this.__invalidate();
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Rectangulo de recorte propio en coords de screen. Null = no recorta a sus hijos.
|
|
144
|
+
* @returns {{ x0: number; y0: number; x1: number; y1: number }|null}
|
|
145
|
+
*/
|
|
146
|
+
getClipRect()
|
|
147
|
+
{
|
|
148
|
+
return null;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Marca el canvas y re-renderiza. No-op si el elemento aun no tiene screen.
|
|
152
|
+
*/
|
|
153
|
+
__invalidate()
|
|
154
|
+
{
|
|
155
|
+
if(!this.#screen) return;
|
|
156
|
+
this.#screen.canvas.mark();
|
|
157
|
+
this.#screen.refresh();
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* @param {IScreen|null} screen
|
|
161
|
+
*/
|
|
162
|
+
__attachScreen(screen)
|
|
163
|
+
{
|
|
164
|
+
this.#screen = screen;
|
|
165
|
+
this.#children.forEach(child => child.__attachScreen(screen));
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* @param {IElement} parent
|
|
170
|
+
*/
|
|
171
|
+
__attachParent(parent)
|
|
172
|
+
{
|
|
173
|
+
this.#parent = parent;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
module.exports = Element;
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file
|
|
3
|
+
* @description box element: contenedor con dimensiones y borde opcional (styles.border)
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @import {BoxStyles} from "#lib/types/index.d.js"
|
|
8
|
+
*/
|
|
9
|
+
const Element = require("#lib/element/index.js");
|
|
10
|
+
|
|
11
|
+
const BORDERS = {
|
|
12
|
+
solid: { tl: "┌", tr: "┐", bl: "└", br: "┘", h: "─", v: "│" },
|
|
13
|
+
double: { tl: "╔", tr: "╗", bl: "╚", br: "╝", h: "═", v: "║" },
|
|
14
|
+
rounded: { tl: "╭", tr: "╮", bl: "╰", br: "╯", h: "─", v: "│" },
|
|
15
|
+
heavy: { tl: "┏", tr: "┓", bl: "┗", br: "┛", h: "━", v: "┃" },
|
|
16
|
+
ascii: { tl: "+", tr: "+", bl: "+", br: "+", h: "-", v: "|" },
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const Box = class extends Element
|
|
20
|
+
{
|
|
21
|
+
/**@type {boolean}*/
|
|
22
|
+
#filled = false;
|
|
23
|
+
/**@type {string}*/
|
|
24
|
+
#fillChar = " ";
|
|
25
|
+
/**@type {Set<number>}*/
|
|
26
|
+
#extraRows = new Set();
|
|
27
|
+
/**
|
|
28
|
+
* @param {number} width
|
|
29
|
+
* @param {number} height
|
|
30
|
+
* @param {BoxStyles} [styles]
|
|
31
|
+
*/
|
|
32
|
+
constructor(width, height, styles)
|
|
33
|
+
{
|
|
34
|
+
super(styles);
|
|
35
|
+
|
|
36
|
+
this.width = width;
|
|
37
|
+
this.height = height;
|
|
38
|
+
|
|
39
|
+
this.#drawBorder();
|
|
40
|
+
}
|
|
41
|
+
/**@returns {BoxStyles}*/
|
|
42
|
+
get styles(){ return super.styles };
|
|
43
|
+
/**
|
|
44
|
+
* @param {BoxStyles} styles
|
|
45
|
+
*/
|
|
46
|
+
set styles(styles)
|
|
47
|
+
{
|
|
48
|
+
super.styles = styles;
|
|
49
|
+
this.#redraw();
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Rellena el interior para que los propios de estilos (backgroundColor) cubran todo el rectangulo.
|
|
53
|
+
* @param {string} [char=" "]
|
|
54
|
+
* @returns {void}
|
|
55
|
+
*/
|
|
56
|
+
fill(char = " ")
|
|
57
|
+
{
|
|
58
|
+
this.#filled = true;
|
|
59
|
+
this.#fillChar = char;
|
|
60
|
+
this.#paintInterior();
|
|
61
|
+
this.__invalidate();
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Rellena una fila completa y la recuerda para sobrevive a los redraws (items de altura 1).
|
|
65
|
+
* @param {number} y
|
|
66
|
+
* @param {string} [char=" "]
|
|
67
|
+
* @returns {void}
|
|
68
|
+
*/
|
|
69
|
+
fillRow(y, char = " ")
|
|
70
|
+
{
|
|
71
|
+
this.#filled = true;
|
|
72
|
+
this.#fillChar = char;
|
|
73
|
+
this.#extraRows.add(y);
|
|
74
|
+
this.#paintRow(y);
|
|
75
|
+
this.__invalidate();
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Rectangulo de recorte para los hijos, en coords de screen.
|
|
79
|
+
* @param {{ x: number; y: number }} origin
|
|
80
|
+
* @returns {{ x0: number; y0: number; x1: number; y1: number }}
|
|
81
|
+
*/
|
|
82
|
+
getClipRect(origin)
|
|
83
|
+
{
|
|
84
|
+
const inset = BORDERS[this.styles.border] ? 1 : 0;
|
|
85
|
+
return {
|
|
86
|
+
x0: origin.x + inset,
|
|
87
|
+
y0: origin.y + inset,
|
|
88
|
+
x1: origin.x + this.width - inset,
|
|
89
|
+
y1: origin.y + this.height - inset,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* @returns {void}
|
|
94
|
+
*/
|
|
95
|
+
#redraw()
|
|
96
|
+
{
|
|
97
|
+
this.matrix.reset();
|
|
98
|
+
this.#drawBorder();
|
|
99
|
+
if(this.#filled) this.#paintInterior();
|
|
100
|
+
this.#extraRows.forEach(y => this.#paintRow(y));
|
|
101
|
+
this.__invalidate();
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Rellena el interior sin tocar las paredes laterales.
|
|
105
|
+
* @returns {void}
|
|
106
|
+
*/
|
|
107
|
+
#paintInterior()
|
|
108
|
+
{
|
|
109
|
+
for(let y = 1; y < this.height - 1; y++)
|
|
110
|
+
{
|
|
111
|
+
for(let x = 1; x < this.width - 1; x++)
|
|
112
|
+
{
|
|
113
|
+
this.matrix.set(x, y, this.#fillChar);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* @param {number} y
|
|
119
|
+
* @returns {void}
|
|
120
|
+
*/
|
|
121
|
+
#paintRow(y)
|
|
122
|
+
{
|
|
123
|
+
for(let x = 0; x < this.width; x++)
|
|
124
|
+
{
|
|
125
|
+
this.matrix.set(x, y, this.#fillChar);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* @returns {void}
|
|
130
|
+
*/
|
|
131
|
+
#drawBorder()
|
|
132
|
+
{
|
|
133
|
+
const glyphset = BORDERS[this.styles.border];
|
|
134
|
+
if(!glyphset) return;
|
|
135
|
+
|
|
136
|
+
const matrix = this.matrix;
|
|
137
|
+
matrix.setString(glyphset.tl + glyphset.h.repeat(this.width - 2) + glyphset.tr, 0, 0);
|
|
138
|
+
matrix.setString(glyphset.bl + glyphset.h.repeat(this.width - 2) + glyphset.br, 0, this.height - 1);
|
|
139
|
+
for(let y = 1; y < this.height - 1; y++)
|
|
140
|
+
{
|
|
141
|
+
matrix.set(0, y, glyphset.v);
|
|
142
|
+
matrix.set(this.width - 1, y, glyphset.v);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
module.exports = Box;
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file
|
|
3
|
+
* @description A element with the user cli input
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
const Element = require("#lib/element/index.js");
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @import {Styles} from "#lib/types/index.d.js"
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
const Input = class extends Element
|
|
13
|
+
{
|
|
14
|
+
/**
|
|
15
|
+
* @param {string} prompt
|
|
16
|
+
* @param {Styles} [styles]
|
|
17
|
+
*/
|
|
18
|
+
constructor(prompt, styles)
|
|
19
|
+
{
|
|
20
|
+
super(styles);
|
|
21
|
+
|
|
22
|
+
this.matrix.setString(prompt);
|
|
23
|
+
/**@type {number}*/
|
|
24
|
+
this.inputY = this.matrix.height + 2;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* @param {boolean} [password=false]
|
|
28
|
+
* @returns {Promise<string>}
|
|
29
|
+
*/
|
|
30
|
+
use(password=false)
|
|
31
|
+
{
|
|
32
|
+
const chars = /**@type {string[]} */([]);
|
|
33
|
+
/**@type {number}*/
|
|
34
|
+
let cursorCurrentX = this.position.x;
|
|
35
|
+
const _this = this;
|
|
36
|
+
|
|
37
|
+
return new Promise(resolve =>
|
|
38
|
+
{
|
|
39
|
+
this.screen.captureKeys(function onKey(info)
|
|
40
|
+
{
|
|
41
|
+
if(info.key === "enter")
|
|
42
|
+
{
|
|
43
|
+
_this.screen.releaseKeys();
|
|
44
|
+
_this.matrix.remove(cursorCurrentX, _this.inputY); // quitar el cursor del render
|
|
45
|
+
_this.refresh();
|
|
46
|
+
resolve(chars.join(""));
|
|
47
|
+
}
|
|
48
|
+
else if(info.key === "ctrl+c")
|
|
49
|
+
{
|
|
50
|
+
_this.screen.releaseKeys();
|
|
51
|
+
process.kill(process.pid, "SIGINT");
|
|
52
|
+
}
|
|
53
|
+
else if(info.key === "backspace")
|
|
54
|
+
{
|
|
55
|
+
if(chars.length === 0) return;
|
|
56
|
+
chars.pop();
|
|
57
|
+
_this.matrix.remove(cursorCurrentX, _this.inputY);
|
|
58
|
+
cursorCurrentX--;
|
|
59
|
+
_this.matrix.set(cursorCurrentX, _this.inputY, "\u2588");
|
|
60
|
+
_this.refresh();
|
|
61
|
+
}
|
|
62
|
+
else
|
|
63
|
+
{
|
|
64
|
+
// teclas de navegacion (up/down/left/right/escape) no son texto
|
|
65
|
+
const text = info.key === "space" ? " " : info.key.length === 1 ? info.key : null;
|
|
66
|
+
if(text === null) return;
|
|
67
|
+
chars.push(text);
|
|
68
|
+
_this.matrix.setString(
|
|
69
|
+
password ? "*\u2588" : text + "\u2588",
|
|
70
|
+
cursorCurrentX,
|
|
71
|
+
_this.inputY,
|
|
72
|
+
);
|
|
73
|
+
cursorCurrentX++;
|
|
74
|
+
_this.refresh();
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
this.matrix.set(this.position.x, this.inputY, "\u2588");
|
|
79
|
+
this.refresh();
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
module.exports = Input;
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file
|
|
3
|
+
* @description List: coleccion de items navegables. use() captura el teclado y resuelve con el texto elegido.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
const Element = require("#lib/element/index.js");
|
|
7
|
+
const Box = require("#lib/elements/box.js");
|
|
8
|
+
const Text = require("#lib/elements/text.js");
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @import {ListOptions, Styles} from "#lib/types/index.d.js"
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
const STYLE_KEYS = ["color", "backgroundColor", "fontWeight", "fontStyle", "textDecoration"];
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @param {ListOptions} opts
|
|
18
|
+
* @returns {Styles}
|
|
19
|
+
*/
|
|
20
|
+
const pickStyles = function(opts)
|
|
21
|
+
{
|
|
22
|
+
const styles = /**@type {Styles}*/({});
|
|
23
|
+
for(const key of STYLE_KEYS) styles[key] = opts[key];
|
|
24
|
+
return styles;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const DELTA_KEYS = ["color", "backgroundColor", "fontWeight", "fontStyle"];
|
|
28
|
+
|
|
29
|
+
const List = class extends Element
|
|
30
|
+
{
|
|
31
|
+
/**@type {Array<{ box: Box, label: Text, text: string }>}*/
|
|
32
|
+
#items = [];
|
|
33
|
+
/**@type {number}*/
|
|
34
|
+
#selected = 0;
|
|
35
|
+
/**@type {ListOptions}*/
|
|
36
|
+
#opts = {};
|
|
37
|
+
/**
|
|
38
|
+
* @param {number} width
|
|
39
|
+
* @param {string[]} items
|
|
40
|
+
* @param {ListOptions} [opts]
|
|
41
|
+
*/
|
|
42
|
+
constructor(width, items, opts = {})
|
|
43
|
+
{
|
|
44
|
+
super(pickStyles(opts));
|
|
45
|
+
|
|
46
|
+
this.#opts = { ...opts };
|
|
47
|
+
this.width = width;
|
|
48
|
+
|
|
49
|
+
items.forEach((text, i) =>
|
|
50
|
+
{
|
|
51
|
+
const box = new Box(width, 1, { border: "none" });
|
|
52
|
+
box.fillRow(0);
|
|
53
|
+
const label = new Text("");
|
|
54
|
+
box.append(label, 1, 0);
|
|
55
|
+
this.append(box, 0, i);
|
|
56
|
+
this.#items.push({ box, label, text });
|
|
57
|
+
});
|
|
58
|
+
this.#renderSelection();
|
|
59
|
+
}
|
|
60
|
+
get selected(){ return this.#selected };
|
|
61
|
+
get length(){ return this.#items.length };
|
|
62
|
+
/**@returns {ListOptions}*/
|
|
63
|
+
get options(){ return { ...this.#opts } };
|
|
64
|
+
/**
|
|
65
|
+
* @param {number} index
|
|
66
|
+
* @returns {string}
|
|
67
|
+
*/
|
|
68
|
+
itemAt(index)
|
|
69
|
+
{
|
|
70
|
+
return this.#items[index].text;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* @param {number} delta
|
|
74
|
+
* @returns {boolean}
|
|
75
|
+
*/
|
|
76
|
+
select(delta)
|
|
77
|
+
{
|
|
78
|
+
const next = Math.max(0, Math.min(this.#items.length - 1, this.#selected + delta));
|
|
79
|
+
if(next === this.#selected) return false;
|
|
80
|
+
this.#selected = next;
|
|
81
|
+
this.#renderSelection();
|
|
82
|
+
this.#scrollIntoView();
|
|
83
|
+
return true;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* @param {ListOptions} opts
|
|
87
|
+
* @returns {void}
|
|
88
|
+
*/
|
|
89
|
+
setOptions(opts)
|
|
90
|
+
{
|
|
91
|
+
const merged = { ...this.#opts, ...opts };
|
|
92
|
+
for(const key in merged) if(merged[key] === null) delete merged[key];
|
|
93
|
+
this.#opts = /**@type {ListOptions}*/(merged);
|
|
94
|
+
this.styles = pickStyles(this.#opts);
|
|
95
|
+
this.#renderSelection();
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Pregunta modal: captura el teclado (up/down/j/k mueven, enter elige, escape cancela).
|
|
99
|
+
* @returns {Promise<string|null>} null si se cancelo con escape
|
|
100
|
+
*/
|
|
101
|
+
use()
|
|
102
|
+
{
|
|
103
|
+
const _this = this;
|
|
104
|
+
return new Promise(resolve =>
|
|
105
|
+
{
|
|
106
|
+
this.screen.captureKeys(function onKey(info)
|
|
107
|
+
{
|
|
108
|
+
if(info.key === "down" || info.key === "j") _this.select(1);
|
|
109
|
+
else if(info.key === "up" || info.key === "k") _this.select(-1);
|
|
110
|
+
else if(info.key === "enter")
|
|
111
|
+
{
|
|
112
|
+
_this.screen.releaseKeys();
|
|
113
|
+
resolve(_this.#items[_this.#selected].text);
|
|
114
|
+
}
|
|
115
|
+
else if(info.key === "escape")
|
|
116
|
+
{
|
|
117
|
+
_this.screen.releaseKeys();
|
|
118
|
+
resolve(null);
|
|
119
|
+
}
|
|
120
|
+
else if(info.key === "ctrl+c")
|
|
121
|
+
{
|
|
122
|
+
_this.screen.releaseKeys();
|
|
123
|
+
process.kill(process.pid, "SIGINT");
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* El unico default es selectedBolder: bold + color brillante, sin tocar lo que el usuario declaro.
|
|
130
|
+
* @returns {Styles}
|
|
131
|
+
*/
|
|
132
|
+
#selectedDelta()
|
|
133
|
+
{
|
|
134
|
+
const o = this.#opts;
|
|
135
|
+
/**@type {Styles}*/
|
|
136
|
+
const delta = {};
|
|
137
|
+
if(o.selectedBgColor !== undefined) delta.backgroundColor = o.selectedBgColor;
|
|
138
|
+
if(o.selectedColor !== undefined) delta.color = o.selectedColor;
|
|
139
|
+
if(o.selectedFontWeight !== undefined) delta.fontWeight = o.selectedFontWeight;
|
|
140
|
+
if(o.selectedFontStyle !== undefined) delta.fontStyle = o.selectedFontStyle;
|
|
141
|
+
if(o.selectedBolder !== false)
|
|
142
|
+
{
|
|
143
|
+
if(delta.fontWeight === undefined) delta.fontWeight = "bold";
|
|
144
|
+
if(delta.color === undefined) delta.color = "brightWhite";
|
|
145
|
+
}
|
|
146
|
+
return delta;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* @returns {void}
|
|
150
|
+
*/
|
|
151
|
+
#renderSelection()
|
|
152
|
+
{
|
|
153
|
+
const delta = this.#selectedDelta();
|
|
154
|
+
const clear = /**@type {Styles}*/({});
|
|
155
|
+
for(const key of DELTA_KEYS) clear[key] = undefined;
|
|
156
|
+
|
|
157
|
+
const marker = this.#opts.selectedChar || "";
|
|
158
|
+
this.#items.forEach(({ box, label, text }, i) =>
|
|
159
|
+
{
|
|
160
|
+
const on = i === this.#selected;
|
|
161
|
+
box.styles = on ? { ...clear, ...delta } : clear;
|
|
162
|
+
label.set((on ? marker : " ".repeat(marker.length)) + text);
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Claves de seleccion de un item con el delta elegido; las demas los hereda del list.
|
|
167
|
+
* @param {number} index
|
|
168
|
+
* @returns {Styles}
|
|
169
|
+
*/
|
|
170
|
+
itemStyle(index)
|
|
171
|
+
{
|
|
172
|
+
return index === this.#selected ? this.#selectedDelta() : {};
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Ajusta el view_point del padre para que la opcion seleccionada quede a la vista.
|
|
176
|
+
* @returns {void}
|
|
177
|
+
*/
|
|
178
|
+
#scrollIntoView()
|
|
179
|
+
{
|
|
180
|
+
/**@type {any}*/
|
|
181
|
+
let parent = null;
|
|
182
|
+
try { parent = this.parent; } catch { return; }
|
|
183
|
+
if(!parent || typeof parent.height !== "number") return;
|
|
184
|
+
const visible = Math.max(1, parent.height - (parent.styles.border ? 2 : 0));
|
|
185
|
+
const i = this.#selected;
|
|
186
|
+
const vp = parent.view_point;
|
|
187
|
+
if(i < vp.y) parent.scrollTo(vp.x, i);
|
|
188
|
+
else if(i > vp.y + visible - 1) parent.scrollTo(vp.x, i - visible + 1);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
module.exports = List;
|