Skip to content

Commit

Permalink
feat: use inner Display implementation (#1097)
Browse files Browse the repository at this point in the history
  • Loading branch information
EdJoPaTo authored May 13, 2024
1 parent 28e81c0 commit eb281df
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
2 changes: 0 additions & 2 deletions src/buffer/cell.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::fmt::Debug;

use compact_str::CompactString;

use crate::prelude::*;
Expand Down
7 changes: 5 additions & 2 deletions src/text/masked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,15 @@ impl<'a> Masked<'a> {
impl fmt::Debug for Masked<'_> {
/// Debug representation of a masked string is the underlying string
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.inner)
// note that calling display instead of Debug here is intentional
fmt::Display::fmt(&self.inner, f)
}
}

impl fmt::Display for Masked<'_> {
/// Display representation of a masked string is the masked string
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.value())
fmt::Display::fmt(&self.value(), f)
}
}

Expand Down Expand Up @@ -109,12 +110,14 @@ mod tests {
fn debug() {
let masked = Masked::new("12345", 'x');
assert_eq!(format!("{masked:?}"), "12345");
assert_eq!(format!("{masked:.3?}"), "123", "Debug truncates");
}

#[test]
fn display() {
let masked = Masked::new("12345", 'x');
assert_eq!(format!("{masked}"), "xxxxx");
assert_eq!(format!("{masked:.3}"), "xxx", "Display truncates");
}

#[test]
Expand Down
6 changes: 3 additions & 3 deletions src/text/span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ impl WidgetRef for Span<'_> {

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

Expand Down Expand Up @@ -529,15 +529,15 @@ mod tests {
#[test]
fn display_span() {
let span = Span::raw("test content");

assert_eq!(format!("{span}"), "test content");
assert_eq!(format!("{span:.4}"), "test");
}

#[test]
fn display_styled_span() {
let stylized_span = Span::styled("stylized test content", Style::new().green());

assert_eq!(format!("{stylized_span}"), "stylized test content");
assert_eq!(format!("{stylized_span:.8}"), "stylized");
}

#[test]
Expand Down

0 comments on commit eb281df

Please sign in to comment.