ansiconst 0.2.1

Library for declaring nestable ANSI styles in const context
Documentation
use std::io;
use std::fmt;

use crate::{styled_write, Ansi};
use super::{AnsiPreference, AnsiWrite};

/// A `Writer` that writes styled output to an inner [`Write`](io::Write) using
/// a configurable default [`Ansi`] instance.
///
/// **Note**: only calls to this `Writer`'s [`write_fmt()`](io::Write::write_fmt()) method
/// will have the default ANSI styling applied. Calls to any other [`Write`](io::Write)
/// methods are unaffected.
pub struct AnsiWriter<W: io::Write + AnsiPreference> {
    ansi: Ansi,
    writer: W,
}

impl<W: io::Write + AnsiPreference> AnsiWriter<W> {
    /// Creates a new instance with the given `Writer` and ANSI style
    #[inline]
    pub fn new(writer: W, ansi: Ansi) -> Self { Self { writer, ansi } }
    /// Creates a new instance with the given `Writer`, using its
    /// [preferred](AnsiPreference::preferred_ansi) ANSI style.
    #[inline]
    pub fn default(writer: W) -> Self { Self { ansi: writer.preferred_ansi(), writer } }
}

impl<W: io::Write + AnsiPreference> AnsiWrite for AnsiWriter<W> {
    fn ansi(&self) -> Ansi { self.ansi }
    fn set_ansi(&mut self, ansi: Ansi) { self.ansi = ansi }
}

impl<W: io::Write + AnsiPreference> AnsiPreference for AnsiWriter<W> {
    fn is_ansi_preferred(&self) -> bool { self.writer.is_ansi_preferred() }
}

impl<W: io::Write + AnsiPreference> io::Write for AnsiWriter<W> {
    fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> io::Result<()> {
        if ! self.ansi.is_empty() {
            styled_write!(self.writer, self.ansi, "{}", fmt)
        } else {
            self.writer.write_fmt(fmt)
        }
    }
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> { self.writer.write(buf) }
    fn flush(&mut self) -> io::Result<()> { self.writer.flush() }
}