Skip to content

do not use color when stdout is not a terminal#51

Merged
vilmibm merged 1 commit intomasterfrom
colors
Nov 12, 2019
Merged

do not use color when stdout is not a terminal#51
vilmibm merged 1 commit intomasterfrom
colors

Conversation

@vilmibm
Copy link
Copy Markdown
Contributor

@vilmibm vilmibm commented Nov 6, 2019

This PR disables all escape codes when printing via utils if gh detects we're not attached to a terminal.

func makeColorFunc(color string) func(string) string {
return func(arg string) string {
output := arg
if isatty.IsTerminal(os.Stdout.Fd()) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This works for stdout, but what if we wanted to output color to stderr in case it's a tty, separately from stdout which might be redirected elsewhere?

I'm wondering whether it would be useful to have an object that could be initialized with an output stream and has color functions available on it that are all no-op unless the stream is a tty:

// within a Cobra command
out := cmd.OutOrStdout()
ansiStdout := utils.NewAnsi(out)
ansiStderr := utils.NewAnsi(cmd.OutOrStderr())
fmt.Fprintln(out, ansiStdout.Cyan("hello on stdout"))
cmd.Println(ansiStderr.Cyan("some error"))

Or maybe we can just cross that bridge when we get there?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I like how clear the interface @vilmibm came up with to handle the color, the complexity of showing or not showing color his hidden away (in a good way!). Maybe I'm underestimating how often stdout and stdin's tty status differs, but I feel like keeping the interface simple is more important at this point than having it work correctly in all scenarios.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sure, that's a good point @probablycorey. We don't have to worry about stderr for now until we need to print ansi codes to stderr too.

For clarity, these are some cases when stdout and stderr tty status differ:

gh pr list | pipe # stdout is to a pipe, stderr is tty
gh pr list 2>error.log # stdout is tty, stderr is file

@vilmibm vilmibm added the master label Nov 6, 2019
// This is really annoying. If you just define Bold as ColorFunc("+b") it will properly bold but
// will not use the default color, resulting in black and probably unreadable text. This forces
// the default color before bolding.
output = ansi.Color(ansi.DefaultFG+arg+ansi.Reset, "+b")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Bummer, but good catch!

func makeColorFunc(color string) func(string) string {
return func(arg string) string {
output := arg
if isatty.IsTerminal(os.Stdout.Fd()) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sure, that's a good point @probablycorey. We don't have to worry about stderr for now until we need to print ansi codes to stderr too.

For clarity, these are some cases when stdout and stderr tty status differ:

gh pr list | pipe # stdout is to a pipe, stderr is tty
gh pr list 2>error.log # stdout is tty, stderr is file

@vilmibm
Copy link
Copy Markdown
Contributor Author

vilmibm commented Nov 12, 2019

Going to merge this as-is since it fixes currently broken behavior. i'm totally into more nuanced support for the stderr/stdout divergence case @mislav brought up in the future -- we should have an issue for it 😉

@vilmibm vilmibm merged commit bd39fb1 into master Nov 12, 2019
@mislav mislav deleted the colors branch November 14, 2019 15:16
vilmibm pushed a commit that referenced this pull request Jun 29, 2021
Fixed parseFileArgs, reformatted tests
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