Socket
Socket
Sign inDemoInstall

cli-truncate

Package Overview
Dependencies
7
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    cli-truncate

Truncate a string to a specific width in the terminal


Version published
Weekly downloads
17M
decreased by-8.58%
Maintainers
1
Install size
37.8 kB
Created
Weekly downloads
 

Package description

What is cli-truncate?

The cli-truncate package is designed to truncate a string to a specific length without breaking the words, making it suitable for command-line interfaces where space is limited. It can handle strings with ANSI escape codes, ensuring that the visual formatting is preserved even when the string is truncated.

What are cli-truncate's main functionalities?

Truncate a string to a specific length

This feature allows you to truncate a string to a specified length, ensuring that the output fits within a certain space in the CLI. It's useful for displaying long strings in a limited space.

"const cliTruncate = require('cli-truncate');\nconsole.log(cliTruncate('unicorn', 4)); // => 'uni…'"

Preserve ANSI escape codes

This feature ensures that ANSI escape codes within the string are preserved when truncating. This is particularly useful for maintaining text styles (like colors) in the CLI output.

"const cliTruncate = require('cli-truncate');\nconsole.log(cliTruncate('\u001B[31municorn\u001B[39m', 4)); // => '\u001B[31muni…\u001B[39m'"

Specify the position of the truncation

This feature allows you to specify where the truncation should occur (start, middle, or end), giving you more control over how the truncated string looks.

"const cliTruncate = require('cli-truncate');\nconsole.log(cliTruncate('unicorn', 4, {position: 'middle'})); // => 'un…n'"

Other packages similar to cli-truncate

Readme

Source

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

Keywords

FAQs

Last updated on 05 Apr 2016

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc