From 67ebdb4ada085ff21716f96cee2d342049b4a6c9 Mon Sep 17 00:00:00 2001 From: Nick Wesselman <27013789+nickwesselman@users.noreply.github.com> Date: Wed, 28 Aug 2024 16:53:38 -0400 Subject: [PATCH] refactored CI rendering logic into option, and enable it for non-TTY output --- src/ink.tsx | 21 +++++++++++++-------- src/render.ts | 7 +++++++ 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/ink.tsx b/src/ink.tsx index 8f816112..5d79b69f 100644 --- a/src/ink.tsx +++ b/src/ink.tsx @@ -25,6 +25,7 @@ export type Options = { debug: boolean; exitOnCtrlC: boolean; patchConsole: boolean; + renderLastFrameOnly?: boolean; waitUntilExit?: () => Promise; }; @@ -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; @@ -107,7 +114,7 @@ export default class Ink { this.patchConsole(); } - if (!isInCi) { + if (!options.renderLastFrameOnly) { options.stdout.on('resize', this.resized); this.unsubscribeResize = () => { @@ -158,7 +165,7 @@ export default class Ink { return; } - if (isInCi) { + if (this.options.renderLastFrameOnly) { if (hasStaticOutput) { this.options.stdout.write(staticOutput); } @@ -221,7 +228,7 @@ export default class Ink { return; } - if (isInCi) { + if (this.options.renderLastFrameOnly) { this.options.stdout.write(data); return; } @@ -242,7 +249,7 @@ export default class Ink { return; } - if (isInCi) { + if (this.options.renderLastFrameOnly) { this.options.stderr.write(data); return; } @@ -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(); @@ -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(); } } diff --git a/src/render.ts b/src/render.ts index 7893f211..95549787 100644 --- a/src/render.ts +++ b/src/render.ts @@ -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 = {