Allow syntax highlighting to be disabled

Fixes #265
pass_end_node
Wilfred Hughes 2022-04-29 12:10:47 +07:00
parent 974a7bd765
commit 12ef8f97da
6 changed files with 72 additions and 39 deletions

@ -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 to display tabs. The default value is 8, consistent with older
versions. versions.
Added an option `--syntax-highlight` that controls whether the output
is syntax highlighted.
### Diffing ### Diffing
Difftastic now diffs files in parallel when diffing whole directories, Difftastic now diffs files in parallel when diffing whole directories,

@ -9,12 +9,14 @@ use crate::{
}; };
use owo_colors::colored::*; use owo_colors::colored::*;
// TODO: take display options
pub fn print( pub fn print(
lhs_src: &str, lhs_src: &str,
rhs_src: &str, rhs_src: &str,
lhs_positions: &[MatchedPos], lhs_positions: &[MatchedPos],
rhs_positions: &[MatchedPos], rhs_positions: &[MatchedPos],
hunks: &[Hunk], hunks: &[Hunk],
syntax_highlight: bool,
display_path: &str, display_path: &str,
lang_name: &str, lang_name: &str,
use_color: bool, use_color: bool,
@ -22,8 +24,8 @@ pub fn print(
) { ) {
let (lhs_colored, rhs_colored) = if use_color { let (lhs_colored, rhs_colored) = if use_color {
( (
apply_colors(lhs_src, true, background, lhs_positions), apply_colors(lhs_src, true, syntax_highlight, background, lhs_positions),
apply_colors(rhs_src, false, background, rhs_positions), apply_colors(rhs_src, false, syntax_highlight, background, rhs_positions),
) )
} else { } else {
(lhs_src.to_string(), rhs_src.to_string()) (lhs_src.to_string(), rhs_src.to_string())

@ -434,6 +434,7 @@ fn print_diff_result(display_options: &DisplayOptions, summary: &DiffResult) {
&summary.lhs_positions, &summary.lhs_positions,
&summary.rhs_positions, &summary.rhs_positions,
&hunks, &hunks,
display_options.syntax_highlight,
&summary.path, &summary.path,
&lang_name, &lang_name,
display_options.use_color, display_options.use_color,
@ -445,6 +446,7 @@ fn print_diff_result(display_options: &DisplayOptions, summary: &DiffResult) {
&hunks, &hunks,
display_options.display_width, display_options.display_width,
display_options.use_color, display_options.use_color,
display_options.syntax_highlight,
display_options.display_mode, display_options.display_mode,
display_options.background_color, display_options.background_color,
&summary.path, &summary.path,

@ -27,6 +27,7 @@ pub struct DisplayOptions {
pub print_unchanged: bool, pub print_unchanged: bool,
pub tab_width: usize, pub tab_width: usize,
pub display_width: usize, pub display_width: usize,
pub syntax_highlight: bool,
} }
fn app() -> clap::Command<'static> { fn app() -> clap::Command<'static> {
@ -106,6 +107,14 @@ fn app() -> clap::Command<'static> {
.possible_values(["dark", "light"]) .possible_values(["dark", "light"])
.help("Set the background brightness. Difftastic will prefer brighter colours on dark backgrounds.") .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(
Arg::new("skip-unchanged").long("skip-unchanged") Arg::new("skip-unchanged").long("skip-unchanged")
.help("Don't display anything if a file is unchanged.") .help("Don't display anything if a file is unchanged.")
@ -298,6 +307,8 @@ pub fn parse_args() -> Mode {
BackgroundColor::Dark BackgroundColor::Dark
}; };
let syntax_highlight = matches.value_of("syntax-highlight") == Some("on");
let node_limit = matches let node_limit = matches
.value_of("node-limit") .value_of("node-limit")
.expect("Always present as we've given clap a default") .expect("Always present as we've given clap a default")
@ -328,6 +339,7 @@ pub fn parse_args() -> Mode {
tab_width, tab_width,
display_mode, display_mode,
display_width, display_width,
syntax_highlight,
}; };
Mode::Diff { Mode::Diff {

@ -250,13 +250,14 @@ pub fn lines_with_novel(
/// both syntax highlighting and added/removed content highlighting. /// both syntax highlighting and added/removed content highlighting.
fn highlight_positions( fn highlight_positions(
background: BackgroundColor, background: BackgroundColor,
syntax_highlight: bool,
lhs_mps: &[MatchedPos], lhs_mps: &[MatchedPos],
rhs_mps: &[MatchedPos], rhs_mps: &[MatchedPos],
) -> ( ) -> (
HashMap<LineNumber, Vec<(SingleLineSpan, Style)>>, HashMap<LineNumber, Vec<(SingleLineSpan, Style)>>,
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. // Preallocate the hashmap assuming the average line will have 2 items on it.
let mut lhs_styles: HashMap<LineNumber, Vec<(SingleLineSpan, Style)>> = let mut lhs_styles: HashMap<LineNumber, Vec<(SingleLineSpan, Style)>> =
HashMap::with_capacity(lhs_positions.len() / 2); HashMap::with_capacity(lhs_positions.len() / 2);
@ -265,7 +266,7 @@ fn highlight_positions(
styles.push((span, style)); 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)>> = let mut rhs_styles: HashMap<LineNumber, Vec<(SingleLineSpan, Style)>> =
HashMap::with_capacity(rhs_positions.len() / 2); HashMap::with_capacity(rhs_positions.len() / 2);
for (span, style) in rhs_positions { for (span, style) in rhs_positions {
@ -300,10 +301,12 @@ fn highlight_as_novel(
false false
} }
// TODO: pass display options here.
pub fn print( pub fn print(
hunks: &[Hunk], hunks: &[Hunk],
display_width: usize, display_width: usize,
use_color: bool, use_color: bool,
syntax_highlight: bool,
display_mode: DisplayMode, display_mode: DisplayMode,
background: BackgroundColor, background: BackgroundColor,
display_path: &str, display_path: &str,
@ -315,8 +318,8 @@ pub fn print(
) { ) {
let (lhs_colored_src, rhs_colored_src) = if use_color { let (lhs_colored_src, rhs_colored_src) = if use_color {
( (
apply_colors(lhs_src, true, background, lhs_mps), apply_colors(lhs_src, true, syntax_highlight, background, lhs_mps),
apply_colors(rhs_src, false, background, rhs_mps), apply_colors(rhs_src, false, syntax_highlight, background, rhs_mps),
) )
} else { } else {
(lhs_src.to_string(), rhs_src.to_string()) (lhs_src.to_string(), rhs_src.to_string())
@ -353,7 +356,7 @@ pub fn print(
// TODO: this is largely duplicating the `apply_colors` logic. // TODO: this is largely duplicating the `apply_colors` logic.
let (lhs_highlights, rhs_highlights) = if use_color { 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 { } else {
(HashMap::new(), HashMap::new()) (HashMap::new(), HashMap::new())
}; };
@ -692,6 +695,7 @@ mod tests {
&hunks, &hunks,
80, 80,
true, true,
true,
DisplayMode::SideBySide, DisplayMode::SideBySide,
BackgroundColor::Dark, BackgroundColor::Dark,
"foo.el", "foo.el",

@ -242,6 +242,7 @@ pub fn novel_style(style: Style, is_lhs: bool, background: BackgroundColor) -> S
pub fn color_positions( pub fn color_positions(
is_lhs: bool, is_lhs: bool,
background: BackgroundColor, background: BackgroundColor,
syntax_highlight: bool,
positions: &[MatchedPos], positions: &[MatchedPos],
) -> Vec<(SingleLineSpan, Style)> { ) -> Vec<(SingleLineSpan, Style)> {
let mut styles = vec![]; let mut styles = vec![];
@ -249,6 +250,7 @@ pub fn color_positions(
let mut style = Style::new(); let mut style = Style::new();
match pos.kind { match pos.kind {
MatchKind::UnchangedToken { highlight, .. } => { MatchKind::UnchangedToken { highlight, .. } => {
if syntax_highlight {
if let TokenKind::Atom(atom_kind) = highlight { if let TokenKind::Atom(atom_kind) = highlight {
match atom_kind { match atom_kind {
AtomKind::String => { AtomKind::String => {
@ -273,8 +275,10 @@ pub fn color_positions(
} }
} }
} }
}
MatchKind::Novel { highlight, .. } => { MatchKind::Novel { highlight, .. } => {
style = novel_style(style, is_lhs, background); style = novel_style(style, is_lhs, background);
if syntax_highlight {
if matches!( if matches!(
highlight, highlight,
TokenKind::Delimiter TokenKind::Delimiter
@ -283,22 +287,27 @@ pub fn color_positions(
) { ) {
style = style.bold(); style = style.bold();
} }
}
if matches!(highlight, TokenKind::Atom(AtomKind::Comment)) { if matches!(highlight, TokenKind::Atom(AtomKind::Comment)) {
style = style.italic(); style = style.italic();
} }
} }
MatchKind::NovelWord { highlight } => { MatchKind::NovelWord { highlight } => {
style = novel_style(style, is_lhs, background).bold(); style = novel_style(style, is_lhs, background).bold();
if syntax_highlight {
if matches!(highlight, TokenKind::Atom(AtomKind::Comment)) { if matches!(highlight, TokenKind::Atom(AtomKind::Comment)) {
style = style.italic(); style = style.italic();
} }
} }
}
MatchKind::NovelLinePart { highlight, .. } => { MatchKind::NovelLinePart { highlight, .. } => {
style = novel_style(style, is_lhs, background); style = novel_style(style, is_lhs, background);
if syntax_highlight {
if matches!(highlight, TokenKind::Atom(AtomKind::Comment)) { if matches!(highlight, TokenKind::Atom(AtomKind::Comment)) {
style = style.italic(); style = style.italic();
} }
} }
}
}; };
styles.push((pos.pos, style)); styles.push((pos.pos, style));
} }
@ -308,10 +317,11 @@ pub fn color_positions(
pub fn apply_colors( pub fn apply_colors(
s: &str, s: &str,
is_lhs: bool, is_lhs: bool,
syntax_highlight: bool,
background: BackgroundColor, background: BackgroundColor,
positions: &[MatchedPos], positions: &[MatchedPos],
) -> String { ) -> String {
let styles = color_positions(is_lhs, background, positions); let styles = color_positions(is_lhs, background, syntax_highlight, positions);
apply(s, &styles) apply(s, &styles)
} }