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>
impl<'a> Formatter<'a>
Sourcepub fn with(&mut self, restyle: impl Restyle) -> Formatter<'_>
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>"
);
Sourcepub fn write_str(&mut self, s: &str) -> Result
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>"
);
Sourcepub fn write_fmt(&mut self, args: Arguments<'_>) -> Result
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<'a> Write for Formatter<'a>
impl<'a> Write for Formatter<'a>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more