/rust/registry/src/index.crates.io-6f17d22bba15001f/nu-ansi-term-0.49.0/src/debug.rs
Line | Count | Source (jump to first uncovered line) |
1 | | use crate::style::Style; |
2 | | use 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())); |
13 | | impl fmt::Debug for Style { |
14 | 0 | fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { |
15 | 0 | if fmt.alternate() { |
16 | 0 | fmt.debug_struct("Style") |
17 | 0 | .field("foreground", &self.foreground) |
18 | 0 | .field("background", &self.background) |
19 | 0 | .field("blink", &self.is_blink) |
20 | 0 | .field("bold", &self.is_bold) |
21 | 0 | .field("dimmed", &self.is_dimmed) |
22 | 0 | .field("hidden", &self.is_hidden) |
23 | 0 | .field("italic", &self.is_italic) |
24 | 0 | .field("reverse", &self.is_reverse) |
25 | 0 | .field("strikethrough", &self.is_strikethrough) |
26 | 0 | .field("underline", &self.is_underline) |
27 | 0 | .finish() |
28 | 0 | } else if self.is_plain() { |
29 | 0 | fmt.write_str("Style {}") |
30 | | } else { |
31 | 0 | fmt.write_str("Style { ")?; |
32 | | |
33 | 0 | let mut written_anything = false; |
34 | | |
35 | 0 | if let Some(fg) = self.foreground { |
36 | 0 | if written_anything { |
37 | 0 | fmt.write_str(", ")? |
38 | 0 | } |
39 | 0 | written_anything = true; |
40 | 0 | write!(fmt, "fg({:?})", fg)? |
41 | 0 | } |
42 | | |
43 | 0 | if let Some(bg) = self.background { |
44 | 0 | if written_anything { |
45 | 0 | fmt.write_str(", ")? |
46 | 0 | } |
47 | 0 | written_anything = true; |
48 | 0 | write!(fmt, "on({:?})", bg)? |
49 | 0 | } |
50 | | |
51 | | { |
52 | 0 | let mut write_flag = |name| { |
53 | 0 | if written_anything { |
54 | 0 | fmt.write_str(", ")? |
55 | 0 | } |
56 | 0 | written_anything = true; |
57 | 0 | fmt.write_str(name) |
58 | 0 | }; |
59 | | |
60 | 0 | if self.is_blink { |
61 | 0 | write_flag("blink")? |
62 | 0 | } |
63 | 0 | if self.is_bold { |
64 | 0 | write_flag("bold")? |
65 | 0 | } |
66 | 0 | if self.is_dimmed { |
67 | 0 | write_flag("dimmed")? |
68 | 0 | } |
69 | 0 | if self.is_hidden { |
70 | 0 | write_flag("hidden")? |
71 | 0 | } |
72 | 0 | if self.is_italic { |
73 | 0 | write_flag("italic")? |
74 | 0 | } |
75 | 0 | if self.is_reverse { |
76 | 0 | write_flag("reverse")? |
77 | 0 | } |
78 | 0 | if self.is_strikethrough { |
79 | 0 | write_flag("strikethrough")? |
80 | 0 | } |
81 | 0 | if self.is_underline { |
82 | 0 | write_flag("underline")? |
83 | 0 | } |
84 | | } |
85 | | |
86 | 0 | write!(fmt, " }}") |
87 | | } |
88 | 0 | } |
89 | | } |
90 | | |
91 | | #[cfg(test)] |
92 | | mod 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 | | } |