Skip to content

Commit

Permalink
Shim all tty methods
Browse files Browse the repository at this point in the history
  • Loading branch information
oantoro authored and novemberborn committed Mar 18, 2019
1 parent 23e302a commit c1f6fdf
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
22 changes: 22 additions & 0 deletions lib/worker/fake-tty.js
@@ -1,5 +1,6 @@
'use strict';
const tty = require('tty');
const ansiEscapes = require('ansi-escapes');
const options = require('./options').get();

const fakeTTYs = new Set();
Expand All @@ -10,6 +11,27 @@ 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;
}
Expand Down
5 changes: 5 additions & 0 deletions test/fixture/tty/is-tty.js
Expand Up @@ -4,6 +4,11 @@ const assertTTY = (t, stream) => {
t.true(stream.isTTY);
t.is(typeof stream.columns, 'number');
t.is(typeof stream.rows, 'number');
t.is(typeof stream.clearLine, 'function');
t.is(typeof stream.clearScreenDown, 'function');
t.is(typeof stream.cursorTo, 'function');
t.is(typeof stream.getWindowSize, 'function');
t.is(typeof stream.moveCursor, 'function');
};

test('stderr is a TTY', assertTTY, process.stderr);
Expand Down

0 comments on commit c1f6fdf

Please sign in to comment.