wrap-ansi
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
ansi-styles | 4.3.0 | 5.71 kB | MIT | prod | |
string-width | 4.2.3 | 2.33 kB | MIT | prod | |
strip-ansi | 6.0.1 | 1.99 kB | MIT | prod |
Wrap-ansi is a highly useful npm package that allows for word wrapping of a string with ANSI escape codes. An ANSI escape code is a sequence of characters that designates an action that should be taken by the software interpreting the text. These codes can be used to change text color, move the cursor, and perform other text manipulations. Wrap-ansi provides a function that wraps text containing these codes to a specified column width without breaking the ANSI escape sequences. This is particularly handy when string styling is done using libraries like chalk
.
To use wrap-ansi, first, it needs to be installed in your project using npm by running the command npm install wrap-ansi
. You can then import it into your JavaScript file using import wrapAnsi from 'wrap-ansi'
. Here's an example of how you can use wrap-ansi:
import chalk from 'chalk';
import wrapAnsi from 'wrap-ansi';
const input = 'The quick brown ' + chalk.red('fox jumped over ') +
'the lazy ' + chalk.green('dog and then ran away with the unicorn.');
console.log(wrapAnsi(input, 20));
In this example, the string input
is styled using the chalk
library, then wrapped to a width of 20 columns using the wrapAnsi
function. The output would be the string input
styled and wrapped to 20 columns.
The documentation for wrap-ansi can be found in the README file in the project's GitHub repository. The GitHub URL for the package is git+https://github.com/chalk/wrap-ansi.git. The instructions for installation, usage, and the API, including the options you can use with the wrapAnsi function (such as hard wrap, word wrap, trim options), can all be found in this README file.