Debian Codegraph @

src/parse_style.rs

raw symbols SCIP
1 use bitflags::bitflags;
2
3 use crate::color;
4 use crate::config::delta_unreachable;
5 use crate::fatal;
6 use crate::git_config::GitConfig;
7 use crate::style::{DecorationStyle, Style};
8
9 impl Style {
10 /// Construct Style from style and decoration-style strings supplied on command line, together
11 /// with defaults. A style string is a space-separated string containing 0, 1, or 2 colors
12 /// (foreground and then background) and an arbitrary number of style attributes. See `delta
13 /// --help` for more precise spec.
14 pub fn from_str(
15 style_string: &str,
16 default: Option<Self>,
17 decoration_style_string: Option<&str>,
18 true_color: bool,
19 git_config: Option<&GitConfig>,
20 ) -> Self {
21 let (ansi_term_style, is_omitted, is_raw, is_syntax_highlighted) =
22 parse_ansi_term_style(style_string, default, true_color, git_config);
23 let decoration_style = DecorationStyle::from_str(
24 decoration_style_string.unwrap_or(""),
25 true_color,
26 git_config,
27 );
28 Self {
29 ansi_term_style,
30 is_emph: false,
31 is_omitted,
32 is_raw,
33 is_syntax_highlighted,
34 decoration_style,
35 }
36 }
37
38 pub fn from_git_str(git_style_string: &str) -> Self {
39 Self::from_str(git_style_string, None, None, true, None)
40 }
41
42 /// Construct Style but interpreting 'ul', 'box', etc as applying to the decoration style.
43 pub fn from_str_with_handling_of_special_decoration_attributes(
44 style_string: &str,
45 default: Option<Self>,
46 decoration_style_string: Option<&str>,
47 true_color: bool,
48 git_config: Option<&GitConfig>,
49 ) -> Self {
50 let (special_attributes_from_style_string, style_string) =
51 extract_special_decoration_attributes_from_non_decoration_style_string(style_string);
52 let mut style = Style::from_str(
53 &style_string,
54 default,
55 decoration_style_string,
56 true_color,
57 git_config,
58 );
59 // TODO: box in this context resulted in box-with-underline for commit and file
60 style.decoration_style = DecorationStyle::apply_special_decoration_attributes(
61 &mut style,
62 special_attributes_from_style_string,
63 );
64 style
65 }
66 }
67
68 bitflags! {
69 #[derive(Clone, Copy, Debug, PartialEq)]
70 struct DecorationAttributes: u8 {
71 const EMPTY = 0b00000000;
72 const BOX = 0b00000001;
73 const OVERLINE = 0b00000010;
74 const UNDERLINE = 0b00000100;
75 }
76 }
77
78 impl DecorationStyle {
79 pub fn from_str(style_string: &str, true_color: bool, git_config: Option<&GitConfig>) -> Self {
80 let (special_attributes, style_string) =
81 extract_special_decoration_attributes(style_string);
82 let (style, is_omitted, is_raw, is_syntax_highlighted) =
83 parse_ansi_term_style(&style_string, None, true_color, git_config);
84 if is_raw {
85 fatal("'raw' may not be used in a decoration style.");
86 };
87 if is_syntax_highlighted {
88 fatal("'syntax' may not be used in a decoration style.");
89 };
90 #[allow(non_snake_case)]
91 let (BOX, UL, OL, EMPTY) = (
92 DecorationAttributes::BOX,
93 DecorationAttributes::UNDERLINE,
94 DecorationAttributes::OVERLINE,
95 DecorationAttributes::EMPTY,
96 );
97 match special_attributes {
98 bits if bits == EMPTY => DecorationStyle::NoDecoration,
99 bits if bits == BOX => DecorationStyle::Box(style),
100 bits if bits == UL => DecorationStyle::Underline(style),
101 bits if bits == OL => DecorationStyle::Overline(style),
102 bits if bits == UL | OL => DecorationStyle::UnderOverline(style),
103 bits if bits == BOX | UL => DecorationStyle::BoxWithUnderline(style),
104 bits if bits == BOX | OL => DecorationStyle::BoxWithOverline(style),
105 bits if bits == BOX | UL | OL => DecorationStyle::BoxWithUnderOverline(style),
106 _ if is_omitted => DecorationStyle::NoDecoration,
107 _ => delta_unreachable("Unreachable code path reached in parse_decoration_style."),
108 }
109 }
110
111 fn apply_special_decoration_attributes(
112 style: &mut Style,
113 special_attributes: DecorationAttributes,
114 ) -> DecorationStyle {
115 let ansi_term_style = match style.decoration_style {
116 DecorationStyle::Box(ansi_term_style) => ansi_term_style,
117 DecorationStyle::Underline(ansi_term_style) => ansi_term_style,
118 DecorationStyle::Overline(ansi_term_style) => ansi_term_style,
119 DecorationStyle::UnderOverline(ansi_term_style) => ansi_term_style,
120 DecorationStyle::BoxWithUnderline(ansi_term_style) => ansi_term_style,
121 DecorationStyle::BoxWithOverline(ansi_term_style) => ansi_term_style,
122 DecorationStyle::BoxWithUnderOverline(ansi_term_style) => ansi_term_style,
123 DecorationStyle::NoDecoration => ansi_term::Style::new(),
124 };
125 #[allow(non_snake_case)]
126 let (BOX, UL, OL, EMPTY) = (
127 DecorationAttributes::BOX,
128 DecorationAttributes::UNDERLINE,
129 DecorationAttributes::OVERLINE,
130 DecorationAttributes::EMPTY,
131 );
132 match special_attributes {
133 bits if bits == EMPTY => style.decoration_style,
134 bits if bits == BOX => DecorationStyle::Box(ansi_term_style),
135 bits if bits == UL => DecorationStyle::Underline(ansi_term_style),
136 bits if bits == OL => DecorationStyle::Overline(ansi_term_style),
137 bits if bits == UL | OL => DecorationStyle::UnderOverline(ansi_term_style),
138 bits if bits == BOX | UL => DecorationStyle::BoxWithUnderline(ansi_term_style),
139 bits if bits == BOX | OL => DecorationStyle::BoxWithOverline(ansi_term_style),
140 bits if bits == BOX | UL | OL => DecorationStyle::BoxWithUnderOverline(ansi_term_style),
141 _ => DecorationStyle::NoDecoration,
142 }
143 }
144 }
145
146 fn parse_ansi_term_style(
147 s: &str,
148 default: Option<Style>,
149 true_color: bool,
150 git_config: Option<&GitConfig>,
151 ) -> (ansi_term::Style, bool, bool, bool) {
152 let mut style = ansi_term::Style::new();
153 let mut seen_foreground = false;
154 let mut seen_background = false;
155 let mut foreground_is_auto = false;
156 let mut background_is_auto = false;
157 let mut is_omitted = false;
158 let mut is_raw = false;
159 let mut seen_omit = false;
160 let mut seen_raw = false;
161 let mut is_syntax_highlighted = false;
162 for word in s
163 .to_lowercase()
164 .split_whitespace()
165 .map(|word| word.trim_matches(|c| c == '"' || c == '\''))
166 {
167 if word == "blink" {
168 style.is_blink = true;
169 } else if word == "bold" {
170 style.is_bold = true;
171 } else if word == "dim" {
172 style.is_dimmed = true;
173 } else if word == "hidden" {
174 style.is_hidden = true;
175 } else if word == "italic" {
176 style.is_italic = true;
177 } else if word == "omit" {
178 seen_omit = true;
179 is_omitted = true;
180 } else if word == "reverse" {
181 style.is_reverse = true;
182 } else if word == "raw" {
183 seen_raw = true;
184 is_raw = true;
185 } else if word == "strike" {
186 style.is_strikethrough = true;
187 } else if word == "ul" || word == "underline" {
188 style.is_underline = true;
189 } else if word == "line-number" || word == "file" || word == "omit-code-fragment" {
190 // Allow: these are meaningful in hunk-header-style.
191 } else if !seen_foreground {
192 if word == "syntax" {
193 is_syntax_highlighted = true;
194 } else if word == "auto" {
195 foreground_is_auto = true;
196 style.foreground = default.and_then(|s| s.ansi_term_style.foreground);
197 is_syntax_highlighted = default.map(|s| s.is_syntax_highlighted).unwrap_or(false);
198 } else {
199 style.foreground = color::parse_color(word, true_color, git_config);
200 }
201 seen_foreground = true;
202 } else if !seen_background {
203 if word == "syntax" {
204 fatal(
205 "You have used the special color 'syntax' as a background color \
206 (second color in a style string). It may only be used as a foreground \
207 color (first color in a style string).",
208 );
209 } else if word == "auto" {
210 background_is_auto = true;
211 style.background = default.and_then(|s| s.ansi_term_style.background);
212 } else {
213 style.background = color::parse_color(word, true_color, git_config);
214 }
215 seen_background = true;
216 } else {
217 fatal(format!(
218 "Invalid style string: {s}. See the STYLES section of delta --help.",
219 ));
220 }
221 }
222 if foreground_is_auto && background_is_auto {
223 if !seen_omit {
224 is_omitted = default.map(|s| s.is_omitted).unwrap_or(false);
225 }
226 if !seen_raw {
227 is_raw = default.map(|s| s.is_raw).unwrap_or(false);
228 }
229 }
230 (style, is_omitted, is_raw, is_syntax_highlighted)
231 }
232
233 /// Extract set of 'special decoration attributes' and return it along with modified style string.
234 fn extract_special_decoration_attributes(style_string: &str) -> (DecorationAttributes, String) {
235 _extract_special_decoration_attributes(style_string, true)
236 }
237
238 fn extract_special_decoration_attributes_from_non_decoration_style_string(
239 style_string: &str,
240 ) -> (DecorationAttributes, String) {
241 _extract_special_decoration_attributes(style_string, false)
242 }
243
244 // If this is being called in the context of processing a decoration style string then we treat
245 // ul/ol as a request for an underline/overline decoration respectively. Otherwise they are
246 // conventional character style attributes.
247 fn _extract_special_decoration_attributes(
248 style_string: &str,
249 is_decoration_style_string: bool,
250 ) -> (DecorationAttributes, String) {
251 let mut attributes = DecorationAttributes::EMPTY;
252 let mut new_style_string = Vec::new();
253 let style_string = style_string.to_lowercase();
254 for token in style_string
255 .split_whitespace()
256 .map(|word| word.trim_matches(|c| c == '"' || c == '\''))
257 {
258 match token {
259 "box" => attributes |= DecorationAttributes::BOX,
260 token if token == "overline" || is_decoration_style_string && token == "ol" => {
261 attributes |= DecorationAttributes::OVERLINE
262 }
263 token if token == "underline" || is_decoration_style_string && token == "ul" => {
264 attributes |= DecorationAttributes::UNDERLINE
265 }
266 token if token == "none" || token == "plain" => {}
267 _ => new_style_string.push(token),
268 }
269 }
270 (attributes, new_style_string.join(" "))
271 }
272
273 #[cfg(test)]
274 mod tests {
275 use super::*;
276
277 #[test]
278 fn test_parse_ansi_term_style() {
279 assert_eq!(
280 parse_ansi_term_style("", None, false, None),
281 (ansi_term::Style::new(), false, false, false)
282 );
283 assert_eq!(
284 parse_ansi_term_style("red", None, false, None),
285 (
286 ansi_term::Style {
287 foreground: Some(ansi_term::Color::Red),
288 ..ansi_term::Style::new()
289 },
290 false,
291 false,
292 false
293 )
294 );
295 assert_eq!(
296 parse_ansi_term_style("red green", None, false, None),
297 (
298 ansi_term::Style {
299 foreground: Some(ansi_term::Color::Red),
300 background: Some(ansi_term::Color::Green),
301 ..ansi_term::Style::new()
302 },
303 false,
304 false,
305 false
306 )
307 );
308 assert_eq!(
309 parse_ansi_term_style("bold red underline green blink", None, false, None),
310 (
311 ansi_term::Style {
312 foreground: Some(ansi_term::Color::Red),
313 background: Some(ansi_term::Color::Green),
314 is_blink: true,
315 is_bold: true,
316 is_underline: true,
317 ..ansi_term::Style::new()
318 },
319 false,
320 false,
321 false
322 )
323 );
324 }
325
326 #[test]
327 fn test_parse_ansi_term_style_with_special_syntax_color() {
328 assert_eq!(
329 parse_ansi_term_style("syntax", None, false, None),
330 (ansi_term::Style::new(), false, false, true)
331 );
332 assert_eq!(
333 parse_ansi_term_style("syntax italic white hidden", None, false, None),
334 (
335 ansi_term::Style {
336 background: Some(ansi_term::Color::White),
337 is_italic: true,
338 is_hidden: true,
339 ..ansi_term::Style::new()
340 },
341 false,
342 false,
343 true
344 )
345 );
346 assert_eq!(
347 parse_ansi_term_style("bold syntax italic white hidden", None, false, None),
348 (
349 ansi_term::Style {
350 background: Some(ansi_term::Color::White),
351 is_bold: true,
352 is_italic: true,
353 is_hidden: true,
354 ..ansi_term::Style::new()
355 },
356 false,
357 false,
358 true
359 )
360 );
361 }
362
363 #[test]
364 fn test_parse_ansi_term_style_with_special_omit_attribute() {
365 assert_eq!(
366 parse_ansi_term_style("omit", None, false, None),
367 (ansi_term::Style::new(), true, false, false)
368 );
369 // It doesn't make sense for omit to be combined with anything else, but it is not an error.
370 assert_eq!(
371 parse_ansi_term_style("omit syntax italic white hidden", None, false, None),
372 (
373 ansi_term::Style {
374 background: Some(ansi_term::Color::White),
375 is_italic: true,
376 is_hidden: true,
377 ..ansi_term::Style::new()
378 },
379 true,
380 false,
381 true
382 )
383 );
384 }
385
386 #[test]
387 fn test_parse_ansi_term_style_with_special_raw_attribute() {
388 assert_eq!(
389 parse_ansi_term_style("raw", None, false, None),
390 (ansi_term::Style::new(), false, true, false)
391 );
392 // It doesn't make sense for raw to be combined with anything else, but it is not an error.
393 assert_eq!(
394 parse_ansi_term_style("raw syntax italic white hidden", None, false, None),
395 (
396 ansi_term::Style {
397 background: Some(ansi_term::Color::White),
398 is_italic: true,
399 is_hidden: true,
400 ..ansi_term::Style::new()
401 },
402 false,
403 true,
404 true
405 )
406 );
407 }
408
409 #[test]
410 fn test_extract_special_decoration_attribute() {
411 #[allow(non_snake_case)]
412 let (BOX, UL, OL, EMPTY) = (
413 DecorationAttributes::BOX,
414 DecorationAttributes::UNDERLINE,
415 DecorationAttributes::OVERLINE,
416 DecorationAttributes::EMPTY,
417 );
418 assert_eq!(
419 extract_special_decoration_attributes(""),
420 (EMPTY, "".to_string(),)
421 );
422 assert_eq!(
423 extract_special_decoration_attributes("box"),
424 (BOX, "".to_string())
425 );
426 assert_eq!(
427 extract_special_decoration_attributes("ul"),
428 (UL, "".to_string())
429 );
430 assert_eq!(
431 extract_special_decoration_attributes("ol"),
432 (OL, "".to_string())
433 );
434 assert_eq!(
435 extract_special_decoration_attributes("box ul"),
436 (BOX | UL, "".to_string())
437 );
438 assert_eq!(
439 extract_special_decoration_attributes("box ol"),
440 (BOX | OL, "".to_string())
441 );
442 assert_eq!(
443 extract_special_decoration_attributes("ul box ol"),
444 (BOX | UL | OL, "".to_string())
445 );
446 assert_eq!(
447 extract_special_decoration_attributes("ol ul"),
448 (UL | OL, "".to_string())
449 );
450 }
451
452 #[test]
453 fn test_decoration_style_from_str_empty_string() {
454 assert_eq!(
455 DecorationStyle::from_str("", true, None),
456 DecorationStyle::NoDecoration,
457 )
458 }
459
460 #[test]
461 fn test_decoration_style_from_str() {
462 assert_eq!(
463 DecorationStyle::from_str("ol red box bold green ul", true, None),
464 DecorationStyle::BoxWithUnderOverline(ansi_term::Style {
465 foreground: Some(ansi_term::Color::Red),
466 background: Some(ansi_term::Color::Green),
467 is_bold: true,
468 ..ansi_term::Style::new()
469 })
470 )
471 }
472
473 #[test]
474 fn test_style_from_str() {
475 let actual_style = Style::from_str(
476 "red green bold",
477 None,
478 Some("ol red box bold green ul"),
479 true,
480 None,
481 );
482 let red_green_bold = ansi_term::Style {
483 foreground: Some(ansi_term::Color::Red),
484 background: Some(ansi_term::Color::Green),
485 is_bold: true,
486 ..ansi_term::Style::new()
487 };
488 assert_eq!(
489 actual_style,
490 Style {
491 ansi_term_style: red_green_bold,
492 decoration_style: DecorationStyle::BoxWithUnderOverline(red_green_bold),
493 ..Style::new()
494 }
495 )
496 }
497
498 #[test]
499 fn test_style_from_str_raw_with_box() {
500 let actual_style = Style::from_str("raw", None, Some("box"), true, None);
501 let empty_ansi_term_style = ansi_term::Style::new();
502 assert_eq!(
503 actual_style,
504 Style {
505 ansi_term_style: empty_ansi_term_style,
506 decoration_style: DecorationStyle::Box(empty_ansi_term_style),
507 is_raw: true,
508 ..Style::new()
509 }
510 )
511 }
512
513 #[test]
514 fn test_style_from_str_decoration_style_only() {
515 let actual_style = Style::from_str("", None, Some("ol red box bold green ul"), true, None);
516 let red_green_bold = ansi_term::Style {
517 foreground: Some(ansi_term::Color::Red),
518 background: Some(ansi_term::Color::Green),
519 is_bold: true,
520 ..ansi_term::Style::new()
521 };
522 assert_eq!(
523 actual_style,
524 Style {
525 decoration_style: DecorationStyle::BoxWithUnderOverline(red_green_bold),
526 ..Style::new()
527 }
528 )
529 }
530
531 #[test]
532 fn test_style_from_str_with_handling_of_special_decoration_attributes() {
533 let actual_style = Style::from_str_with_handling_of_special_decoration_attributes(
534 "",
535 None,
536 Some("ol red box bold green ul"),
537 true,
538 None,
539 );
540 let expected_decoration_style = DecorationStyle::BoxWithUnderOverline(ansi_term::Style {
541 foreground: Some(ansi_term::Color::Red),
542 background: Some(ansi_term::Color::Green),
543 is_bold: true,
544 ..ansi_term::Style::new()
545 });
546 assert_eq!(
547 actual_style,
548 Style {
549 decoration_style: expected_decoration_style,
550 ..Style::new()
551 }
552 )
553 }
554
555 #[test]
556 fn test_style_from_str_with_handling_of_special_decoration_attributes_raw_with_box() {
557 let actual_style = Style::from_str_with_handling_of_special_decoration_attributes(
558 "raw",
559 None,
560 Some("box"),
561 true,
562 None,
563 );
564 let empty_ansi_term_style = ansi_term::Style::new();
565 assert_eq!(
566 actual_style,
567 Style {
568 ansi_term_style: empty_ansi_term_style,
569 decoration_style: DecorationStyle::Box(empty_ansi_term_style),
570 is_raw: true,
571 ..Style::new()
572 }
573 )
574 }
575 }