ANSI escape codes for styling strings in the terminal
Installations
npm install ansi-styles
Score
99.5
Supply Chain
99.5
Quality
78.3
Maintenance
100
Vulnerability
100
License
Developer
chalk
Developer Guide
Module System
ESM
Min. Node Version
>=12
Typescript Support
Yes
Node Version
16.16.0
NPM Version
8.3.2
Statistics
423 Stars
113 Commits
73 Forks
13 Watching
2 Branches
18 Contributors
Updated on 16 Nov 2024
Bundle Size
2.76 kB
Minified
1.20 kB
Minified + Gzipped
Languages
JavaScript (65.09%)
TypeScript (34.91%)
Total Downloads
Cumulative downloads
Total Downloads
49,060,719,673
Last day
-8.4%
66,067,588
Compared to previous day
Last week
-2%
379,200,210
Compared to previous week
Last month
1.9%
1,638,608,067
Compared to previous month
Last year
29.5%
17,896,364,758
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dev Dependencies
4
ansi-styles
ANSI escape codes for styling strings in the terminal
You probably want the higher-level chalk module for styling your strings.
Install
1npm install ansi-styles
Usage
1import styles from 'ansi-styles'; 2 3console.log(`${styles.green.open}Hello world!${styles.green.close}`); 4 5 6// Color conversion between 256/truecolor 7// NOTE: When converting from truecolor to 256 colors, the original color 8// may be degraded to fit the new color palette. This means terminals 9// that do not support 16 million colors will best-match the 10// original color. 11console.log(`${styles.color.ansi(styles.rgbToAnsi(199, 20, 250))}Hello World${styles.color.close}`) 12console.log(`${styles.color.ansi256(styles.rgbToAnsi256(199, 20, 250))}Hello World${styles.color.close}`) 13console.log(`${styles.color.ansi16m(...styles.hexToRgb('#abcdef'))}Hello World${styles.color.close}`)
API
open
and close
Each style has an open
and close
property.
modifierNames
, foregroundColorNames
, backgroundColorNames
, and colorNames
All supported style strings are exposed as an array of strings for convenience. colorNames
is the combination of foregroundColorNames
and backgroundColorNames
.
This can be useful if you need to validate input:
1import {modifierNames, foregroundColorNames} from 'ansi-styles'; 2 3console.log(modifierNames.includes('bold')); 4//=> true 5 6console.log(foregroundColorNames.includes('pink')); 7//=> false
Styles
Modifiers
reset
bold
dim
italic
(Not widely supported)underline
overline
Supported on VTE-based terminals, the GNOME terminal, mintty, and Git Bash.inverse
hidden
strikethrough
(Not widely supported)
Colors
black
red
green
yellow
blue
magenta
cyan
white
blackBright
(alias:gray
,grey
)redBright
greenBright
yellowBright
blueBright
magentaBright
cyanBright
whiteBright
Background colors
bgBlack
bgRed
bgGreen
bgYellow
bgBlue
bgMagenta
bgCyan
bgWhite
bgBlackBright
(alias:bgGray
,bgGrey
)bgRedBright
bgGreenBright
bgYellowBright
bgBlueBright
bgMagentaBright
bgCyanBright
bgWhiteBright
Advanced usage
By default, you get a map of styles, but the styles are also available as groups. They are non-enumerable so they don't show up unless you access them explicitly. This makes it easier to expose only a subset in a higher-level module.
styles.modifier
styles.color
styles.bgColor
Example
1import styles from 'ansi-styles'; 2 3console.log(styles.color.green.open);
Raw escape codes (i.e. without the CSI escape prefix \u001B[
and render mode postfix m
) are available under styles.codes
, which returns a Map
with the open codes as keys and close codes as values.
Example
1import styles from 'ansi-styles'; 2 3console.log(styles.codes.get(36)); 4//=> 39
16 / 256 / 16 million (TrueColor) support
ansi-styles
allows converting between various color formats and ANSI escapes, with support for 16, 256 and 16 million colors.
The following color spaces are supported:
rgb
hex
ansi256
ansi
To use these, call the associated conversion function with the intended output, for example:
1import styles from 'ansi-styles'; 2 3styles.color.ansi(styles.rgbToAnsi(100, 200, 15)); // RGB to 16 color ansi foreground code 4styles.bgColor.ansi(styles.hexToAnsi('#C0FFEE')); // HEX to 16 color ansi foreground code 5 6styles.color.ansi256(styles.rgbToAnsi256(100, 200, 15)); // RGB to 256 color ansi foreground code 7styles.bgColor.ansi256(styles.hexToAnsi256('#C0FFEE')); // HEX to 256 color ansi foreground code 8 9styles.color.ansi16m(100, 200, 15); // RGB to 16 million color foreground code 10styles.bgColor.ansi16m(...styles.hexToRgb('#C0FFEE')); // Hex (RGB) to 16 million color foreground code
Related
- ansi-escapes - ANSI escape codes for manipulating the terminal
Maintainers
For enterprise
Available as part of the Tidelift Subscription.
The maintainers of ansi-styles
and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. Learn more.
No vulnerabilities found.
Reason
security policy file detected
Details
- Info: security policy file detected: .github/security.md:1
- Info: Found linked content: .github/security.md:1
- Info: Found disclosure, vulnerability, and/or timelines in security policy: .github/security.md:1
- Info: Found text in security policy: .github/security.md:1
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
- Info: project has a license file: license:0
- Info: FSF or OSI recognized license: MIT License: license:0
Reason
Found 11/30 approved changesets -- score normalized to 3
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/main.yml:1
- Info: no jobLevel write permissions found
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:17: update your workflow using https://app.stepsecurity.io/secureworkflow/chalk/ansi-styles/main.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:18: update your workflow using https://app.stepsecurity.io/secureworkflow/chalk/ansi-styles/main.yml/main?enable=pin
- Warn: npmCommand not pinned by hash: .github/workflows/main.yml:22
- Info: 0 out of 2 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 1 npmCommand dependencies pinned
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'main'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 11 are checked with a SAST tool
Score
4.3
/10
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More