Skip to content

Commit cda8857

Browse files
committed
add 'colors: boolean' property to Options
1 parent aee7cba commit cda8857

File tree

2 files changed

+112
-41
lines changed

2 files changed

+112
-41
lines changed

lib/index.js

Lines changed: 57 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
* Configuration.
88
* @property {boolean | null | undefined} [showPositions=true]
99
* Whether to include positional information (default: `true`).
10+
* @property {boolean | null | undefined} [colors]
11+
* Whether to include ANSI colors (default: `true` on Node, `false` otherwise).
1012
*
1113
* @typedef Style
1214
* Styling functions.
@@ -27,42 +29,42 @@
2729
* Rendering stylization.
2830
*/
2931

30-
import {color} from '#conditional-color'
32+
import {color as useColorByDefault} from '#conditional-color'
3133

3234
/**
3335
* Inspect a node, with color in Node, without color in browsers.
3436
*
35-
* @param tree
36-
* Tree to inspect.
37-
* @param options
38-
* Configuration (optional).
39-
* @returns
40-
* Pretty printed `tree`.
41-
*/
42-
/* c8 ignore next */
43-
export const inspect = color ? inspectColor : inspectNoColor
44-
45-
const own = {}.hasOwnProperty
46-
47-
/**
48-
* Inspect a node, without color.
49-
*
5037
* @param {unknown} tree
5138
* Tree to inspect.
5239
* @param {Options | null | undefined} [options]
5340
* Configuration.
5441
* @returns {string}
5542
* Pretty printed `tree`.
5643
*/
57-
export function inspectNoColor(tree, options) {
44+
/* c8 ignore next */
45+
export function inspect(tree, options) {
46+
const useColor =
47+
!options || options.colors === null || options.colors === undefined
48+
? useColorByDefault
49+
: options.colors
50+
51+
const style = useColor
52+
? {
53+
bold: ansiColor(1, 22),
54+
dim: ansiColor(2, 22),
55+
yellow: ansiColor(33, 39),
56+
green: ansiColor(32, 39)
57+
}
58+
: {
59+
bold: noColor,
60+
dim: noColor,
61+
yellow: noColor,
62+
green: noColor
63+
}
64+
5865
/** @type {State} */
5966
const state = {
60-
style: {
61-
bold: noColor,
62-
dim: noColor,
63-
yellow: noColor,
64-
green: noColor
65-
},
67+
style,
6668
showPositions:
6769
!options ||
6870
options.showPositions === null ||
@@ -74,34 +76,48 @@ export function inspectNoColor(tree, options) {
7476
return inspectValue(tree, state)
7577
}
7678

79+
const own = {}.hasOwnProperty
80+
81+
/**
82+
* Inspect a node, without color.
83+
*
84+
* @deprecated
85+
* Use `inspect` with the option `{colors: false}`.
86+
*
87+
* @param {unknown} tree
88+
* Tree to inspect.
89+
* @param {Omit<Options, 'colors'> | null | undefined} [options]
90+
* Configuration.
91+
* @returns {string}
92+
* Pretty printed `tree`.
93+
*/
94+
export function inspectNoColor(tree, options) {
95+
/* c8 ignore next 3 */
96+
const optionsWithNoColor = useColorByDefault
97+
? Object.assign({}, options, {colors: false})
98+
: options
99+
return inspect(tree, optionsWithNoColor)
100+
}
101+
77102
/**
78103
* Inspects a node, using color.
79104
*
105+
* @deprecated
106+
* Use `inspect` with the option `{colors: true}`.
107+
*
80108
* @param {unknown} tree
81109
* Tree to inspect.
82-
* @param {Options | null | undefined} [options]
110+
* @param {Omit<Options, 'colors'> | null | undefined} [options]
83111
* Configuration (optional).
84112
* @returns {string}
85113
* Pretty printed `tree`.
86114
*/
87115
export function inspectColor(tree, options) {
88-
/** @type {State} */
89-
const state = {
90-
style: {
91-
bold: ansiColor(1, 22),
92-
dim: ansiColor(2, 22),
93-
yellow: ansiColor(33, 39),
94-
green: ansiColor(32, 39)
95-
},
96-
showPositions:
97-
!options ||
98-
options.showPositions === null ||
99-
options.showPositions === undefined
100-
? true
101-
: options.showPositions
102-
}
103-
104-
return inspectValue(tree, state)
116+
/* c8 ignore next 3 */
117+
const optionsWithColor = useColorByDefault
118+
? options
119+
: Object.assign({}, options, {colors: true})
120+
return inspect(tree, optionsWithColor)
105121
}
106122

107123
/**

test.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ const chalkEnabled = new Chalk({level: 1})
1818

1919
const paragraph = 'Some simple text. Other “sentence”.'
2020

21+
/**
22+
* Split `text` on newlines, keeping the first `count`.
23+
*
24+
* @param {string} text
25+
* @param {number} count
26+
* @return string
27+
* The first `count` lines of `text`.
28+
*/
29+
const lines = (text, count) => text.split('\n').slice(0, count).join('\n')
30+
2131
test('inspect()', async function (t) {
2232
await t.test('should expose the public api', async function () {
2333
assert.deepEqual(Object.keys(await import('unist-util-inspect')).sort(), [
@@ -420,6 +430,51 @@ test('inspect()', async function (t) {
420430
].join('\n')
421431
)
422432
})
433+
434+
await t.test('inspect(…, {colors: false})', async function () {
435+
assert.equal(
436+
lines(inspect(retext().parse(paragraph), {colors: false}), 2),
437+
[
438+
'RootNode[1] (1:1-1:36, 0-35)',
439+
'└─0 ParagraphNode[3] (1:1-1:36, 0-35)'
440+
].join('\n')
441+
)
442+
})
443+
444+
await t.test(
445+
'inspect(…, {colors?: true | null | undefined})',
446+
async function () {
447+
const expectedOutput = [
448+
chalkEnabled.bold('RootNode') +
449+
chalkEnabled.dim('[') +
450+
chalkEnabled.yellow('1') +
451+
chalkEnabled.dim(']') +
452+
' ' +
453+
chalkEnabled.dim('(') +
454+
'1:1-1:36, 0-35' +
455+
chalkEnabled.dim(')'),
456+
chalkEnabled.dim('└─0') +
457+
' ' +
458+
chalkEnabled.bold('ParagraphNode') +
459+
chalkEnabled.dim('[') +
460+
chalkEnabled.yellow('3') +
461+
chalkEnabled.dim(']') +
462+
' ' +
463+
chalkEnabled.dim('(') +
464+
'1:1-1:36, 0-35' +
465+
chalkEnabled.dim(')')
466+
].join('\n')
467+
468+
const parsed = retext().parse(paragraph)
469+
470+
assert.equal(lines(inspect(parsed, {colors: true}), 2), expectedOutput)
471+
assert.equal(lines(inspect(parsed, {colors: null}), 2), expectedOutput)
472+
assert.equal(
473+
lines(inspect(parsed, {colors: undefined}), 2),
474+
expectedOutput
475+
)
476+
}
477+
)
423478
})
424479

425480
test('inspectNoColor()', async function () {

0 commit comments

Comments
 (0)