├── .editorconfig ├── .eslintrc.json ├── .gitattributes ├── .gitignore ├── .npmrc ├── .travis.yml ├── .verb.md ├── LICENSE ├── README.md ├── bench ├── .npmrc ├── colors.js ├── dryrun.js ├── index.js ├── load-ansi-colors.js ├── load-chalk.js └── package.json ├── examples ├── alias.js ├── chained.js ├── colors.js ├── nested.js ├── nesting-bug.js ├── ok.js ├── readme-examples.js ├── screenshot-table.js ├── screenshot.js ├── styles.js ├── theme-custom-fn.js ├── theme.js ├── theme2.js └── unstyle.js ├── index.js ├── package.json ├── symbols.js ├── test.js └── types ├── index.d.ts ├── test.ts └── tsconfig.json /.editorconfig: -------------------------------------------------------------------------------- 1 | # http://editorconfig.org/ 2 | root = true 3 | 4 | [*] 5 | charset = utf-8 6 | end_of_line = lf 7 | indent_size = 2 8 | indent_style = space 9 | insert_final_newline = true 10 | trim_trailing_whitespace = true 11 | 12 | [{**/{actual,fixtures,expected,templates}/**,*.md}] 13 | trim_trailing_whitespace = false 14 | insert_final_newline = false 15 | -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": [ 3 | "eslint:recommended" 4 | ], 5 | 6 | "env": { 7 | "browser": false, 8 | "es6": true, 9 | "node": true, 10 | "mocha": true 11 | }, 12 | 13 | "parserOptions":{ 14 | "ecmaVersion": 9, 15 | "sourceType": "module", 16 | "ecmaFeatures": { 17 | "modules": true, 18 | "experimentalObjectRestSpread": true 19 | } 20 | }, 21 | 22 | "globals": { 23 | "document": false, 24 | "navigator": false, 25 | "window": false 26 | }, 27 | 28 | "rules": { 29 | "accessor-pairs": 2, 30 | "arrow-spacing": [2, { "before": true, "after": true }], 31 | "block-spacing": [2, "always"], 32 | "brace-style": [2, "1tbs", { "allowSingleLine": true }], 33 | "comma-dangle": [2, "never"], 34 | "comma-spacing": [2, { "before": false, "after": true }], 35 | "comma-style": [2, "last"], 36 | "constructor-super": 2, 37 | "curly": [2, "multi-line"], 38 | "dot-location": [2, "property"], 39 | "eol-last": 2, 40 | "eqeqeq": [2, "allow-null"], 41 | "generator-star-spacing": [2, { "before": true, "after": true }], 42 | "handle-callback-err": [2, "^(err|error)$" ], 43 | "indent": [2, 2, { "SwitchCase": 1 }], 44 | "key-spacing": [2, { "beforeColon": false, "afterColon": true }], 45 | "keyword-spacing": [2, { "before": true, "after": true }], 46 | "new-cap": [2, { "newIsCap": true, "capIsNew": false }], 47 | "new-parens": 2, 48 | "no-array-constructor": 2, 49 | "no-caller": 2, 50 | "no-class-assign": 2, 51 | "no-cond-assign": 2, 52 | "no-const-assign": 2, 53 | "no-control-regex": 2, 54 | "no-debugger": 2, 55 | "no-delete-var": 2, 56 | "no-dupe-args": 2, 57 | "no-dupe-class-members": 2, 58 | "no-dupe-keys": 2, 59 | "no-duplicate-case": 2, 60 | "no-empty-character-class": 2, 61 | "no-eval": 2, 62 | "no-ex-assign": 2, 63 | "no-extend-native": 2, 64 | "no-extra-bind": 2, 65 | "no-extra-boolean-cast": 2, 66 | "no-extra-parens": [2, "functions"], 67 | "no-fallthrough": 2, 68 | "no-floating-decimal": 2, 69 | "no-func-assign": 2, 70 | "no-implied-eval": 2, 71 | "no-inner-declarations": [2, "functions"], 72 | "no-invalid-regexp": 2, 73 | "no-irregular-whitespace": 2, 74 | "no-iterator": 2, 75 | "no-label-var": 2, 76 | "no-labels": 2, 77 | "no-lone-blocks": 2, 78 | "no-mixed-spaces-and-tabs": 2, 79 | "no-multi-spaces": 2, 80 | "no-multi-str": 2, 81 | "no-multiple-empty-lines": [2, { "max": 1 }], 82 | "no-native-reassign": 0, 83 | "no-negated-in-lhs": 2, 84 | "no-new": 2, 85 | "no-new-func": 2, 86 | "no-new-object": 2, 87 | "no-new-require": 2, 88 | "no-new-wrappers": 2, 89 | "no-obj-calls": 2, 90 | "no-octal": 2, 91 | "no-octal-escape": 2, 92 | "no-proto": 0, 93 | "no-redeclare": 2, 94 | "no-regex-spaces": 2, 95 | "no-return-assign": 2, 96 | "no-self-compare": 2, 97 | "no-sequences": 2, 98 | "no-shadow-restricted-names": 2, 99 | "no-spaced-func": 2, 100 | "no-sparse-arrays": 2, 101 | "no-this-before-super": 2, 102 | "no-throw-literal": 2, 103 | "no-trailing-spaces": 0, 104 | "no-undef": 2, 105 | "no-undef-init": 2, 106 | "no-unexpected-multiline": 2, 107 | "no-unneeded-ternary": [2, { "defaultAssignment": false }], 108 | "no-unreachable": 2, 109 | "no-unused-vars": [2, { "vars": "all", "args": "none" }], 110 | "no-useless-call": 0, 111 | "no-with": 2, 112 | "one-var": [0, { "initialized": "never" }], 113 | "operator-linebreak": [0, "after", { "overrides": { "?": "before", ":": "before" } }], 114 | "padded-blocks": [0, "never"], 115 | "quotes": [2, "single", "avoid-escape"], 116 | "radix": 2, 117 | "semi": [2, "always"], 118 | "semi-spacing": [2, { "before": false, "after": true }], 119 | "space-before-blocks": [2, "always"], 120 | "space-before-function-paren": [2, "never"], 121 | "space-in-parens": [2, "never"], 122 | "space-infix-ops": 2, 123 | "space-unary-ops": [2, { "words": true, "nonwords": false }], 124 | "spaced-comment": [0, "always", { "markers": ["global", "globals", "eslint", "eslint-disable", "*package", "!", ","] }], 125 | "use-isnan": 2, 126 | "valid-typeof": 2, 127 | "wrap-iife": [2, "any"], 128 | "yoda": [2, "never"] 129 | } 130 | } 131 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Enforce Unix newlines 2 | * text eol=lf 3 | 4 | # binaries 5 | *.ai binary 6 | *.psd binary 7 | *.jpg binary 8 | *.gif binary 9 | *.png binary 10 | *.jpeg binary 11 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # always ignore files 2 | *.DS_Store 3 | .idea 4 | .vscode 5 | *.sublime-* 6 | *.code-* 7 | 8 | # test related, or directories generated by tests 9 | test/actual 10 | actual 11 | coverage 12 | .nyc* 13 | 14 | # npm 15 | node_modules 16 | npm-debug.log 17 | 18 | # yarn 19 | yarn.lock 20 | yarn-error.log 21 | 22 | # misc 23 | _gh_pages 24 | _draft 25 | _drafts 26 | bower_components 27 | vendor 28 | temp 29 | tmp 30 | TODO.md 31 | *-lock.json 32 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | os: 3 | - linux 4 | - osx 5 | - windows 6 | language: node_js 7 | node_js: 8 | - node 9 | - '12' 10 | - '11' 11 | - '10' 12 | - '9' 13 | - '8' 14 | - '7' 15 | - '6' 16 | -------------------------------------------------------------------------------- /.verb.md: -------------------------------------------------------------------------------- 1 | ![image](https://user-images.githubusercontent.com/383994/39635445-8a98a3a6-4f8b-11e8-89c1-068c45d4fff8.png) 2 | 3 | ## Why use this? 4 | 5 | ansi-colors is _the fastest Node.js library for terminal styling_. A more performant drop-in replacement for chalk, with no dependencies. 6 | 7 | * _Blazing fast_ - Fastest terminal styling library in node.js, 10-20x faster than chalk! 8 | - _Drop-in replacement_ for [chalk](https://github.com/chalk/chalk). 9 | - _No dependencies_ (Chalk has 7 dependencies in its tree!) 10 | * _Safe_ - Does not modify the `String.prototype` like [colors][]. 11 | * Supports [nested colors](#nested-colors), **and does not have the [nested styling bug](#nested-styling-bug) that is present in [colorette][], [chalk][], and [kleur][]**. 12 | * Supports [chained colors](#chained-colors). 13 | * [Toggle color support](#toggle-color-support) on or off. 14 | 15 | 16 | ## Usage 17 | 18 | ```js 19 | const c = require('{%= name %}'); 20 | 21 | console.log(c.red('This is a red string!')); 22 | console.log(c.green('This is a red string!')); 23 | console.log(c.cyan('This is a cyan string!')); 24 | console.log(c.yellow('This is a yellow string!')); 25 | ``` 26 | 27 | ![image](https://user-images.githubusercontent.com/383994/39653848-a38e67da-4fc0-11e8-89ae-98c65ebe9dcf.png) 28 | 29 | 30 | ## Chained colors 31 | 32 | ```js 33 | console.log(c.bold.red('this is a bold red message')); 34 | console.log(c.bold.yellow.italic('this is a bold yellow italicized message')); 35 | console.log(c.green.bold.underline('this is a bold green underlined message')); 36 | ``` 37 | 38 | ![image](https://user-images.githubusercontent.com/383994/39635780-7617246a-4f8c-11e8-89e9-05216cc54e38.png) 39 | 40 | 41 | ## Nested colors 42 | 43 | ```js 44 | console.log(c.yellow(`foo ${c.red.bold('red')} bar ${c.cyan('cyan')} baz`)); 45 | ``` 46 | 47 | ![image](https://user-images.githubusercontent.com/383994/39635817-8ed93d44-4f8c-11e8-8afd-8c3ea35f5fbe.png) 48 | 49 | ### Nested styling bug 50 | 51 | `ansi-colors` does not have the nested styling bug found in [colorette][], [chalk][], and [kleur][]. 52 | 53 | ```js 54 | const { bold, red } = require('ansi-styles'); 55 | console.log(bold(`foo ${red.dim('bar')} baz`)); 56 | 57 | const colorette = require('colorette'); 58 | console.log(colorette.bold(`foo ${colorette.red(colorette.dim('bar'))} baz`)); 59 | 60 | const kleur = require('kleur'); 61 | console.log(kleur.bold(`foo ${kleur.red.dim('bar')} baz`)); 62 | 63 | const chalk = require('chalk'); 64 | console.log(chalk.bold(`foo ${chalk.red.dim('bar')} baz`)); 65 | ``` 66 | 67 | **Results in the following** 68 | 69 | (sans icons and labels) 70 | 71 | ![image](https://user-images.githubusercontent.com/383994/47280326-d2ee0580-d5a3-11e8-9611-ea6010f0a253.png) 72 | 73 | 74 | ## Toggle color support 75 | 76 | Easily enable/disable colors. 77 | 78 | ```js 79 | const c = require('ansi-colors'); 80 | 81 | // disable colors manually 82 | c.enabled = false; 83 | 84 | // or use a library to automatically detect support 85 | c.enabled = require('color-support').hasBasic; 86 | 87 | console.log(c.red('I will only be colored red if the terminal supports colors')); 88 | ``` 89 | 90 | ## Strip ANSI codes 91 | 92 | Use the `.unstyle` method to strip ANSI codes from a string. 93 | 94 | ```js 95 | console.log(c.unstyle(c.blue.bold('foo bar baz'))); 96 | //=> 'foo bar baz' 97 | ``` 98 | 99 | ## Available styles 100 | 101 | **Note** that bright and bright-background colors are not always supported. 102 | 103 | | Colors | Background Colors | Bright Colors | Bright Background Colors | 104 | | ------- | ----------------- | ------------- | ------------------------ | 105 | | black | bgBlack | blackBright | bgBlackBright | 106 | | red | bgRed | redBright | bgRedBright | 107 | | green | bgGreen | greenBright | bgGreenBright | 108 | | yellow | bgYellow | yellowBright | bgYellowBright | 109 | | blue | bgBlue | blueBright | bgBlueBright | 110 | | magenta | bgMagenta | magentaBright | bgMagentaBright | 111 | | cyan | bgCyan | cyanBright | bgCyanBright | 112 | | white | bgWhite | whiteBright | bgWhiteBright | 113 | | gray | | | | 114 | | grey | | | | 115 | 116 | _(`gray` is the U.S. spelling, `grey` is more commonly used in the Canada and U.K.)_ 117 | 118 | 119 | ### Style modifiers 120 | 121 | - dim 122 | - **bold** 123 | - hidden 124 | - _italic_ 125 | - underline 126 | - inverse 127 | - ~~strikethrough~~ 128 | - reset 129 | 130 | ## Aliases 131 | 132 | Create custom aliases for styles. 133 | 134 | ```js 135 | const colors = require('ansi-colors'); 136 | 137 | colors.alias('primary', colors.yellow); 138 | colors.alias('secondary', colors.bold); 139 | 140 | console.log(colors.primary.secondary('Foo')); 141 | ``` 142 | 143 | 144 | ## Themes 145 | 146 | A theme is an object of custom aliases. 147 | 148 | ```js 149 | const colors = require('ansi-colors'); 150 | 151 | colors.theme({ 152 | danger: colors.red, 153 | dark: colors.dim.gray, 154 | disabled: colors.gray, 155 | em: colors.italic, 156 | heading: colors.bold.underline, 157 | info: colors.cyan, 158 | muted: colors.dim, 159 | primary: colors.blue, 160 | strong: colors.bold, 161 | success: colors.green, 162 | underline: colors.underline, 163 | warning: colors.yellow 164 | }); 165 | 166 | // Now, we can use our custom styles alongside the built-in styles! 167 | console.log(colors.danger.strong.em('Error!')); 168 | console.log(colors.warning('Heads up!')); 169 | console.log(colors.info('Did you know...')); 170 | console.log(colors.success.bold('It worked!')); 171 | ``` 172 | 173 | ## Performance 174 | 175 | **Libraries tested** 176 | 177 | - ansi-colors v3.0.4 178 | - chalk v2.4.1 179 | 180 | 181 | ### Mac 182 | 183 | > MacBook Pro, Intel Core i7, 2.3 GHz, 16 GB. 184 | 185 | **Load time** 186 | 187 | Time it takes to load the first time `require()` is called: 188 | 189 | - ansi-colors - `1.915ms` 190 | - chalk - `12.437ms` 191 | 192 | **Benchmarks** 193 | 194 | ``` 195 | # All Colors 196 | ansi-colors x 173,851 ops/sec ±0.42% (91 runs sampled) 197 | chalk x 9,944 ops/sec ±2.53% (81 runs sampled))) 198 | 199 | # Chained colors 200 | ansi-colors x 20,791 ops/sec ±0.60% (88 runs sampled) 201 | chalk x 2,111 ops/sec ±2.34% (83 runs sampled) 202 | 203 | # Nested colors 204 | ansi-colors x 59,304 ops/sec ±0.98% (92 runs sampled) 205 | chalk x 4,590 ops/sec ±2.08% (82 runs sampled) 206 | ``` 207 | 208 | ### Windows 209 | 210 | > Windows 10, Intel Core i7-7700k CPU @ 4.2 GHz, 32 GB 211 | 212 | **Load time** 213 | 214 | Time it takes to load the first time `require()` is called: 215 | 216 | - ansi-colors - `1.494ms` 217 | - chalk - `11.523ms` 218 | 219 | **Benchmarks** 220 | 221 | ``` 222 | # All Colors 223 | ansi-colors x 193,088 ops/sec ±0.51% (95 runs sampled)) 224 | chalk x 9,612 ops/sec ±3.31% (77 runs sampled))) 225 | 226 | # Chained colors 227 | ansi-colors x 26,093 ops/sec ±1.13% (94 runs sampled) 228 | chalk x 2,267 ops/sec ±2.88% (80 runs sampled)) 229 | 230 | # Nested colors 231 | ansi-colors x 67,747 ops/sec ±0.49% (93 runs sampled) 232 | chalk x 4,446 ops/sec ±3.01% (82 runs sampled)) 233 | ``` -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015-present, Brian Woodward. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ansi-colors [![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=W8YFZ425KND68) [![NPM version](https://img.shields.io/npm/v/ansi-colors.svg?style=flat)](https://www.npmjs.com/package/ansi-colors) [![NPM monthly downloads](https://img.shields.io/npm/dm/ansi-colors.svg?style=flat)](https://npmjs.org/package/ansi-colors) [![NPM total downloads](https://img.shields.io/npm/dt/ansi-colors.svg?style=flat)](https://npmjs.org/package/ansi-colors) [![Linux Build Status](https://img.shields.io/travis/doowb/ansi-colors.svg?style=flat&label=Travis)](https://travis-ci.org/doowb/ansi-colors) 2 | 3 | > Easily add ANSI colors to your text and symbols in the terminal. A faster drop-in replacement for chalk, kleur and turbocolor (without the dependencies and rendering bugs). 4 | 5 | Please consider following this project's author, [Brian Woodward](https://github.com/doowb), and consider starring the project to show your :heart: and support. 6 | 7 | ## Install 8 | 9 | Install with [npm](https://www.npmjs.com/): 10 | 11 | ```sh 12 | $ npm install --save ansi-colors 13 | ``` 14 | 15 | ![image](https://user-images.githubusercontent.com/383994/39635445-8a98a3a6-4f8b-11e8-89c1-068c45d4fff8.png) 16 | 17 | ## Why use this? 18 | 19 | ansi-colors is _the fastest Node.js library for terminal styling_. A more performant drop-in replacement for chalk, with no dependencies. 20 | 21 | * _Blazing fast_ - Fastest terminal styling library in node.js, 10-20x faster than chalk! 22 | 23 | * _Drop-in replacement_ for [chalk](https://github.com/chalk/chalk). 24 | * _No dependencies_ (Chalk has 7 dependencies in its tree!) 25 | 26 | * _Safe_ - Does not modify the `String.prototype` like [colors](https://github.com/Marak/colors.js). 27 | * Supports [nested colors](#nested-colors), **and does not have the [nested styling bug](#nested-styling-bug) that is present in [colorette](https://github.com/jorgebucaran/colorette), [chalk](https://github.com/chalk/chalk), and [kleur](https://github.com/lukeed/kleur)**. 28 | * Supports [chained colors](#chained-colors). 29 | * [Toggle color support](#toggle-color-support) on or off. 30 | 31 | ## Usage 32 | 33 | ```js 34 | const c = require('ansi-colors'); 35 | 36 | console.log(c.red('This is a red string!')); 37 | console.log(c.green('This is a red string!')); 38 | console.log(c.cyan('This is a cyan string!')); 39 | console.log(c.yellow('This is a yellow string!')); 40 | ``` 41 | 42 | ![image](https://user-images.githubusercontent.com/383994/39653848-a38e67da-4fc0-11e8-89ae-98c65ebe9dcf.png) 43 | 44 | ## Chained colors 45 | 46 | ```js 47 | console.log(c.bold.red('this is a bold red message')); 48 | console.log(c.bold.yellow.italic('this is a bold yellow italicized message')); 49 | console.log(c.green.bold.underline('this is a bold green underlined message')); 50 | ``` 51 | 52 | ![image](https://user-images.githubusercontent.com/383994/39635780-7617246a-4f8c-11e8-89e9-05216cc54e38.png) 53 | 54 | ## Nested colors 55 | 56 | ```js 57 | console.log(c.yellow(`foo ${c.red.bold('red')} bar ${c.cyan('cyan')} baz`)); 58 | ``` 59 | 60 | ![image](https://user-images.githubusercontent.com/383994/39635817-8ed93d44-4f8c-11e8-8afd-8c3ea35f5fbe.png) 61 | 62 | ### Nested styling bug 63 | 64 | `ansi-colors` does not have the nested styling bug found in [colorette](https://github.com/jorgebucaran/colorette), [chalk](https://github.com/chalk/chalk), and [kleur](https://github.com/lukeed/kleur). 65 | 66 | ```js 67 | const { bold, red } = require('ansi-styles'); 68 | console.log(bold(`foo ${red.dim('bar')} baz`)); 69 | 70 | const colorette = require('colorette'); 71 | console.log(colorette.bold(`foo ${colorette.red(colorette.dim('bar'))} baz`)); 72 | 73 | const kleur = require('kleur'); 74 | console.log(kleur.bold(`foo ${kleur.red.dim('bar')} baz`)); 75 | 76 | const chalk = require('chalk'); 77 | console.log(chalk.bold(`foo ${chalk.red.dim('bar')} baz`)); 78 | ``` 79 | 80 | **Results in the following** 81 | 82 | (sans icons and labels) 83 | 84 | ![image](https://user-images.githubusercontent.com/383994/47280326-d2ee0580-d5a3-11e8-9611-ea6010f0a253.png) 85 | 86 | ## Toggle color support 87 | 88 | Easily enable/disable colors. 89 | 90 | ```js 91 | const c = require('ansi-colors'); 92 | 93 | // disable colors manually 94 | c.enabled = false; 95 | 96 | // or use a library to automatically detect support 97 | c.enabled = require('color-support').hasBasic; 98 | 99 | console.log(c.red('I will only be colored red if the terminal supports colors')); 100 | ``` 101 | 102 | ## Strip ANSI codes 103 | 104 | Use the `.unstyle` method to strip ANSI codes from a string. 105 | 106 | ```js 107 | console.log(c.unstyle(c.blue.bold('foo bar baz'))); 108 | //=> 'foo bar baz' 109 | ``` 110 | 111 | ## Available styles 112 | 113 | **Note** that bright and bright-background colors are not always supported. 114 | 115 | | Colors | Background Colors | Bright Colors | Bright Background Colors | 116 | | ------- | ----------------- | ------------- | ------------------------ | 117 | | black | bgBlack | blackBright | bgBlackBright | 118 | | red | bgRed | redBright | bgRedBright | 119 | | green | bgGreen | greenBright | bgGreenBright | 120 | | yellow | bgYellow | yellowBright | bgYellowBright | 121 | | blue | bgBlue | blueBright | bgBlueBright | 122 | | magenta | bgMagenta | magentaBright | bgMagentaBright | 123 | | cyan | bgCyan | cyanBright | bgCyanBright | 124 | | white | bgWhite | whiteBright | bgWhiteBright | 125 | | gray | | | | 126 | | grey | | | | 127 | 128 | _(`gray` is the U.S. spelling, `grey` is more commonly used in the Canada and U.K.)_ 129 | 130 | ### Style modifiers 131 | 132 | * dim 133 | * **bold** 134 | 135 | * hidden 136 | * _italic_ 137 | 138 | * underline 139 | * inverse 140 | * ~~strikethrough~~ 141 | 142 | * reset 143 | 144 | ## Aliases 145 | 146 | Create custom aliases for styles. 147 | 148 | ```js 149 | const colors = require('ansi-colors'); 150 | 151 | colors.alias('primary', colors.yellow); 152 | colors.alias('secondary', colors.bold); 153 | 154 | console.log(colors.primary.secondary('Foo')); 155 | ``` 156 | 157 | ## Themes 158 | 159 | A theme is an object of custom aliases. 160 | 161 | ```js 162 | const colors = require('ansi-colors'); 163 | 164 | colors.theme({ 165 | danger: colors.red, 166 | dark: colors.dim.gray, 167 | disabled: colors.gray, 168 | em: colors.italic, 169 | heading: colors.bold.underline, 170 | info: colors.cyan, 171 | muted: colors.dim, 172 | primary: colors.blue, 173 | strong: colors.bold, 174 | success: colors.green, 175 | underline: colors.underline, 176 | warning: colors.yellow 177 | }); 178 | 179 | // Now, we can use our custom styles alongside the built-in styles! 180 | console.log(colors.danger.strong.em('Error!')); 181 | console.log(colors.warning('Heads up!')); 182 | console.log(colors.info('Did you know...')); 183 | console.log(colors.success.bold('It worked!')); 184 | ``` 185 | 186 | ## Performance 187 | 188 | **Libraries tested** 189 | 190 | * ansi-colors v3.0.4 191 | * chalk v2.4.1 192 | 193 | ### Mac 194 | 195 | > MacBook Pro, Intel Core i7, 2.3 GHz, 16 GB. 196 | 197 | **Load time** 198 | 199 | Time it takes to load the first time `require()` is called: 200 | 201 | * ansi-colors - `1.915ms` 202 | * chalk - `12.437ms` 203 | 204 | **Benchmarks** 205 | 206 | ``` 207 | # All Colors 208 | ansi-colors x 173,851 ops/sec ±0.42% (91 runs sampled) 209 | chalk x 9,944 ops/sec ±2.53% (81 runs sampled))) 210 | 211 | # Chained colors 212 | ansi-colors x 20,791 ops/sec ±0.60% (88 runs sampled) 213 | chalk x 2,111 ops/sec ±2.34% (83 runs sampled) 214 | 215 | # Nested colors 216 | ansi-colors x 59,304 ops/sec ±0.98% (92 runs sampled) 217 | chalk x 4,590 ops/sec ±2.08% (82 runs sampled) 218 | ``` 219 | 220 | ### Windows 221 | 222 | > Windows 10, Intel Core i7-7700k CPU @ 4.2 GHz, 32 GB 223 | 224 | **Load time** 225 | 226 | Time it takes to load the first time `require()` is called: 227 | 228 | * ansi-colors - `1.494ms` 229 | * chalk - `11.523ms` 230 | 231 | **Benchmarks** 232 | 233 | ``` 234 | # All Colors 235 | ansi-colors x 193,088 ops/sec ±0.51% (95 runs sampled)) 236 | chalk x 9,612 ops/sec ±3.31% (77 runs sampled))) 237 | 238 | # Chained colors 239 | ansi-colors x 26,093 ops/sec ±1.13% (94 runs sampled) 240 | chalk x 2,267 ops/sec ±2.88% (80 runs sampled)) 241 | 242 | # Nested colors 243 | ansi-colors x 67,747 ops/sec ±0.49% (93 runs sampled) 244 | chalk x 4,446 ops/sec ±3.01% (82 runs sampled)) 245 | ``` 246 | 247 | ## About 248 | 249 |
250 | Contributing 251 | 252 | Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new). 253 | 254 |
255 | 256 |
257 | Running Tests 258 | 259 | Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command: 260 | 261 | ```sh 262 | $ npm install && npm test 263 | ``` 264 | 265 |
266 | 267 |
268 | Building docs 269 | 270 | _(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_ 271 | 272 | To generate the readme, run the following command: 273 | 274 | ```sh 275 | $ npm install -g verbose/verb#dev verb-generate-readme && verb 276 | ``` 277 | 278 |
279 | 280 | ### Related projects 281 | 282 | You might also be interested in these projects: 283 | 284 | * [ansi-wrap](https://www.npmjs.com/package/ansi-wrap): Create ansi colors by passing the open and close codes. | [homepage](https://github.com/jonschlinkert/ansi-wrap "Create ansi colors by passing the open and close codes.") 285 | * [strip-color](https://www.npmjs.com/package/strip-color): Strip ANSI color codes from a string. No dependencies. | [homepage](https://github.com/jonschlinkert/strip-color "Strip ANSI color codes from a string. No dependencies.") 286 | 287 | ### Contributors 288 | 289 | | **Commits** | **Contributor** | 290 | | --- | --- | 291 | | 48 | [jonschlinkert](https://github.com/jonschlinkert) | 292 | | 42 | [doowb](https://github.com/doowb) | 293 | | 6 | [lukeed](https://github.com/lukeed) | 294 | | 2 | [Silic0nS0ldier](https://github.com/Silic0nS0ldier) | 295 | | 1 | [dwieeb](https://github.com/dwieeb) | 296 | | 1 | [jorgebucaran](https://github.com/jorgebucaran) | 297 | | 1 | [madhavarshney](https://github.com/madhavarshney) | 298 | | 1 | [chapterjason](https://github.com/chapterjason) | 299 | 300 | ### Author 301 | 302 | **Brian Woodward** 303 | 304 | * [GitHub Profile](https://github.com/doowb) 305 | * [Twitter Profile](https://twitter.com/doowb) 306 | * [LinkedIn Profile](https://linkedin.com/in/woodwardbrian) 307 | 308 | ### License 309 | 310 | Copyright © 2019, [Brian Woodward](https://github.com/doowb). 311 | Released under the [MIT License](LICENSE). 312 | 313 | *** 314 | 315 | _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.8.0, on July 01, 2019._ -------------------------------------------------------------------------------- /bench/.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /bench/colors.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const { Suite } = require('benchmark'); 4 | const chalk = require('chalk'); 5 | const colors = require('..'); 6 | const names = [ 7 | 'reset', 8 | 'bold', 9 | 'dim', 10 | 'italic', 11 | 'underline', 12 | 'inverse', 13 | 'hidden', 14 | 'strikethrough', 15 | 'black', 16 | 'red', 17 | 'green', 18 | 'yellow', 19 | 'blue', 20 | 'magenta', 21 | 'cyan', 22 | 'white', 23 | 'gray', 24 | 'bgBlack', 25 | 'bgRed', 26 | 'bgGreen', 27 | 'bgYellow', 28 | 'bgBlue', 29 | 'bgMagenta', 30 | 'bgCyan' 31 | ]; 32 | 33 | const cycle = (e, newline) => { 34 | process.stdout.write('\u001b[G'); 35 | process.stdout.write(` ${e.target}` + (newline ? '\n' : '')); 36 | }; 37 | 38 | function bench(name) { 39 | console.log(`\n# ${name}`); 40 | const suite = new Suite(); 41 | const res = { 42 | run: suite.run.bind(suite), 43 | add: (key, fn) => { 44 | suite.add(key, { 45 | onCycle: e => cycle(e), 46 | onComplete: e => cycle(e, true), 47 | fn: fn 48 | }); 49 | return res; 50 | } 51 | }; 52 | return res; 53 | } 54 | 55 | bench('All Colors') 56 | .add('ansi-colors', () => { 57 | names.forEach(name => colors[name]('foo')); 58 | }) 59 | .add('chalk', () => { 60 | names.forEach(name => chalk[name]('foo')); 61 | }) 62 | .run(); 63 | 64 | bench('Chained colors') 65 | .add('ansi-colors', () => { 66 | names.forEach(name => colors[name].bold.underline.italic('foo')); 67 | }) 68 | .add('chalk', () => { 69 | names.forEach(name => chalk[name].bold.underline.italic('foo')); 70 | }) 71 | .run(); 72 | 73 | bench('Nested colors') 74 | .add('ansi-colors', () => fixture(colors)) 75 | .add('chalk', () => fixture(chalk)) 76 | .run(); 77 | 78 | function fixture(lib) { 79 | return lib.red(`a red ${lib.white('red')} red ${lib.red('red')} red ${lib.gray('red')} red ${lib.red('red')} red ${lib.red('red')} red ${lib.red('red')} red ${lib.red('red')} red ${lib.red('red')} red ${lib.blue('red')} red ${lib.red('red')} red ${lib.red('red')} red ${lib.red('red')} red ${lib.red('red')}red ${lib.red('red')} red ${lib.red('red')} red ${lib.red('red')} red ${lib.red('red')} red ${lib.red('red')} red ${lib.red('red')} red ${lib.red('red')} red ${lib.red('red')} red ${lib.red('red')} red ${lib.red('red')} red ${lib.red('red')} red ${lib.red('red')} red ${lib.red('red')}red ${lib.green('red')} red ${lib.red('red')} red ${lib.red('red')} red ${lib.red('red')} red ${lib.red('red')} red ${lib.red('red')} red ${lib.red('red')} red ${lib.red('red')} red ${lib.red('red')} red ${lib.red('red')} red ${lib.red('red')} red ${lib.magenta('red')} red ${lib.red('red')}red ${lib.red('red')} red ${lib.cyan('red')} red ${lib.red('red')} red ${lib.red('red')} red ${lib.yellow('red')} red ${lib.red('red')} red ${lib.red('red')} red ${lib.red('red')} red ${lib.red('red')} red ${lib.red('red')} red ${lib.red('red')} red ${lib.red('red')} message`); 80 | } 81 | -------------------------------------------------------------------------------- /bench/dryrun.js: -------------------------------------------------------------------------------- 1 | const lib = process.argv[2] || 'colors'; 2 | const libs = { 3 | chalk: require('chalk'), 4 | colors: require('..') 5 | }; 6 | const color = libs[lib]; 7 | 8 | console.log('bgBlack:', color.bgBlack('some string')); 9 | console.log('bgBlue:', color.bgBlue('some string')); 10 | console.log('bgCyan:', color.bgCyan('some string')); 11 | console.log('bgGreen:', color.bgGreen('some string')); 12 | console.log('bgMagenta:', color.bgMagenta('some string')); 13 | console.log('bgRed:', color.bgRed('some string')); 14 | console.log('bgWhite:', color.bgWhite('some string')); 15 | console.log('bgYellow:', color.bgYellow('some string')); 16 | console.log('black:', color.black('some string')); 17 | console.log('blue:', color.blue('some string')); 18 | console.log('bold:', color.bold('some string')); 19 | console.log('cyan:', color.cyan('some string')); 20 | console.log('dim:', color.dim('some string')); 21 | console.log('gray:', color.gray('some string')); 22 | console.log('green:', color.green('some string')); 23 | console.log('hidden:', color.hidden('some string')); 24 | console.log('inverse:', color.inverse('some string')); 25 | console.log('italic:', color.italic('some string')); 26 | console.log('magenta:', color.magenta('some string')); 27 | console.log('red:', color.red('some string')); 28 | console.log('reset:', color.reset('some string')); 29 | console.log('strikethrough:', color.strikethrough('some string')); 30 | console.log('underline:', color.underline('some string')); 31 | console.log('white:', color.white('some string')); 32 | console.log('yellow:', color.yellow('some string')); 33 | 34 | console.log(); 35 | console.log(color.bold(color.cyan('[info]')), color.cyan('This is some information')); 36 | console.log(color.bold(color.yellow('[warning]')), color.yellow('This is a warning')); 37 | console.log(color.bold(color.red('[ERROR]')), color.red('Danger! There was an error!')); 38 | console.log(); 39 | 40 | console.log(color.red(`a red ${color.white('white')} red ${color.red('red')} red ${color.gray('gray')} red ${color.red('red')} red ${color.red('red')} red ${color.red('red')} red ${color.red('red')} red ${color.red('red')} red ${color.blue('blue')} red ${color.red('red')} red ${color.red('red')} red ${color.red('red')} red ${color.red('red')}red ${color.red('red')} red ${color.red('red')} red ${color.red('red')} red ${color.red('red')} red ${color.red('red')} red ${color.red('red')} red ${color.red('red')} red ${color.red('red')} red ${color.red('red')} red ${color.red('red')} red ${color.red('red')} red ${color.red('red')} red ${color.red('red')}red ${color.green('green')} red ${color.red('red')} red ${color.red('red')} red ${color.red('red')} red ${color.red('red')} red ${color.red('red')} red ${color.red('red')} red ${color.red('red')} red ${color.red('red')} red ${color.red('red')} red ${color.red('red')} red ${color.magenta('red')} red ${color.red('red')}red ${color.red('red')} red ${color.cyan('cyan')} red ${color.red('red')} red ${color.red('red')} red ${color.yellow('yellow')} red ${color.red('red')} red ${color.red('red')} red ${color.red('red')} red ${color.blue('blue')} red ${color.red('red')} red ${color.red('red')} red ${color.red('red')} red ${color.red('red')} message`).toString()); 41 | -------------------------------------------------------------------------------- /bench/index.js: -------------------------------------------------------------------------------- 1 | module.exports = require('./colors'); 2 | -------------------------------------------------------------------------------- /bench/load-ansi-colors.js: -------------------------------------------------------------------------------- 1 | console.time('ansi-colors'); 2 | require('..'); 3 | console.timeEnd('ansi-colors'); 4 | -------------------------------------------------------------------------------- /bench/load-chalk.js: -------------------------------------------------------------------------------- 1 | console.time('chalk'); 2 | require('chalk'); 3 | console.timeEnd('chalk'); 4 | -------------------------------------------------------------------------------- /bench/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "benchmarks", 3 | "private": true, 4 | "dependencies": { 5 | "benchmark": "^2.1.4", 6 | "chalk": "^2.4.0", 7 | "clorox": "^2.2.0" 8 | }, 9 | "lintDeps": { 10 | "dependencies": { 11 | "files": { 12 | "patterns": [ 13 | "*.js" 14 | ] 15 | } 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /examples/alias.js: -------------------------------------------------------------------------------- 1 | const colors = require('..'); 2 | 3 | colors.alias('primary', colors.yellow); 4 | colors.alias('strong', colors.bold); 5 | console.log(colors.primary.strong('Foo')); 6 | -------------------------------------------------------------------------------- /examples/chained.js: -------------------------------------------------------------------------------- 1 | 2 | const util = require('util'); 3 | console.time('time'); 4 | const colors = require('..'); 5 | colors.success = (...args) => colors.bold.green(util.format(...args)); 6 | colors.ok = (...args) => `${colors.success('✔')} ${colors.bold(util.format(...args))}`; 7 | console.log(colors.yellow('this is a message')) 8 | console.log(colors.bold.underline.red('foo')); 9 | console.log(colors.yellow.bold.underline.italic('foo')); 10 | console.log(colors.success('Hi, %s!', 'Brian')); 11 | console.log(colors.ok('File "%s" was written to "/%s"', 'foo', 'bar')); 12 | console.timeEnd('time'); 13 | -------------------------------------------------------------------------------- /examples/colors.js: -------------------------------------------------------------------------------- 1 | 2 | const colors = require('..'); 3 | console.log(colors.unstyle(colors.green('this is not green!'))); 4 | 5 | const cyan = colors.cyan; 6 | const key = cyan.underline('a'); 7 | console.log(cyan('foo')); 8 | -------------------------------------------------------------------------------- /examples/nested.js: -------------------------------------------------------------------------------- 1 | console.time('grand total'); 2 | console.time('module loaded'); 3 | const colors = require('..'); 4 | // const colors = require('clorox'); 5 | // const colors = require('chalk'); 6 | console.timeEnd('module loaded'); 7 | 8 | colors.enabled = true; 9 | console.time('colors total'); 10 | console.time('diff'); 11 | console.log(colors.red('a red message')); 12 | console.timeEnd('diff'); 13 | console.time('diff'); 14 | console.log(colors.red('a red message')); 15 | console.timeEnd('diff'); 16 | console.time('diff'); 17 | console.log(colors.red('a red message')); 18 | console.timeEnd('diff'); 19 | console.time('diff'); 20 | 21 | console.log(colors.red('a red message')); 22 | console.timeEnd('diff'); 23 | console.time('diff'); 24 | console.log(colors.red('a red message')); 25 | console.timeEnd('diff'); 26 | console.time('diff'); 27 | console.log(colors.red('a red message')); 28 | console.timeEnd('diff'); 29 | console.time('diff'); 30 | console.log(colors.red('a red message')); 31 | console.timeEnd('diff'); 32 | console.time('diff'); 33 | console.log(colors.red('a red message')); 34 | console.timeEnd('diff'); 35 | console.time('diff'); 36 | console.log(colors.red('a red message')); 37 | console.timeEnd('diff'); 38 | console.time('diff'); 39 | console.log(colors.red('a red message')); 40 | console.timeEnd('diff'); 41 | console.time('diff'); 42 | console.log(colors.red('a red message')); 43 | console.timeEnd('diff'); 44 | console.time('diff'); 45 | console.log(colors.red('a red message')); 46 | console.timeEnd('diff'); 47 | console.time('diff'); 48 | console.log(colors.red.bold('a red bold message')); 49 | console.timeEnd('diff'); 50 | console.time('diff'); 51 | console.log(colors.cyan.bold('a cyan bold message')); 52 | console.timeEnd('diff'); 53 | console.time('diff'); 54 | console.log(colors.blue.bold('a blue bold message')); 55 | console.timeEnd('diff'); 56 | console.time('diff'); 57 | console.log(colors.bold.green('a bold green message')); 58 | console.timeEnd('diff'); 59 | console.time('diff'); 60 | console.log(colors.red(`a red ${colors.blue('and blue')} message`)); 61 | console.timeEnd('diff'); 62 | console.time('diff'); 63 | console.log(colors.red(`a red ${colors.bold.yellow('and bold yellow')} message`)); 64 | console.timeEnd('diff'); 65 | console.time('diff'); 66 | console.log(colors.bold.green(`a bold green ${colors.bold.yellow('and bold yellow')} message`)); 67 | console.timeEnd('diff'); 68 | console.time('diff'); 69 | console.log(colors.red(`a red ${colors.white('white')} red ${colors.red('red')} red ${colors.gray('gray')} red ${colors.red('red')} red ${colors.red('red')} red ${colors.red('red')} red ${colors.red('red')} red ${colors.red('red')} red ${colors.blue('blue')} red ${colors.red('red')} red ${colors.red('red')} red ${colors.red('red')} red ${colors.red('red')}red ${colors.red('red')} red ${colors.red('red')} red ${colors.red('red')} red ${colors.red('red')} red ${colors.red('red')} red ${colors.red('red')} red ${colors.red('red')} red ${colors.red('red')} red ${colors.red('red')} red ${colors.red('red')} red ${colors.red('red')} red ${colors.red('red')} red ${colors.red('red')}red ${colors.cyan('cyan')} red ${colors.red('red')} red ${colors.red('red')} red ${colors.red('red')} red ${colors.red('red')} red ${colors.red('red')} red ${colors.red('red')} red ${colors.red('red')} red ${colors.red('red')} red ${colors.red('red')} red ${colors.red('red')} red ${colors.red('red')} red ${colors.red('red')}red ${colors.red('red')} red ${colors.yellow('yellow')} red ${colors.red('red')} red ${colors.red('red')} red ${colors.red('red')} red ${colors.red('red')} red ${colors.red('red')} red ${colors.red('red')} red ${colors.green('green')} red ${colors.red('red')} red ${colors.red('red')} red ${colors.red('red')} red ${colors.red('red')} message`)); 70 | console.timeEnd('diff'); 71 | console.timeEnd('colors total'); 72 | console.timeEnd('grand total'); 73 | 74 | console.log(colors.yellow(`foo ${colors.red.bold('red')} bar ${colors.cyan('cyan')} baz`)); 75 | -------------------------------------------------------------------------------- /examples/nesting-bug.js: -------------------------------------------------------------------------------- 1 | console.log(); 2 | 3 | const { bold, cyan, gray, green, red, symbols } = require('./'); 4 | const good = green(symbols.check); 5 | const bad = red(symbols.cross); 6 | 7 | console.log(bold(`foo ${cyan.dim('bar')} baz`), good, gray('(ansi-colors)')); 8 | 9 | const colorette = require('colorette'); 10 | console.log(colorette.bold(`foo ${colorette.cyan(colorette.dim('bar'))} baz`), bad, gray('(colorette)')); 11 | 12 | const kleur = require('kleur'); 13 | console.log(kleur.bold(`foo ${kleur.cyan.dim('bar')} baz`), bad, gray('(kleur)')); 14 | 15 | const chalk = require('chalk'); 16 | console.log(chalk.bold(`foo ${chalk.cyan.dim('bar')} baz`), bad, gray('(chalk)')); 17 | 18 | console.log(); 19 | -------------------------------------------------------------------------------- /examples/ok.js: -------------------------------------------------------------------------------- 1 | 2 | const log = require('..'); 3 | log.ok = msg => `${log.green(log.symbols.check)} ${log.bold(msg)}`; 4 | console.log(log.ok('done!')); 5 | -------------------------------------------------------------------------------- /examples/readme-examples.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | const util = require('util'); 3 | const c = require('..'); 4 | console.log(c); 5 | 6 | console.log(); 7 | console.log('nested colors'); 8 | console.log(); 9 | console.log(c.yellow(`foo ${c.red.bold('red')} bar ${c.cyan('cyan')} baz`)); 10 | console.log(c.yellow('foo', c.red.bold('red'), 'bar', c.cyan('cyan'), 'baz')); 11 | console.log(); 12 | console.log(); 13 | console.log('chained colors'); 14 | console.log(); 15 | console.log(c.bold.red('this is a bold red message')); 16 | console.log(c.bold.italic('this is a bold italicized message')); 17 | console.log(c.bold.yellow.italic('this is a bold yellow italicized message')); 18 | console.log(c.green.bold.underline('this is a bold green underlined message')); 19 | console.log(); 20 | console.log(); 21 | console.log('printf-like formatting'); 22 | console.log(); 23 | console.log(c.bold.red(util.format('%s:%s', 'foo', 'bar', 'baz'))); 24 | console.log(); 25 | console.log(); 26 | console.log(c.bold.bold(util.format('%s:%s:%s', 'foo', c.red('bar'), 'baz'))); 27 | console.log(); 28 | console.log(); 29 | console.log('features'); 30 | console.log(); 31 | console.log(c.red('Some', 'red', 'text', 'to', 'display')); 32 | console.log(); 33 | console.log(); 34 | console.log(c.red(' This is a red string!')); 35 | console.log(c.green(' This is a red string!')); 36 | console.log(c.cyan(' This is a cyan string!')); 37 | console.log(c.yellow(' This is a yellow string!')); 38 | console.log(); 39 | console.log(); 40 | -------------------------------------------------------------------------------- /examples/screenshot-table.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const table = require('text-table'); 4 | const colors = require('..'); 5 | const styles = colors.styles; 6 | let arr = [[]]; 7 | let idx = 0; 8 | 9 | /** 10 | * this is inspired by and modified from the 11 | * screenshot code and concept from chalk 12 | */ 13 | 14 | for (const key of Object.keys(styles)) { 15 | let prop = key; 16 | 17 | if (key === 'reset' || key === 'hidden' || key === 'grey' || key === 'verbose') { 18 | continue; 19 | } 20 | if (/bright/i.test(key)) { 21 | continue; 22 | } 23 | 24 | if (/^bg[^B]/.test(key)) { 25 | prop = colors.black(prop); 26 | } 27 | 28 | arr[arr.length - 1].push(colors[key](prop)); 29 | 30 | if (idx++ >= 3) { 31 | arr.push([]); 32 | idx = 0; 33 | } 34 | } 35 | 36 | const stringLength = str => colors.unstyle(str).length; 37 | console.log(table(arr, { stringLength })) 38 | -------------------------------------------------------------------------------- /examples/screenshot.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const colors = require('..'); 4 | const justified = require('justified'); 5 | let str = ''; 6 | 7 | /** 8 | * this is inspired by and modified from the 9 | * screenshot code and concept from chalk 10 | */ 11 | 12 | for (const key of Object.keys(colors.styles)) { 13 | let res = key; 14 | 15 | if (key === 'reset' || key === 'hidden' || key === 'grey' || key === 'verbose') { 16 | continue; 17 | } 18 | 19 | if (/bright/i.test(key)) { 20 | continue; 21 | } 22 | 23 | if (/^bg[^B]/.test(key)) { 24 | res = colors.black(res); 25 | } 26 | 27 | str += colors[key](res) + ' '; 28 | } 29 | 30 | console.log(); 31 | console.log(); 32 | console.log(justified(str, { width: 60, format: colors.unstyle, indent: ' ' })); 33 | console.log(); 34 | console.log(); 35 | -------------------------------------------------------------------------------- /examples/styles.js: -------------------------------------------------------------------------------- 1 | 2 | const util = require('util'); 3 | const c = require('..'); 4 | c.info = (...args) => { 5 | return c.bold(c.cyan('[info] ')) + c.cyan(...args); 6 | }; 7 | 8 | console.log(c.info('This is some information')); 9 | console.log(c.bold(c.yellow('[warning]')), c.yellow('This is a warning')); 10 | console.error(c.bold(c.red('[ERROR]')), c.red('Danger! There was an error!')); 11 | 12 | console.log(c.yellow.bold.underline.italic('foo')); 13 | console.log(c.white('foo')); 14 | console.log(c.red(`a red ${c.bold.yellow('and bold yellow')} message`)); 15 | console.log(c.bold.green(`a bold green ${c.bold.yellow('and bold yellow')} message`)); 16 | console.log(c.yellow(`foo ${c.red.bold('red')} bar ${c.cyan('cyan')} baz`)); 17 | console.log(c.yellow('foo', c.red.bold('red'), 'bar', c.cyan('cyan'), 'baz')); 18 | console.log(c.bold.red('this is a bold red message')); 19 | console.log(c.bold.italic('this is a bold italicized message')); 20 | console.log(c.bold.yellow.italic('this is a bold yellow italicized message')); 21 | console.log(c.green.bold.underline('this is a bold green underlined message')); 22 | console.log(c.bold.red(util.format('%s:%s', 'foo', 'bar', 'baz'))); 23 | console.log(c.bold.bold(util.format('%s:%s:%s', 'foo', c.red('bar'), 'baz'))); 24 | console.log(c.red('Some', 'red', 'text', 'to', 'display')); 25 | 26 | // // console.log(colors); 27 | 28 | -------------------------------------------------------------------------------- /examples/theme-custom-fn.js: -------------------------------------------------------------------------------- 1 | const colors = require('..'); 2 | 3 | colors.theme({ 4 | success: 'blue', 5 | em: 'italic', 6 | strong: 'bold', 7 | s: 'strikethrough', 8 | u: 'underline', 9 | ok(message) { 10 | let { unstyle, success, symbols } = colors; 11 | let plain = unstyle(message); 12 | let match = plain.match(/^( +)(.*)$/); 13 | let prefix = match ? ' '.repeat(match[1].length) : ''; 14 | let rest = match ? match[2] : message; 15 | if (rest !== message) { 16 | rest = message.replace(plain, rest); 17 | } 18 | return prefix + success(symbols.check) + ' ' + rest; 19 | } 20 | }); 21 | 22 | const { ok, em, s, u } = colors; 23 | 24 | console.log(ok(' Success!')); 25 | console.log(ok.em.s(' Success!')); 26 | console.log(em.ok.s(' Success!')); 27 | console.log(em.ok.u(' Success!')); 28 | 29 | console.log(em.s.ok(' Success!')); 30 | console.log(s.ok.em(' Success!')); 31 | console.log(s.em.ok(' Success!')); 32 | console.log(u.em.ok(' Success!')); 33 | -------------------------------------------------------------------------------- /examples/theme.js: -------------------------------------------------------------------------------- 1 | const colors = require('..'); 2 | 3 | colors.theme({ 4 | primary: 'yellow', 5 | error: colors.red, 6 | strong: 'bold', 7 | em: 'underline' 8 | }); 9 | 10 | colors.theme({ 11 | secondary: 'primary', 12 | warning: 'error' 13 | }); 14 | 15 | const { primary, error, strong, red, bold, yellow, underline, secondary, warning } = colors; 16 | 17 | console.log(colors.primary.bold('Foo')); 18 | console.log(colors.error.strong.em('Foo')); 19 | console.log('---'); 20 | console.log(primary.bold('Foo')); 21 | console.log(error.strong.em('Foo')); 22 | console.log(strong.em('Foo')); 23 | console.log('---'); 24 | console.log(yellow.bold('Foo')); 25 | console.log(red.bold.underline('Foo')); 26 | console.log(bold.underline('Foo')); 27 | console.log('---'); 28 | console.log(secondary('Foo')); 29 | console.log(secondary.bold('Foo')); 30 | console.log(secondary.strong('Foo')); 31 | console.log(warning('Foo')); 32 | console.log(warning.underline('Foo')); 33 | 34 | console.log('---'); 35 | colors.alias('warning', colors.green); 36 | console.log(bold.warning('Foo')); 37 | console.log(colors.warning('Foo')); 38 | console.log('---'); 39 | 40 | colors.warning = colors.red; 41 | console.log(warning.bold('Foo')); 42 | console.log(bold.underline.warning('Foo')); 43 | console.log(colors.warning('Foo')); 44 | 45 | console.log('---'); 46 | colors.yellow = colors.blue; 47 | colors.bold = colors.dim; 48 | colors.dim = bold; 49 | console.log(yellow.bold('Foo')); 50 | console.log(colors.yellow.dim('Foo')); 51 | console.log(red.bold.underline('Foo')); 52 | console.log(bold.underline('Foo')); 53 | console.log('---'); 54 | -------------------------------------------------------------------------------- /examples/theme2.js: -------------------------------------------------------------------------------- 1 | const colors = require('..'); 2 | 3 | colors.theme({ 4 | danger: colors.red, 5 | dark: colors.dim.gray, 6 | disabled: colors.gray, 7 | em: colors.italic, 8 | heading: colors.bold.underline, 9 | info: colors.cyan, 10 | muted: colors.dim, 11 | primary: colors.blue, 12 | strong: colors.bold, 13 | success: colors.green, 14 | underline: colors.underline, 15 | warning: colors.yellow 16 | }); 17 | 18 | console.log(colors.danger.strong.em('Error!')); 19 | console.log(colors.warning('Heads up!')); 20 | console.log(colors.info('Did you know...')); 21 | -------------------------------------------------------------------------------- /examples/unstyle.js: -------------------------------------------------------------------------------- 1 | const colors = require('..'); 2 | 3 | console.log(colors.unstyle(colors.green('This should NOT be green!'))); 4 | console.log(colors.red(colors.green('This SHOULD be GREEN!'))); 5 | console.log(); 6 | console.log(colors.red.unstyle(colors.green('This SHOULD be RED!'))); 7 | console.log(colors.unstyle.red.bold(colors.green('This SHOULD be RED and BOLD!'))); 8 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const isObject = val => val !== null && typeof val === 'object' && !Array.isArray(val); 4 | 5 | /* eslint-disable no-control-regex */ 6 | // this is a modified version of https://github.com/chalk/ansi-regex (MIT License) 7 | const ANSI_REGEX = /[\u001b\u009b][[\]#;?()]*(?:(?:(?:[^\W_]*;?[^\W_]*)\u0007)|(?:(?:[0-9]{1,4}(;[0-9]{0,4})*)?[~0-9=<>cf-nqrtyA-PRZ]))/g; 8 | 9 | const hasColor = () => { 10 | if (typeof process !== 'undefined') { 11 | return process.env.FORCE_COLOR !== '0'; 12 | } 13 | return false; 14 | }; 15 | 16 | const create = () => { 17 | const colors = { 18 | enabled: hasColor(), 19 | visible: true, 20 | styles: {}, 21 | keys: {} 22 | }; 23 | 24 | const ansi = style => { 25 | let open = style.open = `\u001b[${style.codes[0]}m`; 26 | let close = style.close = `\u001b[${style.codes[1]}m`; 27 | let regex = style.regex = new RegExp(`\\u001b\\[${style.codes[1]}m`, 'g'); 28 | style.wrap = (input, newline) => { 29 | if (input.includes(close)) input = input.replace(regex, close + open); 30 | let output = open + input + close; 31 | // see https://github.com/chalk/chalk/pull/92, thanks to the 32 | // chalk contributors for this fix. However, we've confirmed that 33 | // this issue is also present in Windows terminals 34 | return newline ? output.replace(/\r*\n/g, `${close}$&${open}`) : output; 35 | }; 36 | return style; 37 | }; 38 | 39 | const wrap = (style, input, newline) => { 40 | return typeof style === 'function' ? style(input) : style.wrap(input, newline); 41 | }; 42 | 43 | const style = (input, stack) => { 44 | if (input === '' || input == null) return ''; 45 | if (colors.enabled === false) return input; 46 | if (colors.visible === false) return ''; 47 | let str = '' + input; 48 | let nl = str.includes('\n'); 49 | let n = stack.length; 50 | if (n > 0 && stack.includes('unstyle')) { 51 | stack = [...new Set(['unstyle', ...stack])].reverse(); 52 | } 53 | while (n-- > 0) str = wrap(colors.styles[stack[n]], str, nl); 54 | return str; 55 | }; 56 | 57 | const define = (name, codes, type) => { 58 | colors.styles[name] = ansi({ name, codes }); 59 | let keys = colors.keys[type] || (colors.keys[type] = []); 60 | keys.push(name); 61 | 62 | Reflect.defineProperty(colors, name, { 63 | configurable: true, 64 | enumerable: true, 65 | set(value) { 66 | colors.alias(name, value); 67 | }, 68 | get() { 69 | let color = input => style(input, color.stack); 70 | Reflect.setPrototypeOf(color, colors); 71 | color.stack = this.stack ? this.stack.concat(name) : [name]; 72 | return color; 73 | } 74 | }); 75 | }; 76 | 77 | define('reset', [0, 0], 'modifier'); 78 | define('bold', [1, 22], 'modifier'); 79 | define('dim', [2, 22], 'modifier'); 80 | define('italic', [3, 23], 'modifier'); 81 | define('underline', [4, 24], 'modifier'); 82 | define('inverse', [7, 27], 'modifier'); 83 | define('hidden', [8, 28], 'modifier'); 84 | define('strikethrough', [9, 29], 'modifier'); 85 | 86 | define('black', [30, 39], 'color'); 87 | define('red', [31, 39], 'color'); 88 | define('green', [32, 39], 'color'); 89 | define('yellow', [33, 39], 'color'); 90 | define('blue', [34, 39], 'color'); 91 | define('magenta', [35, 39], 'color'); 92 | define('cyan', [36, 39], 'color'); 93 | define('white', [37, 39], 'color'); 94 | define('gray', [90, 39], 'color'); 95 | define('grey', [90, 39], 'color'); 96 | 97 | define('bgBlack', [40, 49], 'bg'); 98 | define('bgRed', [41, 49], 'bg'); 99 | define('bgGreen', [42, 49], 'bg'); 100 | define('bgYellow', [43, 49], 'bg'); 101 | define('bgBlue', [44, 49], 'bg'); 102 | define('bgMagenta', [45, 49], 'bg'); 103 | define('bgCyan', [46, 49], 'bg'); 104 | define('bgWhite', [47, 49], 'bg'); 105 | 106 | define('blackBright', [90, 39], 'bright'); 107 | define('redBright', [91, 39], 'bright'); 108 | define('greenBright', [92, 39], 'bright'); 109 | define('yellowBright', [93, 39], 'bright'); 110 | define('blueBright', [94, 39], 'bright'); 111 | define('magentaBright', [95, 39], 'bright'); 112 | define('cyanBright', [96, 39], 'bright'); 113 | define('whiteBright', [97, 39], 'bright'); 114 | 115 | define('bgBlackBright', [100, 49], 'bgBright'); 116 | define('bgRedBright', [101, 49], 'bgBright'); 117 | define('bgGreenBright', [102, 49], 'bgBright'); 118 | define('bgYellowBright', [103, 49], 'bgBright'); 119 | define('bgBlueBright', [104, 49], 'bgBright'); 120 | define('bgMagentaBright', [105, 49], 'bgBright'); 121 | define('bgCyanBright', [106, 49], 'bgBright'); 122 | define('bgWhiteBright', [107, 49], 'bgBright'); 123 | 124 | colors.ansiRegex = ANSI_REGEX; 125 | colors.hasColor = colors.hasAnsi = str => { 126 | colors.ansiRegex.lastIndex = 0; 127 | return typeof str === 'string' && str !== '' && colors.ansiRegex.test(str); 128 | }; 129 | 130 | colors.alias = (name, color) => { 131 | let fn = typeof color === 'string' ? colors[color] : color; 132 | 133 | if (typeof fn !== 'function') { 134 | throw new TypeError('Expected alias to be the name of an existing color (string) or a function'); 135 | } 136 | 137 | if (!fn.stack) { 138 | Reflect.defineProperty(fn, 'name', { value: name }); 139 | colors.styles[name] = fn; 140 | fn.stack = [name]; 141 | } 142 | 143 | Reflect.defineProperty(colors, name, { 144 | configurable: true, 145 | enumerable: true, 146 | set(value) { 147 | colors.alias(name, value); 148 | }, 149 | get() { 150 | let color = input => style(input, color.stack); 151 | Reflect.setPrototypeOf(color, colors); 152 | color.stack = this.stack ? this.stack.concat(fn.stack) : fn.stack; 153 | return color; 154 | } 155 | }); 156 | }; 157 | 158 | colors.theme = custom => { 159 | if (!isObject(custom)) throw new TypeError('Expected theme to be an object'); 160 | for (let name of Object.keys(custom)) { 161 | colors.alias(name, custom[name]); 162 | } 163 | return colors; 164 | }; 165 | 166 | colors.alias('unstyle', str => { 167 | if (typeof str === 'string' && str !== '') { 168 | colors.ansiRegex.lastIndex = 0; 169 | return str.replace(colors.ansiRegex, ''); 170 | } 171 | return ''; 172 | }); 173 | 174 | colors.alias('noop', str => str); 175 | colors.none = colors.clear = colors.noop; 176 | 177 | colors.stripColor = colors.unstyle; 178 | colors.symbols = require('./symbols'); 179 | colors.define = define; 180 | return colors; 181 | }; 182 | 183 | module.exports = create(); 184 | module.exports.create = create; 185 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ansi-colors", 3 | "description": "Easily add ANSI colors to your text and symbols in the terminal. A faster drop-in replacement for chalk, kleur and turbocolor (without the dependencies and rendering bugs).", 4 | "version": "4.1.3", 5 | "homepage": "https://github.com/doowb/ansi-colors", 6 | "author": "Brian Woodward (https://github.com/doowb)", 7 | "contributors": [ 8 | "Brian Woodward (https://twitter.com/doowb)", 9 | "Jason Schilling (https://sourecode.de)", 10 | "Jon Schlinkert (http://twitter.com/jonschlinkert)", 11 | "Jordan (https://github.com/Silic0nS0ldier)" 12 | ], 13 | "repository": "doowb/ansi-colors", 14 | "bugs": { 15 | "url": "https://github.com/doowb/ansi-colors/issues" 16 | }, 17 | "license": "MIT", 18 | "files": [ 19 | "index.js", 20 | "symbols.js", 21 | "types/index.d.ts" 22 | ], 23 | "main": "index.js", 24 | "types": "./types/index.d.ts", 25 | "engines": { 26 | "node": ">=6" 27 | }, 28 | "scripts": { 29 | "test": "mocha" 30 | }, 31 | "devDependencies": { 32 | "decache": "^4.5.1", 33 | "gulp-format-md": "^2.0.0", 34 | "justified": "^1.0.1", 35 | "mocha": "^6.1.4", 36 | "text-table": "^0.2.0" 37 | }, 38 | "keywords": [ 39 | "256", 40 | "ansi", 41 | "bgblack", 42 | "bgBlack", 43 | "bgblue", 44 | "bgBlue", 45 | "bgcyan", 46 | "bgCyan", 47 | "bggreen", 48 | "bgGreen", 49 | "bgmagenta", 50 | "bgMagenta", 51 | "bgred", 52 | "bgRed", 53 | "bgwhite", 54 | "bgWhite", 55 | "bgyellow", 56 | "bgYellow", 57 | "black", 58 | "blue", 59 | "bold", 60 | "cli", 61 | "clorox", 62 | "color", 63 | "colors", 64 | "colour", 65 | "command line", 66 | "command-line", 67 | "console", 68 | "cyan", 69 | "dim", 70 | "formatting", 71 | "gray", 72 | "green", 73 | "grey", 74 | "hidden", 75 | "inverse", 76 | "italic", 77 | "kleur", 78 | "log", 79 | "logging", 80 | "magenta", 81 | "red", 82 | "reset", 83 | "rgb", 84 | "shell", 85 | "str", 86 | "strikethrough", 87 | "string", 88 | "style", 89 | "styles", 90 | "terminal", 91 | "text", 92 | "tty", 93 | "underline", 94 | "white", 95 | "xterm", 96 | "yellow" 97 | ], 98 | "verb": { 99 | "toc": false, 100 | "layout": "default", 101 | "tasks": [ 102 | "readme" 103 | ], 104 | "data": { 105 | "author": { 106 | "linkedin": "woodwardbrian", 107 | "twitter": "doowb" 108 | } 109 | }, 110 | "plugins": [ 111 | "gulp-format-md" 112 | ], 113 | "lint": { 114 | "reflinks": true 115 | }, 116 | "related": { 117 | "list": [ 118 | "ansi-wrap", 119 | "strip-color" 120 | ] 121 | }, 122 | "reflinks": [ 123 | "chalk", 124 | "colorette", 125 | "colors", 126 | "kleur" 127 | ] 128 | } 129 | } 130 | -------------------------------------------------------------------------------- /symbols.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const isHyper = typeof process !== 'undefined' && process.env.TERM_PROGRAM === 'Hyper'; 4 | const isWindows = typeof process !== 'undefined' && process.platform === 'win32'; 5 | const isLinux = typeof process !== 'undefined' && process.platform === 'linux'; 6 | 7 | const common = { 8 | ballotDisabled: '☒', 9 | ballotOff: '☐', 10 | ballotOn: '☑', 11 | bullet: '•', 12 | bulletWhite: '◦', 13 | fullBlock: '█', 14 | heart: '❤', 15 | identicalTo: '≡', 16 | line: '─', 17 | mark: '※', 18 | middot: '·', 19 | minus: '-', 20 | multiplication: '×', 21 | obelus: '÷', 22 | pencilDownRight: '✎', 23 | pencilRight: '✏', 24 | pencilUpRight: '✐', 25 | percent: '%', 26 | pilcrow2: '❡', 27 | pilcrow: '¶', 28 | plusMinus: '±', 29 | question: '?', 30 | section: '§', 31 | starsOff: '☆', 32 | starsOn: '★', 33 | upDownArrow: '↕' 34 | }; 35 | 36 | const windows = Object.assign({}, common, { 37 | check: '√', 38 | cross: '×', 39 | ellipsisLarge: '...', 40 | ellipsis: '...', 41 | info: 'i', 42 | questionSmall: '?', 43 | pointer: '>', 44 | pointerSmall: '»', 45 | radioOff: '( )', 46 | radioOn: '(*)', 47 | warning: '‼' 48 | }); 49 | 50 | const other = Object.assign({}, common, { 51 | ballotCross: '✘', 52 | check: '✔', 53 | cross: '✖', 54 | ellipsisLarge: '⋯', 55 | ellipsis: '…', 56 | info: 'ℹ', 57 | questionFull: '?', 58 | questionSmall: '﹖', 59 | pointer: isLinux ? '▸' : '❯', 60 | pointerSmall: isLinux ? '‣' : '›', 61 | radioOff: '◯', 62 | radioOn: '◉', 63 | warning: '⚠' 64 | }); 65 | 66 | module.exports = (isWindows && !isHyper) ? windows : other; 67 | Reflect.defineProperty(module.exports, 'common', { enumerable: false, value: common }); 68 | Reflect.defineProperty(module.exports, 'windows', { enumerable: false, value: windows }); 69 | Reflect.defineProperty(module.exports, 'other', { enumerable: false, value: other }); 70 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | require('mocha'); 4 | const assert = require('assert'); 5 | const decache = require('decache'); 6 | const colors = require('./'); 7 | 8 | describe('ansi-colors', () => { 9 | it('should wrap a string with ansi codes:', () => { 10 | assert.equal(colors.bgBlack('string'), '\u001b[40mstring\u001b[49m'); 11 | assert.equal(colors.bgBlue('string'), '\u001b[44mstring\u001b[49m'); 12 | assert.equal(colors.bgCyan('string'), '\u001b[46mstring\u001b[49m'); 13 | assert.equal(colors.bgGreen('string'), '\u001b[42mstring\u001b[49m'); 14 | assert.equal(colors.bgMagenta('string'), '\u001b[45mstring\u001b[49m'); 15 | assert.equal(colors.bgRed('string'), '\u001b[41mstring\u001b[49m'); 16 | assert.equal(colors.bgWhite('string'), '\u001b[47mstring\u001b[49m'); 17 | assert.equal(colors.bgYellow('string'), '\u001b[43mstring\u001b[49m'); 18 | assert.equal(colors.black('string'), '\u001b[30mstring\u001b[39m'); 19 | assert.equal(colors.blue('string'), '\u001b[34mstring\u001b[39m'); 20 | assert.equal(colors.bold('string'), '\u001b[1mstring\u001b[22m'); 21 | assert.equal(colors.cyan('string'), '\u001b[36mstring\u001b[39m'); 22 | assert.equal(colors.dim('string'), '\u001b[2mstring\u001b[22m'); 23 | assert.equal(colors.gray('string'), '\u001b[90mstring\u001b[39m'); 24 | assert.equal(colors.green('string'), '\u001b[32mstring\u001b[39m'); 25 | assert.equal(colors.hidden('string'), '\u001b[8mstring\u001b[28m'); 26 | assert.equal(colors.inverse('string'), '\u001b[7mstring\u001b[27m'); 27 | assert.equal(colors.italic('string'), '\u001b[3mstring\u001b[23m'); 28 | assert.equal(colors.magenta('string'), '\u001b[35mstring\u001b[39m'); 29 | assert.equal(colors.red('string'), '\u001b[31mstring\u001b[39m'); 30 | assert.equal(colors.reset('string'), '\u001b[0mstring\u001b[0m'); 31 | assert.equal(colors.strikethrough('string'), '\u001b[9mstring\u001b[29m'); 32 | assert.equal(colors.underline('string'), '\u001b[4mstring\u001b[24m'); 33 | assert.equal(colors.white('string'), '\u001b[37mstring\u001b[39m'); 34 | assert.equal(colors.yellow('string'), '\u001b[33mstring\u001b[39m'); 35 | }); 36 | 37 | it('should wrap a string with bold ansi codes:', () => { 38 | assert.equal(colors.black.bold('string'), '\u001b[30m\u001b[1mstring\u001b[22m\u001b[39m'); 39 | assert.equal(colors.blue.bold('string'), '\u001b[34m\u001b[1mstring\u001b[22m\u001b[39m'); 40 | assert.equal(colors.cyan.bold('string'), '\u001b[36m\u001b[1mstring\u001b[22m\u001b[39m'); 41 | assert.equal(colors.dim.bold('string'), '\u001b[2m\u001b[1mstring\u001b[22m\u001b[2m\u001b[22m'); 42 | assert.equal(colors.gray.bold('string'), '\u001b[90m\u001b[1mstring\u001b[22m\u001b[39m'); 43 | assert.equal(colors.green.bold('string'), '\u001b[32m\u001b[1mstring\u001b[22m\u001b[39m'); 44 | assert.equal(colors.magenta.bold('string'), '\u001b[35m\u001b[1mstring\u001b[22m\u001b[39m'); 45 | assert.equal(colors.red.bold('string'), '\u001b[31m\u001b[1mstring\u001b[22m\u001b[39m'); 46 | assert.equal(colors.white.bold('string'), '\u001b[37m\u001b[1mstring\u001b[22m\u001b[39m'); 47 | assert.equal(colors.yellow.bold('string'), '\u001b[33m\u001b[1mstring\u001b[22m\u001b[39m'); 48 | 49 | assert.equal(colors.bold.black('string'), '\u001b[1m\u001b[30mstring\u001b[39m\u001b[22m'); 50 | assert.equal(colors.bold.blue('string'), '\u001b[1m\u001b[34mstring\u001b[39m\u001b[22m'); 51 | assert.equal(colors.bold.cyan('string'), '\u001b[1m\u001b[36mstring\u001b[39m\u001b[22m'); 52 | assert.equal(colors.bold.dim('string'), '\u001b[1m\u001b[2mstring\u001b[22m\u001b[1m\u001b[22m'); 53 | assert.equal(colors.bold.gray('string'), '\u001b[1m\u001b[90mstring\u001b[39m\u001b[22m'); 54 | assert.equal(colors.bold.green('string'), '\u001b[1m\u001b[32mstring\u001b[39m\u001b[22m'); 55 | assert.equal(colors.bold.magenta('string'), '\u001b[1m\u001b[35mstring\u001b[39m\u001b[22m'); 56 | assert.equal(colors.bold.red('string'), '\u001b[1m\u001b[31mstring\u001b[39m\u001b[22m'); 57 | assert.equal(colors.bold.white('string'), '\u001b[1m\u001b[37mstring\u001b[39m\u001b[22m'); 58 | assert.equal(colors.bold.yellow('string'), '\u001b[1m\u001b[33mstring\u001b[39m\u001b[22m'); 59 | }); 60 | 61 | describe('chaining', () => { 62 | it('should create a color stack for chained colors', () => { 63 | let dim = colors.dim; 64 | assert.deepEqual(dim.stack, ['dim']); 65 | assert.deepEqual(dim.gray.stack, ['dim', 'gray']); 66 | assert.deepEqual(dim.gray.underline.stack, ['dim', 'gray', 'underline']); 67 | }); 68 | 69 | it('should correctly reset the color stack on bound colors', () => { 70 | let dim = colors.dim; 71 | let foo = dim('FOO'); 72 | let codes = dim.gray.underline('FOO'); 73 | assert.equal(dim('FOO'), foo); 74 | assert.equal(dim.gray.underline('FOO'), codes); 75 | assert.equal(dim('FOO'), foo); 76 | assert.equal(dim.gray.underline('FOO'), codes); 77 | assert.equal(dim('FOO'), foo); 78 | }); 79 | 80 | it('should correctly reset the color stack on chained _bound_ colors', () => { 81 | let dimRed = colors.dim.red; 82 | let dim = colors.dim; 83 | let underline = dimRed.underline; 84 | let foo = dim('FOO'); 85 | let codes = dimRed.underline('FOO'); 86 | assert.equal(dim('FOO'), foo); 87 | assert.equal(dimRed.underline('FOO'), codes); 88 | assert.equal(dim('FOO'), foo); 89 | assert.equal(dimRed.underline('FOO'), codes); 90 | assert.equal(dim('FOO'), foo); 91 | assert.equal(underline('foo'), colors.dim.red.underline('foo')); 92 | 93 | let redBold = colors.red.bold; 94 | let blueBold = colors.red.blue.bold('Blue Bold'); 95 | assert.equal(blueBold, '\u001b[31m\u001b[34m\u001b[1mBlue Bold\u001b[22m\u001b[39m\u001b[31m\u001b[39m'); 96 | assert.equal(redBold('Red Bold'), '\u001b[31m\u001b[1mRed Bold\u001b[22m\u001b[39m'); 97 | assert.equal(colors.red.bold('Red Bold'), '\u001b[31m\u001b[1mRed Bold\u001b[22m\u001b[39m'); 98 | }); 99 | }); 100 | 101 | describe('nesting', () => { 102 | it('should correctly wrap the colors on nested colors', () => { 103 | assert.equal(colors.red(`R${colors.green(`G${colors.blue('B')}G`)}R`), '\u001b[31mR\u001b[32mG\u001b[34mB\u001b[39m\u001b[31m\u001b[32mG\u001b[39m\u001b[31mR\u001b[39m'); 104 | }); 105 | }); 106 | 107 | describe('newlines', () => { 108 | it('should correctly wrap colors around newlines', () => { 109 | assert.equal(colors.bgRed('foo\nbar') + 'baz qux', '\u001b[41mfoo\u001b[49m\n\u001b[41mbar\u001b[49mbaz qux'); 110 | }); 111 | }); 112 | 113 | describe('enabled', () => { 114 | it('should disable ansi styling when colors.enabled is false', () => { 115 | colors.enabled = false; 116 | assert.equal(colors.red('foo bar'), 'foo bar'); 117 | assert.equal(colors.blue('foo bar'), 'foo bar'); 118 | assert.equal(colors.bold('foo bar'), 'foo bar'); 119 | colors.enabled = true; 120 | }); 121 | }); 122 | 123 | describe('FORCE_COLOR', () => { 124 | beforeEach(() => { 125 | delete process.env.FORCE_COLOR; 126 | decache('./'); 127 | }); 128 | 129 | it('should be enabled if FORCE_COLOR is not set', () => { 130 | const colors = require('./'); 131 | assert.equal(colors.enabled, true); 132 | }); 133 | 134 | it('should be enabled if FORCE_COLOR is set to 1', () => { 135 | process.env.FORCE_COLOR = '1'; 136 | const colors = require('./'); 137 | assert.equal(colors.enabled, true); 138 | }); 139 | 140 | it('should be disabled if FORCE_COLOR is set to 0', () => { 141 | process.env.FORCE_COLOR = '0'; 142 | const colors = require('./'); 143 | assert.equal(colors.enabled, false); 144 | }); 145 | }); 146 | 147 | describe('visible', () => { 148 | it('should mute output when colors.visible is false', () => { 149 | colors.visible = false; 150 | assert.equal(colors.red('foo bar'), ''); 151 | assert.equal(colors.blue('foo bar'), ''); 152 | assert.equal(colors.bold('foo bar'), ''); 153 | colors.visible = true; 154 | }); 155 | }); 156 | 157 | describe('unstyle', () => { 158 | it('should strip ANSI codes', () => { 159 | assert.equal(colors.unstyle(colors.blue.bold('foo bar baz')), 'foo bar baz'); 160 | assert.equal(colors.stripColor(colors.blue.bold('foo bar baz')), 'foo bar baz'); 161 | }); 162 | }); 163 | 164 | describe('hasColor', () => { 165 | it('should return true if a string has ansi styling', () => { 166 | assert(colors.hasColor(colors.blue.bold('foo bar baz'))); 167 | assert(colors.hasAnsi(colors.blue.bold('foo bar baz'))); 168 | }); 169 | 170 | it('should return false if a string does not have ansi styling', () => { 171 | assert(!colors.hasColor('foo bar baz')); 172 | assert(!colors.hasAnsi('foo bar baz')); 173 | }); 174 | 175 | it('should return true when used multiple times', () => { 176 | assert(colors.hasColor(colors.blue.bold('foo bar baz'))); 177 | assert(colors.hasColor(colors.blue.bold('foo bar baz'))); 178 | assert(colors.hasColor(colors.blue.bold('foo bar baz'))); 179 | assert(colors.hasColor(colors.blue.bold('foo bar baz'))); 180 | assert(colors.hasColor(colors.blue.bold('foo bar baz'))); 181 | }); 182 | }); 183 | }); 184 | -------------------------------------------------------------------------------- /types/index.d.ts: -------------------------------------------------------------------------------- 1 | // Imported from DefinitelyTyped project. 2 | // TypeScript definitions for ansi-colors 3 | // Definitions by: Rogier Schouten 4 | // Integrated by: Jordan Mele 5 | 6 | interface SymbolsType { 7 | /** 8 | * `undefined` on windows, `✘` on other platforms. 9 | */ 10 | ballotCross?: "✘"; 11 | ballotDisabled: "☒"; 12 | ballotOff: "☐"; 13 | ballotOn: "☑"; 14 | bullet: "•"; 15 | bulletWhite: "◦"; 16 | /** 17 | * `√` on windows, `✔` on other platforms. 18 | */ 19 | check: "√" | "✔"; 20 | /** 21 | * `×` on windows, `✖` on other platforms. 22 | */ 23 | cross: "×" | "✖"; 24 | /** 25 | * `...` on windows, `⋯` on other platforms. 26 | */ 27 | ellipsisLarge: "..." | "⋯"; 28 | /** 29 | * `...` on windows, `…` on other platforms. 30 | */ 31 | ellipsis: "..." | "…"; 32 | fullBlock: "█"; 33 | heart: "❤"; 34 | identicalTo: "≡"; 35 | info: "i" | "ℹ"; 36 | line: "─"; 37 | mark: "※"; 38 | middot: "·"; 39 | minus: "-"; 40 | multiplication: "×"; 41 | obelus: "÷"; 42 | pencilDownRight: "✎"; 43 | pencilRight: "✏"; 44 | pencilUpRight: "✐"; 45 | percent: "%"; 46 | pilcrow2: "❡"; 47 | pilcrow: "¶"; 48 | plusMinus: "±"; 49 | /** 50 | * `>` on windows, `▸` on linux, and `❯` on other platforms. 51 | */ 52 | pointer: ">" | "▸" | "❯"; 53 | /** 54 | * `»` on windows, `‣` on linux, and `›` on other platforms. 55 | */ 56 | pointerSmall: "»" | "‣" | "›"; 57 | question: "?"; 58 | /** 59 | * `undefined` on windows, `?` on other platforms. 60 | */ 61 | questionFull?: "?"; 62 | /** 63 | * `?` on windows, `﹖` on other platforms. 64 | */ 65 | questionSmall: "?" | "﹖"; 66 | /** 67 | * `( )` on windows, `◯` on other platforms. 68 | */ 69 | radioOff: "( )" | "◯"; 70 | /** 71 | * `(*)` on windows, `◉` on other platforms. 72 | */ 73 | radioOn: "(*)" | "◉"; 74 | section: "§"; 75 | starsOff: "☆"; 76 | starsOn: "★"; 77 | upDownArrow: "↕"; 78 | /** 79 | * `‼` on windows, `⚠` on other platforms. 80 | */ 81 | warning: "‼" | "⚠"; 82 | } 83 | 84 | type StyleArrayStructure = [number, number]; 85 | interface StyleArrayProperties { 86 | open: string; 87 | close: string; 88 | closeRe: string; 89 | } 90 | 91 | type StyleType = StyleArrayStructure & StyleArrayProperties; 92 | 93 | 94 | interface StylesType { 95 | // modifiers 96 | reset: T; 97 | bold: T; 98 | dim: T; 99 | italic: T; 100 | underline: T; 101 | inverse: T; 102 | hidden: T; 103 | strikethrough: T; 104 | 105 | // colors 106 | black: T; 107 | red: T; 108 | green: T; 109 | yellow: T; 110 | blue: T; 111 | magenta: T; 112 | cyan: T; 113 | white: T; 114 | gray: T; 115 | grey: T; 116 | 117 | // bright colors 118 | blackBright: T; 119 | redBright: T; 120 | greenBright: T; 121 | yellowBright: T; 122 | blueBright: T; 123 | magentaBright: T; 124 | cyanBright: T; 125 | whiteBright: T; 126 | 127 | // background colors 128 | bgBlack: T; 129 | bgRed: T; 130 | bgGreen: T; 131 | bgYellow: T; 132 | bgBlue: T; 133 | bgMagenta: T; 134 | bgCyan: T; 135 | bgWhite: T; 136 | 137 | // bright background colors 138 | bgBlackBright: T; 139 | bgRedBright: T; 140 | bgGreenBright: T; 141 | bgYellowBright: T; 142 | bgBlueBright: T; 143 | bgMagentaBright: T; 144 | bgCyanBright: T; 145 | bgWhiteBright: T; 146 | } 147 | 148 | declare namespace ansiColors { 149 | interface StyleFunction extends StylesType { 150 | (s: string): string; 151 | } 152 | 153 | // modifiers 154 | const reset: StyleFunction; 155 | const bold: StyleFunction; 156 | const dim: StyleFunction; 157 | const italic: StyleFunction; 158 | const underline: StyleFunction; 159 | const inverse: StyleFunction; 160 | const hidden: StyleFunction; 161 | const strikethrough: StyleFunction; 162 | 163 | // colors 164 | const black: StyleFunction; 165 | const red: StyleFunction; 166 | const green: StyleFunction; 167 | const yellow: StyleFunction; 168 | const blue: StyleFunction; 169 | const magenta: StyleFunction; 170 | const cyan: StyleFunction; 171 | const white: StyleFunction; 172 | const gray: StyleFunction; 173 | const grey: StyleFunction; 174 | 175 | // bright colors 176 | const blackBright: StyleFunction; 177 | const redBright: StyleFunction; 178 | const greenBright: StyleFunction; 179 | const yellowBright: StyleFunction; 180 | const blueBright: StyleFunction; 181 | const magentaBright: StyleFunction; 182 | const cyanBright: StyleFunction; 183 | const whiteBright: StyleFunction; 184 | 185 | // background colors 186 | const bgBlack: StyleFunction; 187 | const bgRed: StyleFunction; 188 | const bgGreen: StyleFunction; 189 | const bgYellow: StyleFunction; 190 | const bgBlue: StyleFunction; 191 | const bgMagenta: StyleFunction; 192 | const bgCyan: StyleFunction; 193 | const bgWhite: StyleFunction; 194 | 195 | // bright background colors 196 | const bgBlackBright: StyleFunction; 197 | const bgRedBright: StyleFunction; 198 | const bgGreenBright: StyleFunction; 199 | const bgYellowBright: StyleFunction; 200 | const bgBlueBright: StyleFunction; 201 | const bgMagentaBright: StyleFunction; 202 | const bgCyanBright: StyleFunction; 203 | const bgWhiteBright: StyleFunction; 204 | 205 | let enabled: boolean; 206 | let visible: boolean; 207 | const ansiRegex: RegExp; 208 | 209 | /** 210 | * Remove styles from string 211 | */ 212 | function stripColor(s: string): string; 213 | 214 | /** 215 | * Remove styles from string 216 | */ 217 | function strip(s: string): string; 218 | 219 | /** 220 | * Remove styles from string 221 | */ 222 | function unstyle(s: string): string; 223 | 224 | const styles: StylesType; 225 | const symbols: SymbolsType; 226 | 227 | /** 228 | * Outputs a string with check-symbol as prefix 229 | */ 230 | function ok(...args: string[]): string; 231 | 232 | function create(): typeof ansiColors; 233 | } 234 | 235 | export = ansiColors; 236 | -------------------------------------------------------------------------------- /types/test.ts: -------------------------------------------------------------------------------- 1 | // Imported from DefinitelyTyped project. 2 | 3 | import * as colors from '../'; 4 | 5 | let s: string; 6 | 7 | s = colors.reset("hello"); 8 | s = colors.bold("hello"); 9 | s = colors.dim("hello"); 10 | s = colors.italic("hello"); 11 | s = colors.underline("hello"); 12 | s = colors.inverse("hello"); 13 | s = colors.hidden("hello"); 14 | s = colors.strikethrough("hello"); 15 | 16 | // colors 17 | s = colors.black("hello"); 18 | s = colors.red("hello"); 19 | s = colors.green("hello"); 20 | s = colors.yellow("hello"); 21 | s = colors.blue("hello"); 22 | s = colors.magenta("hello"); 23 | s = colors.cyan("hello"); 24 | s = colors.white("hello"); 25 | s = colors.gray("hello"); 26 | s = colors.grey("hello"); 27 | 28 | // bright colors 29 | s = colors.blackBright("hello"); 30 | s = colors.redBright("hello"); 31 | s = colors.greenBright("hello"); 32 | s = colors.yellowBright("hello"); 33 | s = colors.blueBright("hello"); 34 | s = colors.magentaBright("hello"); 35 | s = colors.cyanBright("hello"); 36 | s = colors.whiteBright("hello"); 37 | 38 | // background colors 39 | s = colors.bgBlack("hello"); 40 | s = colors.bgRed("hello"); 41 | s = colors.bgGreen("hello"); 42 | s = colors.bgYellow("hello"); 43 | s = colors.bgBlue("hello"); 44 | s = colors.bgMagenta("hello"); 45 | s = colors.bgCyan("hello"); 46 | s = colors.bgWhite("hello"); 47 | 48 | // bright background colors 49 | s = colors.bgBlackBright("hello"); 50 | s = colors.bgRedBright("hello"); 51 | s = colors.bgGreenBright("hello"); 52 | s = colors.bgYellowBright("hello"); 53 | s = colors.bgBlueBright("hello"); 54 | s = colors.bgMagentaBright("hello"); 55 | s = colors.bgCyanBright("hello"); 56 | s = colors.bgRed.white.bold.underline("test"); 57 | 58 | s = colors.stripColor(colors.red("hello")); 59 | s = colors.unstyle(colors.red("hello")); 60 | s = colors.strip(colors.red("hello")); 61 | 62 | s = colors.ok("deployment succeeded!"); 63 | 64 | s = colors.create().reset("hello"); 65 | 66 | // common symbols 67 | s = colors.symbols.ballotDisabled; 68 | s = colors.symbols.ballotOff; 69 | s = colors.symbols.ballotOn; 70 | s = colors.symbols.bullet; 71 | s = colors.symbols.bulletWhite; 72 | s = colors.symbols.fullBlock; 73 | s = colors.symbols.heart; 74 | s = colors.symbols.identicalTo; 75 | s = colors.symbols.line; 76 | s = colors.symbols.mark; 77 | s = colors.symbols.middot; 78 | s = colors.symbols.minus; 79 | s = colors.symbols.multiplication; 80 | s = colors.symbols.obelus; 81 | s = colors.symbols.pencilDownRight; 82 | s = colors.symbols.pencilRight; 83 | s = colors.symbols.pencilUpRight; 84 | s = colors.symbols.percent; 85 | s = colors.symbols.pilcrow2; 86 | s = colors.symbols.pilcrow; 87 | s = colors.symbols.plusMinus; 88 | s = colors.symbols.section; 89 | s = colors.symbols.starsOff; 90 | s = colors.symbols.starsOn; 91 | s = colors.symbols.upDownArrow; 92 | 93 | // conditional symbols (differ across platforms) 94 | s = colors.symbols.check; 95 | s = colors.symbols.cross; 96 | s = colors.symbols.ellipsisLarge; 97 | s = colors.symbols.ellipsis; 98 | s = colors.symbols.info; 99 | s = colors.symbols.question; 100 | s = colors.symbols.questionSmall; 101 | s = colors.symbols.pointer; 102 | s = colors.symbols.pointerSmall; 103 | s = colors.symbols.radioOff; 104 | s = colors.symbols.radioOn; 105 | s = colors.symbols.warning; 106 | 107 | let maybeString: string | undefined; 108 | // unmatched other symbols (may be undefined) 109 | maybeString = colors.symbols.ballotCross; 110 | maybeString = colors.symbols.questionFull; 111 | -------------------------------------------------------------------------------- /types/tsconfig.json: -------------------------------------------------------------------------------- 1 | { // Imported from DefinitelyTyped project. 2 | "compilerOptions": { 3 | "module": "commonjs", 4 | "lib": [ 5 | "es6" 6 | ], 7 | "noImplicitAny": true, 8 | "noImplicitThis": true, 9 | "strictNullChecks": true, 10 | "strictFunctionTypes": true, 11 | "baseUrl": "../", 12 | "typeRoots": [ 13 | "../" 14 | ], 15 | "types": [], 16 | "noEmit": true, 17 | "forceConsistentCasingInFileNames": true, 18 | "esModuleInterop": true 19 | }, 20 | "files": [ 21 | "index.d.ts", 22 | "test.ts" 23 | ] 24 | } --------------------------------------------------------------------------------