Comments (8)
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!
from chalk.
There's a WIP on 4f1bdd1, if you want you can take a look. Just reinstall with -tags smartmode.
from chalk.
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.
from chalk.
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!
from chalk.
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.
from chalk.
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.
from chalk.
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!
from chalk.
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!
from chalk.
Related Issues (3)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from chalk.