Trait Write

Source
pub trait Write {
    // Required method
    fn write_str(&mut self, s: &str, style: Style) -> Result;

    // Provided methods
    fn write_char(&mut self, c: char, style: Style) -> Result { ... }
    fn write_fmt(&mut self, args: Arguments<'_>) -> Result { ... }
}
Expand description

A trait for writing or formatting into attributed Unicode-accepting buffers or streams.

This trait only accepts UTF-8–encoded data and is not flushable. If you only want to accept Unicode and you don’t need flushing, you should implement this trait; otherwise you should implement stylish::io::Write.

Required Methods§

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.

This method can only succeed if the entire string slice was successfully written, and this method will not return until all data has been written or an error occurs.

use stylish::{Color, Foreground, Style, Write};

let blue = Style::default().with(Foreground(Color::Blue));

let mut s = String::new();
{
    let mut output = stylish::html(&mut s);
    output.write_str("water is ", Style::default())?;
    output.write_str("blue", blue)?;
    output.finish()?;
}

assert_eq!(s, "water is <span style=color:blue>blue</span>");

Provided Methods§

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.

A single char may be encoded as more than one byte. This method can only succeed if the entire byte sequence was successfully written, and this method will not return until all data has been written or an error occurs.

use stylish::{Color, Foreground, Style, Write};

let yellow = Style::default().with(Foreground(Color::Yellow));
let red = Style::default().with(Foreground(Color::Red));

let mut s = String::new();
{
    let mut output = stylish::html(&mut s);
    output.write_char('⚠', yellow)?;
    output.write_char(' ', Style::default())?;
    output.write_char('⛔', red)?;
    output.finish()?;
}

assert_eq!(
    s,
    "<span style=color:yellow>⚠</span> <span style=color:red>⛔</span>"
);
Source

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

Glue for usage of the stylish::write! macro with implementors of this trait.

This method should generally not be invoked manually, but rather through the stylish::write! macro itself.

let mut s = String::new();
{
    let mut output = stylish::html(&mut s);
    output.write_fmt(stylish::format_args!("{:(fg=red)}", '☎'))?;
    output.finish()?;
}

assert_eq!(s, "<span style=color:red>☎</span>");

Implementations on Foreign Types§

Source§

impl<W: Write + ?Sized> Write for &mut W

Source§

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

Source§

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

Source§

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

Implementors§

Source§

impl Write for String

Available on crate feature alloc only.
Source§

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