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

lib: ignore ANSI escape sequences #179

Merged
merged 1 commit into from
Apr 19, 2020
Merged

Conversation

mgeisler
Copy link
Owner

ANSI escape sequences are typically used for colored text. The
sequences start with a so-called CSI, followed by some "parameter
bytes" before ending with a "final byte".

We now handle these escape sequences by simply skipping over the
bytes. This works well for escape sequences that change colors since
they don't take up space and since they continue to work across any
line breaks we insert.

See https://en.wikipedia.org/wiki/ANSI_escape_code for details.

Fixes: #140.

@mgeisler mgeisler mentioned this pull request Apr 15, 2020
@mgeisler
Copy link
Owner Author

@pksunkara, would you be able to do a test with this commit and tell me if this solves your problem in clap-rs/clap#1246?

@mgeisler
Copy link
Owner Author

This PR will of course not do anything sensible for Windows. As far as I understand, colored text depends on using special Windows APIs and so I guess the color information isn't embedded into the string like on Linux?

@pksunkara
Copy link

I can't say because we refactored our color writing and we don't construct strings anymore but write directly to io. So, all the strings we now give to textwrap are plain.

@mgeisler
Copy link
Owner Author

Aha, I thought based on clap-rs/clap#1246 and #140 (comment) that clap was needing this feature? I was hoping you could test it for me to help guide it and make sure it's useful.

@mgeisler
Copy link
Owner Author

I'll merge it right after I fix Travis -- my crate has again stopped building on Rust 1.31.0.

@pksunkara
Copy link

Yeah, we will probably need it again when we are fixing clap-rs/clap#1456. But, unfortunately, it is not a priority until after we release 3.0

@mgeisler
Copy link
Owner Author

Okay, cool! I'll get a release out of textwrap in the meantime and then I hope it'll be useful for you guys.

ANSI escape sequences are typically used for colored text. The
sequences start with a so-called CSI, followed by some "parameter
bytes" before ending with a "final byte".

We now handle these escape sequences by simply skipping over the
bytes. This works well for escape sequences that change colors since
they don't take up space and since they continue to work across any
line breaks we insert.

See https://en.wikipedia.org/wiki/ANSI_escape_code for details.

Fixes: #140.
@codecov-io
Copy link

Codecov Report

Merging #179 into master will increase coverage by 0.17%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #179      +/-   ##
==========================================
+ Coverage   94.02%   94.19%   +0.17%     
==========================================
  Files           4        4              
  Lines         368      379      +11     
==========================================
+ Hits          346      357      +11     
  Misses         22       22              
Impacted Files Coverage Δ
src/lib.rs 91.43% <100.00%> (+0.38%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d0ca2e8...827dbe6. Read the comment docs.

@mgeisler mgeisler merged commit a3bd809 into master Apr 19, 2020
@pksunkara
Copy link

Before you release, do you think you can port this to https://lib.rs/crates/terminal_size? term_size is not being maintained and I would appreciate if you could port it to a more maintained version.

@mgeisler
Copy link
Owner Author

Sure, let me update that right away.

mgeisler added a commit that referenced this pull request Apr 21, 2020
The term_size crate seems unmaintained, as mentioned in #179.
mgeisler added a commit that referenced this pull request Apr 21, 2020
The term_size crate seems unmaintained, as mentioned in #179.
@mgeisler mgeisler deleted the handle-ansi-escape-sequences branch September 2, 2020 20:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Handle ANSI color codes
3 participants