Skip to content

Return the text unchanged when columns is NaN - #33

Closed
hong4rc wants to merge 1 commit into
sindresorhus:mainfrom
hong4rc:fix/nan-columns
Closed

Return the text unchanged when columns is NaN#33
hong4rc wants to merge 1 commit into
sindresorhus:mainfrom
hong4rc:fix/nan-columns

Conversation

@hong4rc

@hong4rc hong4rc commented Jun 28, 2026

Copy link
Copy Markdown

Issue

When columns is NaN, the input is returned with a truncation character always appended — so the output is longer than the input and nothing is actually truncated. It happens for any input, even an empty string:

input current expected
'' '…' ''
'hi' 'hi…' 'hi'
'unicorns' 'unicorns…' 'unicorns'

Easy repro

import cliTruncate from 'cli-truncate';

cliTruncate('unicorns', NaN);
//=> 'unicorns…'   ❌ not truncated, stray "…" appended

How NaN shows up in practice

The common idiom of reserving a margin off the terminal width:

cliTruncate(text, process.stdout.columns - 2);

When stdout is not a TTY (piped to a file, redirected, or CI), process.stdout.columns is undefined, so undefined - 2 is NaN:

$ node demo.js > out.txt   # stdout is not a TTY
$ cat out.txt
this is a long status line…   ← stray "…", not truncated

Fix

A NaN budget means the target width is unknown, so return the text untruncated instead of appending a truncation character. Other non-finite inputs are unchanged: Infinity already returned the text, negatives already returned ''.

Added a NaN columns test (plain + ANSI-styled input).

@sindresorhus

Copy link
Copy Markdown
Owner

This should throw instead. columns is expected to be an actual width, and NaN is not a meaningful terminal column count. The process.stdout.columns - 2 case should be handled by the caller with an explicit fallback, rather than making cli-truncate silently opt out of truncation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants