node icon indicating copy to clipboard operation
node copied to clipboard

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

Open jcbhmr opened this issue 1 year ago • 37 comments

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:

  • small basic CSS prop splitter https://github.com/denoland/deno/blob/ece2a3de5b19588160634452638aa656218853c5/ext/console/01_console.js#L2821
  • CSS to ansi https://github.com/denoland/deno/blob/ece2a3de5b19588160634452638aa656218853c5/ext/console/01_console.js#L2933
  • the actual %c substitution https://github.com/denoland/deno/blob/ece2a3de5b19588160634452638aa656218853c5/ext/console/01_console.js#L3115
i would think this is a good improvement idea but idk it was shot down in #29605 due to complexity concerns.

fix #52350

jcbhmr avatar Aug 16 '23 21:08 jcbhmr

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.

bnoordhuis avatar Aug 19 '23 10:08 bnoordhuis

i think it works! image

jcbhmr avatar Aug 19 '23 20:08 jcbhmr

time to figure out how to add tests 😵 i also need to actually run the linter. and make sure i did prrimordials stuff right. and make sure it fits the style guide. 😆

jcbhmr avatar Aug 19 '23 20:08 jcbhmr

i think i fixed all the lint errors and i made some very basic tests.

jcbhmr avatar Aug 19 '23 22:08 jcbhmr

📫 pinging previous participants from https://github.com/nodejs/node/issues/29605 @humphd @jasnell @devsnek

jcbhmr avatar Aug 21 '23 00:08 jcbhmr

@jcbhmr very cool, thanks for pushing this forward!

humphd avatar Aug 27 '23 16:08 humphd

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

jcbhmr avatar Oct 09 '23 17:10 jcbhmr

@nodejs/util

Uzlopak avatar Oct 09 '23 17:10 Uzlopak

(went ahead and filed https://github.com/denoland/deno/pull/20856 to upstream some of these suggestions)

ljharb avatar Oct 09 '23 18:10 ljharb

I'm +1 on this moving forward but this needs docs.

jasnell avatar Oct 28 '23 16:10 jasnell

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.

jcbhmr avatar Dec 19 '23 02:12 jcbhmr

CI: https://ci.nodejs.org/job/node-test-pull-request/56455/

nodejs-github-bot avatar Dec 22 '23 04:12 nodejs-github-bot

There are relevant failures in CI on this.

jasnell avatar Dec 23 '23 17:12 jasnell

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 avatar Dec 23 '23 17:12 jcbhmr

@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 avatar Dec 29 '23 22:12 jcbhmr

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

jcbhmr avatar Mar 06 '24 18:03 jcbhmr

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

jcbhmr avatar Mar 10 '24 22:03 jcbhmr

GH actions finally all passed ✅

jcbhmr avatar Mar 17 '24 16:03 jcbhmr

@nodejs/util

jcbhmr avatar Apr 06 '24 23:04 jcbhmr

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

jcbhmr avatar Apr 10 '24 14:04 jcbhmr

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

redyetidev avatar Apr 12 '24 17:04 redyetidev

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

jcbhmr avatar Apr 12 '24 18:04 jcbhmr

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

redyetidev avatar Apr 16 '24 17:04 redyetidev

@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?

jcbhmr avatar Apr 16 '24 19:04 jcbhmr

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

redyetidev avatar Apr 16 '24 19:04 redyetidev

@nodejs/console WDYT?

redyetidev avatar Apr 21 '24 12:04 redyetidev

If implemented, this (IMO) would be a semver-minor change, as it adds a new control code to the console (%c).

I curious for a review from the team, as it's been several months since this was first made, and it's possible there are changes that need to be made

[!TIP] I'm a member of the triage team, and not a core collaborator.

redyetidev avatar May 05 '24 23:05 redyetidev

Can you please rebase and fix the linter errors please?

lib/internal/util/inspect_colors.js
   5:3  error  Out of ASCIIbetical order - SafeMap >= RegExpPrototypeExec                node-core/alphabetize-primordials
   6:3  error  Out of ASCIIbetical order - RegExpPrototypeExec >= NumberParseInt         node-core/alphabetize-primordials
   7:3  error  Out of ASCIIbetical order - NumberParseInt >= MathMax                     node-core/alphabetize-primordials
   9:3  error  Out of ASCIIbetical order - MathMin >= MathAbs                            node-core/alphabetize-primordials
  12:3  error  Out of ASCIIbetical order - StringPrototypeTrim >= ArrayPrototypePush     node-core/alphabetize-primordials
  13:3  error  Out of ASCIIbetical order - ArrayPrototypePush >= ArrayPrototypeIncludes  node-core/alphabetize-primordials
  15:3  error  Out of ASCIIbetical order - StringPrototypeSplit >= ArrayIsArray          node-core/alphabetize-primordials

aduh95 avatar May 11 '24 14:05 aduh95

@aduh95 i have rebased against upstream/main and fixed the lint-js errors (I did it manually; is there an automated way like lint --fix or something?) and squashed all commits into one

jcbhmr avatar May 15 '24 21:05 jcbhmr

@aduh95 i have rebased against upstream/main and fixed the lint-js errors (I did it manually; is there an automated way like lint --fix or something?) and squashed all commits into one

AFAIK make lint-js-fix

redyetidev avatar May 15 '24 22:05 redyetidev