diff options
author | Wilfred Hughes <me@wilfred.me.uk> | 2022-04-29 12:10:47 -0700 |
---|---|---|
committer | Wilfred Hughes <me@wilfred.me.uk> | 2022-04-29 12:12:21 -0700 |
commit | 12ef8f97da70bfa0ff18232ee1d0313de1906a75 (patch) | |
tree | d4a96fa3a8308b8c2e289cf40dc1ac4ff0c2bcd3 | |
parent | 974a7bd76568af490ca7eca7d9d949a6ccfd5fc2 (diff) |
Allow syntax highlighting to be disabled
Fixes #265
-rw-r--r-- | CHANGELOG.md | 3 | ||||
-rw-r--r-- | src/inline.rs | 6 | ||||
-rw-r--r-- | src/main.rs | 2 | ||||
-rw-r--r-- | src/options.rs | 12 | ||||
-rw-r--r-- | src/side_by_side.rs | 14 | ||||
-rw-r--r-- | src/style.rs | 74 |
6 files changed, 72 insertions, 39 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 7b262cfdb2..3d119860fd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,9 @@ Added an option `--tab-width` that controls how many spaces are used to display tabs. The default value is 8, consistent with older versions. +Added an option `--syntax-highlight` that controls whether the output +is syntax highlighted. + ### Diffing Difftastic now diffs files in parallel when diffing whole directories, diff --git a/src/inline.rs b/src/inline.rs index c512928b56..3ba82d06a8 100644 --- a/src/inline.rs +++ b/src/inline.rs @@ -9,12 +9,14 @@ use crate::{ }; use owo_colors::colored::*; +// TODO: take display options pub fn print( lhs_src: &str, rhs_src: &str, lhs_positions: &[MatchedPos], rhs_positions: &[MatchedPos], hunks: &[Hunk], + syntax_highlight: bool, display_path: &str, lang_name: &str, use_color: bool, @@ -22,8 +24,8 @@ pub fn print( ) { let (lhs_colored, rhs_colored) = if use_color { ( - apply_colors(lhs_src, true, background, lhs_positions), - apply_colors(rhs_src, false, background, rhs_positions), + apply_colors(lhs_src, true, syntax_highlight, background, lhs_positions), + apply_colors(rhs_src, false, syntax_highlight, background, rhs_positions), ) } else { (lhs_src.to_string(), rhs_src.to_string()) diff --git a/src/main.rs b/src/main.rs index 3c2f9b8bf1..2c73f40574 100644 --- a/src/main.rs +++ b/src/main.rs @@ -434,6 +434,7 @@ fn print_diff_result(display_options: &DisplayOptions, summary: &DiffResult) { &summary.lhs_positions, &summary.rhs_positions, &hunks, + display_options.syntax_highlight, &summary.path, &lang_name, display_options.use_color, @@ -445,6 +446,7 @@ fn print_diff_result(display_options: &DisplayOptions, summary: &DiffResult) { &hunks, display_options.display_width, display_options.use_color, + display_options.syntax_highlight, display_options.display_mode, display_options.background_color, &summary.path, diff --git a/src/options.rs b/src/options.rs index fca70f45cd..bc7a88406a 100644 --- a/src/options.rs +++ b/src/options.rs @@ -27,6 +27,7 @@ pub struct DisplayOptions { pub print_unchanged: bool, pub tab_width: usize, pub display_width: usize, + pub syntax_highlight: bool, } fn app() -> clap::Command<'static> { @@ -107,6 +108,14 @@ fn app() -> clap::Command<'static> { .help("Set the background brightness. Difftastic will prefer brighter colours on dark backgrounds.") ) .arg( + Arg::new("syntax-highlight").long("syntax-highlight") + .value_name("ON/OFF") + .env("DFT_SYNTAX_HIGHLIGHT") + .possible_values(["on", "off"]) + .default_value("on") + .help("Enable or disable syntax highlighting.") + ) + .arg( Arg::new("skip-unchanged").long("skip-unchanged") .help("Don't display anything if a file is unchanged.") ) @@ -298,6 +307,8 @@ pub fn parse_args() -> Mode { BackgroundColor::Dark }; + let syntax_highlight = matches.value_of("syntax-highlight") == Some("on"); + let node_limit = matches .value_of("node-limit") .expect("Always present as we've given clap a default") @@ -328,6 +339,7 @@ pub fn parse_args() -> Mode { tab_width, display_mode, display_width, + syntax_highlight, }; Mode::Diff { diff --git a/src/side_by_side.rs b/src/side_by_side.rs index 66912e5176..5d36df6cb7 100644 --- a/src/side_by_side.rs +++ b/src/side_by_side.rs @@ -250,13 +250,14 @@ pub fn lines_with_novel( /// both syntax highlighting and added/removed content highlighting. fn highlight_positions( background: BackgroundColor, + syntax_highlight: bool, lhs_mps: &[MatchedPos], rhs_mps: &[MatchedPos], ) -> ( HashMap<LineNumber, Vec<(SingleLineSpan, Style)>>, HashMap<LineNumber, Vec<(SingleLineSpan, Style)>>, ) { - let lhs_positions = color_positions(true, background, lhs_mps); + let lhs_positions = color_positions(true, background, syntax_highlight, lhs_mps); // Preallocate the hashmap assuming the average line will have 2 items on it. let mut lhs_styles: HashMap<LineNumber, Vec<(SingleLineSpan, Style)>> = HashMap::with_capacity(lhs_positions.len() / 2); @@ -265,7 +266,7 @@ fn highlight_positions( styles.push((span, style)); } - let rhs_positions = color_positions(false, background, rhs_mps); + let rhs_positions = color_positions(false, background, syntax_highlight, rhs_mps); let mut rhs_styles: HashMap<LineNumber, Vec<(SingleLineSpan, Style)>> = HashMap::with_capacity(rhs_positions.len() / 2); for (span, style) in rhs_positions { @@ -300,10 +301,12 @@ fn highlight_as_novel( false } +// TODO: pass display options here. pub fn print( hunks: &[Hunk], display_width: usize, use_color: bool, + syntax_highlight: bool, display_mode: DisplayMode, background: BackgroundColor, display_path: &str, @@ -315,8 +318,8 @@ pub fn print( ) { let (lhs_colored_src, rhs_colored_src) = if use_color { ( - apply_colors(lhs_src, true, background, lhs_mps), - apply_colors(rhs_src, false, background, rhs_mps), + apply_colors(lhs_src, true, syntax_highlight, background, lhs_mps), + apply_colors(rhs_src, false, syntax_highlight, background, rhs_mps), ) } else { (lhs_src.to_string(), rhs_src.to_string()) @@ -353,7 +356,7 @@ pub fn print( // TODO: this is largely duplicating the `apply_colors` logic. let (lhs_highlights, rhs_highlights) = if use_color { - highlight_positions(background, lhs_mps, rhs_mps) + highlight_positions(background, syntax_highlight, lhs_mps, rhs_mps) } else { (HashMap::new(), HashMap::new()) }; @@ -692,6 +695,7 @@ mod tests { &hunks, 80, true, + true, DisplayMode::SideBySide, BackgroundColor::Dark, "foo.el", diff --git a/src/style.rs b/src/style.rs index 246dbb4d73..1ac662fafd 100644 --- a/src/style.rs +++ b/src/style.rs @@ -242,6 +242,7 @@ pub fn novel_style(style: Style, is_lhs: bool, background: BackgroundColor) -> S pub fn color_positions( is_lhs: bool, background: BackgroundColor, + syntax_highlight: bool, positions: &[MatchedPos], ) -> Vec<(SingleLineSpan, Style)> { let mut styles = vec![]; @@ -249,39 +250,43 @@ pub fn color_positions( let mut style = Style::new(); match pos.kind { MatchKind::UnchangedToken { highlight, .. } => { - if let TokenKind::Atom(atom_kind) = highlight { - match atom_kind { - AtomKind::String => { - style = if background.is_dark() { - style.bright_magenta() - } else { - style.magenta() - }; + if syntax_highlight { + if let TokenKind::Atom(atom_kind) = highlight { + match atom_kind { + AtomKind::String => { + style = if background.is_dark() { + style.bright_magenta() + } else { + style.magenta() + }; + } + AtomKind::Comment => { + style = style.italic(); + style = if background.is_dark() { + style.bright_blue() + } else { + style.blue() + }; + } + AtomKind::Keyword | AtomKind::Type => { + style = style.bold(); + } + AtomKind::Normal => {} } - AtomKind::Comment => { - style = style.italic(); - style = if background.is_dark() { - style.bright_blue() - } else { - style.blue() - }; - } - AtomKind::Keyword | AtomKind::Type => { - style = style.bold(); - } - AtomKind::Normal => {} } } } MatchKind::Novel { highlight, .. } => { style = novel_style(style, is_lhs, background); - if matches!( - highlight, - TokenKind::Delimiter - | TokenKind::Atom(AtomKind::Keyword) - | TokenKind::Atom(AtomKind::Type) - ) { - style = style.bold(); + if syntax_highlight { + if matches!( + highlight, + TokenKind::Delimiter + | TokenKind::Atom(AtomKind::Keyword) + | TokenKind::Atom(AtomKind::Type) + ) { + style = style.bold(); + } } if matches!(highlight, TokenKind::Atom(AtomKind::Comment)) { style = style.italic(); @@ -289,14 +294,18 @@ pub fn color_positions( } MatchKind::NovelWord { highlight } => { style = novel_style(style, is_lhs, background).bold(); - if matches!(highlight, TokenKind::Atom(AtomKind::Comment)) { - style = style.italic(); + if syntax_highlight { + if matches!(highlight, TokenKind::Atom(AtomKind::Comment)) { + style = style.italic(); + } } } MatchKind::NovelLinePart { highlight, .. } => { style = novel_style(style, is_lhs, background); - if matches!(highlight, TokenKind::Atom(AtomKind::Comment)) { - style = style.italic(); + if syntax_highlight { + if matches!(highlight, TokenKind::Atom(AtomKind::Comment)) { + style = style.italic(); + } } } }; @@ -308,10 +317,11 @@ pub fn color_positions( pub fn apply_colors( s: &str, is_lhs: bool, + syntax_highlight: bool, background: BackgroundColor, positions: &[MatchedPos], ) -> String { - let styles = color_positions(is_lhs, background, positions); + let styles = color_positions(is_lhs, background, syntax_highlight, positions); apply(s, &styles) } |