Skip to content

Commit

Permalink
Fix rendering when terminal is erased on first render
Browse files Browse the repository at this point in the history
  • Loading branch information
vadimdemedes committed May 3, 2023
1 parent 8a04760 commit bbf597e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/ink.tsx
Expand Up @@ -175,8 +175,10 @@ export default class Ink {

if (outputHeight >= this.options.stdout.rows) {
this.options.stdout.write(
ansiEscapes.clearTerminal + this.fullStaticOutput + output
ansiEscapes.clearTerminal + this.fullStaticOutput
);

this.log(output);
this.lastOutput = output;
return;
}
Expand Down
27 changes: 27 additions & 0 deletions test/fixtures/erase-once.tsx
@@ -0,0 +1,27 @@
import process from 'node:process';
import React, {useState} from 'react';
import {Box, Text, render, useInput} from '../../src/index.js';

function Test() {
const [fullHeight, setFullHeight] = useState(true);

useInput(
input => {
if (input === 'x') {
setFullHeight(false);
}
},
{isActive: fullHeight}
);

return (
<Box flexDirection="column">
<Text>A</Text>
<Text>B</Text>
{fullHeight && <Text>C</Text>}
</Box>
);
}

process.stdout.rows = Number(process.argv[2]);
render(<Test />);
21 changes: 21 additions & 0 deletions test/render.tsx
Expand Up @@ -106,6 +106,27 @@ test.serial('erase screen', async t => {
}
});

test.serial('erase screen once then continue rendering as usual', async t => {
const ps = term('erase-once', ['3']);
await delay(1000);

t.true(ps.output.includes(ansiEscapes.clearTerminal));
t.true(ps.output.includes('A'));
t.true(ps.output.includes('B'));
t.true(ps.output.includes('C'));

ps.output = '';
ps.write('x');

await ps.waitForExit();

t.false(ps.output.includes(ansiEscapes.clearTerminal));
t.true(ps.output.includes(ansiEscapes.eraseLines(3)));
t.true(ps.output.includes('A'));
t.true(ps.output.includes('B'));
t.false(ps.output.includes('C'));
});

test.serial(
'erase screen where <Static> exists but interactive part is taller than viewport',
async t => {
Expand Down

0 comments on commit bbf597e

Please sign in to comment.