nu_ansi_term/
debug.rs

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