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

Migrate to Anstyle #7

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ license = "Apache-2.0/MIT"
edition = "2018"

[dependencies]
term = "0.7"
anstyle = "1.0"

[dev-dependencies]
diff = "0.1"
3 changes: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use crate::style::Style;
use std::cmp;
use std::iter::ExactSizeIterator;
use std::ops::Range;
use term::Terminal;

mod row;
#[cfg(test)]
Expand Down Expand Up @@ -142,7 +141,7 @@ impl AsciiCanvas {
self.in_range_index(r, self.columns)
}

pub fn write_to<T: Terminal + ?Sized>(&self, term: &mut T) -> term::Result<()> {
pub fn write_to<T: std::io::Write>(&self, term: &mut T) -> std::io::Result<()> {
for row in self.to_strings() {
row.write_to(term)?;
writeln!(term, "")?;
Expand Down
3 changes: 1 addition & 2 deletions src/row.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::style::{Style, StyleCursor};
use std::fmt::{Debug, Display, Error, Formatter};
use term::{self, Terminal};

pub struct Row {
text: String,
Expand All @@ -16,7 +15,7 @@ impl Row {
}
}

pub fn write_to<T: Terminal + ?Sized>(&self, term: &mut T) -> term::Result<()> {
pub fn write_to<T: std::io::Write>(&self, term: &mut T) -> std::io::Result<()> {
let mut cursor = StyleCursor::new(term)?;
for (character, &style) in self.text.trim_end().chars().zip(&self.styles) {
cursor.set_style(style)?;
Expand Down
108 changes: 51 additions & 57 deletions src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
//! etc.

use std::default::Default;
use term::{self, Terminal};

#[derive(Copy, Clone, Default, PartialEq, Eq)]
pub struct Style {
Expand Down Expand Up @@ -93,96 +92,91 @@ impl Style {
/// Attempts to apply the given style to the given terminal. If
/// the style is not supported, either there is no effect or else
/// a similar, substitute style may be applied.
pub fn apply<T: Terminal + ?Sized>(self, term: &mut T) -> term::Result<()> {
term.reset()?;
pub fn apply<T: std::io::Write>(self, term: &mut T) -> std::io::Result<()> {
let mut s = anstyle::Style::new();
s.write_reset_to(term)?;

macro_rules! fg_color {
($color:expr, $term_color:ident) => {
if self.contains($color) {
if term.supports_color() {
Copy link

@yannham yannham May 27, 2024

Choose a reason for hiding this comment

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

I think this functionality is lost in this PR. That is, if the thing we output to isn't a proper terminal (a file), we used to not add any styling, while we probably now always output ANSI escape codes - which can be annoying when redirecting stderr to a log file.

The simplest approximation is to use a bound std::io::Write + std::io::IsTerminal, and use is_terminal() as a replacement for supports_color() (I don't know if it's an exact replacement but I've seen is_terminal being used for that in practice).

Copy link
Contributor

Choose a reason for hiding this comment

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

The approach you mention is also the official recommendation of the termcolor library, which seems like a relevant endorsement: https://docs.rs/termcolor/latest/termcolor/#detecting-presence-of-a-terminal

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, fair point, I didn't know about std::io::IsTerminal. I think for Lalrpop, the functionality was maintained because there I started using AutoStream/StripStream which manage this for me. I pushed the change that has been suggested here. It does break my lalrpop implementation because AutoStream/StripStream don't implement std::io::IsTerminal. It might be related to IsTerminal being a sealed trait? Some trait shenanigans I think.

Maybe I don't need Anstream on the Lalrpop side if all of the style/color checking can happen here?

term.fg(term::color::$term_color)?;
}
s = s.fg_color(Some(anstyle::Color::Ansi(anstyle::AnsiColor::$term_color)));
}
};
}

fg_color!(FG_BLACK, BLACK);
fg_color!(FG_BLUE, BLUE);
fg_color!(FG_BRIGHT_BLACK, BRIGHT_BLACK);
fg_color!(FG_BRIGHT_BLUE, BRIGHT_BLUE);
fg_color!(FG_BRIGHT_CYAN, BRIGHT_CYAN);
fg_color!(FG_BRIGHT_GREEN, BRIGHT_GREEN);
fg_color!(FG_BRIGHT_MAGENTA, BRIGHT_MAGENTA);
fg_color!(FG_BRIGHT_RED, BRIGHT_RED);
fg_color!(FG_BRIGHT_WHITE, BRIGHT_WHITE);
fg_color!(FG_BRIGHT_YELLOW, BRIGHT_YELLOW);
fg_color!(FG_CYAN, CYAN);
fg_color!(FG_GREEN, GREEN);
fg_color!(FG_MAGENTA, MAGENTA);
fg_color!(FG_RED, RED);
fg_color!(FG_WHITE, WHITE);
fg_color!(FG_YELLOW, YELLOW);
fg_color!(FG_BLACK, Black);
fg_color!(FG_BLUE, Blue);
fg_color!(FG_BRIGHT_BLACK, BrightBlack);
fg_color!(FG_BRIGHT_BLUE, BrightBlue);
fg_color!(FG_BRIGHT_CYAN, BrightCyan);
fg_color!(FG_BRIGHT_GREEN, BrightGreen);
fg_color!(FG_BRIGHT_MAGENTA, BrightMagenta);
fg_color!(FG_BRIGHT_RED, BrightRed);
fg_color!(FG_BRIGHT_WHITE, BrightWhite);
fg_color!(FG_BRIGHT_YELLOW, BrightYellow);
fg_color!(FG_CYAN, Cyan);
fg_color!(FG_GREEN, Green);
fg_color!(FG_MAGENTA, Magenta);
fg_color!(FG_RED, Red);
fg_color!(FG_WHITE, White);
fg_color!(FG_YELLOW, Yellow);

macro_rules! bg_color {
($color:expr, $term_color:ident) => {
if self.contains($color) {
if term.supports_color() {
term.bg(term::color::$term_color)?;
}
s = s.bg_color(Some(anstyle::Color::Ansi(anstyle::AnsiColor::$term_color)));
}
};
}

bg_color!(BG_BLACK, BLACK);
bg_color!(BG_BLUE, BLUE);
bg_color!(BG_BRIGHT_BLACK, BRIGHT_BLACK);
bg_color!(BG_BRIGHT_BLUE, BRIGHT_BLUE);
bg_color!(BG_BRIGHT_CYAN, BRIGHT_CYAN);
bg_color!(BG_BRIGHT_GREEN, BRIGHT_GREEN);
bg_color!(BG_BRIGHT_MAGENTA, BRIGHT_MAGENTA);
bg_color!(BG_BRIGHT_RED, BRIGHT_RED);
bg_color!(BG_BRIGHT_WHITE, BRIGHT_WHITE);
bg_color!(BG_BRIGHT_YELLOW, BRIGHT_YELLOW);
bg_color!(BG_CYAN, CYAN);
bg_color!(BG_GREEN, GREEN);
bg_color!(BG_MAGENTA, MAGENTA);
bg_color!(BG_RED, RED);
bg_color!(BG_WHITE, WHITE);
bg_color!(BG_YELLOW, YELLOW);
bg_color!(BG_BLACK, Black);
bg_color!(BG_BLUE, Blue);
bg_color!(BG_BRIGHT_BLACK, BrightBlack);
bg_color!(BG_BRIGHT_BLUE, BrightBlue);
bg_color!(BG_BRIGHT_CYAN, BrightCyan);
bg_color!(BG_BRIGHT_GREEN, BrightGreen);
bg_color!(BG_BRIGHT_MAGENTA, BrightMagenta);
bg_color!(BG_BRIGHT_RED, BrightRed);
bg_color!(BG_BRIGHT_WHITE, BrightWhite);
bg_color!(BG_BRIGHT_YELLOW, BrightYellow);
bg_color!(BG_CYAN, Cyan);
bg_color!(BG_GREEN, Green);
bg_color!(BG_MAGENTA, Magenta);
bg_color!(BG_RED, Red);
bg_color!(BG_WHITE, White);
bg_color!(BG_YELLOW, Yellow);

macro_rules! attr {
($attr:expr, $term_attr:expr) => {
if self.contains($attr) {
let attr = $term_attr;
if term.supports_attr(attr) {
term.attr(attr)?;
}
s = s.effects(attr);
}
};
}

attr!(BOLD, term::Attr::Bold);
attr!(DIM, term::Attr::Dim);
attr!(ITALIC, term::Attr::Italic(true));
attr!(UNDERLINE, term::Attr::Underline(true));
attr!(BLINK, term::Attr::Blink);
attr!(STANDOUT, term::Attr::Standout(true));
attr!(REVERSE, term::Attr::Reverse);
attr!(SECURE, term::Attr::Secure);
attr!(BOLD, anstyle::Effects::BOLD);
attr!(DIM, anstyle::Effects::DIMMED);
attr!(ITALIC, anstyle::Effects::ITALIC);
attr!(UNDERLINE, anstyle::Effects::UNDERLINE);
attr!(BLINK, anstyle::Effects::BLINK);
attr!(STANDOUT, anstyle::Effects::BOLD | anstyle::Effects::INVERT);
attr!(REVERSE, anstyle::Effects::INVERT);
attr!(SECURE, anstyle::Effects::HIDDEN);

Ok(())
s.write_to(term)
}
}

///////////////////////////////////////////////////////////////////////////

pub struct StyleCursor<'term, T: ?Sized + Terminal> {
pub struct StyleCursor<'term, T: std::io::Write> {
current_style: Style,
term: &'term mut T,
}

impl<'term, T: ?Sized + Terminal> StyleCursor<'term, T> {
pub fn new(term: &'term mut T) -> term::Result<StyleCursor<'term, T>> {
impl<'term, T: std::io::Write> StyleCursor<'term, T> {
pub fn new(term: &'term mut T) -> std::io::Result<StyleCursor<'term, T>> {
let current_style = Style::default();
current_style.apply(term)?;
Ok(StyleCursor {
Expand All @@ -195,7 +189,7 @@ impl<'term, T: ?Sized + Terminal> StyleCursor<'term, T> {
self.term
}

pub fn set_style(&mut self, style: Style) -> term::Result<()> {
pub fn set_style(&mut self, style: Style) -> std::io::Result<()> {
if style != self.current_style {
style.apply(self.term)?;
self.current_style = style;
Expand Down