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

Disabling colors for less and non-terminals #4

Open
trashcan opened this issue Jun 2, 2015 · 8 comments
Open

Disabling colors for less and non-terminals #4

trashcan opened this issue Jun 2, 2015 · 8 comments

Comments

@trashcan
Copy link

trashcan commented Jun 2, 2015

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's terminal.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.

@ttacon
Copy link
Owner

ttacon commented Jun 2, 2015

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!

@ttacon
Copy link
Owner

ttacon commented Jun 9, 2015

There's a WIP on 4f1bdd1, if you want you can take a look. Just reinstall with -tags smartmode.

@trashcan
Copy link
Author

trashcan commented Jun 9, 2015

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.

@ttacon
Copy link
Owner

ttacon commented Jun 9, 2015

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!

@trashcan
Copy link
Author

Maybe I'm an idiot but it's working now, but I'm seeing the behavior reversed. I have a call to chalk.DetectTerminal() and then my colors are disabled at the terminal, and enabled when piped to less.

Should have color:
https://www.dropbox.com/s/qb43ff8uopu9l1g/Screenshot%202015-06-09%2019.19.13.png?dl=0

Should not have color:
https://www.dropbox.com/s/8f8wz3amznwrsdu/Screenshot%202015-06-09%2019.19.27.png?dl=0 (less)
https://www.dropbox.com/s/2m1k6e4sfspzwek/Screenshot%202015-06-09%2019.20.09.png?dl=0 (less -R)

Let me know if I was doing the go build/install incorrect that I posted above.

@ttacon
Copy link
Owner

ttacon commented Jun 17, 2015

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.

@trashcan
Copy link
Author

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 golang.org/x/crypto/ssh/terminal's terminal.IsTerminal() to detect that. It will have a false positive in that you might be piping something to less -R or something else that understands ansi escape codes, but most apps have a --color option to force color output.

Here's how it looks in another application that supports detecting if the output is a terminal or if the output is being piped:
https://www.dropbox.com/s/t3xxr7u8snjtvn6/Screenshot%202015-06-17%2022.06.51.png?dl=0

Hope that helps!

@ttacon
Copy link
Owner

ttacon commented Jun 19, 2015

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!

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

No branches or pull requests

2 participants