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

RFC: Style code/reset rendering via Display #165

Merged
merged 5 commits into from
Jan 29, 2024

Conversation

joshtriplett
Copy link
Contributor

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() rather
verbose, 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 and Reset implement Display directly,
rendering the ANSI code, without having to call .render(). That part I'm
hoping 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:

use anstyle::{AnsiColor, Style};

fn main() {
    const RB: Style = AnsiColor::Red.on_default().bold();
    const GU: Style = AnsiColor::Green.on_default().underline();
    println!("{RB}Hello{RB:#}, {GU}world{GU:#}!");
}

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:

use anstyle::{AnsiColor, Reset, Style};

fn main() {
    const RESET: Reset = Reset;
    const RB: Style = AnsiColor::Red.on_default().bold();
    const GU: Style = AnsiColor::Green.on_default().underline();
    println!("{RB}Hello{RESET}, {GU}world{RESET}!");
}

(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.)

@epage
Copy link
Collaborator

epage commented Jan 26, 2024

Moving the conversation over at #166.

  • This is multiple decisions to be made and could have multiple PRs through the life of it
  • I, and I assume users, are more likely to look up a discussion like this in the Issues, rather than PRs

epage and others added 5 commits January 29, 2024 13:34
Putting it next to `write_to` will help keep them in-sync
This makes it easy to render a style directly using inline format args.
This makes it easy to render a reset directly using inline format args.
This makes it easy to render both a style and its reset via inline
format args.
@epage epage marked this pull request as ready for review January 29, 2024 19:44
@epage epage merged commit ea42606 into rust-cli:main Jan 29, 2024
13 checks passed
@joshtriplett joshtriplett deleted the anstyle-render-helper branch January 29, 2024 23:42
@joshtriplett
Copy link
Contributor Author

Thanks for the merge! I'm using the new API now, and it works well.

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.

2 participants