Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
ansi-styles | 2.2.1 | 2.39 kB | MIT | prod |
The npm package ansi-styles
gives you access to ANSI escape codes which can be used for styling strings that will be output to the terminal. It allows transforming a simple string into a colored, or otherwise stylized, piece of text that is viewable in your terminal display.
To start using ansi-styles
, you first need to install the package using npm with the command: npm install ansi-styles
. Once installed, you can start styling your terminal strings. Here are a few examples of how you could use ansi-styles
:
import styles from 'ansi-styles';
// This will output the text 'Hello world!' in green color
console.log(`${styles.green.open}Hello world!${styles.green.close}`);
// The following statements will output 'Hello World' in true-color, based on RGB values provided
console.log(`${styles.color.ansi(styles.rgbToAnsi(199, 20, 250))}Hello World${styles.color.close}`);
console.log(`${styles.color.ansi256(styles.rgbToAnsi256(199, 20, 250))}Hello World${styles.color.close}`);
console.log(`${styles.color.ansi16m(...styles.hexToRgb('#abcdef'))}Hello World${styles.color.close}`);
// This will text whether certain styles are included in the available modifiers and colors
console.log(styles.modifierNames.includes('bold')); // true
console.log(styles.foregroundColorNames.includes('pink')); // false
The official documentation for ansi-styles
is contained within its README file on GitHub. Here, you can find detailed usage examples, the API description, a list of all available styles which range from color modifiers to background colors, and advanced usage tips about the available color spaces and the conversion functions for turning various color formats into ANSI escapes.