travel/admin/node_modules/cli-truncate
yaosen 242417d45d init... 2024-06-24 11:28:18 +08:00
..
node_modules init... 2024-06-24 11:28:18 +08:00
index.js init... 2024-06-24 11:28:18 +08:00
license init... 2024-06-24 11:28:18 +08:00
package.json init... 2024-06-24 11:28:18 +08:00
readme.md init... 2024-06-24 11:28:18 +08:00

readme.md

cli-truncate Build Status

Truncate a string to a specific width in the terminal

Gracefully handles ANSI escapes. Like a string styled with chalk.

Install

$ npm install --save cli-truncate

Usage

const cliTruncate = require('cli-truncate');

cliTruncate('unicorn', 4);
//=> 'uni…'

// truncate at different positions
cliTruncate('unicorn', 4, {position: 'start'});
//=> '…orn'

cliTruncate('unicorn', 4, {position: 'middle'});
//=> 'un…n'

cliTruncate('\u001b[31municorn\u001b[39m', 4);
//=> '\u001b[31muni\u001b[39m…'

// 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(input, columns, [options])

input

Type: string

Text to truncate.

columns

Type: number

Columns to occupy in the terminal.

options

position

Type: string
Default: 'end'
Values: 'start', 'middle', 'end'

Position to truncate the string.

  • wrap-ansi - Wordwrap a string with ANSI escape codes
  • slice-ansi - Slice a string with ANSI escape codes

License

MIT © Sindre Sorhus