What is @types/wrap-ansi?
@types/wrap-ansi provides TypeScript type definitions for the wrap-ansi package, which is used to wrap words to a specified width while preserving ANSI escape codes.
What are @types/wrap-ansi's main functionalities?
Wrap text to a specified width
This feature allows you to wrap a string of text to a specified width. In this example, the input string is wrapped to a width of 20 characters.
const wrapAnsi = require('wrap-ansi');
const input = 'The quick brown fox jumps over the lazy dog';
const output = wrapAnsi(input, 20);
console.log(output);
Preserve ANSI escape codes
This feature ensures that ANSI escape codes are preserved when wrapping text. In this example, the input string contains ANSI escape codes for red text, and these codes are preserved in the output.
const wrapAnsi = require('wrap-ansi');
const input = '\u001B[31mThe quick brown fox jumps over the lazy dog\u001B[39m';
const output = wrapAnsi(input, 20);
console.log(output);
Hard wrapping
This feature allows for hard wrapping, which means that words will be split if they exceed the specified width. In this example, the input string is hard-wrapped to a width of 20 characters.
const wrapAnsi = require('wrap-ansi');
const input = 'The quick brown fox jumps over the lazy dog';
const output = wrapAnsi(input, 20, { hard: true });
console.log(output);
Other packages similar to @types/wrap-ansi
word-wrap
The word-wrap package provides similar functionality for wrapping text to a specified width, but it does not handle ANSI escape codes. It is simpler and more lightweight compared to wrap-ansi.
cli-width
The cli-width package is used to get the width of the terminal, which can be useful in conjunction with wrap-ansi for dynamically wrapping text based on the terminal width. It does not provide text wrapping functionality by itself.
slice-ansi
The slice-ansi package is used to slice a string containing ANSI escape codes without breaking the codes. It can be used in combination with other text-wrapping libraries to handle ANSI escape codes properly.