-
-
Notifications
You must be signed in to change notification settings - Fork 33.8k
lib: respect terminal capabilities on styleText #54389
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
9e3ada1
66cd3f3
cc08b60
0d3a65d
dd807c0
de39601
317c284
5efe9b8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -1810,13 +1810,24 @@ console.log(util.stripVTControlCharacters('\u001B[4mvalue\u001B[0m')); | |||
| added: | ||||
| - v21.7.0 | ||||
| - v20.12.0 | ||||
| changes: | ||||
| - version: REPLACEME | ||||
| pr-url: https://github.com/nodejs/node/pull/54389 | ||||
| description: Respect isTTY and colors environment variables | ||||
| such as NO_COLORS, NODE_DISABLE_COLORS, and FORCE_COLOR. | ||||
| --> | ||||
|
|
||||
| * `format` {string | Array} A text format or an Array | ||||
| of text formats defined in `util.inspect.colors`. | ||||
| * `text` {string} The text to to be formatted. | ||||
| * `options` {Object} | ||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You need to add this to the function's signature above.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done in cc08b60
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not what I meant. You have to update Line 1805 in 22daeba
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @RafaelGSS I've updated the doc for ya :) |
||||
| * `validateStream` {boolean} When true, `stream` is checked to see if it can handle colors. **Default:** `true`. | ||||
| * `stream` {Stream} A stream that will be validated if it can be colored. **Default:** `process.stdout`. | ||||
|
|
||||
| This function returns a formatted text considering the `format` passed. | ||||
| This function returns a formatted text considering the `format` passed | ||||
| for printing in a terminal, it is aware of the terminal's capabilities | ||||
RafaelGSS marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
| and act according to the configuration set via `NO_COLORS`, | ||||
| `NODE_DISABLE_COLORS` and `FORCE_COLOR` environment variables. | ||||
RafaelGSS marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
|
|
||||
| ```mjs | ||||
| import { styleText } from 'node:util'; | ||||
|
|
||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,15 @@ | ||
| 'use strict'; | ||
| require('../common'); | ||
| const assert = require('assert'); | ||
| const util = require('util'); | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this test is being significantly updated, it would be nice if it could be structured more using
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it blocking? Otherwise, I prefer to do it in a follow-up PR. |
||
| const common = require('../common'); | ||
| const assert = require('node:assert'); | ||
| const util = require('node:util'); | ||
| const { WriteStream } = require('node:tty'); | ||
|
|
||
| const styled = '\u001b[31mtest\u001b[39m'; | ||
| const noChange = 'test'; | ||
|
|
||
| const fd = common.getTTYfd(); | ||
| const writeStream = new WriteStream(fd); | ||
|
|
||
| [ | ||
| undefined, | ||
|
|
@@ -41,3 +49,26 @@ assert.throws(() => { | |
| }, { | ||
| code: 'ERR_INVALID_ARG_VALUE', | ||
| }); | ||
|
|
||
| assert.strictEqual(util.styleText('red', 'test'), styled); | ||
|
|
||
| const originalEnv = process.env; | ||
| [ | ||
| { isTTY: true, env: {}, expected: styled }, | ||
| { isTTY: false, env: {}, expected: noChange }, | ||
| { isTTY: true, env: { NODE_DISABLE_COLORS: '1' }, expected: noChange }, | ||
| { isTTY: true, env: { NO_COLOR: '1' }, expected: noChange }, | ||
| { isTTY: true, env: { FORCE_COLOR: '1' }, expected: styled }, | ||
| { isTTY: true, env: { FORCE_COLOR: '1', NODE_DISABLE_COLORS: '1' }, expected: styled }, | ||
| { isTTY: false, env: { FORCE_COLOR: '1', NO_COLOR: '1', NODE_DISABLE_COLORS: '1' }, expected: styled }, | ||
| { isTTY: true, env: { FORCE_COLOR: '1', NO_COLOR: '1', NODE_DISABLE_COLORS: '1' }, expected: styled }, | ||
| ].forEach((testCase) => { | ||
| writeStream.isTTY = testCase.isTTY; | ||
| process.env = { | ||
| ...process.env, | ||
| ...testCase.env | ||
| }; | ||
| const output = util.styleText('red', 'test', { stream: writeStream }); | ||
| assert.strictEqual(output, testCase.expected); | ||
| process.env = originalEnv; | ||
| }); | ||
Uh oh!
There was an error while loading. Please reload this page.