Skip to content
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

add optional colors: boolean flag to Options #21

Closed
wants to merge 4 commits into from

Conversation

rwe
Copy link
Contributor

@rwe rwe commented Sep 24, 2023

Initial checklist

  • I read the support docs
  • I read the contributing guide
  • I agree to follow the code of conduct
  • I searched issues and couldn’t find anything (or linked relevant results below)
  • If applicable, I’ve added docs and tests

Description of changes

Previously, inspect() would always add ANSI color sequences when on Node and always omit them otherwise. inspectNoColor() and inspectColor() were exported to override this, but that was a little bit clunky. This adds an optional colors: boolean flag to Options. (Note that this is the same flag name as in node:util's inspect).

The default behaviour is unchanged: this feature is purely additive.

Internally, this also introduces the colorization/stylization functions to State, allowing the no-color path to simply omit colorization, rather than stripping ANSI sequences after the fact.

@github-actions github-actions bot added 👋 phase/new Post is being triaged automatically 🤞 phase/open Post is being triaged manually and removed 👋 phase/new Post is being triaged automatically labels Sep 24, 2023
@rwe rwe changed the title feat: colors: boolean flag add optional colors: boolean flag to Options Sep 24, 2023
@rwe
Copy link
Contributor Author

rwe commented Sep 25, 2023

Rebased to resolve the trivial import-name conflict.

Copy link
Member

@ChristianMurphy ChristianMurphy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @rwe!
Appreciate you taking the time to contribute.
@wooorm would probably be the most insightful for reviewing this.
But is both traveling and is in the midst of releasing major versions across most of the 500+ unified ecosystem packages.
Appreciate your patience with the review.
If this is urgent, feel free to ping.

@ChristianMurphy ChristianMurphy requested a review from wooorm October 2, 2023 14:48
@rwe
Copy link
Contributor Author

rwe commented Oct 4, 2023

Thanks! It's not urgent, just some tinkering I felt might be useful to contribute.

For a little context where I was going with this before getting distracted-from-distractions, was to add a "depth" parameter and then use inspect from node:util instead of JSON.stringify in that environment. That would have been impossible or at least brittle with the post-format color erasure.

@wooorm
Copy link
Member

wooorm commented Oct 26, 2023

Thanks for your patience!

but that was a little bit clunky

If we’re getting more options, sure.

For a little context where I was going with this before getting distracted-from-distractions, was to add a "depth" parameter and then use inspect from node:util instead of JSON.stringify in that environment. That would have been impossible or at least brittle with the post-format color erasure.

Oh nice!


Hmm, I also feel the plural colors is not needed, color: boolean is what we use everywhere.
unifiedjs/unified-engine, vfile/vfile-reporter.
There’s something to say for consistency with node:util, but I’d prefer consistency with our stuff.


This needs docs!

Copy link
Member

@wooorm wooorm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like my review comments were never posted :'( Sorry about that.

Comment on lines +21 to +22
* @property {(_: string) => string} green
* Style a non-tree value.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you type the function explicitly somewhere? So @callback StyleFunction ..., and use that in each case here? Then we can describe parameters/return types

* Configuration.
* @returns {string}
* Pretty printed `tree`.
*/
export function inspectNoColor(tree, options) {
return inspectColor(tree, options).replace(colorExpression, '')
/* c8 ignore next 3 */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this ignored? Wouldn’t it be better to add a test?

return inspectColor(tree, options).replace(colorExpression, '')
/* c8 ignore next 3 */
const optionsWithNoColor = useColorByDefault
? Object.assign({}, options, {colors: false})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using spreads instead of object.assign is fine by me!

* @return string
* The first `count` lines of `text`.
*/
const lines = (text, count) => text.split('\n').slice(0, count).join('\n')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Describe the parameters, use a regular function, and move it to the bottom of the file. (and: use the braces for the return type, {string})

@@ -420,6 +430,51 @@ test('inspect()', async function (t) {
].join('\n')
)
})

await t.test('inspect(…, {colors: false})', async function () {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
await t.test('inspect(…, {colors: false})', async function () {
await t.test('should support `colors: false`', async function () {

})

await t.test(
'inspect(…, {colors?: true | null | undefined})',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'inspect(…, {colors?: true | null | undefined})',
'should support `colors: true` (default)',

@wooorm wooorm closed this in d5e0dcd Jul 16, 2024
@wooorm wooorm added the 💪 phase/solved Post is done label Jul 16, 2024

This comment has been minimized.

@github-actions github-actions bot removed the 🤞 phase/open Post is being triaged manually label Jul 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
💪 phase/solved Post is done
Development

Successfully merging this pull request may close these issues.

3 participants