node
node copied to clipboard
util: add %c to ANSI transform for console.log()
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
fix #52350
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.
i think it works!
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. 😆
i think i fixed all the lint errors and i made some very basic tests.
📫 pinging previous participants from https://github.com/nodejs/node/issues/29605 @humphd @jasnell @devsnek
@jcbhmr very cool, thanks for pushing this forward!
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
@nodejs/util
(went ahead and filed https://github.com/denoland/deno/pull/20856 to upstream some of these suggestions)
I'm +1 on this moving forward but this needs docs.
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.
CI: https://ci.nodejs.org/job/node-test-pull-request/56455/
There are relevant failures in CI on this.
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?
@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.
ive added it to the test-bootstrap-modules.js safelist to fix the test error
is there anything else i need to do assuming the ci test passes? @nodejs/util @nodejs/startup
GH actions finally all passed ✅
@nodejs/util
Is there anything else I need to do? Anyone I need to ping?
Hi! Do we think this feature would deprecate util.styleText
?
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
Thanks for the information. I am +1 on this addition, and I look forward to its future!
@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 intest-bootstrap-modules.js
- added docs to
console.md
andutil.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?
The only thing I could think of might be squashing the commits, but that's not always needed
@nodejs/console WDYT?
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.
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 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
@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