Skip to content

Commit

Permalink
refactored CI rendering logic into option, and enable it for non-TTY …
Browse files Browse the repository at this point in the history
…output
  • Loading branch information
nickwesselman committed Aug 28, 2024
1 parent 7fedab0 commit 67ebdb4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/ink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export type Options = {
debug: boolean;
exitOnCtrlC: boolean;
patchConsole: boolean;
renderLastFrameOnly?: boolean;
waitUntilExit?: () => Promise<void>;
};

Expand Down Expand Up @@ -67,6 +68,12 @@ export default class Ink {
trailing: true,
});

// CI environments and piped output don't handle erasing ansi escapes well,
// so it's better to only render last frame of non-static output
if (options.renderLastFrameOnly === undefined) {
options.renderLastFrameOnly = isInCi || !options.stdout.isTTY;
}

// Ignore last render after unmounting a tree to prevent empty output before exit
this.isUnmounted = false;

Expand Down Expand Up @@ -107,7 +114,7 @@ export default class Ink {
this.patchConsole();
}

if (!isInCi) {
if (!options.renderLastFrameOnly) {
options.stdout.on('resize', this.resized);

this.unsubscribeResize = () => {
Expand Down Expand Up @@ -158,7 +165,7 @@ export default class Ink {
return;
}

if (isInCi) {
if (this.options.renderLastFrameOnly) {
if (hasStaticOutput) {
this.options.stdout.write(staticOutput);
}
Expand Down Expand Up @@ -221,7 +228,7 @@ export default class Ink {
return;
}

if (isInCi) {
if (this.options.renderLastFrameOnly) {
this.options.stdout.write(data);
return;
}
Expand All @@ -242,7 +249,7 @@ export default class Ink {
return;
}

if (isInCi) {
if (this.options.renderLastFrameOnly) {
this.options.stderr.write(data);
return;
}
Expand Down Expand Up @@ -270,9 +277,7 @@ export default class Ink {
this.unsubscribeResize();
}

// CIs don't handle erasing ansi escapes well, so it's better to
// only render last frame of non-static output
if (isInCi) {
if (this.options.renderLastFrameOnly) {
this.options.stdout.write(this.lastOutput + '\n');
} else if (!this.options.debug) {
this.log.done();
Expand Down Expand Up @@ -300,7 +305,7 @@ export default class Ink {
}

clear(): void {
if (!isInCi && !this.options.debug) {
if (!this.options.renderLastFrameOnly && !this.options.debug) {
this.log.clear();
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ export type RenderOptions = {
* @default true
*/
patchConsole?: boolean;

/**
* Configure whether Ink should only render the last frame of non-static output. Useful when ANSI erase codes are not supported.
*
* @default true if in CI or stdout is not a TTY
*/
renderLastFrameOnly?: boolean;
};

export type Instance = {
Expand Down

0 comments on commit 67ebdb4

Please sign in to comment.