RFC: Style code/reset rendering via Display #165
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is an unusual suggestion, but I've tried it out in practice and it feels
really comfortable to use. I wanted to run it by you to see if it seemed
reasonable.
I found the requirement to call
.render()
and.render_reset()
ratherverbose, particularly when trying to render several color styles in one line. I
started thinking about ways to make that more convenient, and this idea
occurred to me.
With this PR applied,
Style
andReset
implementDisplay
directly,rendering the ANSI code, without having to call
.render()
. That part I'mhoping will be relatively uncontroversial; that's in the first two commits.
The more unusual part of this PR: if you set the alternate format flag, a
Style
will print its reset code, instead.The combination of the two makes it easy to use inline formats to render
styles. Here's an example of how this looks:
I'm hoping this seems reasonable. If you find the use of the alternate flag too
much, though, then as an alternative, you could drop the last two commits, and
that would still let people render codes and resets using inline styles:
(This would get even easier if anstyle had a
pub const RESET: Reset = Reset;
,which I'd be happy to send in a patch for.)