@teaui/core
Version:
A high-level terminal UI library for Node
99 lines • 2.67 kB
JavaScript
import { Screen } from './Screen.js';
import { TestProgram } from './TestProgram.js';
class TestScreen {
options;
#screen;
#program;
constructor(view, options) {
this.options = options;
this.#program = new TestProgram({
cols: options.width,
rows: options.height,
});
this.#screen = new Screen(this.#program, view, {
isFocused: options.isFocused !== false,
});
this.#screen.start();
}
get terminal() {
return this.#program.terminal;
}
get view() {
return this.#screen.rootView;
}
/**
* Advance time by `dt` milliseconds, triggering any registered tick
* animations and re-rendering.
*/
tick(dt) {
this.#screen.tick(dt);
}
render() {
this.#screen.render();
}
sendKey(key, mods = {}) {
const ctrl = mods.ctrl ?? false;
const alt = mods.alt ?? false;
const gui = mods.gui ?? false;
const shift = mods.shift ?? false;
let full = '';
if (ctrl)
full += 'C-';
if (alt)
full += 'A-';
if (gui)
full += 'G-';
if (shift)
full += 'S-';
full += key;
const char = key === 'space' ? ' ' : key.length === 1 ? key : '';
const event = {
type: 'key',
name: key,
char,
full: full,
ctrl,
alt,
gui,
shift,
};
this.#screen.trigger(event);
}
sendMouse(name, pos, mods = {}) {
const button = name.startsWith('mouse.wheel')
? 'wheel'
: name === 'mouse.move.in'
? 'unknown'
: 'left';
const event = {
type: 'mouse',
name,
x: pos.x,
y: pos.y,
ctrl: mods.ctrl ?? false,
alt: mods.alt ?? false,
gui: mods.gui ?? false,
shift: mods.shift ?? false,
button,
};
this.#screen.trigger(event);
}
sendPaste(text) {
this.#screen.trigger({ type: 'paste', text });
}
}
/**
* Render a component headlessly and return a test harness for interaction testing.
*
* @example
* ```ts
* const t = testRender(new Input({value: 'hello'}), {width: 20, height: 1})
* expect(t.terminal.textContent()).toContain('hello')
* t.sendKey('!') // type a character
* expect(t.terminal.textContent()).toContain('hello!')
* ```
*/
export function testRender(view, size) {
return new TestScreen(view, size);
}
//# sourceMappingURL=TestScreen.js.map