Skip to content

Commit

Permalink
fix(anstyle): Don't display with formatter's settings
Browse files Browse the repository at this point in the history
Before, we were inconsistent about this.  Now, we err on the side of
always ignoring the settings as there isn't really much reason to pad
ansi escape codes and the padding could cumulate over multiple calls.
  • Loading branch information
epage committed Feb 5, 2024
1 parent 93c3dec commit ec33d22
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 23 deletions.
33 changes: 19 additions & 14 deletions crates/anstyle/src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ impl AnsiColor {
/// Render the ANSI code for a foreground color
#[inline]
pub fn render_fg(self) -> impl core::fmt::Display + Copy + Clone {
self.as_fg_str()
NullFormatter(self.as_fg_str())
}

#[inline]
Expand Down Expand Up @@ -225,7 +225,7 @@ impl AnsiColor {
/// Render the ANSI code for a background color
#[inline]
pub fn render_bg(self) -> impl core::fmt::Display + Copy + Clone {
self.as_bg_str()
NullFormatter(self.as_bg_str())
}

#[inline]
Expand Down Expand Up @@ -605,7 +605,19 @@ impl DisplayBuffer {
impl core::fmt::Display for DisplayBuffer {
#[inline]
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
self.as_str().fmt(f)
let s = self.as_str();
write!(f, "{s}")
}
}

#[derive(Copy, Clone, Default, Debug)]
struct NullFormatter<D: core::fmt::Display>(D);

impl<D: core::fmt::Display> core::fmt::Display for NullFormatter<D> {
#[inline]
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let d = &self.0;
write!(f, "{d}")
}
}

Expand Down Expand Up @@ -634,27 +646,20 @@ mod test {

#[test]
fn no_align() {
#[track_caller]
fn assert_align(d: impl core::fmt::Display) {
let expected = format!("{d:<10}");
let actual = format!("{d:<10}");
assert_eq!(expected, actual);
}

#[track_caller]
fn assert_no_align(d: impl core::fmt::Display) {
let expected = format!("{d}");
let actual = format!("{d:<10}");
assert_eq!(expected, actual);
}

assert_align(AnsiColor::White.render_fg());
assert_align(AnsiColor::White.render_bg());
assert_no_align(AnsiColor::White.render_fg());
assert_no_align(AnsiColor::White.render_bg());
assert_no_align(Ansi256Color(0).render_fg());
assert_no_align(Ansi256Color(0).render_bg());
assert_no_align(RgbColor(0, 0, 0).render_fg());
assert_no_align(RgbColor(0, 0, 0).render_bg());
assert_align(Color::Ansi(AnsiColor::White).render_fg());
assert_align(Color::Ansi(AnsiColor::White).render_bg());
assert_no_align(Color::Ansi(AnsiColor::White).render_fg());
assert_no_align(Color::Ansi(AnsiColor::White).render_bg());
}
}
9 changes: 5 additions & 4 deletions crates/anstyle/src/effect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,8 @@ impl core::fmt::Display for EffectsDisplay {
#[inline]
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
for index in self.0.index_iter() {
METADATA[index].escape.fmt(f)?;
let escape = METADATA[index].escape;
write!(f, "{escape}")?;
}
Ok(())
}
Expand Down Expand Up @@ -383,12 +384,12 @@ mod test {
#[test]
fn no_align() {
#[track_caller]
fn assert_align(d: impl core::fmt::Display) {
let expected = format!("{d:<10}");
fn assert_no_align(d: impl core::fmt::Display) {
let expected = format!("{d}");
let actual = format!("{d:<10}");
assert_eq!(expected, actual);
}

assert_align(Effects::BOLD.render());
assert_no_align(Effects::BOLD.render());
}
}
10 changes: 5 additions & 5 deletions crates/anstyle/src/reset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ impl Reset {

impl core::fmt::Display for Reset {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
RESET.fmt(f)
write!(f, "{RESET}")
}
}

Expand All @@ -34,13 +34,13 @@ mod test {
#[test]
fn no_align() {
#[track_caller]
fn assert_align(d: impl core::fmt::Display) {
let expected = format!("{d:<10}");
fn assert_no_align(d: impl core::fmt::Display) {
let expected = format!("{d}");
let actual = format!("{d:<10}");
assert_eq!(expected, actual);
}

assert_align(Reset);
assert_align(Reset.render());
assert_no_align(Reset);
assert_no_align(Reset.render());
}
}

0 comments on commit ec33d22

Please sign in to comment.