/
display.ts
76 lines (64 loc) · 1.76 KB
/
display.ts
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
// namespace object imports won't work in the bundle for function exports
// eslint-disable-next-line @typescript-eslint/no-require-imports
const wrapAnsi = require('wrap-ansi');
/**
* A class representing rewritable display lines
*/
export class RewritableBlock {
private lastHeight = 0;
constructor(private readonly stream: NodeJS.WriteStream) {
}
public get width() {
// Might get changed if the user resizes the terminal
return this.stream.columns;
}
public displayLines(lines: string[]) {
lines = terminalWrap(this.width, expandNewlines(lines));
this.stream.write(cursorUp(this.lastHeight));
for (const line of lines) {
this.stream.write(cll() + line + '\n');
}
// Clear remainder of unwritten lines
for (let i = 0; i < this.lastHeight - lines.length; i++) {
this.stream.write(cll() + '\n');
}
// The block can only ever get bigger
this.lastHeight = Math.max(this.lastHeight, lines.length);
}
}
const ESC = '\u001b';
/*
* Move cursor up `n` lines. Default is 1
*/
function cursorUp(n: number) {
n = typeof n === 'number' ? n : 1;
return n > 0 ? ESC + '[' + n + 'A' : '';
}
/**
* Clear to end of line
*/
function cll() {
return ESC + '[K';
}
function terminalWrap(width: number | undefined, lines: string[]) {
if (width === undefined) { return lines; }
const ret = new Array<string>();
for (const line of lines) {
ret.push(...wrapAnsi(line, width - 1, {
hard: true,
trim: true,
wordWrap: false,
}).split('\n'));
}
return ret;
}
/**
* Make sure there are no hidden newlines in the gin strings
*/
function expandNewlines(lines: string[]): string[] {
const ret = new Array<string>();
for (const line of lines) {
ret.push(...line.split('\n'));
}
return ret;
}