-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Refactor color support (+Add Windows Support) #836
Comments
I've began work on this issue - I'm hoping to have something mergable within the week. |
@pkgw I don't have a public branch yet. I've just been mocking it out, I literally just started messing with it today ;) Moving the conversation here so as not to clog up ripgrep traffic. |
I started taking a look at this. I've seen at least a few places where |
Not only does this remove some unsafe code from clap itself, `atty` does the right thing on Windows too. This isn't relevant now since we don't currently support colorized output on Windows, but will come in handy if/when we implement that feature (clap-rs#836).
@pkgw Out of curiosity, why do you need the |
@BurntSushi There are functions in |
Ah, yup, that's the key I was missing. If that's the case, then |
refactor: use the atty crate for isatty() detection Not only does this remove some unsafe code from clap itself, `atty` does the right thing on Windows too. This isn't relevant now since we don't currently support colorized output on Windows, but will come in handy if/when we implement that feature (#836).
Of course I'd prefer to use dummy types or work around pulling in unneeded deps in that case, but if the dep is small and doesn't bloat a resulting binary excessively I would be willing to consider pulling it in unconditionally. I guess I'd need a concrete example to really see trade-off between the pain of using dummy types and resulting binary size.
The current coloring support was built around using |
@kbknapp OK, well, dummy types certainly won't be that painful to implement, and they're unconditionally better once implemented, so it sounds like that's the way to go. |
Hmmm, I took more of a look at this and I think that making the change from |
@issuehuntfest has funded $50.00 to this issue. See it on IssueHunt |
If |
I made a refactor attempt at https://github.com/clap-rs/clap/tree/color. Wasn't enough. @BurntSushi I am missing some functions for termcolor's Buffer.
|
@pksunkara Sorry, but I don't understand what you're asking. Could you please say more words? Examples and context would help a lot. |
What's happening is, when we try to print help, currently clap treats them as ansi strings and creates severals strings and combines them later. A good example is this function. If I am passing around just 1 buffer for everything to write into, this will not work. So, I wanted to create buffers (like how the strings were created) and then combine them. I went through the termcolor docs and code and I couldn't understand how I can achieve this. Sorry, If I missed anything. Secondly, clap tries to wrap text based on terminal size. We try to check if we need to wrap based on the string that was returned. You can see an example here. That basically calculates the length of the colored string which is not possible with Buffer. Hope I was able to give more clarity. |
@pksunkara Thanks, that helps a bit. It might help for you to read the docs on the internal In particular, the whole point of
I'm not following why this isn't possible. |
Yup, I got that part from reading the code. What I was asking was basically if we can support the append somehow. Maybe refactoring the WindowsBuffer to be like: struct WindowsBufferItem {
buf: Vec<u8>,
colors: Vec<(uszie, Option<ColorSpec>)>,
}
struct WindowsBuffer(Vec<WindowsBufferItem>); (basically
Because non-windows buffer has the ansi codes in the string. When I am calculating the length, I want the plain content. We could try to make it possible by changing the |
@pksunkara That's a possibility. I'm open to PRs for that. I don't think there should be any breaking changes. It still seems to me like you should be able to refactor the code so that you don't append to buffers. That is, instead of: let buf1 = do_foo();
let buf2 = do_bar();
let buf3 = buf1 + buf2; you would do let mut buf = String::new();
do_foo(&mut buf);
do_bar(&mut buf); And then at that point, it should be possible to swap out
Yes. But wasn't that true before? I'm missing something here. Why wasn't this a problem before? But if you just need the buffer without ANSI codes, then it's pretty easy to just strip them: https://github.com/BurntSushi/tabwriter/blob/2c0c9ff0d7a2bc522dfed55ab64605a0a21b23d2/src/lib.rs#L412-L420
I see why you might want that, but that's definitely not possible. That's almost certainly going to result in a noticeable loss in performance due to the extra indirection. Windows does it because it has to. |
It is a problem currently too. I was hoping to fix it with this refactor and thats why I mentioned it originally. I will give it another attempt and see if I really need the appending. Thanks for letting me bounce my thoughts with you. |
@pksunkara No problem. If it ends up being impossible, then I'm open to making it possible to append Note that I would definitely recommend against using a |
I will probably end up building a non colored string at the same time and using it for calculation |
Whenever I see a technical dispute is going on, I can't resist the urge to jump in, you know me. I would probably end up with storing an error as |
@pksunkara has rewarded $45.00 to @pksunkara. See it on IssueHunt
|
The formatting really needs some love, and adding Windows support would be a dream.
This is the tracking issues for The Great Color Formatting Refactor as it will henceforth be thou known.
I plan to take all cues from BurntSushi/ripgrep's handling of color formatting.
Another thing that'd be great to add, but is not the focus for this issue is the ability to change the colors and formats. Just something to keep in mind while working.
It'd also be nice to get rid of the
Format
enums anduse(Done in #862)atty
The text was updated successfully, but these errors were encountered: