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

Terminal colors built-in to Node #42770

Closed
Exortions opened this issue Apr 18, 2022 · 8 comments
Closed

Terminal colors built-in to Node #42770

Exortions opened this issue Apr 18, 2022 · 8 comments
Labels
feature request Issues that request new features to be added to Node.js. stale

Comments

@Exortions
Copy link

What is the problem this feature will solve?

Having to download external libraries like chalk, colors, etc.
This also aids the scenario where programmers are without package managers. Sometimes, one standalone file in JavaScript is best. Implementing this feature would aid those individuals, and also reduce the bundle size because no package managers would have to be used for a simple JavaScript program.

Using Yarn or NPM makes a node_modules folder, which is especially large by only adding one dependency just for terminal colors.

Hopefully, you take this issue into consideration, and I would be happy to help implement a such feature.

What is the feature you are proposing to solve the problem?

An enumeration of ANSI escape codes for users to easily access to change the color of the terminal.

Example usage of this would be:

console.log(`${Colors.PURPLE}${Colors.ITALIC}This is italicized purple text`

What alternatives have you considered?

One alternative (already exists) would be to use a package manager like Yarn or NPM to download a package like 'chalk' or 'colors'.
However, Node.JS users not using package managers would be unable to accomplish this.

@Exortions Exortions added the feature request Issues that request new features to be added to Node.js. label Apr 18, 2022
@Trott
Copy link
Member

Trott commented Apr 18, 2022

I know this isn't what you're asking for exactly, but just in case anyone wants to be able to use these codes right now without a library:

List of built-in colors:

console.log(util.inspect.colors)

Note that despite it being named colors, it contains non-color formatting too like italic. They are also in the docs.

Creating/using ANSI codes from these:

const util = require('util')
const formatTxt = (format, txt) =>  `\u001b[${format[0]}m${txt}\u001b[${format[1]}m`;
const blue = util.inspect.colors.blue
const italic = util.inspect.colors.italic
console.log(formatTxt(blue, 'This is blue text'))
console.log(formatTxt(italic, 'This is italic text'))
console.log(formatTxt(blue, formatTxt(italic, 'This is blue, italic text')))

A robust library would also likely do things like check the return value of tty.hasColors() and omit the ANSI codes if that is false.

@BobFrankston
Copy link

BobFrankston commented Apr 22, 2022

console.log already supports colors in Visual Studio Code with warnings and errors highlighted. There is also the %c which works in Visual Studio Code. Doing the same for console output in the command shell (terminal) would be valuable. At the very least, it would match the error handling capabilities found when running under VSC.

The problem with using escape sequences is that they often produce spurious output on devices without VT-100 emulation. It would be more consistent to share the same mechanism.

@Trott
Copy link
Member

Trott commented Apr 22, 2022

console.log already supports colors in Visual Studio Code with warnings and errors highlighted.

So does Node.js, although we could do more with color than we do perhaps. Right now, for example, in the following error, strings are highlighted in green and parts of the stack trace that are internal are slightly greyed out a bit, but that's it.

image

There is also the %c which works in Visual Studio Code. Doing the same for console output in the command shell (terminal) would be valuable.

That would require adding CSS support to Node.js, which is neither a small undertaking nor something very likely to happen in the near future. I suppose we could consider supporting a tiny subset of CSS, but I'm inclined to be skeptical of that approach. (UPDATE: I see that %c is already limited to a subset of CSS. Perhaps this is more workable than I thought, although still very much not trivial I imagine. 🤔 Refs: https://developer.mozilla.org/en-US/docs/Web/API/console)

The problem with using escape sequences is that they often produce spurious output on devices without VT-100 emulation. It would be more consistent to share the same mechanism.

Unless we're going to do what VS Code does and basically ship an entire Chromium browser along with Node.js, I'm not sure there's going to be any getting around those limitations. (Maybe I'm misunderstanding what you mean.)

@BobFrankston
Copy link

Thanks, I see there is already some color capability for error messages. Yes, the subset for VSC is exactly what I want so that it works in both environments without having to depend on particular libraries. The goal is to be able to highlight some messages.

As you note, going further is a major step and not necessary for the modest purpose of highlighting messages.

@hemanth
Copy link
Contributor

hemanth commented May 2, 2022

util.inspect.formatText would be a good addition.

@ljharb
Copy link
Member

ljharb commented May 2, 2022

cc @nodejs/util

It would be a worthy goal to eliminate the need for colors/chalk; i don’t think new general-purpose formatting APIs are particularly valuable.

@github-actions
Copy link
Contributor

github-actions bot commented Dec 7, 2022

There has been no activity on this feature request for 5 months and it is unlikely to be implemented. It will be closed 6 months after the last non-automated comment.

For more information on how the project manages feature requests, please consult the feature request management document.

@github-actions github-actions bot added the stale label Dec 7, 2022
@github-actions
Copy link
Contributor

github-actions bot commented Jan 7, 2023

There has been no activity on this feature request and it is being closed. If you feel closing this issue is not the right thing to do, please leave a comment.

For more information on how the project manages feature requests, please consult the feature request management document.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Jan 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Issues that request new features to be added to Node.js. stale
Projects
Development

No branches or pull requests

5 participants