Skip to content

Reserve room for the truncation character in preferTruncationOnSpace - #34

Merged
sindresorhus merged 1 commit into
sindresorhus:mainfrom
spokodev:fix/prefer-space-truncation-character-width
Jul 9, 2026
Merged

Reserve room for the truncation character in preferTruncationOnSpace#34
sindresorhus merged 1 commit into
sindresorhus:mainfrom
spokodev:fix/prefer-space-truncation-character-width

Conversation

@spokodev

@spokodev spokodev commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

preferTruncationOnSpace picks the cut point assuming the truncation character occupies a single column. With a wider truncationCharacter (for example the common '...', width 3) the result exceeds columns.

import cliTruncate from 'cli-truncate';

cliTruncate('unicorns are awesome dragons here today', 6, {
	position: 'end',
	preferTruncationOnSpace: true,
	truncationCharacter: '...',
});
//=> 'unico...'  (width 8, budget 6)

cliTruncate('unicorns are awesome dragons here today', 6, {
	position: 'start',
	preferTruncationOnSpace: true,
	truncationCharacter: '...',
});
//=> '...here today'  (width 13, budget 6)

The start and end preferTruncationOnSpace branches hard-code length - columns + 1 and columns - 1, reserving a single column for the truncation character. The non-preferTruncationOnSpace paths already reserve stringWidth(truncationCharacter), and the middle branch already uses truncationWidth. This brings the two branches in line so the result stays within columns.

With the default '…' (width 1) the output is unchanged.

Adds a regression test asserting stringWidth(result) <= columns for start/end + preferTruncationOnSpace with a multi-column truncation character.

@sindresorhus
sindresorhus merged commit f16fcab into sindresorhus:main Jul 9, 2026
2 checks passed
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