Trait Display

Source
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§

Source

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>",
);

Implementations on Foreign Types§

Source§

impl<T: Display + ?Sized> Display for &T

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Implementors§

Source§

impl Display for Arguments<'_>

Source§

impl Display for String

Available on crate feature alloc only.