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

Please support painting bytes (&[u8]) #19

Merged
merged 1 commit into from Aug 27, 2016
Merged

Conversation

joshtriplett
Copy link
Collaborator

ansi_term currently only supports painting strings (String or &str). Sometimes, to handle text in unknown character sets, a program will need to emit raw bytes; for instance, git diffs from git2-rs provide the diff text as bytes, because the files diffed might not use UTF-8. I'd like to render diffs with color using ansi_term.

Please consider adding support to paint bytes with a Colour or Style, perhaps via a new paint_bytes method. This would need to return a new type like ANSIBytes, which couldn't implement Display, but which could support writing to a std::io::Write via .write_all.

@ogham ogham self-assigned this Jul 19, 2016
@ogham
Copy link
Owner

ogham commented Jul 19, 2016

Yeah, this is something I'd like to do (and your use case is exactly it).

@joshtriplett
Copy link
Collaborator Author

I have this mostly implemented; I'm working on test cases right now.

Applications working with text in unknown or non-UTF-8 character sets
need to handle that text as opaque [u8] bytestrings.  For example, git
diffs from git2-rs provide the diff text as [u8] bytestrings, because
the files diffed might not use UTF-8.

Add support for painting [u8] bytestrings, producing a new
ANSIByteString type.  Add an ANSIByteStrings type to print many
ANSIByteString values with minimal escape sequences.

To avoid duplicating code for str and [u8], introduce generic versions
of ANSIString and ANSIStrings, with the specific versions for str and
[u8] as type aliases.  Also introduce a private trait AnyWrite to
abstract code that works with either fmt::Write and io::Write.

This implementation provides full source compatibility with code written
for previous versions of ansi_term.  All the existing tests pass
unmodified.  Nonetheless, this should likely bump the version to 0.9.0,
because the introduction of generic types may potentially affect type
inference.

Extend the testsuite to run all the tests on ANSIByteString, as well.
@joshtriplett
Copy link
Collaborator Author

I just attached a pull request implementing this.

@ogham ogham merged commit e20f927 into ogham:master Aug 27, 2016
@ogham
Copy link
Owner

ogham commented Aug 27, 2016

Published as v0.9.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants