What is slice-ansi?
The slice-ansi npm package is designed for slicing strings with ANSI escape codes without breaking the visual appearance. This is particularly useful for terminal applications where text styling (like colors, bold, underline) is applied through ANSI escape codes. It allows developers to extract or truncate portions of the styled text without losing or corrupting the intended styles.
What are slice-ansi's main functionalities?
Slicing styled strings
This feature allows you to slice a string that contains ANSI escape codes, ensuring that the visual styling is preserved in the sliced string. The code sample demonstrates how to slice a string that starts with red-colored text and then resets to default color. The sliced result contains only the portion of the string within the specified range, along with the necessary ANSI codes to maintain the original styling.
const sliceAnsi = require('slice-ansi');
const input = '\u001B[31mThis is red\u001B[39m and this is not.';
const sliced = sliceAnsi(input, 0, 10);
console.log(sliced);
Other packages similar to slice-ansi
chalk
Chalk is a popular npm package for styling strings in terminal applications with ANSI escape codes. While it doesn't offer slicing functionality, it's often used in conjunction with slice-ansi for creating and then slicing styled strings. Chalk focuses on easily adding styles to strings, whereas slice-ansi is about manipulating those styled strings without losing the styling.
strip-ansi
Strip-ansi is another npm package that removes ANSI escape codes from a string. It's useful for cleaning up strings before logging to a non-terminal output or for string manipulation where the ANSI codes are not desired. Unlike slice-ansi, which preserves and properly handles the escape codes for slicing, strip-ansi completely removes them.
ansi-styles
Ansi-styles provides the ANSI escape codes for styling strings in the terminal but does not offer direct functionality for slicing or manipulating strings with ANSI codes. It can be used in combination with slice-ansi for developers who want to manually manage the styling and slicing of strings in their applications. Ansi-styles is more about providing the tools for styling, while slice-ansi is about preserving those styles during string manipulation.
slice-ansi
Slice a string with ANSI escape codes
Install
npm install slice-ansi
Usage
import chalk from 'chalk';
import sliceAnsi from 'slice-ansi';
const string = 'The quick brown ' + chalk.red('fox jumped over ') +
'the lazy ' + chalk.green('dog and then ran away with the unicorn.');
console.log(sliceAnsi(string, 20, 30));
API
sliceAnsi(string, startSlice, endSlice?)
string
Type: string
String with ANSI escape codes. Like one styled by chalk
.
startSlice
Type: number
Zero-based index at which to start the slice.
endSlice
Type: number
Zero-based index at which to end the slice.
Related
- wrap-ansi - Wordwrap a string with ANSI escape codes
- cli-truncate - Truncate a string to a specific width in the terminal
- chalk - Terminal string styling done right
Maintainers