From 4372d27f7eb887c4d33cdca1f9484f321ceab3dd Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Sun, 22 Oct 2017 15:20:23 +0700 Subject: [PATCH 1/2] Add Awesome mentioned badge --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 7158d5a..9423ebc 100644 --- a/readme.md +++ b/readme.md @@ -9,7 +9,7 @@ > Terminal string styling done right -[![Build Status](https://travis-ci.org/chalk/chalk.svg?branch=master)](https://travis-ci.org/chalk/chalk) [![Coverage Status](https://coveralls.io/repos/github/chalk/chalk/badge.svg?branch=master)](https://coveralls.io/github/chalk/chalk?branch=master) [![](https://img.shields.io/badge/unicorn-approved-ff69b4.svg)](https://www.youtube.com/watch?v=9auOCbH5Ns4) [![XO code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/sindresorhus/xo) +[![Build Status](https://travis-ci.org/chalk/chalk.svg?branch=master)](https://travis-ci.org/chalk/chalk) [![Coverage Status](https://coveralls.io/repos/github/chalk/chalk/badge.svg?branch=master)](https://coveralls.io/github/chalk/chalk?branch=master) [![](https://img.shields.io/badge/unicorn-approved-ff69b4.svg)](https://www.youtube.com/watch?v=9auOCbH5Ns4) [![XO code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/sindresorhus/xo) [![Mentioned in Awesome Node.js](https://awesome.re/mentioned-badge.svg)](https://github.com/sindresorhus/awesome-nodejs) ### [See what's new in Chalk 2](https://github.com/chalk/chalk/releases/tag/v2.0.0) From dc092b4a5f5ca77dd1e22607cdf2fdd388803064 Mon Sep 17 00:00:00 2001 From: Danny Kirchmeier Date: Mon, 23 Oct 2017 20:39:21 -0500 Subject: [PATCH 2/2] Add .visible for emitting text only when enabled (fixes #192) --- index.js | 10 +++++++++- readme.md | 1 + test/visible.js | 24 ++++++++++++++++++++++++ types/index.d.ts | 2 ++ types/test.ts | 4 ++++ 5 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 test/visible.js diff --git a/index.js b/index.js index de65bd4..861908c 100644 --- a/index.js +++ b/index.js @@ -63,6 +63,13 @@ for (const key of Object.keys(ansiStyles)) { }; } +styles.visible = { + get() { + this._emptyIfNotVisible = true; + return build.call(this, this._styles ? this._styles : [], 'visible'); + } +}; + ansiStyles.color.closeRe = new RegExp(escapeStringRegexp(ansiStyles.color.close), 'g'); for (const model of Object.keys(ansiStyles.color.ansi)) { if (skipModels.has(model)) { @@ -116,6 +123,7 @@ function build(_styles, key) { }; builder._styles = _styles; + builder._emptyIfNotVisible = this._emptyIfNotVisible; const self = this; @@ -167,7 +175,7 @@ function applyStyle() { } if (!this.enabled || this.level <= 0 || !str) { - return str; + return this._emptyIfNotVisible ? '' : str; } // Turns out that on Windows dimmed gray text becomes invisible in cmd.exe, diff --git a/readme.md b/readme.md index 9423ebc..9bb2e65 100644 --- a/readme.md +++ b/readme.md @@ -170,6 +170,7 @@ Explicit 256/Truecolor mode can be enabled using the `--color=256` and `--color= - `inverse` - `hidden` - `strikethrough` *(Not widely supported)* +- `visible` (Text is emitted only if enabled) ### Colors diff --git a/test/visible.js b/test/visible.js new file mode 100644 index 0000000..20f7ecb --- /dev/null +++ b/test/visible.js @@ -0,0 +1,24 @@ +import test from 'ava'; + +// Spoof supports-color +require('./_supports-color')(__dirname); + +const m = require('..'); + +test('visible: normal output when enabled', t => { + const ctx = new m.constructor({level: 3, enabled: true}); + t.is(ctx.visible.red('foo'), '\u001B[31mfoo\u001B[39m'); + t.is(ctx.red.visible('foo'), '\u001B[31mfoo\u001B[39m'); +}); + +test('visible: no output when disabled', t => { + const ctx = new m.constructor({level: 3, enabled: false}); + t.is(ctx.red.visible('foo'), ''); + t.is(ctx.visible.red('foo'), ''); +}); + +test('visible: no output when level is too low', t => { + const ctx = new m.constructor({level: 0, enabled: true}); + t.is(ctx.visible.red('foo'), ''); + t.is(ctx.red.visible('foo'), ''); +}); diff --git a/types/index.d.ts b/types/index.d.ts index 7505746..9312e2d 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -49,6 +49,8 @@ export interface Chalk { hidden: Chalk; strikethrough: Chalk; + visible: Chalk; + black: Chalk; red: Chalk; green: Chalk; diff --git a/types/test.ts b/types/test.ts index 01509fd..63ca1e9 100644 --- a/types/test.ts +++ b/types/test.ts @@ -45,3 +45,7 @@ chalk.rgb(1, 14, 9).bgBlue('foo'); chalk.hsl(1, 14, 9).bgBlue('foo'); chalk.hsv(1, 14, 9).bgBlue('foo'); chalk.hwb(1, 14, 9).bgBlue('foo'); + +chalk.visible('foo'); +chalk.red.visible('foo'); +chalk.visible.red('foo');