7e1564f4053f15c83c4ff7d5e1e62e9d56c808ef.svn-base
2.06 KB
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
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _stringLength = _interopRequireDefault(require("string-length"));
var _sliceAnsi = _interopRequireDefault(require("slice-ansi"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* "Virtual" output class
*
* Handles the positioning and saving of the output of each node in the tree.
* Also responsible for applying transformations to each character of the output.
*
* Used to generate the final output of all nodes before writing it to actual output stream (e.g. stdout)
*/
class Output {
constructor({
width,
height
}) {
this.width = width;
this.height = height;
this.writes = []; // Initialize output array with a specific set of rows, so that margin/padding at the bottom is preserved
}
write(x, y, text, {
transformers
}) {
if (!text) {
return;
}
this.writes.push({
x,
y,
text,
transformers
});
}
get() {
const output = [];
for (let y = 0; y < this.height; y++) {
output.push(' '.repeat(this.width));
}
for (const write of this.writes) {
const {
x,
y,
text,
transformers
} = write;
const lines = text.split('\n');
let offsetY = 0;
for (let line of lines) {
const currentLine = output[y + offsetY]; // Line can be missing if `text` is taller than height of pre-initialized `this.output`
if (!currentLine) {
continue;
}
const length = (0, _stringLength.default)(line);
for (const transformer of transformers) {
line = transformer(line);
}
output[y + offsetY] = (0, _sliceAnsi.default)(currentLine, 0, x) + line + (0, _sliceAnsi.default)(currentLine, x + length);
offsetY++;
}
}
const generatedOutput = output.map(line => line.trimRight()).join('\n');
return {
output: generatedOutput,
height: output.length
};
}
}
exports.default = Output;