forked from vadimdemedes/ink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
104 lines (82 loc) · 2.44 KB
/
index.js
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
'use strict';
const readline = require('readline');
const logUpdate = require('log-update');
const StringComponent = require('./lib/string-component');
const Component = require('./lib/component');
const renderToString = require('./lib/render-to-string');
const diff = require('./lib/diff');
const h = require('./lib/h');
const Indent = require('./lib/components/indent');
const Renderer = require('./lib/renderer');
const Color = require('./lib/components/color');
const Bold = require('./lib/components/bold');
const Underline = require('./lib/components/underline');
exports.StringComponent = StringComponent;
exports.Component = Component;
exports.h = h;
exports.Fragment = h.Fragment;
exports.Indent = Indent;
exports.Renderer = Renderer;
exports.Color = Color;
exports.Underline = Underline;
exports.Bold = Bold;
const noop = () => {};
const build = (nextTree, prevTree, onUpdate = noop, context = {}) => {
return diff(prevTree, nextTree, onUpdate, context);
};
exports.build = build;
exports.diff = diff;
exports.renderToString = (...args) => renderToString(build(...args));
exports.render = (tree, options) => {
if (options && typeof options.write === 'function') {
options = {
stdout: options
};
}
const {stdin, stdout} = Object.assign({
stdin: process.stdin,
stdout: process.stdout
}, options);
const log = logUpdate.create(stdout);
readline.emitKeypressEvents(stdin);
if (stdin.isTTY) {
stdin.setRawMode(true);
}
const currentTree = new Renderer(tree);
currentTree.on('update', log);
currentTree.update();
const onKeyPress = (ch, key) => {
if (key.name === 'escape' || (key.ctrl && key.name === 'c')) {
exit(); // eslint-disable-line no-use-before-define
}
};
if (stdin.isTTY) {
stdin.on('keypress', onKeyPress);
stdout.on('resize', currentTree.update);
}
const consoleMethods = ['dir', 'log', 'info', 'warn', 'error'];
consoleMethods.forEach(method => {
const originalFn = console[method];
console[method] = (...args) => {
log.clear();
log.done();
originalFn.apply(console, args);
currentTree.update();
};
console[method].restore = () => {
console[method] = originalFn;
};
});
const exit = () => {
if (stdin.isTTY) {
stdin.setRawMode(false);
stdin.removeListener('keypress', onKeyPress);
stdin.pause();
stdout.removeListener('resize', currentTree.update);
}
currentTree.unmount();
log.done();
consoleMethods.forEach(method => console[method].restore());
};
return exit;
};