-
Notifications
You must be signed in to change notification settings - Fork 34
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
Embrace Standardized Color Control #15
Comments
Relevant internals thread: https://internals.rust-lang.org/t/terminal-platform-abstraction/6746 |
I've looked at
I would in principle support coalescing around one crate that just did color detection, but on that same token, I'd also like to see people coalesce around one crate for dealing with a Windows console for coloring, e.g., |
@BurntSushi for what it's worth I do not really want to defend either the crate nor the defaults. I mostly just want a crate that holds the state of color on/off with a sensible default.
The reason I never ended up using termcolor was that it does not actually (to the best of my knowledge) store the state of color on/off. It just has the auto color choice which derives the default for the writers. You cannot query it and ask: does the user want colors. (My goal effectively is that I can parse |
The way I would like to see terminal colours work is a wrapper around std(out/err) that interperets ansi escape sequences. If we are on windows before windows 10, we have to use the win32 api to process escape sequences, but on most platforms we can just pass them through. There is also terminfo/cap that should be respected. If we do this, everyone just uses ansi escape sequences, and the wrapper can choose to discard them based on anything (TERM, configuration, etc.). Idea from this post.
Using this option, disabling colors would happen in the consuming crate, all crates further down the chain just write their ansi escape sequences as usual. A bit of code: struct AnsiPolyfillWrapper<W: io::Write>(W);
impl<W: io::Write> io::Write for AnsiPolyfillWrapper<W> {
fn write(&mut self, buf: &[u8]) -> Result<usize> {
// read buf and if necessary modify the escape codes, with many write calls, otherwise
self.0.write(buf)
}
} |
I explicitly chose to not respect this, at least for things like colors or other simplistic styling like underlining or bold. The reason why is because when I did use a library that respected terminfo, I got numerous bug reports where the wrong escape codes were being emitted. Instead, I chose to follow in the footsteps of GNU grep: just stick to the basics. GNU grep is widely deployed, and I never hear anyone complaining about whether its colors work or not, which is compelling evidence from my perspective that it has the right approach here. For CLI tools that need to do more extensive manipulation of the terminal, then this may be the wrong calculus. In general, I agree that an ANSI interpreter would be great, and it would, for example, slide nicely into |
@BurntSushi what do you think are the most pressing issues in this area? Sorry if this question is answered elsewhere. |
@derekdreery you still need to know if colors are on or off for other reasons. For instance I shell out to other processes which will typically not detect color correctly so I want to propagate my understanding of if color is on or off to that subprocess. WRT to the wrapping stream: I agree that would be nice. Sadly right now there is only an undocumented API used by rusttest to intercept prints. (Also now that windows 10 supports ansi colors after enabling i'm hoping we can just pretend ansi color is a thing and get rid of more complex wrappers around it) |
@derekdreery The hardest possible problem: getting people to agree on one library. It may be the case that you need an ANSI interpreter to do that, because @mitsuhiko's idea about at least getting all of them to agree on whether colors are enabled or not (and permit the caller to control) is a really nice middle ground and possibly much easier to achieve. I would certainly be on board with doing that for |
The idea of interpreting ANSI codes alarms me for a number of reasons:
All that said, it may still be the case that the ease and flexibility of a custom ANSI parser outweighs all the downsides, in which case fair enough. However, taking a cue from the "cross site scripting" analogy, I'd be interested to see/use a terminal colour API based on the ideas of HTML templating libraries like Elm's HTML DSL, ScalaTags or rust-tags. That is, building up a type-safe data-structure that represents text and its formatting, which can be stored and later serialized to ANSI-codes or Windows console API calls or HTML with inline CSS or whatever. That would hopefully have all the thread-safety advantages of ripgrep, with a more ergonomic and teachable API. |
@Screwtapello you raise a lot of good points I hadn't considered! Briefly, I will just respond to one thing here, and that's the idea of building up something more structured. In particular, for tools like ripgrep, printing output is a performance sensitive aspect of execution. It is likely that any overhead associated with emitting colors would need to be avoided, and I imagine execution templates probably wouldn't be acceptable. With that said, a convenient API doesn't need to solve every use case, so long as there are lower level APIs for when that is necessary. |
The wider discussions a bout ansi, terminal abstractions etc. are quite complex which is why i thought about reducing this problem to initial color detection and control. I think it might make sense to have a crate like clicolors control that just has a flippable global and then maybe various compile features to pick a sensible default (on, off, CLICOLOR, term detection etc.) Then it should be easy for everybody to use it as a base and the rest of the discussion can be held separately. |
@Screwtapello Thanks for explaining all that! I had not considered ideas like cross-site scripting. What I'm trying to define is the most general, minimal level, and fast API that can do everything that's required.
We would draw a line somewhere, probably text styling and cursor movement to start with. This line could be moved in future, and if people used the library it would show where the gaps are and what features are most missed. These could be prioritised.
I think there are a few options here. I believe that in production (release mode) the correct thing to do is to strip out these sequences, but in debug mode it may be useful to log their presence.
If someone comes to you with a formatting issue, you can hand them a version of the program compiled to write out a warning when it comes across an unexpected sequence.
I'd argue this would just be a bug that should be fixed. Any implementation may have bugs.
I agree. This library should not be for user consumption. An alternative library should be advertised that implements a programmatic API, using the type system to prevent invalid input. But we still need the implementation of that API, and I propose that this is the best way to do it, or at least that it is worth investigation.
I need to think more about this to fully understand the risks. I think it would be the role of the higher-level API to sanitize any input, so that escape sequences are only emitted from the API calls. This would need careful auditing. So, in summary you would have 2 levels
If you went straight for a higher level API you still have the issue of what to do with escape sequences in the input. Should you remove them, or leave them in? If you remove them, how do you know you are getting them all? @mitsuhiko what are the specific things that are missing now that you would like to see as a minimum? |
@derekdreery there needs to be an agreed upon way to turn color on and off. clicolors-control exists but nobody uses it and @BurntSushi outlined some of his reasons for not doing it. So what I can propose right now is to take that crate, incorporate his suggestions but I won't be able to force the rest of the community to use it :) |
Could you simplify it to:
|
FYI: There is an attempt to make standard with |
I've played with this a bit, reading the xterm guide. I've also been reading the standard. My plan is to implement the ansi standard, at least to recognise all escape sequences specified there. Then I can process a subset of them and skip the rest. For the issue with hostile escape sequence injection, any utf-8 sanitized text will not contain escape sequences. Therefore it is sufficient to check that input is utf-8 (see the xterm guide near the beginning for more details). |
@derekdreery If I'm understanding you right, I'm not sure that will work. In particular, assuming or requiring that output (or input) is UTF-8 is unfortunately inappropriate in almost any UNIX command line tool. |
@BurntSushi my argument is that you can avoid injection by using utf8, but that if you are not then there may be escape sequences present. My approach (at least to start with) will be to just strip any escape sequence as defined in the spec (including things like STX), and add back in the ones that I'm willing to handle for the given platform. I think what I'm doing is an experiment at this point. I think having a working (by my probably incorrect definition) library will help to spark more debate. |
I think you're talking about C1 control codes, which overlap with UTF-8 extension bytes. While it's true that a C1 control code like 0x9B (CSI, Control Sequence Introducer) is not directly valid in UTF-8, terminals date back to a time when only 7-bit ASCII was reliably available, so every C1 control-code has a 7-bit encoding of |
@Screwtapello you are correct, I'm still learning :). |
@iquiw there was an earlier attempt to standardize on |
One thing I would like to point out when developing a terminal color API is the annoyance of testing it. It should be easy to:
I wrote termstyle to address some of these issues, but I thought it would be good to bring them up here. (not saying termstyle is the be-all-end-all -- I've only just started using it myself). I totally agree that the actual coloring should be done through a single low-level (standardized) crate. I didn't actually know the trick with windows and I'm now inclined to use ANSI escape codes for all platforms. |
For using colours in CLI output, also consider that for example Terminfo may include other control codes in its colour sequences, which are fine for a TUI but not what you want in this case. |
Considering that a CLI tool (but not a TUI) generally needs to handle having its output redirected to a file and then read later in a pager, this really cuts down on what ANSI sequences need to be handled. Other ANSI sequences might be used inside a readline-like library, but this is not visible outside of that library, and wouldn't get into a log file from non-interactive use of the CLI tool (e.g. where there is no use of stdin, or stdin is redirected from a file). |
If anyone's interested on progress I'm currently gaining the ability to
recognise all ansi escape sequences according to the spec. At that point
I'll be able to strip escape sequences. After that, I will start to
interperest some of the more simple sequences like colour.
…On 9 Mar 2018 18:20, "Jim Peters" ***@***.***> wrote:
Considering that a CLI tool (but not a TUI) generally needs to handle
having its output redirected to a file and then read later in a pager, this
really cuts down on what ANSI sequences need to be handled.
Other ANSI sequences might be used inside a readline-like library, but
this is not visible outside of that library, and wouldn't get into a log
file from non-interactive use of the CLI tool (e.g. where there is no use
of stdin, or stdin is redirected from a file).
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<https://github.com/rust-lang-nursery/cli-wg/issues/15#issuecomment-371900673>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABU-XqoPo2VRS00EHFazr7Dtj91uxyFtks5tcsfogaJpZM4SZwS0>
.
|
For the sorts of tools I wrote, declaring that you can only have color support on Windows 10 is just fine. Most command-line tools tend to be developer-focused, and most developers aren't running old versions of Windows anyway. I think as long as tools work on older versions of Windows, color is simply an added bonus, so I don't think we should be writing foundational crates that jump through hoops to support color in that situation.
You might be interested in the strip-ansi-escapes crate I put together recently for sccache. sccache runs compilers and captures their stdout/stderr and then outputs it elsewhere, so the standard "is stdout a tty" checks don't work well. With this crate I can force color output on from compilers and then do the tty check when it's time to output and strip escapes at that point. (My crate uses the |
Do you have numbers on this? |
It's already done. But I don't know what we're arguing about. You aren't going to convince me to stop supporting <Windows 10 users. |
Here is some data - the numbers come from what I would call early adopters using the open source version of PowerShell. I'm not sure what the numbers are exactly - I think they are process launch, not number of users, but you can see 4% (80K) on pre-Win10 systems. |
Sorry if this was already mentioned before but I want to just throw it out there. Are there inspirations that answer some of the questions outlined here that can be derived from prior established standards in other language communities? Color control standards doesn't necessarily feel like rust specific territory. If such standards exist I would opt for rust to find ways to honor them. As a cli user, I dont typically expect to find ux to vary by the language a cli was written in. In fact I prefer it didn't! If such standards do not exist in other communities that have been have mature cli ecosystems, does this WG feel this is still an important area to standardize on? |
@softprops There are various things I'm aware of like the cli colors spec, but I don't know what kind of adoption it has. For me, when I wrote Separately from that, there is the terminfo database, but I ran into problems that I didn't know how to solve, and it seemed like overkill for just needing to emit colors. I personally would be very weary of standards in this area. Actual behavior of common command line tools wins the day IMO. (There are other questions, which |
I personally like this idea as ultimately I think it's the end user who needs to decide if they want color or not (and when). The only issue I see with a shared global state such as I might be misunderstanding, but I see it like this: Whereas maybe something more like And could benefit from fewer branches (maybe?) and moves the global state from Maybe I'm way off and need coffee, but just some thoughts 😜 |
Oh, look, this was already discussed. Wish I had checked before researching this topic. When this thread started, I had skipped over it because I hadn't gotten into colored output yet for my applications and knew I was over my head. Now its being a pain for me and am looking to do something about it. styling crate authors are still interested in the global state, I'm game for implementing it. Now onto my research: Inspiration cases
AspirationsA developer can quickly adopt color within their CLI that follows best practices
A developer can get richly formatted test-failure messages
Other considerations: performance should not be so bad that it is noticed (like stdout locking on Survey of ecosystemCapability crates
Styling crates
Color logic
"Middleware"
Applications
Open questions
Challenges
Proposed solution:Crates:
All of this is subject to adoption, see XKCD #927 This ends up being similar to what was proposed earlier when WG-CLI discussed this Other ideas
|
You forgot
|
Huh, surprised I missed one that has so many downloads. I suspect it doesn't add anything new but I'll try to take a look at it earlier
Yes, as I mention in the beginning, I found this thread (and clicolors-control) after doing all of this and only did minor touch ups to acknowledge it. I need to spend some more time on it to see how well it meets the aspirations listed out. |
A relevant comment regarding that crate's current status. console-rs/console#90 (comment) |
Why do you feel checking tty is too much? |
Things missing from clicolors-control
The API needs to be able to detect level of color support given either stdout/stderr (names), a stream, or "lowest common denominator". If we build on clicolors-control, We can introduce these without a breaking change, carefully choosing what new behavior the old APIs map to, but I suspect we should consider breaking the API to optimize for people using it correctly with these new requirements, since no one is listed as depending on it at the moment. I suspect the easiest way to implement this will be to put all of this into a bitfield in an atomic u32. It can be a starting point if @mitsuhiko is up for it though it won't be hard to do something similar. EDIT: Also seems like we could learn some lessons from termcolor |
Just read up on the new feature resolver (blog post, edition guide). It sounds like |
Remember that terminfo and termcap came from a time when there were a lot of very weird non-VT-series terminals around, and if you wanted your app to work at all, then you had to use the peculiar sequences required by those terminals. Nowadays it's reasonable to assume basic ANSI support. So, for colour if you just use plain 8-colour ANSI sequences and maybe bold, it shouldn't need any feature testing. So either aim for the lowest common denominator (easy) or try to get fancy and detect extra features (harder). Also bear in mind that the user might be running with either a white or black default background (colour 49). So blue on default-background might be readable on white, but probably won't be on black. This is all UNIX, though -- I don't know the Windows situation. |
I now have a first pass of concolor-control. I tried to model the detection logic off of EDIT: |
This is a PoC to collect feedback on a crate meant improve interoperability across various colored output crates. I'm a little uncertain on the `Buffer` vs `WriterInner` logic. See rust-cli/team#15 (comment)
This is a PoC to collect feedback on a crate meant improve interoperability across various colored output crates. I'm a little uncertain on the `Buffer` - It handles `console` differently than `WriterInner` - I used the lowest common denominator of the streams but I wonder if we should add support for completely skipping stream detection For more on the motivation, see rust-cli/team#15 (comment)
Looked at your code and it looks good. One concern I have is about allowing |
We need to handle the CLI case, like concolor-clap. The developer needs to the ability to say "do what you want".
On top of those
|
I was trying to get people to embrace this crate before as a unified way to control colors but it did not get far: https://crates.io/crates/clicolors-control (that crate also turns on ansi colors for the windows console as an added bonus)
The idea was that you have one common crate that turns on and off colors for the entire cli app.
I'm fine finding a different mechanism for this but right now controlling colored output is really not a particularly fun experience.
Anything got some thoughts on this matter?
The text was updated successfully, but these errors were encountered: