diff options
-rw-r--r-- | src/delta.rs | 43 | ||||
-rw-r--r-- | src/draw.rs | 11 | ||||
-rw-r--r-- | src/paint.rs | 8 | ||||
-rw-r--r-- | src/style.rs | 86 |
4 files changed, 77 insertions, 71 deletions
diff --git a/src/delta.rs b/src/delta.rs index 86720418..0db2d2c6 100644 --- a/src/delta.rs +++ b/src/delta.rs @@ -5,7 +5,6 @@ use console::strip_ansi_codes; use std::io::BufRead; use unicode_segmentation::UnicodeSegmentation; -use crate::cli::unreachable; use crate::config::Config; use crate::draw; use crate::paint::Painter; @@ -191,25 +190,27 @@ fn handle_commit_meta_header_line( let decoration_ansi_term_style; let mut pad = false; let draw_fn = match config.commit_style.decoration_style { - Some(DecorationStyle::Box(style)) => { + DecorationStyle::Box(style) => { pad = true; decoration_ansi_term_style = style; draw::write_boxed_with_line } - Some(DecorationStyle::Underline(style)) => { + DecorationStyle::Underline(style) => { decoration_ansi_term_style = style; draw::write_underlined } - Some(DecorationStyle::Overline(style)) => { + DecorationStyle::Overline(style) => { decoration_ansi_term_style = style; draw::write_overlined } - Some(DecorationStyle::Underoverline(style)) => { + DecorationStyle::Underoverline(style) => { decoration_ansi_term_style = style; draw::write_underoverlined } - Some(DecorationStyle::NoDecoration) => return Ok(()), - None => unreachable("Unreachable code path reached in handle_commit_meta_header_line."), + DecorationStyle::NoDecoration => { + decoration_ansi_term_style = ansi_term::Style::new(); + draw::write_no_decoration + } }; draw_fn( painter.writer, @@ -242,26 +243,26 @@ fn handle_generic_file_meta_header_line( let decoration_ansi_term_style; let mut pad = false; let draw_fn = match config.file_style.decoration_style { - Some(DecorationStyle::Box(style)) => { + DecorationStyle::Box(style) => { pad = true; decoration_ansi_term_style = style; draw::write_boxed_with_line } - Some(DecorationStyle::Underline(style)) => { + DecorationStyle::Underline(style) => { decoration_ansi_term_style = style; draw::write_underlined } - Some(DecorationStyle::Overline(style)) => { + DecorationStyle::Overline(style) => { decoration_ansi_term_style = style; draw::write_overlined } - Some(DecorationStyle::Underoverline(style)) => { + DecorationStyle::Underoverline(style) => { decoration_ansi_term_style = style; draw::write_underoverlined } - Some(DecorationStyle::NoDecoration) => return Ok(()), - None => { - unreachable("Unreachable code path reached in handle_generic_file_meta_header_line.") + DecorationStyle::NoDecoration => { + decoration_ansi_term_style = ansi_term::Style::new(); + draw::write_no_decoration } }; writeln!(painter.writer)?; @@ -285,24 +286,26 @@ fn handle_hunk_header_line( ) -> std::io::Result<()> { let decoration_ansi_term_style; let draw_fn = match config.hunk_header_style.decoration_style { - Some(DecorationStyle::Box(style)) => { + DecorationStyle::Box(style) => { decoration_ansi_term_style = style; draw::write_boxed } - Some(DecorationStyle::Underline(style)) => { + DecorationStyle::Underline(style) => { decoration_ansi_term_style = style; draw::write_underlined } - Some(DecorationStyle::Overline(style)) => { + DecorationStyle::Overline(style) => { decoration_ansi_term_style = style; draw::write_overlined } - Some(DecorationStyle::Underoverline(style)) => { + DecorationStyle::Underoverline(style) => { decoration_ansi_term_style = style; draw::write_underoverlined } - Some(DecorationStyle::NoDecoration) => return Ok(()), - None => unreachable("Unreachable code path reached in handle_hunk_header_line."), + DecorationStyle::NoDecoration => { + decoration_ansi_term_style = ansi_term::Style::new(); + draw::write_no_decoration + } }; let (raw_code_fragment, line_number) = parse::parse_hunk_metadata(&line); let line = match prepare(raw_code_fragment, false, config) { diff --git a/src/draw.rs b/src/draw.rs index 67a15bb2..0dae0880 100644 --- a/src/draw.rs +++ b/src/draw.rs @@ -6,6 +6,17 @@ use box_drawing; use console::strip_ansi_codes; use unicode_width::UnicodeWidthStr; +pub fn write_no_decoration( + writer: &mut dyn Write, + text: &str, + _line_width: usize, // ignored + _text_style: ansi_term::Style, + _decoration_style: ansi_term::Style, +) -> std::io::Result<()> { + writeln!(writer, "{}", text)?; + Ok(()) +} + /// Write text to stream, surrounded by a box, leaving the cursor just /// beyond the bottom right corner. pub fn write_boxed( diff --git a/src/paint.rs b/src/paint.rs index aa8aec4e..f200701d 100644 --- a/src/paint.rs +++ b/src/paint.rs @@ -400,7 +400,7 @@ mod superimpose_style_sections { use syntect::highlighting::FontStyle as SyntectFontStyle; use syntect::highlighting::Style as SyntectStyle; - use crate::style::Style; + use crate::style::{DecorationStyle, Style}; lazy_static! { static ref SYNTAX_STYLE: SyntectStyle = SyntectStyle { @@ -419,7 +419,7 @@ mod superimpose_style_sections { }, is_raw: false, is_syntax_highlighted: true, - decoration_style: None, + decoration_style: DecorationStyle::NoDecoration, }; } lazy_static! { @@ -432,7 +432,7 @@ mod superimpose_style_sections { }, is_raw: false, is_syntax_highlighted: false, - decoration_style: None, + decoration_style: DecorationStyle::NoDecoration, }; } lazy_static! { @@ -445,7 +445,7 @@ mod superimpose_style_sections { }, is_raw: false, is_syntax_highlighted: true, - decoration_style: None, + decoration_style: DecorationStyle::NoDecoration, }; } diff --git a/src/style.rs b/src/style.rs index 75a08b82..05fa8655 100644 --- a/src/style.rs +++ b/src/style.rs @@ -10,7 +10,7 @@ pub struct Style { pub ansi_term_style: ansi_term::Style, pub is_raw: bool, pub is_syntax_highlighted: bool, - pub decoration_style: Option<DecorationStyle>, + pub decoration_style: DecorationStyle, } #[derive(Clone, Copy, Debug, PartialEq)] @@ -28,7 +28,7 @@ impl Style { ansi_term_style: ansi_term::Style::new(), is_raw: false, is_syntax_highlighted: false, - decoration_style: None, + decoration_style: DecorationStyle::NoDecoration, } } @@ -51,7 +51,7 @@ impl Style { ); let decoration_style = match decoration_style_string { Some(s) if s != "" => DecorationStyle::from_str(s, true_color), - _ => None, + _ => DecorationStyle::NoDecoration, }; Style { ansi_term_style, @@ -79,13 +79,11 @@ impl Style { true_color, ); if let Some(special_attribute) = special_attribute_from_style_string { - if let Some(decoration_style) = DecorationStyle::apply_special_decoration_attribute( + style.decoration_style = DecorationStyle::apply_special_decoration_attribute( style.decoration_style, &special_attribute, true_color, - ) { - style.decoration_style = Some(decoration_style) - } + ) } style } @@ -115,23 +113,23 @@ impl Style { .foreground; style.ansi_term_style.foreground = foreground_from_deprecated_arg; style.decoration_style = match style.decoration_style { - Some(DecorationStyle::Box(mut ansi_term_style)) => { + DecorationStyle::Box(mut ansi_term_style) => { ansi_term_style.foreground = foreground_from_deprecated_arg; - Some(DecorationStyle::Box(ansi_term_style)) + DecorationStyle::Box(ansi_term_style) } - Some(DecorationStyle::Underline(mut ansi_term_style)) => { + DecorationStyle::Underline(mut ansi_term_style) => { ansi_term_style.foreground = foreground_from_deprecated_arg; - Some(DecorationStyle::Underline(ansi_term_style)) + DecorationStyle::Underline(ansi_term_style) } - Some(DecorationStyle::Overline(mut ansi_term_style)) => { + DecorationStyle::Overline(mut ansi_term_style) => { ansi_term_style.foreground = foreground_from_deprecated_arg; - Some(DecorationStyle::Overline(ansi_term_style)) + DecorationStyle::Overline(ansi_term_style) } - Some(DecorationStyle::Underoverline(mut ansi_term_style)) => { + DecorationStyle::Underoverline(mut ansi_term_style) => { ansi_term_style.foreground = foreground_from_deprecated_arg; - Some(DecorationStyle::Underoverline(ansi_term_style)) + DecorationStyle::Underoverline(ansi_term_style) } - _ => style.decoration_style, + DecorationStyle::NoDecoration => style.decoration_style, }; } style @@ -139,17 +137,17 @@ impl Style { pub fn decoration_ansi_term_style(&self) -> Option<ansi_term::Style> { match self.decoration_style { - Some(DecorationStyle::Box(style)) => Some(style), - Some(DecorationStyle::Underline(style)) => Some(style), - Some(DecorationStyle::Overline(style)) => Some(style), - Some(DecorationStyle::Underoverline(style)) => Some(style), - _ => None, + DecorationStyle::Box(style) => Some(style), + DecorationStyle::Underline(style) => Some(style), + DecorationStyle::Overline(style) => Some(style), + DecorationStyle::Underoverline(style) => Some(style), + DecorationStyle::NoDecoration => None, } } } impl DecorationStyle { - pub fn from_str(style_string: &str, true_color: bool) -> Option<Self> { + pub fn from_str(style_string: &str, true_color: bool) -> Self { let (style_string, special_attribute) = extract_special_decoration_attribute(&style_string); let special_attribute = special_attribute.unwrap_or_else(|| { eprintln!( @@ -170,41 +168,35 @@ impl DecorationStyle { process::exit(1); }; match special_attribute.as_ref() { - "box" => Some(DecorationStyle::Box(style)), - "underline" => Some(DecorationStyle::Underline(style)), - "ul" => Some(DecorationStyle::Underline(style)), - "overline" => Some(DecorationStyle::Overline(style)), - "underoverline" => Some(DecorationStyle::Underoverline(style)), - "omit" => Some(DecorationStyle::NoDecoration), - "plain" => Some(DecorationStyle::NoDecoration), + "box" => DecorationStyle::Box(style), + "underline" => DecorationStyle::Underline(style), + "ul" => DecorationStyle::Underline(style), + "overline" => DecorationStyle::Overline(style), + "underoverline" => DecorationStyle::Underoverline(style), + "omit" => DecorationStyle::NoDecoration, + "plain" => DecorationStyle::NoDecoration, _ => unreachable("Unreachable code path reached in parse_decoration_style."), } } fn apply_special_decoration_attribute( - decoration_style: Option<DecorationStyle>, + decoration_style: DecorationStyle, special_attribute: &str, true_color: bool, - ) -> Option<DecorationStyle> { + ) -> DecorationStyle { let ansi_term_style = match decoration_style { - None => ansi_term::Style::new(), - Some(DecorationStyle::Box(ansi_term_style)) => ansi_term_style, - Some(DecorationStyle::Underline(ansi_term_style)) => ansi_term_style, - Some(DecorationStyle::Overline(ansi_term_style)) => ansi_term_style, - Some(DecorationStyle::Underoverline(ansi_term_style)) => ansi_term_style, - Some(DecorationStyle::NoDecoration) => ansi_term::Style::new(), + DecorationStyle::Box(ansi_term_style) => ansi_term_style, + DecorationStyle::Underline(ansi_term_style) => ansi_term_style, + DecorationStyle::Overline(ansi_term_style) => ansi_term_style, + DecorationStyle::Underoverline(ansi_term_style) => ansi_term_style, + DecorationStyle::NoDecoration => ansi_term::Style::new(), }; match DecorationStyle::from_str(special_attribute, true_color) { - Some(DecorationStyle::Box(_)) => Some(DecorationStyle::Box(ansi_term_style)), - Some(DecorationStyle::Underline(_)) => { - Some(DecorationStyle::Underline(ansi_term_style)) - } - Some(DecorationStyle::Overline(_)) => Some(DecorationStyle::Overline(ansi_term_style)), - Some(DecorationStyle::Underoverline(_)) => { - Some(DecorationStyle::Underoverline(ansi_term_style)) - } - Some(DecorationStyle::NoDecoration) => Some(DecorationStyle::NoDecoration), - None => None, + DecorationStyle::Box(_) => DecorationStyle::Box(ansi_term_style), + DecorationStyle::Underline(_) => DecorationStyle::Underline(ansi_term_style), + DecorationStyle::Overline(_) => DecorationStyle::Overline(ansi_term_style), + DecorationStyle::Underoverline(_) => DecorationStyle::Underoverline(ansi_term_style), + DecorationStyle::NoDecoration => DecorationStyle::NoDecoration, } } } |