Skip to content

Commit 2daf599

Browse files
ThesuperflyVadim Demedes
authored andcommitted
Disable input in a non-tty environment (#42)
1 parent 6ae7acc commit 2daf599

2 files changed

Lines changed: 15 additions & 7 deletions

File tree

index.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ exports.render = (tree, options) => {
5858
let currentTree;
5959

6060
readline.emitKeypressEvents(stdin);
61-
stdin.setRawMode(true);
61+
62+
if (stdin.isTTY) {
63+
stdin.setRawMode(true);
64+
}
6265

6366
const update = () => {
6467
const nextTree = build(tree, currentTree, onUpdate, context, false); // eslint-disable-line no-use-before-define
@@ -85,8 +88,10 @@ exports.render = (tree, options) => {
8588
}
8689
};
8790

88-
stdin.on('keypress', onKeyPress);
89-
stdout.on('resize', update);
91+
if (stdin.isTTY) {
92+
stdin.on('keypress', onKeyPress);
93+
stdout.on('resize', update);
94+
}
9095

9196
const consoleMethods = ['dir', 'log', 'info', 'warn', 'error'];
9297

@@ -110,10 +115,12 @@ exports.render = (tree, options) => {
110115
return;
111116
}
112117

113-
stdin.setRawMode(false);
114-
stdin.removeListener('keypress', onKeyPress);
115-
stdin.pause();
116-
stdout.removeListener('resize', update);
118+
if (stdin.isTTY) {
119+
stdin.setRawMode(false);
120+
stdin.removeListener('keypress', onKeyPress);
121+
stdin.pause();
122+
stdout.removeListener('resize', update);
123+
}
117124

118125
isUnmounted = true;
119126
unmount(currentTree);

test/render.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const createStdin = () => {
1111
const stdin = new EventEmitter();
1212
stdin.setRawMode = spy();
1313
stdin.pause = spy();
14+
stdin.isTTY = spy();
1415

1516
return stdin;
1617
};

0 commit comments

Comments
 (0)