Skip to content

Comments

Follow full xparsecolor spec in escape sequences#2504

Merged
chrisduerr merged 2 commits intoalacritty:masterfrom
rbong:patch-xparsecolor-spec
Aug 19, 2019
Merged

Follow full xparsecolor spec in escape sequences#2504
chrisduerr merged 2 commits intoalacritty:masterfrom
rbong:patch-xparsecolor-spec

Conversation

@rbong
Copy link
Contributor

@rbong rbong commented Jun 4, 2019

Escape sequences in xterm are parsed according to xparsecolor. xparsecolor supports 1, 2, 3, and 4 digit hex colors. Previously, only 2 digits were supported.

This also fixes a bug where "fX" was parsed as "0xf" instead of "0xf0". xterm handles invalid hex values as 0, except for '/'.

The response to a request for fg/bg must be a valid escape sequence. Most terminals output a 4-digit hex response on request for fg/bg color. This changes the response to 4-digit, improving compatibility.

@rbong
Copy link
Contributor Author

rbong commented Jun 4, 2019

Besides the issue above I will add this to the changelog.

@rbong rbong force-pushed the patch-xparsecolor-spec branch from a71c151 to 9b50657 Compare June 4, 2019 16:06
@rbong
Copy link
Contributor Author

rbong commented Jun 4, 2019

Everything looks good from my end now.

@chrisduerr chrisduerr self-requested a review June 4, 2019 17:47
@chrisduerr chrisduerr added this to the Version 0.3.3 milestone Jun 4, 2019
@rbong
Copy link
Contributor Author

rbong commented Jun 4, 2019

Looks like rustfmt failed on CI and it failed a check for cognitive complexity. I will run rustfmt. Any tips for bringing down cognitive complexity is appreciated, since I'm not pretty new to Rust, but I will figure it out if not. I'll run any local checks before pushing next time.

@chrisduerr
Copy link
Member

I've only taken a super small peek at this, but this looks like extreme amounts of macros. You'll want to keep macros to a minimum since they add a lot of complexity and compile time, instead you should prefer abstracting over complexity with methods when possible.

@rbong
Copy link
Contributor Author

rbong commented Jun 4, 2019

Good to know, thanks

@rbong rbong force-pushed the patch-xparsecolor-spec branch from 9b50657 to 147ba5a Compare June 4, 2019 22:24
@rbong
Copy link
Contributor Author

rbong commented Jun 4, 2019

Hopefully that should fix rustfmt and clippy problems.

As far as the issues with macros goes, I removed some unnecessary macros. I was able to cut out one macro that was originally in the function by using .map instead. I'm about as far as I can get reducing the complexity of this function while somewhat preserving the original style, if I need to take a step further and refactor parsing colors entirely now that it's a bit more complex, let me know.

@rbong
Copy link
Contributor Author

rbong commented Jun 17, 2019

I have refactored the function based on the example given above - thank you, that was a much better starting point. All of the issues should be resolved. There are no macros, legacy colors are parsed in the same function, and a simple loop is used.

@rbong
Copy link
Contributor Author

rbong commented Jun 17, 2019

I am working on the CI issues.

@rbong rbong force-pushed the patch-xparsecolor-spec branch 2 times, most recently from 24a64b0 to 7669aaf Compare June 17, 2019 15:21
@rbong
Copy link
Contributor Author

rbong commented Jun 17, 2019

Fixed clippy and rustfmt problems, it's ready to be reviewed again whenever.

@rbong rbong force-pushed the patch-xparsecolor-spec branch from 7669aaf to cdbcd83 Compare June 18, 2019 01:48
@chrisduerr
Copy link
Member

I thought I'd answer in a top-level comment for visibility and to make things a bit easier to read.

Regarding the way additional depth should be calculated, you can find the full information I know about here:
https://linux.die.net/man/3/xparsecolor

To go into a bit more detail, the legacy #RRGGBB colors are supposed to define the most significant bits of the color. So #RGB would be the same as #R000G000B000.

The rgb: colors are handled a bit differently. If not enough depth is specified, they're supposed to be scaled. So f[fff] would be equal to 1.0/255 and [0/000] is equal to 0. So a value of 42 would be equal to 25.88/66, if we'd scale that back up it would be 428F instead of 4200 or 4242. At least that's how I understand it based on this documentation.

Full disclosure, my main goal with this PR is really to support 4-digit color query responses, which has already been added to the main branch for an unrelated issue. Improving XParseColor compatibility was just a prerequisite. However I am happy to help and I have added support for more color formats here with tests here

If you're not interested in adding full XParseColor support, we can decide on a subset of it too. Though I feel like it would be nice to get this out of the way. In theory with XLookupColors even colors like red would be valid, however I'd understand if the line would be drawn here.

If we decide to go all the way, it would probably be a nice idea to separate that into a different workspace module (/crate/library) so we could offer an XParseColor crate for the broader Rust community, however that might be a bigger project than anyone's signed up for with this PR. :)

@rbong rbong force-pushed the patch-xparsecolor-spec branch 2 times, most recently from 43afc4f to 26bca11 Compare June 22, 2019 16:22
@rbong
Copy link
Contributor Author

rbong commented Jun 22, 2019

The rgb: colors are handled a bit differently. If not enough depth is specified, they're supposed to be scaled. So f[fff] would be equal to 1.0/255 and [0/000] is equal to 0. So a value of 42 would be equal to 25.88/66

I missed that, thank you. It should now be fixed and tested. I thought you meant that we were supposed to do something with the third and fourth bit.

If you're not interested in adding full XParseColor support, we can decide on a subset of it too.

I think at this point, if I missed a format, I'd be more comfortable with creating an issue than fixing it here. Though obviously, if some color format is parsed incorrectly here, I will fix it.

If we decide to go all the way, it would probably be a nice idea to separate that into a different workspace module

It's a good idea, but I wouldn't be comfortable with maintaining a rust library of any size right now given my skill level with the language.

@chrisduerr chrisduerr self-requested a review June 22, 2019 16:46
rbong and others added 2 commits August 18, 2019 19:17
Escape sequences in xterm are parsed according to xparsecolor.
xparsecolor supports 1, 2, 3, and 4 digit hex colors.
Previously, only 2 digits were supported.

This also fixes a bug where "fX" was parsed as "0xf", where X is an invalid character.

The response to a request for fg/bg must be a valid escape sequence.
The current response uses 4-digit hex, which was previously invalid.

All of this required a refactoring of the rgb parsing function.
@chrisduerr chrisduerr force-pushed the patch-xparsecolor-spec branch from 26bca11 to 6da64f2 Compare August 18, 2019 23:11
Copy link
Member

@chrisduerr chrisduerr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've rebased this code and cleaned up some of the remaining style issues.

Thanks for your contribution, I think everything should be good to go now.

@rbong
Copy link
Contributor Author

rbong commented Aug 19, 2019

Awesome, thank you!

@chrisduerr chrisduerr merged commit 629ea24 into alacritty:master Aug 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants