-
Notifications
You must be signed in to change notification settings - Fork 257
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
#285 add color output for mage list #301
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs to be configurable... I think we should default to off, since that's how it has worked in the past. I'd also like to offer configuration for what color for the labels.
I suggest making the config an environment variable - MAGEFILE_ENABLE_COLOR. Use strconv.ParseBool to detect if it's true or false (and false if not set or set to something weird). And then let people set MAGEFILE_TARGET_COLOR to something different if they don't want cyan.
Black: \u001b[30m
Red: \u001b[31m
Green: \u001b[32m
Yellow: \u001b[33m
Blue: \u001b[34m
Magenta: \u001b[35m
Cyan: \u001b[36m
White: \u001b[37m
(black or white might make sense if they have a colored background)
Maybe also the bright colors:
Bright Black: \u001b[30;1m
Bright Red: \u001b[31;1m
Bright Green: \u001b[32;1m
Bright Yellow: \u001b[33;1m
Bright Blue: \u001b[34;1m
Bright Magenta: \u001b[35;1m
Bright Cyan: \u001b[36;1m
Bright White: \u001b[37;1m
Oh, and since we're adding environment variables, we'll need to update the docs here:
https://github.com/magefile/mage/blob/master/site/content/environment/_index.en.md
Thanks! This is a cool feature that I'm sure people will enjoy (I'll definitely turn it on!)
mage/template.go
Outdated
// windows cmd.exe, powerShell.exe | ||
terminalSupportsColor := func() bool { | ||
envTerm := os.Getenv("TERM") | ||
if strings.Contains(envTerm, "xterm") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This kinda skeeves me out. Are we sure there are no existing terminals that include the substring xterm that don't support color? (I ask because I have no clue)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, the best I could find was that there's "xterm-mono". I've added a condition to disable terminal support if this is detected.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can say from experience that this topic could be a separate study course 🙉
Here's a list of possible terminal identifiers from my color theme project for GNU dircolors (the tool that provides the color database for the GNU ls
command by populating the LS_COLORS
environment variable).
The list contains a lot more IDs and I'm sure it still misses some, e.g. new terminals like Alacritty bring their own new identifiers and terminfo definitions.
Anyway, maybe it's easier to only check for terminal IDs that are definitely known to not support colors and let all other IDs pass. This way the list could be extended from time to time when users report terminal IDs that are not checked yet. Since this is a opt-in feature it should not break anyone's output.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
only check of terminal IDs that definitely known to not support colors and let all others IDs pass
Great idea! @natefinch any objections? The code would get significantly simpler.
// Terminals which don't support color:
// TERM=vt100
// TERM=cygwin
// TERM=xterm-mono
var noColorTerms := [3]string{"vt100", "cygwin", "xterm-mono"}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I missed this was a question for me. Yes, let's do that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool - done, see the update.
I've used a map rather than a list for performance, so that I don't need to iterate over it and just use the terminal name as a key.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"It works on my machine" including all tests, but having some trouble with tests in Travis CI. Trying to find out what is the TERM set to...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good now. Ready for a merge & release?
Sorry this sat around so long, I can't believe it's been 23 days. Time no longer has meaning, I guess. I love that you did this without requiring an import of a 3rd party library. |
No problem at all, my time is also completely skewed - can't believe it's almost June! I'll crack on the suggestions :-) Thank you |
…ET_COLOR env vars
- also handling xterm-mono, so that it forces non-color output
@natefinch please have a look if it looks sensible and I'll add more tests around it. I know, it's not really TDD and so on, but it would be quite difficult to follow in this case while I was just exploring the best approach. Please note I've left the new I specifically wanted to use plain English names for the colors, as opposed to the cryptic ANSI codes. So that has expanded the code quite a bit. But I think it's worth it. We don't want to add external dependencies, so I've copied Exploratory manual test scenarios:
|
This looks great. Lemme see if anyone else wants to get eyes on it. |
* use a list of specfic terminals which don't support color
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM with a few nitpicky changes.
I've chosen a cyan color, because it's a good contrast on a typically black terminal (better than e.g. blue) and it's neutral, as opposed to e.g. green, red, yellow.
Added one test to verify the colorized output. All other tests remain without the colorized output, because I unset the TERM env var, using
resetTerm
func. I've added a call to theresetTerm
into thetestmain
func, but it wasn't enough and some tests were still failing so I've added an explicit call to theresetTerm
to them as well.