pub trait Display {
// Required method
fn fmt(&self, f: &mut Formatter<'_>) -> Result;
}
Expand description
Format trait for the stylish
format,
{:s}
.
Display
is similar to core::fmt::Display
, but allows attaching
additional style attributes to the output.
struct Name(&'static str);
impl stylish::Display for Name {
fn fmt(&self, f: &mut stylish::Formatter<'_>) -> stylish::Result {
let color = match self.0 {
"Ferris" => stylish::Color::Red,
"Gorris" => stylish::Color::Cyan,
_ => stylish::Color::Default,
};
f.with(stylish::Foreground(color)).write_str(self.0)
}
}
let formatted = stylish::html::format!("Hello {:s} and {:s}", Name("Ferris"), Name("Gorris"));
assert_eq!(
formatted,
"Hello <span style=color:red>Ferris</span> and <span style=color:cyan>Gorris</span>",
);
Required Methods§
Sourcefn fmt(&self, f: &mut Formatter<'_>) -> Result
fn fmt(&self, f: &mut Formatter<'_>) -> Result
Formats the value using the given formatter.
struct Name(&'static str);
impl stylish::Display for Name {
fn fmt(&self, f: &mut stylish::Formatter<'_>) -> stylish::Result {
let color = match self.0 {
"Ferris" => stylish::Color::Red,
"Gorris" => stylish::Color::Cyan,
_ => stylish::Color::Default,
};
f.with(stylish::Foreground(color)).write_str(self.0)
}
}
let formatted = stylish::html::format!("Hello {:s} and {:s}", Name("Ferris"), Name("Gorris"));
assert_eq!(
formatted,
"Hello <span style=color:red>Ferris</span> and <span style=color:cyan>Gorris</span>",
);