Skip to content

Conversation

addaleax
Copy link
Collaborator

Don’t assume that process.stdout.isTTY is a good indicator
for coloring strings:

  • Not all terminals have color support
  • We should honour environment variables like NO_COLOR
  • Not all formatted strings end up printed to stdout

Also, don’t tell the Node.js repl that we are working with a terminal
if we don’t know so for sure.

Copy link
Collaborator

@mcasimir mcasimir left a comment

Choose a reason for hiding this comment

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

Nice!

getFormatOptions(): { colors: boolean } {
return {
colors: this.repl ? this.repl.useColors :
process.stdout.isTTY && process.stdout.getColorDepth() > 1
Copy link
Collaborator

Choose a reason for hiding this comment

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

nice stuff!

}
throw new Error(
` ${clr(i18n.__(UNKNOWN), ['red', 'bold'])} ${clr(parameter, 'bold')}
` ${clr(i18n.__(UNKNOWN), ['red', 'bold'], { colors: true })} ${clr(parameter, 'bold', { colors: true })}
Copy link
Collaborator

Choose a reason for hiding this comment

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

why here is ok to set colors= true?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It’s not really, but we’re also not explicitly printing anything here … if I understand correctly, this gets passed down to the Node.js error handler itself

So a “clean” solution would be e.g. creating an error object here that has a colored and a non-colored message, and then handling that at the parseCliArgs call side in src/run.ts and deciding what to print from there … but I’m not sure if that’s worth the extra complexity? Especially that we’re targeting scripted usage with the color-on/color-off handling, and wouldn’t typically contain argument name misspellings (or not for a long time :))

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fwiw, I’ve made this use the same thing as above, i.e. basically assume that this ends up on stderr (which I think is accurate)

terminal: true,
breakEvalOnSigint: true,
preview: false,
terminal: process.env.MONGOSH_FORCE_TERMINAL ? true : undefined
Copy link
Collaborator

Choose a reason for hiding this comment

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

nice and simple!

};

const originalEditorAction = this.repl.commands.editor.action.bind(this.repl);
if (this.repl.commands.editor) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍


export default function clr(text: string, style: any): string {
return process.stdout.isTTY ? ansi.format(text, style) : text;
export default function clr(text: string, style: string|string[], options: { colors: boolean }): string {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Wouldn't be a bit confusing without context for a clr function (that i believe stands for color) to take a color true | false argument?

Should we rename something, or do something to make this clear?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I would absolutely be in favor of renaming clr to something more expressive :) I’ll try to think of something

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I’ve renamed the functions themselves but not the places where they were imported, still not ideal but at least not that strongly abbreviated

Don’t assume that `process.stdout.isTTY` is a good indicator
for coloring strings:

- Not all terminals have color support
- We should honour environment variables like `NO_COLOR`
- Not all formatted strings end up printed to stdout

Also, don’t tell the Node.js repl that we are working with a terminal
if we don’t know so for sure.
Copy link
Contributor

@lrlna lrlna left a comment

Choose a reason for hiding this comment

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

This looks good to me! Thanks for this clean up ^__^

@addaleax addaleax merged commit f1f5afc into master Oct 20, 2020
@addaleax addaleax deleted the cli-repl-color branch October 20, 2020 10:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants