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

util: add %c to ANSI transform for console.log() #49205

Open
wants to merge 19 commits into
base: main
Choose a base branch
from

Conversation

jcbhmr
Copy link
Contributor

@jcbhmr jcbhmr commented Aug 16, 2023

this is something that I saw already had a closed issue #29605 and I figured I should put my code where my mouth is and open a PR instead of waiting for a long-dead issue response. idk if i should open a new issue instead of a pr first?

I'm also not sure of the policy of nodejs around copying code from other places like from deno. deno already has this %c => ansi thing "solved" so i copied some code from there. idk if this is allowed or what. answered in comment

relevant deno bits:

i would think this is a good improvement idea but idk it was shot down in #29605 due to complexity concerns.

fix #52350

@nodejs-github-bot nodejs-github-bot added needs-ci PRs that need a full CI run. util Issues and PRs related to the built-in util module. labels Aug 16, 2023
@bnoordhuis
Copy link
Member

deno already has this %c => ansi thing "solved" so i copied some code from there. idk if this is allowed or what

It is, provided it's compatible with node's MIT license (which deno is also licensed under so that's okay.)

also is this %c support something that nodejs actually wants?

Good question. I personally don't see the value and very few people ever feature-requested it.

@jcbhmr
Copy link
Contributor Author

jcbhmr commented Aug 19, 2023

i think it works!
image

@jcbhmr jcbhmr changed the title [wip] lib: add %c ➡ ANSI for console.log() console: add %c ➡ ANSI for console.log() Aug 19, 2023
@jcbhmr

This comment was marked as outdated.

@jcbhmr jcbhmr marked this pull request as ready for review August 19, 2023 20:31
@jcbhmr jcbhmr marked this pull request as draft August 19, 2023 20:36
@jcbhmr

This comment was marked as outdated.

@jcbhmr jcbhmr marked this pull request as ready for review August 19, 2023 23:02
@jcbhmr
Copy link
Contributor Author

jcbhmr commented Aug 21, 2023

📫 pinging previous participants from #29605 @humphd @jasnell @devsnek

@jcbhmr jcbhmr changed the title console: add %c ➡ ANSI for console.log() lib: add %c ➡ ANSI for console.log() Aug 26, 2023
@humphd
Copy link
Contributor

humphd commented Aug 27, 2023

@jcbhmr very cool, thanks for pushing this forward!

@jcbhmr jcbhmr changed the title lib: add %c ➡ ANSI for console.log() lib: add %c -> ANSI for console.log() Aug 31, 2023
@jcbhmr jcbhmr changed the title lib: add %c -> ANSI for console.log() lib: add %c to ANSI transform for console.log() Sep 12, 2023
@jcbhmr jcbhmr changed the title lib: add %c to ANSI transform for console.log() util: add %c to ANSI transform for console.log() Sep 12, 2023
@jcbhmr
Copy link
Contributor Author

jcbhmr commented Oct 9, 2023

its been a month lol 😅 what would be the best way to go about getting feedback/progress on this PR? do i need to @ someone? what should i do? anything? just wait? 🤷‍♀️ idk

@Uzlopak
Copy link
Contributor

Uzlopak commented Oct 9, 2023

@nodejs/util

lib/internal/util/inspect.js Outdated Show resolved Hide resolved
lib/internal/util/inspect.js Outdated Show resolved Hide resolved
lib/internal/util/inspect.js Outdated Show resolved Hide resolved
lib/internal/util/inspect.js Outdated Show resolved Hide resolved
lib/internal/util/inspect.js Outdated Show resolved Hide resolved
lib/internal/util/inspect.js Outdated Show resolved Hide resolved
lib/internal/util/inspect.js Outdated Show resolved Hide resolved
lib/internal/util/inspect.js Outdated Show resolved Hide resolved
lib/internal/util/inspect.js Outdated Show resolved Hide resolved
lib/internal/util/inspect.js Outdated Show resolved Hide resolved
jcbhmr and others added 4 commits October 9, 2023 13:10
Co-authored-by: Jordan Harband <ljharb@gmail.com>
Co-authored-by: Jordan Harband <ljharb@gmail.com>
Co-authored-by: Jordan Harband <ljharb@gmail.com>
Co-authored-by: Jordan Harband <ljharb@gmail.com>
ljharb added a commit to ljharb/deno that referenced this pull request Oct 9, 2023
Upstream some changes from nodejs/node#49205

Signed-off-by: Jordan Harband <ljharb@gmail.com>
@ljharb
Copy link
Member

ljharb commented Oct 9, 2023

(went ahead and filed denoland/deno#20856 to upstream some of these suggestions)

@jcbhmr
Copy link
Contributor Author

jcbhmr commented Dec 19, 2023

ok i moved the stuff into a new file inspect_colors.js as suggested. its right next to the inspect.js. hopefully everything is still 🟢 lol.

@jasnell jasnell added the request-ci Add this label to start a Jenkins CI on a PR. label Dec 22, 2023
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Dec 22, 2023
@nodejs-github-bot
Copy link
Collaborator

@jasnell
Copy link
Member

jasnell commented Dec 23, 2023

There are relevant failures in CI on this.

@jcbhmr
Copy link
Contributor Author

jcbhmr commented Dec 23, 2023

AssertionError [ERR_ASSERTION]: These builtins are now unexpectedly loaded before pre-execution.
If this is intentional, add them to `expected.beforePreExec`.

# Note: loading more builtins before pre-execution can lead to startup performance regression or invalid snapshots.
- Consider lazy loading builtins that are not used universally.
- Make sure that the builtins do not access environment dependent states e.g. command line arguments or environment variables during loading.
- When in doubt, ask @nodejs/startup.

--- These could be added to expected.beforePreExec ---
[
  'NativeModule internal/util/inspect_colors'
]

heyyy @nodejs/startup, what do i do? should i lazy-load this when a %c thing is encountered? should i add it to the "expected.beforePreExec" (whatever that is)? should I go back to putting it in inspect.js instead of breaking it into a new file?

@jcbhmr
Copy link
Contributor Author

jcbhmr commented Dec 29, 2023

@nodejs/startup 👈 that ping works right? is there someone else who i am supposed to ping? idk. it all broke once i moved it to a new file lol.

@jcbhmr jcbhmr requested a review from jasnell March 1, 2024 01:47
@jcbhmr
Copy link
Contributor Author

jcbhmr commented Mar 6, 2024

ive added it to the test-bootstrap-modules.js safelist to fix the test error

@jcbhmr
Copy link
Contributor Author

jcbhmr commented Mar 10, 2024

is there anything else i need to do assuming the ci test passes? @nodejs/util @nodejs/startup

@jcbhmr
Copy link
Contributor Author

jcbhmr commented Mar 17, 2024

GH actions finally all passed ✅

@jcbhmr
Copy link
Contributor Author

jcbhmr commented Apr 6, 2024

@nodejs/util

@jcbhmr
Copy link
Contributor Author

jcbhmr commented Apr 10, 2024

Is there anything else I need to do? Anyone I need to ping?

@RedYetiDev
Copy link
Member

Hi! Do we think this feature would deprecate util.styleText?

@jcbhmr
Copy link
Contributor Author

jcbhmr commented Apr 12, 2024

Hi! Do we think this feature would deprecate util.styleText?

imo no. At least, I would not like to do that in this PR.

util.styleText is useful when you want to pass around a single string with all the color info baked into the string as ANSI colors/formatting without remembering the ANSI codes or importing a third party ANSI index/lib. This %c specifier is useful when you want to print a colorful message to the console in a cross platform way that works everywhere and degrades gacefully. These are two slightly different use cases that I think can coexist nicely.

This PR is about matching the existing feature set of browser and Bun and Deno in Node.js so that "%c works in all browsers and Bun and Deno but not Node.js" can become "%c works everywhere!"

console.log("%cWow this works in any JS console! 😎", "color:green")

👆 That's my goal with this PR

@RedYetiDev
Copy link
Member

Thanks for the information. I am +1 on this addition, and I look forward to its future!

@jcbhmr
Copy link
Contributor Author

jcbhmr commented Apr 16, 2024

@jasnell (since you helpfully commented earlier a few months ago) I believe I have:

  • addressed the CI failures from adding a new inspect_colors.js file in test-bootstrap-modules.js
  • added docs to console.md and util.md to indicate the updated behavior of %c
  • added appropriate MIT license attribution comments to the code referencing Deno
  • added some basic tests to make sure format() with %c results in ANSI

Is there anything else I need to do to get this merged?

@RedYetiDev
Copy link
Member

RedYetiDev commented Apr 16, 2024

The only thing I could think of might be squashing the commits, but that's not always needed

Copy link
Member

Choose a reason for hiding this comment

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

Great PR. We do have util.inspect.colors, instead of providing all of the math calculations, it may be more efficient to use that instead,

Other than that, I am +1 on this change!

Copy link
Contributor Author

@jcbhmr jcbhmr Apr 21, 2024

Choose a reason for hiding this comment

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

in fact since this pr has been made deno has updated their color console code to now use an object that looks exactly like util.inspect.colors lol https://github.com/denoland/deno/blame/2f5a6a8514ad8eadce1a0a9f1a7a419692e337ef/ext/console/01_console.js#L200

Copy link
Member

Choose a reason for hiding this comment

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

Maybe you could add that change to your PR?

@RedYetiDev RedYetiDev added the console Issues and PRs related to the console subsystem. label Apr 20, 2024
@RedYetiDev
Copy link
Member

@nodejs/console WDYT?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
console Issues and PRs related to the console subsystem. needs-ci PRs that need a full CI run. util Issues and PRs related to the built-in util module.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add console.log() %c support for parity with Bun, Deno, and browsers
8 participants