From d4b0fe4dc0f038369e6c48bdbb3b26d07011ea04 Mon Sep 17 00:00:00 2001 From: TUFYIK <2499123744@qq.com> Date: Thu, 3 Dec 2026 00:15:30 +0800 Subject: [PATCH] fix(prompts): overwrite active line on carriage-return updates in taskLog --- .changeset/crisp-logs-overwrite.md | 5 + packages/prompts/src/task-log.ts | 179 ++++-- .../test/__snapshots__/task-log.test.ts.snap | 548 ++++++++++++++++++ packages/prompts/test/task-log.test.ts | 189 ++++++ 4 files changed, 875 insertions(+), 46 deletions(-) create mode 100644 .changeset/crisp-logs-overwrite.md diff --git a/.changeset/crisp-logs-overwrite.md b/.changeset/crisp-logs-overwrite.md new file mode 100644 index 00000000..5cb755ec --- /dev/null +++ b/.changeset/crisp-logs-overwrite.md @@ -0,0 +1,5 @@ +--- +"@clack/prompts": patch +--- + +Fix `taskLog` handling of carriage-return (`\r`) updates so spinner and progress frames overwrite the current line in place instead of accumulating in the log buffer. diff --git a/packages/prompts/src/task-log.ts b/packages/prompts/src/task-log.ts index 0c71bf14..21c1b98d 100644 --- a/packages/prompts/src/task-log.ts +++ b/packages/prompts/src/task-log.ts @@ -1,6 +1,7 @@ import type { Writable } from 'node:stream'; import { styleText } from 'node:util'; import { getColumns } from '@clack/core'; +import stringWidth from 'fast-string-width'; import { erase } from 'sisteransi'; import { type CommonOptions, @@ -28,8 +29,10 @@ export interface TaskLogCompletionOptions { interface BufferEntry { header?: string; - value: string; - full: string; + lines: string[]; + full: string[]; + lineOpen: boolean; + pendingCR: boolean; result?: { status: 'success' | 'error'; message: string; @@ -41,12 +44,101 @@ const stripDestructiveANSI = (input: string): string => { return input.replace(/\x1b\[(?:\d+;)*\d*[ABCDEFGHfJKSTsu]|\x1b\[(s|u)/g, ''); }; +const createBuffer = (header?: string): BufferEntry => ({ + header, + lines: [], + full: [], + lineOpen: false, + pendingCR: false, +}); + +const resetBuffer = (buffer: BufferEntry): void => { + buffer.lines = []; + buffer.full = []; + buffer.lineOpen = false; + buffer.pendingCR = false; + buffer.result = undefined; +}; + +const collapseCarriageReturn = (line: string): string => { + if (!line.includes('\r')) { + return line; + } + let current = ''; + for (const segment of line.split('\r')) { + if (segment !== '') { + current = segment; + } + } + return current; +}; + +const appendLineMessage = (buffer: BufferEntry, sanitized: string): void => { + buffer.lineOpen = false; + buffer.pendingCR = false; + if (buffer.lines.length === 0 && sanitized === '') { + return; + } + for (const rawLine of sanitized.split(/\r?\n/)) { + buffer.lines.push(collapseCarriageReturn(rawLine)); + } +}; + +const appendRawMessage = (buffer: BufferEntry, sanitized: string): void => { + let i = 0; + while (i < sanitized.length) { + const ch = sanitized[i]; + if (ch === '\r') { + buffer.pendingCR = true; + i += 1; + } else if (ch === '\n') { + if (buffer.lineOpen) { + buffer.lineOpen = false; + } else { + buffer.lines.push(''); + } + buffer.pendingCR = false; + i += 1; + } else { + let end = i + 1; + while (end < sanitized.length && sanitized[end] !== '\r' && sanitized[end] !== '\n') { + end += 1; + } + const segment = sanitized.slice(i, end); + const lastIndex = buffer.lines.length - 1; + const lastLine = buffer.lines[lastIndex]; + if (!buffer.lineOpen || lastLine === undefined) { + buffer.lines.push(segment); + buffer.lineOpen = true; + } else if (buffer.pendingCR) { + buffer.lines[lastIndex] = segment; + } else { + buffer.lines[lastIndex] = `${lastLine}${segment}`; + } + buffer.pendingCR = false; + i = end; + } + } +}; + +const countLinesHeight = (lines: string[], columns: number, barSize: number): number => { + let height = 0; + for (const line of lines) { + const width = stringWidth(line); + if (width === 0) { + height += 1; + } else { + height += Math.ceil((width + barSize) / columns); + } + } + return height; +}; + /** * Renders a log which clears on success and remains on failure */ export const taskLog = (opts: TaskLogOptions) => { const output: Writable = opts.output ?? process.stdout; - const columns = getColumns(output); const secondarySymbol = styleText('gray', S_BAR); const spacing = opts.spacing ?? 1; const barSize = 3; @@ -59,19 +151,14 @@ export const taskLog = (opts: TaskLogOptions) => { output.write(`${secondarySymbol}\n`); } - const buffers: [BufferEntry, ...BufferEntry[]] = [ - { - value: '', - full: '', - }, - ]; - let lastMessageWasRaw = false; + const buffers: [BufferEntry, ...BufferEntry[]] = [createBuffer()]; const clear = (clearTitle: boolean): void => { if (buffers.length === 0) { return; } + const columns = getColumns(output); let lines = 0; if (clearTitle) { @@ -79,25 +166,24 @@ export const taskLog = (opts: TaskLogOptions) => { } for (const buffer of buffers) { - const { value, result } = buffer; - let text = result?.message ?? value; - - if (text.length === 0) { + const { result } = buffer; + if (result !== undefined) { + if (result.message.length === 0) { + continue; + } + lines += countLinesHeight(result.message.split('\n'), columns, barSize); continue; } - if (result === undefined && buffer.header !== undefined && buffer.header !== '') { - text += `\n${buffer.header}`; + if (buffer.lines.length === 0) { + continue; } - const bufferHeight = text.split('\n').reduce((count, line) => { - if (line === '') { - return count + 1; - } - return count + Math.ceil((line.length + barSize) / columns); - }, 0); + if (buffer.header !== undefined && buffer.header !== '') { + lines += countLinesHeight(buffer.header.split('\n'), columns, barSize); + } - lines += bufferHeight; + lines += countLinesHeight(buffer.lines, columns, barSize); } if (lines > 0) { @@ -106,7 +192,7 @@ export const taskLog = (opts: TaskLogOptions) => { } }; const printBuffer = (buffer: BufferEntry, messageSpacing?: number, full?: boolean): void => { - const messages = full ? `${buffer.full}\n${buffer.value}` : buffer.value; + const messageLines = full ? [...buffer.full, ...buffer.lines] : buffer.lines; if (buffer.header !== undefined && buffer.header !== '') { log.message( buffer.header.split('\n').map((line) => styleText('bold', line)), @@ -119,7 +205,7 @@ export const taskLog = (opts: TaskLogOptions) => { ); } log.message( - messages.split('\n').map((line) => styleText('dim', line)), + (messageLines.length > 0 ? messageLines : ['']).map((line) => styleText('dim', line)), { output, secondarySymbol, @@ -130,8 +216,12 @@ export const taskLog = (opts: TaskLogOptions) => { }; const renderBuffer = (): void => { for (const buffer of buffers) { - const { header, value, full } = buffer; - if ((header === undefined || header.length === 0) && value.length === 0) { + const { header, lines, full } = buffer; + if ( + (header === undefined || header.length === 0) && + lines.length === 0 && + full.length === 0 + ) { continue; } printBuffer(buffer, undefined, retainLog === true && full.length > 0); @@ -139,21 +229,24 @@ export const taskLog = (opts: TaskLogOptions) => { }; const message = (buffer: BufferEntry, msg: string, mopts?: TaskLogMessageOptions) => { clear(false); - if ((mopts?.raw !== true || !lastMessageWasRaw) && buffer.value !== '') { - buffer.value += '\n'; + const sanitized = stripDestructiveANSI(msg); + if (mopts?.raw === true) { + appendRawMessage(buffer, sanitized); + } else { + appendLineMessage(buffer, sanitized); } - buffer.value += stripDestructiveANSI(msg); - lastMessageWasRaw = mopts?.raw === true; if (opts.limit !== undefined) { - const lines = buffer.value.split('\n'); - const linesToRemove = lines.length - opts.limit; + const linesToRemove = buffer.lines.length - opts.limit; if (linesToRemove > 0) { - const removedLines = lines.splice(0, linesToRemove); + const removedLines = buffer.lines.splice(0, linesToRemove); if (retainLog) { - buffer.full += (buffer.full === '' ? '' : '\n') + removedLines.join('\n'); + buffer.full.push(...removedLines); + } + if (buffer.lines.length === 0) { + buffer.lineOpen = false; + buffer.pendingCR = false; } } - buffer.value = lines.join('\n'); } if (isTTY) { printBuffers(); @@ -167,7 +260,7 @@ export const taskLog = (opts: TaskLogOptions) => { } else { log.success(buffer.result.message, { output, secondarySymbol, spacing: 0 }); } - } else if (buffer.value !== '') { + } else if (buffer.lines.length > 0) { printBuffer(buffer, 0); } } @@ -187,11 +280,7 @@ export const taskLog = (opts: TaskLogOptions) => { message(buffers[0], msg, mopts); }, group(name: string) { - const buffer: BufferEntry = { - header: name, - value: '', - full: '', - }; + const buffer = createBuffer(name); buffers.push(buffer); return { message(msg: string, mopts?: TaskLogMessageOptions) { @@ -219,8 +308,7 @@ export const taskLog = (opts: TaskLogOptions) => { } // clear buffer since error is an end state buffers.splice(1, buffers.length - 1); - buffers[0].value = ''; - buffers[0].full = ''; + resetBuffer(buffers[0]); }, success(message: string, opts?: TaskLogCompletionOptions): void { clear(true); @@ -230,8 +318,7 @@ export const taskLog = (opts: TaskLogOptions) => { } // clear buffer since success is an end state buffers.splice(1, buffers.length - 1); - buffers[0].value = ''; - buffers[0].full = ''; + resetBuffer(buffers[0]); }, }; }; diff --git a/packages/prompts/test/__snapshots__/task-log.test.ts.snap b/packages/prompts/test/__snapshots__/task-log.test.ts.snap index 39242084..8b0a3db0 100644 --- a/packages/prompts/test/__snapshots__/task-log.test.ts.snap +++ b/packages/prompts/test/__snapshots__/task-log.test.ts.snap @@ -1,5 +1,34 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html +exports[`taskLog (isCI = false) > error > clears exact rendered line count and shows only latest spinner frame on error 1`] = ` +[ + "│ +", + "◇ foo +", + "│ +", + "│ ◒ Cloning repository from remote origin +", + "", + "│ ◐ Cloning repository from remote origin +", + "", + "│ ◓ Cloning repository from remote origin +", + "", + "│ ◑ Cloning repository from remote origin +", + "", + "│ +■ clone failed! +", + "│ +│ ◑ Cloning repository from remote origin +", +] +`; + exports[`taskLog (isCI = false) > error > clears output if showLog = false 1`] = ` [ "│ @@ -21,6 +50,26 @@ exports[`taskLog (isCI = false) > error > clears output if showLog = false 1`] = ] `; +exports[`taskLog (isCI = false) > error > clears wrapped spinner frame accurately when overwritten by shorter frame 1`] = ` +[ + "│ +", + "◇ foo +", + "│ +", + "│ ◒ Downloading a very long package name that wraps across lines +", + "", + "│ ◇ Downloaded! +", + "", + "│ +■ verification failed! +", +] +`; + exports[`taskLog (isCI = false) > error > renders output with message 1`] = ` [ "│ @@ -596,6 +645,84 @@ exports[`taskLog (isCI = false) > group > showLog shows all groups in order 1`] ] `; +exports[`taskLog (isCI = false) > group > tracks raw chunks and carriage returns independently per group 1`] = ` +[ + "│ +", + "◇ Some log +", + "│ +", + "│ Group 0 +", + "│ ◒ Group 0 loading +", + "", + "│ Group 0 +", + "│ ◒ Group 0 loading +", + "│ Group 1 +", + "│ Group 1 header +", + "", + "│ Group 0 +", + "│ ◇ Group 0 ready +", + "│ Group 1 +", + "│ Group 1 header +", + "", + "│ Group 0 +", + "│ ◇ Group 0 ready +", + "│ Group 1 +", + "│ Group 1 header +│ ◒ Group 1 step +", + "", + "│ Group 0 +", + "│ ◇ Group 0 ready +", + "│ Group 1 +", + "│ Group 1 header +│ ◒ Group 1 step still running +", + "", + "│ Group 0 +", + "│ ◇ Group 0 ready +", + "│ Group 1 +", + "│ Group 1 header +│ ◇ Group 1 done +", + "", + "│ +■ overall error +", + "│ Group 0 +", + "│ +│ ◇ Group 0 ready +", + "│ Group 1 +", + "│ +│ Group 1 header +│ ◇ Group 1 done +", +] +`; + exports[`taskLog (isCI = false) > message > can write line by line 1`] = ` [ "│ @@ -670,6 +797,60 @@ exports[`taskLog (isCI = false) > message > enforces limit if set 1`] = ` ] `; +exports[`taskLog (isCI = false) > message > non-raw messages collapse bare carriage returns and preserve CRLF lines 1`] = ` +[ + "│ +", + "◇ foo +", + "│ +", + "│ line 0 +│ ◇ Done +", + "", + "│ +■ failed! +", + "│ +│ line 0 +│ ◇ Done +", +] +`; + +exports[`taskLog (isCI = false) > message > preserves CRLF newlines including split chunks while collapsing bare carriage returns 1`] = ` +[ + "│ +", + "◇ foo +", + "│ +", + "│ line 0 +│ frame 2 +", + "", + "│ line 0 +│ frame 2 +", + "", + "│ line 0 +│ frame 2 +│ frame C +", + "", + "│ +■ failed! +", + "│ +│ line 0 +│ frame 2 +│ frame C +", +] +`; + exports[`taskLog (isCI = false) > message > prints empty lines 1`] = ` [ "│ @@ -712,6 +893,55 @@ exports[`taskLog (isCI = false) > message > raw = true appends message text unti ] `; +exports[`taskLog (isCI = false) > message > raw = true overwrites current line on leading carriage returns 1`] = ` +[ + "│ +", + "◇ foo +", + "│ +", + "│ ◒ Cloning repository +", + "", + "│ ◐ Cloning repository +", + "", + "│ ◓ Cloning repository +", + "", + "│ ◇ Repository cloned +", + "", + "│ ◇ Repository cloned +│ Installing dependencies... +", +] +`; + +exports[`taskLog (isCI = false) > message > raw = true overwrites current line on trailing carriage returns 1`] = ` +[ + "│ +", + "◇ foo +", + "│ +", + "│ Downloading 10% +", + "", + "│ Downloading 50% +", + "", + "│ Downloading 100% +", + "", + "│ Downloading 100% +│ Extracting archive +", +] +`; + exports[`taskLog (isCI = false) > message > raw = true works when mixed with non-raw messages 1`] = ` [ "│ @@ -930,6 +1160,48 @@ exports[`taskLog (isCI = false) > retainLog > error > retainLog = true outputs f ] `; +exports[`taskLog (isCI = false) > retainLog > error > retainLog = true with limit does not accumulate overwritten spinner frames 1`] = ` +[ + "│ +", + "◇ foo +", + "│ +", + "│ step 1 +", + "", + "│ step 1 +│ ◒ step 2 +", + "", + "│ step 1 +│ ◐ step 2 +", + "", + "│ step 1 +│ ◇ step 2 done +", + "", + "│ ◇ step 2 done +│ ◒ step 3 +", + "", + "│ ◇ step 2 done +│ ◇ step 3 done +", + "", + "│ +■ woo! +", + "│ +│ step 1 +│ ◇ step 2 done +│ ◇ step 3 done +", +] +`; + exports[`taskLog (isCI = false) > retainLog > success > outputs limited log with limit by default 1`] = ` [ "│ @@ -1107,6 +1379,48 @@ exports[`taskLog (isCI = false) > retainLog > success > retainLog = true outputs ] `; +exports[`taskLog (isCI = false) > retainLog > success > retainLog = true with limit does not accumulate overwritten spinner frames 1`] = ` +[ + "│ +", + "◇ foo +", + "│ +", + "│ step 1 +", + "", + "│ step 1 +│ ◒ step 2 +", + "", + "│ step 1 +│ ◐ step 2 +", + "", + "│ step 1 +│ ◇ step 2 done +", + "", + "│ ◇ step 2 done +│ ◒ step 3 +", + "", + "│ ◇ step 2 done +│ ◇ step 3 done +", + "", + "│ +◆ woo! +", + "│ +│ step 1 +│ ◇ step 2 done +│ ◇ step 3 done +", +] +`; + exports[`taskLog (isCI = false) > success > clears output and renders message 1`] = ` [ "│ @@ -1128,6 +1442,32 @@ exports[`taskLog (isCI = false) > success > clears output and renders message 1` ] `; +exports[`taskLog (isCI = false) > success > renders only latest carriage return frame when showLog = true 1`] = ` +[ + "│ +", + "◇ foo +", + "│ +", + "│ ◒ Building bundle +", + "", + "│ ◐ Building bundle +", + "", + "│ ◇ Bundle built +", + "", + "│ +◆ success! +", + "│ +│ ◇ Bundle built +", +] +`; + exports[`taskLog (isCI = false) > success > renders output if showLog = true 1`] = ` [ "│ @@ -1164,6 +1504,27 @@ exports[`taskLog (isCI = false) > writes message header 1`] = ` ] `; +exports[`taskLog (isCI = true) > error > clears exact rendered line count and shows only latest spinner frame on error 1`] = ` +[ + "│ +", + "◇ foo +", + "│ +", + "", + "", + "", + "", + "│ +■ clone failed! +", + "│ +│ ◑ Cloning repository from remote origin +", +] +`; + exports[`taskLog (isCI = true) > error > clears output if showLog = false 1`] = ` [ "│ @@ -1180,6 +1541,22 @@ exports[`taskLog (isCI = true) > error > clears output if showLog = false 1`] = ] `; +exports[`taskLog (isCI = true) > error > clears wrapped spinner frame accurately when overwritten by shorter frame 1`] = ` +[ + "│ +", + "◇ foo +", + "│ +", + "", + "", + "│ +■ verification failed! +", +] +`; + exports[`taskLog (isCI = true) > error > renders output with message 1`] = ` [ "│ @@ -1357,6 +1734,37 @@ exports[`taskLog (isCI = true) > group > showLog shows all groups in order 1`] = ] `; +exports[`taskLog (isCI = true) > group > tracks raw chunks and carriage returns independently per group 1`] = ` +[ + "│ +", + "◇ Some log +", + "│ +", + "", + "", + "", + "", + "", + "", + "│ +■ overall error +", + "│ Group 0 +", + "│ +│ ◇ Group 0 ready +", + "│ Group 1 +", + "│ +│ Group 1 header +│ ◇ Group 1 done +", +] +`; + exports[`taskLog (isCI = true) > message > can write line by line 1`] = ` [ "│ @@ -1406,6 +1814,47 @@ exports[`taskLog (isCI = true) > message > enforces limit if set 1`] = ` ] `; +exports[`taskLog (isCI = true) > message > non-raw messages collapse bare carriage returns and preserve CRLF lines 1`] = ` +[ + "│ +", + "◇ foo +", + "│ +", + "", + "│ +■ failed! +", + "│ +│ line 0 +│ ◇ Done +", +] +`; + +exports[`taskLog (isCI = true) > message > preserves CRLF newlines including split chunks while collapsing bare carriage returns 1`] = ` +[ + "│ +", + "◇ foo +", + "│ +", + "", + "", + "", + "│ +■ failed! +", + "│ +│ line 0 +│ frame 2 +│ frame C +", +] +`; + exports[`taskLog (isCI = true) > message > prints empty lines 1`] = ` [ "│ @@ -1432,6 +1881,35 @@ exports[`taskLog (isCI = true) > message > raw = true appends message text until ] `; +exports[`taskLog (isCI = true) > message > raw = true overwrites current line on leading carriage returns 1`] = ` +[ + "│ +", + "◇ foo +", + "│ +", + "", + "", + "", + "", +] +`; + +exports[`taskLog (isCI = true) > message > raw = true overwrites current line on trailing carriage returns 1`] = ` +[ + "│ +", + "◇ foo +", + "│ +", + "", + "", + "", +] +`; + exports[`taskLog (isCI = true) > message > raw = true works when mixed with non-raw messages 1`] = ` [ "│ @@ -1574,6 +2052,31 @@ exports[`taskLog (isCI = true) > retainLog > error > retainLog = true outputs fu ] `; +exports[`taskLog (isCI = true) > retainLog > error > retainLog = true with limit does not accumulate overwritten spinner frames 1`] = ` +[ + "│ +", + "◇ foo +", + "│ +", + "", + "", + "", + "", + "", + "", + "│ +■ woo! +", + "│ +│ step 1 +│ ◇ step 2 done +│ ◇ step 3 done +", +] +`; + exports[`taskLog (isCI = true) > retainLog > success > outputs limited log with limit by default 1`] = ` [ "│ @@ -1690,6 +2193,31 @@ exports[`taskLog (isCI = true) > retainLog > success > retainLog = true outputs ] `; +exports[`taskLog (isCI = true) > retainLog > success > retainLog = true with limit does not accumulate overwritten spinner frames 1`] = ` +[ + "│ +", + "◇ foo +", + "│ +", + "", + "", + "", + "", + "", + "", + "│ +◆ woo! +", + "│ +│ step 1 +│ ◇ step 2 done +│ ◇ step 3 done +", +] +`; + exports[`taskLog (isCI = true) > success > clears output and renders message 1`] = ` [ "│ @@ -1706,6 +2234,26 @@ exports[`taskLog (isCI = true) > success > clears output and renders message 1`] ] `; +exports[`taskLog (isCI = true) > success > renders only latest carriage return frame when showLog = true 1`] = ` +[ + "│ +", + "◇ foo +", + "│ +", + "", + "", + "", + "│ +◆ success! +", + "│ +│ ◇ Bundle built +", +] +`; + exports[`taskLog (isCI = true) > success > renders output if showLog = true 1`] = ` [ "│ diff --git a/packages/prompts/test/task-log.test.ts b/packages/prompts/test/task-log.test.ts index dbbc31a6..1ea5da75 100644 --- a/packages/prompts/test/task-log.test.ts +++ b/packages/prompts/test/task-log.test.ts @@ -146,6 +146,79 @@ describe.each(['true', 'false'])('taskLog (isCI = %s)', (isCI) => { expect(output.buffer).toMatchSnapshot(); }); + + test('raw = true overwrites current line on leading carriage returns', () => { + const log = prompts.taskLog({ + input, + output, + title: 'foo', + }); + + log.message('◒ Cloning repository', { raw: true }); + log.message('\r◐ Cloning repository', { raw: true }); + log.message('\r◓ Cloning repository', { raw: true }); + log.message('\r◇ Repository cloned\n', { raw: true }); + log.message('Installing dependencies...', { raw: true }); + + expect(output.buffer).toMatchSnapshot(); + }); + + test('raw = true overwrites current line on trailing carriage returns', () => { + const log = prompts.taskLog({ + input, + output, + title: 'foo', + }); + + log.message('Downloading 10%\r', { raw: true }); + log.message('Downloading 50%\r', { raw: true }); + log.message('Downloading 100%\r\n', { raw: true }); + log.message('Extracting archive', { raw: true }); + + expect(output.buffer).toMatchSnapshot(); + }); + + test('preserves CRLF newlines including split chunks while collapsing bare carriage returns', () => { + const log = prompts.taskLog({ + input, + output, + title: 'foo', + }); + + log.message('line 0\r\nframe 1\rframe 2', { raw: true }); + log.message('\r', { raw: true }); + log.message('\nframe A\rframe B\rframe C', { raw: true }); + log.error('failed!'); + + if (isCI === 'false') { + const finalLog = output.buffer.at(-1); + expect(finalLog).toContain('line 0'); + expect(finalLog).toContain('frame 2'); + expect(finalLog).toContain('frame C'); + expect(finalLog).not.toContain('frame 1'); + expect(finalLog).not.toContain('frame A'); + expect(finalLog).not.toContain('frame B'); + } + expect(output.buffer).toMatchSnapshot(); + }); + + test('non-raw messages collapse bare carriage returns and preserve CRLF lines', () => { + const log = prompts.taskLog({ + input, + output, + title: 'foo', + }); + + log.message('line 0\r\n◒ Working\r◐ Working\r◇ Done\r'); + log.error('failed!'); + + const finalLog = output.buffer.at(-1); + expect(finalLog).toContain('line 0'); + expect(finalLog).toContain('◇ Done'); + expect(finalLog).not.toContain('◒ Working'); + expect(finalLog).not.toContain('◐ Working'); + expect(output.buffer).toMatchSnapshot(); + }); }); describe('error', () => { @@ -178,6 +251,45 @@ describe.each(['true', 'false'])('taskLog (isCI = %s)', (isCI) => { expect(output.buffer).toMatchSnapshot(); }); + + test('clears exact rendered line count and shows only latest spinner frame on error', () => { + const log = prompts.taskLog({ + input, + output, + title: 'foo', + }); + + log.message('◒ Cloning repository from remote origin', { raw: true }); + log.message('\r◐ Cloning repository from remote origin', { raw: true }); + log.message('\r◓ Cloning repository from remote origin', { raw: true }); + log.message('\r◑ Cloning repository from remote origin', { raw: true }); + + log.error('clone failed!'); + + const finalLog = output.buffer.at(-1); + expect(finalLog).toContain('◑ Cloning repository from remote origin'); + expect(finalLog).not.toContain('◒ Cloning repository'); + expect(finalLog).not.toContain('◐ Cloning repository'); + expect(finalLog).not.toContain('◓ Cloning repository'); + expect(output.buffer).toMatchSnapshot(); + }); + + test('clears wrapped spinner frame accurately when overwritten by shorter frame', () => { + output.columns = 30; + const log = prompts.taskLog({ + input, + output, + title: 'foo', + }); + + log.message('◒ Downloading a very long package name that wraps across lines', { + raw: true, + }); + log.message('\r◇ Downloaded!', { raw: true }); + log.error('verification failed!', { showLog: false }); + + expect(output.buffer).toMatchSnapshot(); + }); }); describe('success', () => { @@ -210,6 +322,26 @@ describe.each(['true', 'false'])('taskLog (isCI = %s)', (isCI) => { expect(output.buffer).toMatchSnapshot(); }); + + test('renders only latest carriage return frame when showLog = true', () => { + const log = prompts.taskLog({ + input, + output, + title: 'foo', + }); + + log.message('◒ Building bundle', { raw: true }); + log.message('\r◐ Building bundle', { raw: true }); + log.message('\r◇ Bundle built', { raw: true }); + + log.success('success!', { showLog: true }); + + const finalLog = output.buffer.at(-1); + expect(finalLog).toContain('◇ Bundle built'); + expect(finalLog).not.toContain('◒ Building bundle'); + expect(finalLog).not.toContain('◐ Building bundle'); + expect(output.buffer).toMatchSnapshot(); + }); }); describe('retainLog', () => { @@ -300,6 +432,34 @@ describe.each(['true', 'false'])('taskLog (isCI = %s)', (isCI) => { expect(output.buffer).toMatchSnapshot(); }); + + test('retainLog = true with limit does not accumulate overwritten spinner frames', () => { + const log = prompts.taskLog({ + input, + output, + title: 'foo', + retainLog: true, + limit: 2, + }); + + log.message('step 1\n', { raw: true }); + log.message('◒ step 2', { raw: true }); + log.message('\r◐ step 2', { raw: true }); + log.message('\r◇ step 2 done\n', { raw: true }); + log.message('◒ step 3', { raw: true }); + log.message('\r◇ step 3 done', { raw: true }); + + log[method]('woo!', { showLog: true }); + + const finalLog = output.buffer.at(-1); + expect(finalLog).toContain('step 1'); + expect(finalLog).toContain('◇ step 2 done'); + expect(finalLog).toContain('◇ step 3 done'); + expect(finalLog).not.toContain('◒ step 2'); + expect(finalLog).not.toContain('◐ step 2'); + expect(finalLog).not.toContain('◒ step 3'); + expect(output.buffer).toMatchSnapshot(); + }); }); }); @@ -446,5 +606,34 @@ describe.each(['true', 'false'])('taskLog (isCI = %s)', (isCI) => { expect(output.buffer).toMatchSnapshot(); }); + + test('tracks raw chunks and carriage returns independently per group', () => { + const log = prompts.taskLog({ + title: 'Some log', + input, + output, + }); + const group0 = log.group('Group 0'); + const group1 = log.group('Group 1'); + + group0.message('◒ Group 0 loading', { raw: true }); + group1.message('Group 1 header'); + group0.message('\r◇ Group 0 ready', { raw: true }); + group1.message('◒ Group 1 step', { raw: true }); + group1.message(' still running', { raw: true }); + group1.message('\r◇ Group 1 done', { raw: true }); + + log.error('overall error', { showLog: true }); + + const outputText = output.buffer.join(''); + const finalGroupLogs = output.buffer.slice(-4).join(''); + expect(outputText).toContain('overall error'); + expect(finalGroupLogs).toContain('◇ Group 0 ready'); + expect(finalGroupLogs).toContain('Group 1 header'); + expect(finalGroupLogs).toContain('◇ Group 1 done'); + expect(finalGroupLogs).not.toContain('◒ Group 0 loading'); + expect(finalGroupLogs).not.toContain('◒ Group 1 step'); + expect(output.buffer).toMatchSnapshot(); + }); }); });