Skip to content

Commit

Permalink
fixup! fixup! lib: respect terminal capabilities on styleText
Browse files Browse the repository at this point in the history
  • Loading branch information
RafaelGSS committed Aug 16, 2024
1 parent 66cd3f3 commit cc08b60
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
28 changes: 24 additions & 4 deletions doc/api/util.md
Original file line number Diff line number Diff line change
Expand Up @@ -1831,14 +1831,34 @@ and act according to the configuration set via `NO_COLORS`,
```mjs
import { styleText } from 'node:util';
const errorMessage = styleText('red', 'Error! Error!');
console.log(errorMessage);
import { stderr } from 'node:process';

const successMessage = styleText('green', 'Success!');
console.log(successMessage);

const errorMessage = styleText(
'red',
'Error! Error!',
// Validate if process.stderr has TTY
{ stream: stderr },
);
console.error(successMessage);
```
```cjs
const { styleText } = require('node:util');
const errorMessage = styleText('red', 'Error! Error!');
console.log(errorMessage);
const { stderr } = require('node:process');

const successMessage = styleText('green', 'Success!');
console.log(successMessage);

const errorMessage = styleText(
'red',
'Error! Error!',
// Validate if process.stderr has TTY
{ stream: stderr },
);
console.error(successMessage);
```
`util.inspect.colors` also provides text formats such as `italic`, and
Expand Down
15 changes: 15 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,17 @@ const {
} = require('internal/util/inspect');
const { debuglog } = require('internal/util/debuglog');
const {
validateBoolean,
validateFunction,
validateNumber,
validateString,
validateOneOf,
} = require('internal/validators');
const {
isReadableStream,
isWritableStream,
isNodeStream,
} = require('internal/streams/utils');
const types = require('internal/util/types');

let utilColors;
Expand Down Expand Up @@ -106,8 +112,17 @@ function escapeStyleCode(code) {
*/
function styleText(format, text, { validateStream = true, stream = process.stdout } = {}) {
validateString(text, 'text');
validateBoolean(validateStream, 'options.validateStream');

if (validateStream) {
if (
!isReadableStream(stream) &&
!isWritableStream(stream) &&
!isNodeStream(stream)
) {
throw new ERR_INVALID_ARG_TYPE('stream', ['ReadableStream', 'WritableStream', 'Stream'], stream);
}

if (
!stream ||
!lazyUtilColors().shouldColorize(stream)
Expand Down
9 changes: 9 additions & 0 deletions test/parallel/test-util-styletext.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ assert.throws(() => {
code: 'ERR_INVALID_ARG_VALUE',
});

assert.throws(() => {
util.styleText('red', 'text', { stream: {} });
}, {
code: 'ERR_INVALID_ARG_TYPE',
});

// does not throw
util.styleText('red', 'text', { stream: {}, validateStream: false });

assert.strictEqual(util.styleText('red', 'test'), styled);

const originalEnv = process.env;
Expand Down

0 comments on commit cc08b60

Please sign in to comment.