Skip to content

Expose IsColorForced#117

Merged
mislav merged 1 commit intocli:trunkfrom
heaths:is-color-forced
Apr 20, 2023
Merged

Expose IsColorForced#117
mislav merged 1 commit intocli:trunkfrom
heaths:is-color-forced

Conversation

@heaths
Copy link
Contributor

@heaths heaths commented Apr 12, 2023

Since IsColorDisabled - based on env vars - is public, also expose IsColorForced so the gamut of GitHub CLI env vars for color overrides are available to callers.

This is desirable when using something other than term which does not allow for mocking or otherwise capturing output. Since I use another library, access to APIs that encapsulate the various env vars the GitHub CLI (and extensions using this module) is ideal so my extensions can do a better job at matching the behavior of the CLI.

Since IsColorDisabled - based on env vars - is public, also expose
IsColorForced so the gamut of GitHub CLI env vars for color overrides
are available to callers.
Copy link
Contributor

@samcoe samcoe left a comment

Choose a reason for hiding this comment

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

This seems reasonable to expose to me. My only thought is that I don't love the name. The only other option I could come up with was IsColorEnabledForcibly which is a bit verbose but I think more descriptive. Thoughts?

@samcoe samcoe self-assigned this Apr 19, 2023
Copy link
Contributor

@mislav mislav left a comment

Choose a reason for hiding this comment

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

I'm up for adding this for symmetry with IsColorDisabled. However, I think that a typical caller should still instantiate a Term and then call t.IsColorEnabled() on it to get the “final” state of color enabledness after all the conditions have been applied.

BTW for mocking input and capturing output you could always wrap Term in an application-specific type and override In(), Out(), and ErrOut() streams there.

@heaths
Copy link
Contributor Author

heaths commented Apr 19, 2023

@mislav sure, but to mock whether it's a TTY - which only checks stdout (redirects can be separate, of course, for stdout and stderr), whether color is enabled, etc., you end up wrapping the whole thing. And if you want to add in theme a la charmbracelet there's even more work. When cli/go-gh was initially proposed, I was hoping you'd port iostreams from cli/cli - it's powerful - but ended up doing something similar with heaths/go-console that I've been using in pretty much all my CLI-related modules.

As for the name, whatever you guys like is fine. :) I just what was there public. Alternatively, what about just wrapping both into an IsColorEnabled e.g.,

func IsColorEnabled() bool {
  return os.Getenv("CLICOLOR_FORCE") != "" && os.Getenv("CLICOLOR_FORCE") != "0" ||
         os.Getenv("NO_COLOR") == "" || os.Getenv("CLICOLOR") != "0")
}

That would seem a lot easier for external callers: a single check that wraps the logic of cli/cli.

@mislav mislav merged commit ccb69a6 into cli:trunk Apr 20, 2023
@mislav
Copy link
Contributor

mislav commented Apr 20, 2023

Alternatively, what about just wrapping both into an IsColorEnabled

I wanted to avoid wrapping too many checks in a single function call because that wouldn't be granular enough. For instance, color enabledness is not just a result of environment variables, but also whether stdout is a terminal.

I was hoping you'd port iostreams from cli/cli - it's powerful

The term package is an extraction from iostreams but I intentionally didn't want to export everything from iostreams because it's too large, unwieldy, and conflates too many concepts under a single level of abstraction.

@heaths heaths deleted the is-color-forced branch April 20, 2023 15:29
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