Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix rendering when terminal is erased on first render #609

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -149,7 +149,8 @@
"unicorn/import-index": "off",
"import/no-useless-path-segments": "off",
"react-hooks/exhaustive-deps": "off",
"complexity": "off"
"complexity": "off",
"react/prefer-read-only-props": "off"
},
"ignores": [
"src/parse-keypress.ts"
Expand Down
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, {force: true, erase: false});
this.lastOutput = output;
return;
}
Expand Down
14 changes: 10 additions & 4 deletions src/log-update.ts
Expand Up @@ -5,27 +5,33 @@ import cliCursor from 'cli-cursor';
export type LogUpdate = {
clear: () => void;
done: () => void;
(str: string): void;
(str: string, options?: {force?: boolean; erase?: boolean}): void;
};

const create = (stream: Writable, {showCursor = false} = {}): LogUpdate => {
let previousLineCount = 0;
let previousOutput = '';
let hasHiddenCursor = false;

const render = (str: string) => {
const render = (
str: string,
{force = false, erase = true}: {force?: boolean; erase?: boolean} = {}
) => {
if (!showCursor && !hasHiddenCursor) {
cliCursor.hide();
hasHiddenCursor = true;
}

const output = str + '\n';
if (output === previousOutput) {
if (output === previousOutput && !force) {
return;
}

previousOutput = output;
stream.write(ansiEscapes.eraseLines(previousLineCount) + output);

const eraser = erase ? ansiEscapes.eraseLines(previousLineCount) : '';
stream.write(eraser + output);

previousLineCount = output.split('\n').length;
};

Expand Down
31 changes: 31 additions & 0 deletions test/fixtures/erase-once-with-static.tsx
@@ -0,0 +1,31 @@
import React, {useState} from 'react';
import {Box, Static, Text, render, useInput} from '../../src/index.js';

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

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

return (
<>
<Static items={['X', 'Y', 'Z']}>
{item => <Text key={item}>{item}</Text>}
</Static>

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

render(<EraseOnceWithStatic />);
25 changes: 25 additions & 0 deletions test/fixtures/erase-once.tsx
@@ -0,0 +1,25 @@
import React, {useState} from 'react';
import {Box, Text, render, useInput} from '../../src/index.js';

function EraseOnce() {
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>
);
}

render(<EraseOnce />);
2 changes: 0 additions & 2 deletions test/fixtures/erase-with-static.tsx
@@ -1,4 +1,3 @@
import process from 'node:process';
import React from 'react';
import {Static, Box, Text, render} from '../../src/index.js';

Expand All @@ -18,5 +17,4 @@ function EraseWithStatic() {
);
}

process.stdout.rows = Number(process.argv[3]);
render(<EraseWithStatic />);
2 changes: 0 additions & 2 deletions test/fixtures/erase.tsx
@@ -1,4 +1,3 @@
import process from 'node:process';
import React from 'react';
import {Box, Text, render} from '../../src/index.js';

Expand All @@ -12,5 +11,4 @@ function Erase() {
);
}

process.stdout.rows = Number(process.argv[2]);
render(<Erase />);
63 changes: 57 additions & 6 deletions test/render.tsx
Expand Up @@ -18,7 +18,11 @@ const {spawn} = require('node-pty') as typeof import('node-pty');

const __dirname = url.fileURLToPath(new URL('.', import.meta.url));

const term = (fixture: string, args: string[] = []) => {
const term = (
fixture: string,
args: string[] = [],
{rows}: {rows?: number} = {}
) => {
let resolve: (value?: unknown) => void;
let reject: (error: Error) => void;

Expand All @@ -30,7 +34,9 @@ const term = (fixture: string, args: string[] = []) => {
const env = {
...process.env,
// eslint-disable-next-line @typescript-eslint/naming-convention
NODE_NO_WARNINGS: '1'
NODE_NO_WARNINGS: '1',
// eslint-disable-next-line @typescript-eslint/naming-convention
CI: 'false'
};

const ps = spawn(
Expand All @@ -43,6 +49,7 @@ const term = (fixture: string, args: string[] = []) => {
{
name: 'xterm-color',
cols: 100,
rows,
cwd: __dirname,
env
}
Expand Down Expand Up @@ -73,7 +80,7 @@ const term = (fixture: string, args: string[] = []) => {
};

test.serial('do not erase screen', async t => {
const ps = term('erase', ['4']);
const ps = term('erase', [], {rows: 4});
await ps.waitForExit();
t.false(ps.output.includes(ansiEscapes.clearTerminal));

Expand All @@ -85,7 +92,7 @@ test.serial('do not erase screen', async t => {
test.serial(
'do not erase screen where <Static> is taller than viewport',
async t => {
const ps = term('erase-with-static', ['4']);
const ps = term('erase-with-static', [], {rows: 4});

await ps.waitForExit();
t.false(ps.output.includes(ansiEscapes.clearTerminal));
Expand All @@ -97,7 +104,7 @@ test.serial(
);

test.serial('erase screen', async t => {
const ps = term('erase', ['3']);
const ps = term('erase', [], {rows: 3});
await ps.waitForExit();
t.true(ps.output.includes(ansiEscapes.clearTerminal));

Expand All @@ -106,10 +113,54 @@ test.serial('erase screen', async t => {
}
});

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

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.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 once then continue rendering as usual with <Static> present',
async t => {
const ps = term('erase-once-with-static', [], {rows: 3});
await delay(1000);

t.true(ps.output.includes('X'));
t.true(ps.output.includes('Y'));
t.true(ps.output.includes('Z'));
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.true(ps.output.includes(ansiEscapes.eraseLines(2)));
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 => {
const ps = term('erase', ['3']);
const ps = term('erase', [], {rows: 3});
await ps.waitForExit();
t.true(ps.output.includes(ansiEscapes.clearTerminal));

Expand Down