Skip to content

Commit

Permalink
rerender when terminal resizes
Browse files Browse the repository at this point in the history
  • Loading branch information
Vadim Demedes committed Jul 16, 2017
1 parent 0db786e commit e68f2d7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
2 changes: 2 additions & 0 deletions index.js
Expand Up @@ -86,6 +86,7 @@ exports.render = (tree, options) => {
};

stdin.on('keypress', onKeyPress);
stdout.on('resize', update);

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

Expand All @@ -112,6 +113,7 @@ exports.render = (tree, options) => {
stdin.setRawMode(false);
stdin.removeListener('keypress', onKeyPress);
stdin.pause();
stdout.removeListener('resize', update);

isUnmounted = true;
unmount(currentTree);
Expand Down
41 changes: 34 additions & 7 deletions test/render.js
Expand Up @@ -8,16 +8,19 @@ import {rerender} from '../lib/render-queue';
const stripOutput = str => stripAnsi(str).trim();

const createStdin = () => {
const events = new EventEmitter();
events.setRawMode = spy();
events.pause = spy();
const stdin = new EventEmitter();
stdin.setRawMode = spy();
stdin.pause = spy();

return events;
return stdin;
};

const createStdout = () => ({
write: spy()
});
const createStdout = () => {
const stdout = new EventEmitter();
stdout.write = spy();

return stdout;
};

test.serial('set up stdin to emit keypress events', t => {
const Test = () => 'Test';
Expand Down Expand Up @@ -200,3 +203,27 @@ test.serial('ignore updates when unmounted', t => {
console[method].restore();
});
});

test.serial('rerender on resize', t => {
let i = 0;

const Test = () => String(i++);

const stdin = createStdin();
const stdout = createStdout();

const unmount = render(<Test/>, {stdin, stdout});

t.is(stdout.listenerCount('resize'), 1);
t.true(stdout.write.calledOnce);
t.is(stripOutput(stdout.write.getCall(0).args[0]), '0');

stdout.emit('resize');

t.true(stdout.write.calledTwice);
t.is(stripOutput(stdout.write.getCall(1).args[0]), '1');

unmount();

t.is(stdout.listenerCount('resize'), 0);
});

0 comments on commit e68f2d7

Please sign in to comment.