1 | use super::Style; |
2 | |
3 | /// When printing out one colored string followed by another, use one of |
4 | /// these rules to figure out which *extra* control codes need to be sent. |
5 | #[derive(PartialEq, Clone, Copy, Debug)] |
6 | pub enum Difference { |
7 | /// Print out the control codes specified by this style to end up looking |
8 | /// like the second string's styles. |
9 | ExtraStyles(Style), |
10 | |
11 | /// Converting between these two is impossible, so just send a reset |
12 | /// command and then the second string's styles. |
13 | Reset, |
14 | |
15 | /// The before style is exactly the same as the after style, so no further |
16 | /// control codes need to be printed. |
17 | Empty, |
18 | } |
19 | |
20 | impl Difference { |
21 | /// Compute the 'style difference' required to turn an existing style into |
22 | /// the given, second style. |
23 | /// |
24 | /// For example, to turn green text into green bold text, it's redundant |
25 | /// to write a reset command then a second green+bold command, instead of |
26 | /// just writing one bold command. This method should see that both styles |
27 | /// use the foreground color green, and reduce it to a single command. |
28 | /// |
29 | /// This method returns an enum value because it's not actually always |
30 | /// possible to turn one style into another: for example, text could be |
31 | /// made bold and underlined, but you can't remove the bold property |
32 | /// without also removing the underline property. So when this has to |
33 | /// happen, this function returns None, meaning that the entire set of |
34 | /// styles should be reset and begun again. |
35 | pub fn between(first: &Style, next: &Style) -> Difference { |
36 | use self::Difference::*; |
37 | |
38 | // XXX(Havvy): This algorithm is kind of hard to replicate without |
39 | // having the Plain/Foreground enum variants, so I'm just leaving |
40 | // it commented out for now, and defaulting to Reset. |
41 | |
42 | if first == next { |
43 | return Empty; |
44 | } |
45 | |
46 | // Cannot un-bold, so must Reset. |
47 | if first.is_bold && !next.is_bold { |
48 | return Reset; |
49 | } |
50 | |
51 | if first.is_dimmed && !next.is_dimmed { |
52 | return Reset; |
53 | } |
54 | |
55 | if first.is_italic && !next.is_italic { |
56 | return Reset; |
57 | } |
58 | |
59 | // Cannot un-underline, so must Reset. |
60 | if first.is_underline && !next.is_underline { |
61 | return Reset; |
62 | } |
63 | |
64 | if first.is_blink && !next.is_blink { |
65 | return Reset; |
66 | } |
67 | |
68 | if first.is_reverse && !next.is_reverse { |
69 | return Reset; |
70 | } |
71 | |
72 | if first.is_hidden && !next.is_hidden { |
73 | return Reset; |
74 | } |
75 | |
76 | if first.is_strikethrough && !next.is_strikethrough { |
77 | return Reset; |
78 | } |
79 | |
80 | // Cannot go from foreground to no foreground, so must Reset. |
81 | if first.foreground.is_some() && next.foreground.is_none() { |
82 | return Reset; |
83 | } |
84 | |
85 | // Cannot go from background to no background, so must Reset. |
86 | if first.background.is_some() && next.background.is_none() { |
87 | return Reset; |
88 | } |
89 | |
90 | let mut extra_styles = Style::default(); |
91 | |
92 | if first.is_bold != next.is_bold { |
93 | extra_styles.is_bold = true; |
94 | } |
95 | |
96 | if first.is_dimmed != next.is_dimmed { |
97 | extra_styles.is_dimmed = true; |
98 | } |
99 | |
100 | if first.is_italic != next.is_italic { |
101 | extra_styles.is_italic = true; |
102 | } |
103 | |
104 | if first.is_underline != next.is_underline { |
105 | extra_styles.is_underline = true; |
106 | } |
107 | |
108 | if first.is_blink != next.is_blink { |
109 | extra_styles.is_blink = true; |
110 | } |
111 | |
112 | if first.is_reverse != next.is_reverse { |
113 | extra_styles.is_reverse = true; |
114 | } |
115 | |
116 | if first.is_hidden != next.is_hidden { |
117 | extra_styles.is_hidden = true; |
118 | } |
119 | |
120 | if first.is_strikethrough != next.is_strikethrough { |
121 | extra_styles.is_strikethrough = true; |
122 | } |
123 | |
124 | if first.foreground != next.foreground { |
125 | extra_styles.foreground = next.foreground; |
126 | } |
127 | |
128 | if first.background != next.background { |
129 | extra_styles.background = next.background; |
130 | } |
131 | |
132 | ExtraStyles(extra_styles) |
133 | } |
134 | } |
135 | |
136 | #[cfg (test)] |
137 | mod test { |
138 | use super::Difference::*; |
139 | use super::*; |
140 | use crate::style::Color::*; |
141 | use crate::style::Style; |
142 | |
143 | fn style() -> Style { |
144 | Style::new() |
145 | } |
146 | |
147 | macro_rules! test { |
148 | ($name: ident: $first: expr; $next: expr => $result: expr) => { |
149 | #[test] |
150 | fn $name() { |
151 | assert_eq!($result, Difference::between(&$first, &$next)); |
152 | } |
153 | }; |
154 | } |
155 | |
156 | test!(nothing: Green.normal(); Green.normal() => Empty); |
157 | test!(uppercase: Green.normal(); Green.bold() => ExtraStyles(style().bold())); |
158 | test!(lowercase: Green.bold(); Green.normal() => Reset); |
159 | test!(nothing2: Green.bold(); Green.bold() => Empty); |
160 | |
161 | test!(color_change: Red.normal(); Blue.normal() => ExtraStyles(Blue.normal())); |
162 | |
163 | test!(addition_of_blink: style(); style().blink() => ExtraStyles(style().blink())); |
164 | test!(addition_of_dimmed: style(); style().dimmed() => ExtraStyles(style().dimmed())); |
165 | test!(addition_of_hidden: style(); style().hidden() => ExtraStyles(style().hidden())); |
166 | test!(addition_of_reverse: style(); style().reverse() => ExtraStyles(style().reverse())); |
167 | test!(addition_of_strikethrough: style(); style().strikethrough() => ExtraStyles(style().strikethrough())); |
168 | |
169 | test!(removal_of_strikethrough: style().strikethrough(); style() => Reset); |
170 | test!(removal_of_reverse: style().reverse(); style() => Reset); |
171 | test!(removal_of_hidden: style().hidden(); style() => Reset); |
172 | test!(removal_of_dimmed: style().dimmed(); style() => Reset); |
173 | test!(removal_of_blink: style().blink(); style() => Reset); |
174 | } |
175 | |