Struct Formatter

Source
pub struct Formatter<'a> { /* private fields */ }
Expand description

A configured output stream.

A Formatter wraps a target output stream with a set of configuration options for formatting of data written to the stream. There is (currently) no public constructors for Formatter, an instance is created and passed to implementations of stylish::Display when they are used in the stylish macros.

Implementations§

Source§

impl<'a> Formatter<'a>

Source

pub fn with(&mut self, restyle: impl Restyle) -> Formatter<'_>

Create a sub-Formatter with some styles changed. This may be useful in implementations of stylish::Display to dynamically configure how some parts are formatted.

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

pub fn write_str(&mut self, s: &str) -> Result

Writes some data to the underlying output stream, using the current style.

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

pub fn write_fmt(&mut self, args: Arguments<'_>) -> Result

Writes some formatted data into this instance, overriding the current style as appropriate.

struct Name(&'static str);

impl stylish::Display for Name {
    fn fmt(&self, f: &mut stylish::Formatter<'_>) -> stylish::Result {
        match self.0 {
            "Ferris" => f.write_fmt(stylish::format_args!("{:(fg=red)}", self.0)),
            "Gorris" => f.write_fmt(stylish::format_args!("{:(fg=cyan)}", self.0)),
            _ => f.write_fmt(stylish::format_args!("{}", 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>"
);

Trait Implementations§

Source§

impl Debug for Formatter<'_>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<'a> Write for Formatter<'a>

Source§

fn write_str(&mut self, s: &str, style: Style) -> Result

Writes a string slice with a particular Style into this writer, returning whether the write succeeded. Read more
Source§

fn write_fmt(&mut self, args: Arguments<'_>) -> Result

Glue for usage of the stylish::write! macro with implementors of this trait. Read more
Source§

fn write_char(&mut self, c: char, style: Style) -> Result

Writes a char with a particular Style into this writer, returning whether the write succeeded. Read more
Source§

impl<'a> Write for Formatter<'a>

Source§

fn write_str(&mut self, s: &str) -> Result

Writes a string slice into this writer, returning whether the write succeeded. Read more
1.1.0 · Source§

fn write_char(&mut self, c: char) -> Result<(), Error>

Writes a char into this writer, returning whether the write succeeded. Read more
1.0.0 · Source§

fn write_fmt(&mut self, args: Arguments<'_>) -> Result<(), Error>

Glue for usage of the write! macro with implementors of this trait. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for Formatter<'a>

§

impl<'a> !RefUnwindSafe for Formatter<'a>

§

impl<'a> !Send for Formatter<'a>

§

impl<'a> !Sync for Formatter<'a>

§

impl<'a> Unpin for Formatter<'a>

§

impl<'a> !UnwindSafe for Formatter<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.