Product Promotion
0x5a.live
for different kinds of informations and explorations.
GitHub - sindresorhus/cli-truncate: Truncate a string to a specific width in the terminal
Truncate a string to a specific width in the terminal - sindresorhus/cli-truncate
Visit SiteGitHub - sindresorhus/cli-truncate: Truncate a string to a specific width in the terminal
Truncate a string to a specific width in the terminal - sindresorhus/cli-truncate
Powered by 0x5a.live š
cli-truncate
Truncate a string to a specific width in the terminal
Gracefully handles ANSI escapes. Like a string styled with chalk
. It also supports Unicode surrogate pairs and fullwidth characters.
Install
npm install cli-truncate
Usage
import cliTruncate from 'cli-truncate';
cliTruncate('unicorn', 4);
//=> 'uniā¦'
// Truncate at different positions
cliTruncate('unicorn', 4, {position: 'start'});
//=> 'ā¦orn'
cliTruncate('unicorn', 4, {position: 'middle'});
//=> 'unā¦n'
cliTruncate('unicorns rainbow dragons', 6, {position: 'end'})
//=> 'unicoā¦'
cliTruncate('\u001B[31municorn\u001B[39m', 4);
//=> '\u001B[31muni\u001B[39mā¦'
// Truncate Unicode surrogate pairs
cliTruncate('uni\uD83C\uDE00corn', 5);
//=> 'uni\uD83C\uDE00ā¦'
// Truncate fullwidth characters
cliTruncate('ģė
ķģøģ', 3);
//=> 'ģā¦'
// Truncate the paragraph to the terminal width
const paragraph = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa.';
cliTruncate(paragraph, process.stdout.columns);
//=> 'Lorem ipsum dolor sit amet, consectetuer adipiscingā¦'
API
cliTruncate(text, columns, options?)
text
Type: string
The text to truncate.
columns
Type: number
The number of columns to occupy in the terminal.
options
Type: object
position
Type: string
Default: 'end'
Values: 'start' | 'middle' | 'end'
The position to truncate the string.
space
Type: boolean
Default: false
Add a space between the text and the ellipsis.
import cliTruncate from 'cli-truncate';
cliTruncate('unicorns', 5, {space: false});
//=> 'unicā¦'
cliTruncate('unicorns', 5, {space: true});
//=> 'uni ā¦'
cliTruncate('unicorns', 6, {position: 'start', space: true});
//=> 'ā¦ orns'
cliTruncate('unicorns', 7, {position: 'middle', space: true});
//=> 'uni ā¦ s'
preferTruncationOnSpace
Type: boolean
Default: false
Truncate the string from a whitespace if it is within 3 characters from the actual breaking point.
import cliTruncate from 'cli-truncate';
cliTruncate('unicorns rainbow dragons', 20, {position: 'start', preferTruncationOnSpace: true})
//=> 'ā¦rainbow dragons'
// without preferTruncationOnSpace
cliTruncate('unicorns rainbow dragons', 20, {position: 'start'})
//=> 'ā¦rns rainbow dragons'
cliTruncate('unicorns rainbow dragons', 20, {position: 'middle', preferTruncationOnSpace: true})
//=> 'unicornsā¦dragons'
cliTruncate('unicorns rainbow dragons', 6, {position: 'end', preferTruncationOnSpace: true})
//=> 'unicoā¦'
// preferTruncationOnSpace would have no effect if space isn't found within next 3 indexes
cliTruncate('unicorns rainbow dragons', 6, {position: 'middle', preferTruncationOnSpace: true})
//=> 'uniā¦ns'
truncationCharacter
Type: string
Default: ā¦
The character to use at the breaking point.
import cliTruncate from 'cli-truncate';
cliTruncate('unicorns', 5, {position: 'end'});
//=> 'unicā¦'
cliTruncate('unicorns', 5, {position: 'end', truncationCharacter: '.'});
//=> 'unic.'
cliTruncate('unicorns', 5, {position: 'end', truncationCharacter: ''});
//=> 'unico'
Related
- wrap-ansi - Wordwrap a string with ANSI escape codes
- slice-ansi - Slice a string with ANSI escape codes
NodeJS Resources
are all listed below.
Made with ā¤ļø
to provide different kinds of informations and resources.