1use super::Style;
234/// When printing out one coloured string followed by another, use one of
5/// these rules to figure out which *extra* control codes need to be sent.
6#[derive(PartialEq, Clone, Copy, Debug)]
7pub enum Difference {
89/// Print out the control codes specified by this style to end up looking
10 /// like the second string's styles.
11ExtraStyles(Style),
1213/// Converting between these two is impossible, so just send a reset
14 /// command and then the second string's styles.
15Reset,
1617/// The before style is exactly the same as the after style, so no further
18 /// control codes need to be printed.
19NoDifference,
20}
212223impl Difference {
2425/// Compute the 'style difference' required to turn an existing style into
26 /// the given, second style.
27 ///
28 /// For example, to turn green text into green bold text, it's redundant
29 /// to write a reset command then a second green+bold command, instead of
30 /// just writing one bold command. This method should see that both styles
31 /// use the foreground colour green, and reduce it to a single command.
32 ///
33 /// This method returns an enum value because it's not actually always
34 /// possible to turn one style into another: for example, text could be
35 /// made bold and underlined, but you can't remove the bold property
36 /// without also removing the underline property. So when this has to
37 /// happen, this function returns None, meaning that the entire set of
38 /// styles should be reset and begun again.
39pub fn between(first: &Style, next: &Style) -> Difference {
40use self::Difference::*;
4142// XXX(Havvy): This algorithm is kind of hard to replicate without
43 // having the Plain/Foreground enum variants, so I'm just leaving
44 // it commented out for now, and defaulting to Reset.
4546if first == next {
47return NoDifference;
48 }
4950// Cannot un-bold, so must Reset.
51if first.is_bold && !next.is_bold {
52return Reset;
53 }
5455if first.is_dimmed && !next.is_dimmed {
56return Reset;
57 }
5859if first.is_italic && !next.is_italic {
60return Reset;
61 }
6263// Cannot un-underline, so must Reset.
64if first.is_underline && !next.is_underline {
65return Reset;
66 }
6768if first.is_blink && !next.is_blink {
69return Reset;
70 }
7172if first.is_reverse && !next.is_reverse {
73return Reset;
74 }
7576if first.is_hidden && !next.is_hidden {
77return Reset;
78 }
7980if first.is_strikethrough && !next.is_strikethrough {
81return Reset;
82 }
8384// Cannot go from foreground to no foreground, so must Reset.
85if first.foreground.is_some() && next.foreground.is_none() {
86return Reset;
87 }
8889// Cannot go from background to no background, so must Reset.
90if first.background.is_some() && next.background.is_none() {
91return Reset;
92 }
9394let mut extra_styles = Style::default();
9596if first.is_bold != next.is_bold {
97 extra_styles.is_bold = true;
98 }
99100if first.is_dimmed != next.is_dimmed {
101 extra_styles.is_dimmed = true;
102 }
103104if first.is_italic != next.is_italic {
105 extra_styles.is_italic = true;
106 }
107108if first.is_underline != next.is_underline {
109 extra_styles.is_underline = true;
110 }
111112if first.is_blink != next.is_blink {
113 extra_styles.is_blink = true;
114 }
115116if first.is_reverse != next.is_reverse {
117 extra_styles.is_reverse = true;
118 }
119120if first.is_hidden != next.is_hidden {
121 extra_styles.is_hidden = true;
122 }
123124if first.is_strikethrough != next.is_strikethrough {
125 extra_styles.is_strikethrough = true;
126 }
127128if first.foreground != next.foreground {
129 extra_styles.foreground = next.foreground;
130 }
131132if first.background != next.background {
133 extra_styles.background = next.background;
134 }
135136 ExtraStyles(extra_styles)
137 }
138}
139140141#[cfg(test)]
142mod test {
143use super::*;
144use super::Difference::*;
145use style::Colour::*;
146use style::Style;
147148fn style() -> Style {
149 Style::new()
150 }
151152macro_rules! test {
153 ($name: ident: $first: expr; $next: expr => $result: expr) => {
154#[test]
155fn $name() {
156assert_eq!($result, Difference::between(&$first, &$next));
157 }
158 };
159 }
160161test!(nothing: Green.normal(); Green.normal() => NoDifference);
162test!(uppercase: Green.normal(); Green.bold() => ExtraStyles(style().bold()));
163test!(lowercase: Green.bold(); Green.normal() => Reset);
164test!(nothing2: Green.bold(); Green.bold() => NoDifference);
165166test!(colour_change: Red.normal(); Blue.normal() => ExtraStyles(Blue.normal()));
167168test!(addition_of_blink: style(); style().blink() => ExtraStyles(style().blink()));
169test!(addition_of_dimmed: style(); style().dimmed() => ExtraStyles(style().dimmed()));
170test!(addition_of_hidden: style(); style().hidden() => ExtraStyles(style().hidden()));
171test!(addition_of_reverse: style(); style().reverse() => ExtraStyles(style().reverse()));
172test!(addition_of_strikethrough: style(); style().strikethrough() => ExtraStyles(style().strikethrough()));
173174test!(removal_of_strikethrough: style().strikethrough(); style() => Reset);
175test!(removal_of_reverse: style().reverse(); style() => Reset);
176test!(removal_of_hidden: style().hidden(); style() => Reset);
177test!(removal_of_dimmed: style().dimmed(); style() => Reset);
178test!(removal_of_blink: style().blink(); style() => Reset);
179}