ansi_term/
debug.rs

1use std::fmt;
2
3use style::Style;
4
5
6/// Styles have a special `Debug` implementation that only shows the fields that
7/// are set. Fields that haven’t been touched aren’t included in the output.
8///
9/// This behaviour gets bypassed when using the alternate formatting mode
10/// `format!("{:#?}")`.
11///
12///     use ansi_term::Colour::{Red, Blue};
13///     assert_eq!("Style { fg(Red), on(Blue), bold, italic }",
14///                format!("{:?}", Red.on(Blue).bold().italic()));
15impl fmt::Debug for Style {
16    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
17        if fmt.alternate() {
18            fmt.debug_struct("Style")
19               .field("foreground",    &self.foreground)
20               .field("background",    &self.background)
21               .field("blink",         &self.is_blink)
22               .field("bold",          &self.is_bold)
23               .field("dimmed",        &self.is_dimmed)
24               .field("hidden",        &self.is_hidden)
25               .field("italic",        &self.is_italic)
26               .field("reverse",       &self.is_reverse)
27               .field("strikethrough", &self.is_strikethrough)
28               .field("underline",     &self.is_underline)
29               .finish()
30        }
31        else if self.is_plain() {
32            fmt.write_str("Style {}")
33        }
34        else {
35            fmt.write_str("Style { ")?;
36
37            let mut written_anything = false;
38
39            if let Some(fg) = self.foreground {
40                if written_anything { fmt.write_str(", ")? }
41                written_anything = true;
42                write!(fmt, "fg({:?})", fg)?
43            }
44
45            if let Some(bg) = self.background {
46                if written_anything { fmt.write_str(", ")? }
47                written_anything = true;
48                write!(fmt, "on({:?})", bg)?
49            }
50
51            {
52                let mut write_flag = |name| {
53                    if written_anything { fmt.write_str(", ")? }
54                    written_anything = true;
55                    fmt.write_str(name)
56                };
57
58                if self.is_blink          { write_flag("blink")? }
59                if self.is_bold           { write_flag("bold")? }
60                if self.is_dimmed         { write_flag("dimmed")? }
61                if self.is_hidden         { write_flag("hidden")? }
62                if self.is_italic         { write_flag("italic")? }
63                if self.is_reverse        { write_flag("reverse")? }
64                if self.is_strikethrough  { write_flag("strikethrough")? }
65                if self.is_underline      { write_flag("underline")? }
66            }
67
68            write!(fmt, " }}")
69        }
70    }
71}
72
73
74#[cfg(test)]
75mod test {
76    use style::Colour::*;
77    use style::Style;
78
79    fn style() -> Style {
80        Style::new()
81    }
82
83    macro_rules! test {
84        ($name: ident: $obj: expr => $result: expr) => {
85            #[test]
86            fn $name() {
87                assert_eq!($result, format!("{:?}", $obj));
88            }
89        };
90    }
91
92    test!(empty:   style()                  => "Style {}");
93    test!(bold:    style().bold()           => "Style { bold }");
94    test!(italic:  style().italic()         => "Style { italic }");
95    test!(both:    style().bold().italic()  => "Style { bold, italic }");
96
97    test!(red:     Red.normal()                     => "Style { fg(Red) }");
98    test!(redblue: Red.normal().on(RGB(3, 2, 4))    => "Style { fg(Red), on(RGB(3, 2, 4)) }");
99
100    test!(everything:
101            Red.on(Blue).blink().bold().dimmed().hidden().italic().reverse().strikethrough().underline() =>
102            "Style { fg(Red), on(Blue), blink, bold, dimmed, hidden, italic, reverse, strikethrough, underline }");
103
104    #[test]
105    fn long_and_detailed() {
106        let debug = r##"Style {
107    foreground: Some(
108        Blue
109    ),
110    background: None,
111    blink: false,
112    bold: true,
113    dimmed: false,
114    hidden: false,
115    italic: false,
116    reverse: false,
117    strikethrough: false,
118    underline: false
119}"##;
120        assert_eq!(debug, format!("{:#?}", Blue.bold()));
121    }
122}