-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
fake-tty.js
48 lines (37 loc) · 1.1 KB
/
fake-tty.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
'use strict';
const tty = require('tty');
const ansiEscapes = require('ansi-escapes');
const options = require('./options').get();
const fakeTTYs = new Set();
const {isatty} = tty;
tty.isatty = fd => fakeTTYs.has(fd) || isatty(fd);
const simulateTTY = (stream, {colorDepth, columns, rows}) => {
Object.assign(stream, {isTTY: true, columns, rows});
stream.clearLine = dir => {
switch (dir) {
case -1:
stream.write(ansiEscapes.eraseStartLine);
break;
case 1:
stream.write(ansiEscapes.eraseEndLine);
break;
default:
stream.write(ansiEscapes.eraseLine);
}
};
stream.clearScreenDown = () => stream.write(ansiEscapes.eraseDown);
stream.cursorTo = (x, y) => stream.write(ansiEscapes.cursorTo(x, y));
stream.getWindowSize = () => [80, 24];
stream.moveCursor = (x, y) => stream.write(ansiEscapes.cursorMove(x, y));
if (colorDepth !== undefined) {
stream.getColorDepth = () => colorDepth;
}
};
if (options.tty.stderr) {
simulateTTY(process.stderr, options.tty.stderr);
fakeTTYs.add(2);
}
if (options.tty.stdout) {
simulateTTY(process.stdout, options.tty.stdout);
fakeTTYs.add(1);
}