-
Notifications
You must be signed in to change notification settings - Fork 21
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
Disabling colors for less and non-terminals #4
Comments
Never thought about it before, but this is probably a feature I'd like myself. I have an idea of how to do it. I'll give it a shot tomorrow or the day after. Thanks for the feature request! |
There's a WIP on 4f1bdd1, if you want you can take a look. Just reinstall with -tags smartmode. |
Stupid question, how do I do that? I switched to the tty-version branch locally and installed it with go install (tried go build -tags smartmode), but that just disables color for me. |
My bad! Pull it down and try again, I had quite the significant typo in the last commit - that's what I get for rushing it! |
Maybe I'm an idiot but it's working now, but I'm seeing the behavior reversed. I have a call to Should have color: Should not have color: Let me know if I was doing the go build/install incorrect that I posted above. |
So, last week I didn't sleep alot (I'm the idiot) ... XP. I've now got it fixed so that DetectTerminal needs the file descriptor of the file it's going to write to (so if it's stdin, it works like as normal with all it's beautiful color, if it's a file, no colors). HOWEVER - this is tickling a larger problem I hadn't envisioned chalk being used for (basically being able to toggle color streams depending if you want color or not). I'll make a larger commit later with a clean API for using the detection of a tty device for color or just plain ol' turning color off temporarily. As for the situation where you're piping to less, I'm not actually sure it can be done. This is because to detect whether or not color is supported we need access to the file descriptor we're writing to. But if we're piping to less, the shell is redirecting the output meaning we still think we're writing to stdout. At least this is how I see it at the moment, I have some ideas for some bash magic to try to tomorrow and I'll let you know how those go. Let me know if you have any ideas! Also, as an example, there's an example which doesn't have output as it uses a file. |
Hey, no need to apologize, you're doing me a favor. You definitely can detect when the output is being piped and not being sent to the terminal. Try Here's how it looks in another application that supports detecting if the output is a terminal or if the output is being piped: Hope that helps! |
Turns out I had missed resetting one value and wasn't checking for a sentinel value in one method so when I piped to less it didn't seem to work (even though the color codes weren't there). Should be good now on that branch. Having said that, I am going to make a larger change to support this on master - hopefully I can get some time this weekend, if not I'm moving it towards the top of my plate. You're also welcome to take a stab at it! |
Any suggestions on how to disable colors when the output is not to a terminal that supports color?
I can use
golang.org/x/crypto/ssh/terminal
'sterminal.IsTerminal
to detect it, but not sure how to go about modifying chalk to support this. Ideally I'd like to add color in my code, and then have chalk return nothing when it detects this so I don't have to write a colored and non-colored version.I tried overwriting the colors (chalk.Black, chalk.Red etc) from my project but that doesn't seem to work correctly.
The text was updated successfully, but these errors were encountered: