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

turning colors on/off #445

Open
marcotrosi opened this issue Nov 23, 2024 · 13 comments
Open

turning colors on/off #445

marcotrosi opened this issue Nov 23, 2024 · 13 comments
Labels
enhancement New feature or request

Comments

@marcotrosi
Copy link

Hello everyone,

this is maybe more a question, that becomes a Feature Request if there is no simple solution yet, but at least I couldn't find anything in the documentation.

As you know some command line tools have something like a --no-color option to turn off all escape sequences for colors etc., or some do it automatically when they detect the tools is getting used in a pipe chain.

My first question would be "how do you implement such feature and is there some switch or other easy solution available in lipgloss?"

Describe alternatives you've considered
Currently I'm overwriting the Styles like this (just the basic concept, the code looks actually a bit different) ...

StyleBigger  = lipgloss.NewStyle().Foreground(lipgloss.Color("10"))
StyleSmaller = lipgloss.NewStyle().Foreground(lipgloss.Color("9"))

if NoColor {
    StyleBigger  = lipgloss.NewStyle()
    StyleSmaller = lipgloss.NewStyle()
    ...
}

Result = StyleBigger.Render(myString)

I want to avoid wrapping everything and having if-else statements everywhere.

Describe the solution you'd like
I thought maybe something simple like the following concept would be simple ...

StyleBigger  = lipgloss.NewStyle().Foreground(lipgloss.Color("10"))
StyleSmaller = lipgloss.NewStyle().Foreground(lipgloss.Color("9"))

if NoColor {
    lipgloss.ColorOff()
}

Result = StyleBigger.Render(myString)

Additional context
Of course in my example I only used 2 Styles, imagine it being many more.
And for every new Style I shall not forget to add also the overwriting.

thanks for your feedback upfront.
looking forward to learn how you guys do it
have a great day
--marco

@marcotrosi marcotrosi added the enhancement New feature or request label Nov 23, 2024
@testinfected
Copy link

IIUC you can use the following code today to do that:

func DisableColors() {
	os.Setenv("NO_COLOR", "true")
}

func ForceColors() {
	os.Unsetenv("NO_COLOR")
	os.Setenv("CLICOLOR_FORCE", "true")
}

func EnableColors() {
	os.Unsetenv("NO_COLOR")
	os.Unsetenv("CLICOLOR_FORCE")
	os.Setenv("CLICOLOR", "true")
}

@marcotrosi
Copy link
Author

thank you very much @testinfected
would you be so kind and show me where this is documented?
then I'll be able to find it myself in the future.

@testinfected
Copy link

Hey @marcotrosi my pleasure

You can find more info here: Standard for ANSI Colors in Terminals

I had the same issue as you a few months ago and dived into lipgloss source code to see this was properly supported.

@marcotrosi
Copy link
Author

marcotrosi commented Nov 28, 2024

thanks again @testinfected
one more question
let's say we have 3 tools in a pipe
t1 | t2 | t3
what does it mean for the tools and their output?
especially t3. is t3 in its own sub shell and unaffected or would it have the colors turned off too, as we set the env var?

edit: assuming t2 is my tool

@testinfected
Copy link

thanks again @testinfected one more question let's say we have 3 tools in a pipe t1 | t2 | t3 what does it mean for the tools and their output? especially t3. is t3 in its own sub shell and unaffected or would it have the colors turned off too, as we set the env var?

I believe the env vars would be inherited by the subshell right? In which case let's that you want to re-enable colors for t3 you would have to use something like:

t1 | t2 | (unset NOCOLOR; unset CLICOLOR_FORCE; CLICOLOR="true" t3)

(hope I got the syntax right)

@marcotrosi
Copy link
Author

as far as I know on unixoid systems env vars are only inherited to sub shells if one uses the export command

supporting the NO_COLOR solution is definitely a very good and needed feature, but looking at your proposed of changing the environment variables before piping into t3, I must say I would definitely prefer a proper command line parameter for t2

t1 | t2 --no-color | t3

basically back to my original request/question.

@testinfected
Copy link

I must say I would definitely prefer a proper command line parameter for t2

t1 | t2 --no-color | t3

Lipgloss doesn't have a CLI tool right?

@marcotrosi
Copy link
Author

marcotrosi commented Nov 28, 2024

the link you sent @testinfected perfectly states what I mean.
it says ..

Also consider letting the user override these with command-line options like --color and --color=WHEN ...

@marcotrosi
Copy link
Author

I must say I would definitely prefer a proper command line parameter for t2
t1 | t2 --no-color | t3

Lipgloss doesn't have a CLI tool right?

correct, lipgloss is only a lib, but I'm the one who has to take care of the cli options.

@testinfected
Copy link

testinfected commented Nov 28, 2024

Agreed, so we're in the same situation, I added the following to my CLI app:

--color=auto|always|never

Then I use the functions above depending on the flag value.

@marcotrosi
Copy link
Author

exactly, and now the question if you would have manipulated the behavior of t3 in the example?
See what I mean? It would be horrible if using an option in t2 would affect also t3 in the pipe example. That wouldn't make sense, right?

@marcotrosi
Copy link
Author

Your link shows it perfectly. Again thanks for sharing it. We need both, NO_COLOR and cli options, but we shouldn't solve the cli options by setting the env vars, this will cause problems from what I see. Do you agree?

@testinfected
Copy link

Sure does. It's only a workaround for now. I hope #293 will help here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants