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
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,

@ -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())

@ -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,

@ -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> {
@ -106,6 +107,14 @@ fn app() -> clap::Command<'static> {
.possible_values(["dark", "light"])
.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 {

@ -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",

@ -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)
}