1//! A module which contains [`Color`] trait and its implementation [`AnsiColor`].
2
3#[cfg(feature = "std")]
4mod ansi_color;
5mod static_color;
6
7#[cfg(feature = "std")]
8pub use ansi_color::AnsiColor;
9
10pub use static_color::StaticColor;
11
12use core::fmt::{self, Write};
13
14#[allow(unreachable_pub)]
15/// A trait which prints an ANSI prefix and suffix.
16pub trait Color {
17 /// Print ANSI prefix.
18 fn fmt_prefix<W: Write>(&self, f: &mut W) -> fmt::Result;
19
20 /// Print ANSI suffix.
21 fn fmt_suffix<W: Write>(&self, f: &mut W) -> fmt::Result {
22 f.write_str("\u{1b}[0m")
23 }
24
25 /// Print colored text.
26 ///
27 /// It may not handle `\n` (new lines).
28 fn colorize<W: Write>(&self, f: &mut W, text: &str) -> fmt::Result {
29 self.fmt_prefix(f)?;
30 f.write_str(text)?;
31 self.fmt_suffix(f)?;
32 Ok(())
33 }
34}
35
36impl<C> Color for &C
37where
38 C: Color,
39{
40 fn fmt_prefix<W: Write>(&self, f: &mut W) -> fmt::Result {
41 C::fmt_prefix(self, f)
42 }
43
44 fn fmt_suffix<W: Write>(&self, f: &mut W) -> fmt::Result {
45 C::fmt_suffix(self, f)
46 }
47
48 fn colorize<W: Write>(&self, f: &mut W, text: &str) -> fmt::Result {
49 C::colorize(self, f, text)
50 }
51}
52