blob: 7ee048068d9a743143edfb4b1608d8dc4f9f8769 (
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
|
// General-purpose wrapper code that can be used as the base of a tui-lib
// program. Contained to reduce boilerplate and improve consistency between
// programs.
const ansi = require('./ansi');
const CommandLineInterfacer = require('./CommandLineInterfacer');
const Flushable = require('./Flushable');
const Root = require('../ui/Root');
module.exports = async function tuiApp(callback) {
// TODO: Support other interfacers.
const interfacer = new CommandLineInterfacer();
const flushable = new Flushable(process.stdout, true);
const root = new Root(interfacer);
const size = await interfacer.getScreenSize();
root.w = size.width;
root.h = size.height;
flushable.resizeScreen(size);
interfacer.on('resize', newSize => {
root.w = newSize.width;
root.h = newSize.height;
flushable.resizeScreen(newSize);
root.fixAllLayout();
});
const clean = function() {
process.stdout.write(ansi.cleanCursor());
process.stdout.write(ansi.disableAlternateScreen());
};
const quitProgram = function(status = 0) {
clean();
process.exit(status);
};
const startRenderLoop = function() {
process.stdout.write(ansi.enableAlternateScreen());
setInterval(() => {
root.renderTo(flushable);
flushable.flush();
});
};
try {
return await callback({
root,
startRenderLoop,
quitProgram
});
} catch (error) {
clean();
console.error(error);
process.exit(1);
};
};
|