stylish_plain/
plain.rs

1use stylish_core::{Result, Style, Write};
2
3/// An adaptor to allow writing [`stylish`] attributed data to an output stream
4/// by discarding style attributes.
5///
6/// ```rust
7/// let mut writer = stylish::Plain::new(String::new());
8/// stylish::write!(writer, "Hello {:(fg=red)}", "Ferris")?;
9/// assert_eq!(writer.into_inner(), "Hello Ferris");
10/// # Ok::<(), core::fmt::Error>(())
11/// ```
12#[derive(Clone, Debug, Default)]
13pub struct Plain<T> {
14    inner: T,
15}
16
17impl<T: core::fmt::Write> Plain<T> {
18    /// Wrap the given output stream in this adaptor.
19    pub fn new(inner: T) -> Self {
20        Self { inner }
21    }
22
23    /// Inherent delegation to
24    /// [`stylish::Write::write_fmt`](stylish_core::Write::write_fmt) to not
25    /// require a trait import.
26    pub fn write_fmt(&mut self, args: stylish_core::Arguments<'_>) -> Result {
27        stylish_core::Write::write_fmt(self, args)
28    }
29
30    /// Get back the wrapped output stream.
31    pub fn into_inner(self) -> T {
32        self.inner
33    }
34}
35
36impl<T: core::fmt::Write> Write for Plain<T> {
37    fn write_str(&mut self, s: &str, _style: Style) -> Result {
38        self.inner.write_str(s)
39    }
40}