1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//! A module which contains [`ANSIFmt`] trait and its implementation [`ANSIStr`]
#[cfg_attr(feature = "std", doc = "and [`ANSIBuf`].")]
#[cfg(feature = "std")]
mod ansi_buf;
mod ansi_str;

#[cfg(feature = "std")]
pub use ansi_buf::ANSIBuf;

pub use self::ansi_str::ANSIStr;

use core::fmt::{self, Write};

/// A trait which prints an ANSI prefix and suffix.
pub trait ANSIFmt {
    /// Print ANSI prefix.
    fn fmt_ansi_prefix<W: Write>(&self, f: &mut W) -> fmt::Result;

    /// Print ANSI suffix.
    fn fmt_ansi_suffix<W: Write>(&self, f: &mut W) -> fmt::Result {
        f.write_str("\u{1b}[0m")
    }
}

impl<C> ANSIFmt for &C
where
    C: ANSIFmt,
{
    fn fmt_ansi_prefix<W: Write>(&self, f: &mut W) -> fmt::Result {
        C::fmt_ansi_prefix(self, f)
    }

    fn fmt_ansi_suffix<W: Write>(&self, f: &mut W) -> fmt::Result {
        C::fmt_ansi_suffix(self, f)
    }
}