4.0.0 • Published 1 year ago

cli-truncate v4.0.0

Weekly downloads
6,747,222
License
MIT
Repository
github
Last release
1 year ago

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
listr-update-rendererlistr2@prisma/sdkarchetype-librarybb-chatwrm-troubleshooting-poc@jttechnic/interpreter@sheba/commit-template@newhorizon-tech/dd-npm-package-templatecogoportutils@infinitebrahmanuniverse/nolb-cli-t@prodam/prodam-typescz-ly-emojicz-gamedev@everything-registry/sub-chunk-1336elementify-ui-kittailwind-vector-effecttempomattempomat-weektest-openapiwienrwithhold-message-avawlectrionsuperset-plugin-chart-hello-world2speedracersvavatarsweetalert2-denazificationvue-compmentv9u-smb2-singstorvcloudcam-playkit-js-hlsvbb-stations-clivc-cz-emojivue-dev-clone@dotconf-pro/dotconf-pro@dotconf-pro/dotenv@drubin/garden-cli@cybermega/flutter-resource-manager-rs@conneryn/c6o-cli@conneryn/cli@conneryn/cli-bundle@conneryn/cli-test@conneryn/cli-tmp@daiki48/keisan@prisma/ink-components@peak-stone/cz-fbi@prisma/lift@poscredit/plugin-chart-boris@rbc-public/react-selectable-fast@roysung/cz-jira-smart-emojijinqrjovo-clilaoitdev-core-plugins3.0.0spring-riverss-actionsstock-ticker-clisomafmtypingstldrawlignintrpc-toolstwine-libtwine-library@anyversion/elecpack@amiruldev/wajs@atlassian/wrm-troubleshooting@asiz33/smartblok-vendure-plugin@aureooms/listr-aggregate-renderer@arielapaula/components@arielapaula/test@alessiodf/listr@baeta/ink@beisen/bsapp-cz-emoji@batbayar/superset-plugin-chart-hello-world@bee-icons/qwik@bigtest/ink@blazinaj/app-kit@c6o/cli@c6o/daemon@celp/celp-cli@chi_eee/another-test-package@0y0/cucumber-kits@acot/html-pickup@abdullahceylan/listr-aggregate-renderer@codingdud/coolicons@boost/terminal@devorso/dcountdown@danielkalen/listryangtao-jsxshellzsbonc-node-opcuavue-termui@groselha/create-component@hash-bang/pss@hdkhoa1302/app-themes@iamstarkov/listr-update-renderer@hnp/cz@ideasonpurpose/build-tools-wordpress@garonx/oracle-zkapp@garden-io/core@garden-io/grow-sdk
4.0.0

1 year ago

3.1.0

4 years ago

3.0.0

4 years ago

2.1.0

5 years ago

2.0.0

6 years ago

1.1.0

8 years ago

1.0.0

8 years ago

0.2.1

9 years ago

0.2.0

9 years ago

0.1.0

9 years ago