Skip to content

Commit

Permalink
New option to map raw styles encountered in input
Browse files Browse the repository at this point in the history
Unify handling of styles parsed from raw line and computed diff
styles. This enables syntax highlighting to be used in color-moved
sections.

Fixes #72
  • Loading branch information
dandavison committed Nov 23, 2021
1 parent 8354b62 commit 16d9deb
Show file tree
Hide file tree
Showing 8 changed files with 244 additions and 67 deletions.
8 changes: 8 additions & 0 deletions etc/examples/72-color-moved-2.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
diff --git a/file.py b/file.py
index f07db74..3cb162d 100644
--- a/file.py
+++ b/file.py
@@ -1,2 +1,2 @@
-class X: pass
class Y: pass
+class X: pass
14 changes: 14 additions & 0 deletions etc/examples/72-color-moved-3.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
commit fffb6bf94087c432b6e2e29cab97bf1f8987c641
Author: Dan Davison <dandavison7@gmail.com>
Date: Tue Nov 23 14:51:46 2021 -0500

DEBUG

diff --git a/src/config.rs b/src/config.rs
index efe6adb..9762222 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -400,6 +400,7 @@ fn make_blame_palette(blame_palette: Option<String>, is_light_mode: bool) -> Vec
+pub fn user_supplied_option(option: &str, arg_matches: &clap::ArgMatches) -> bool {
@@ -416,29 +433,30 @@ fn make_styles_map(opt: &cli::Opt) -> Option<HashMap<style::AnsiTermStyleEqualit
-pub fn user_supplied_option(option: &str, arg_matches: &clap::ArgMatches) -> bool {
6 changes: 6 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,12 @@ pub struct Opt {
/// (underline), 'ol' (overline), or the combination 'ul ol'.
pub hunk_header_decoration_style: String,

#[structopt(long = "map-styles")]
/// A string specifying a mapping styles encountered in raw input to desired
/// output styles. An example is --map-styles='black cyan => white magenta,
/// red cyan => white blue'
pub map_styles: Option<String>,

/// Format string for git blame commit metadata. Available placeholders are
/// "{timestamp}", "{author}", and "{commit}".
#[structopt(
Expand Down
26 changes: 26 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use crate::git_config::{GitConfig, GitConfigEntry};
use crate::minusplus::MinusPlus;
use crate::paint::BgFillMethod;
use crate::parse_styles;
use crate::style;
use crate::style::Style;
use crate::tests::TESTING;
use crate::utils::bat::output::PagingMode;
Expand Down Expand Up @@ -105,6 +106,7 @@ pub struct Config {
pub line_numbers_style_minusplus: MinusPlus<Style>,
pub line_numbers_zero_style: Style,
pub line_numbers: bool,
pub styles_map: Option<HashMap<style::AnsiTermStyleEqualityKey, Style>>,
pub max_line_distance_for_naively_paired_lines: f64,
pub max_line_distance: f64,
pub max_line_length: usize,
Expand Down Expand Up @@ -157,6 +159,7 @@ impl Config {
impl From<cli::Opt> for Config {
fn from(opt: cli::Opt) -> Self {
let styles = parse_styles::parse_styles(&opt);
let styles_map = make_styles_map(&opt);

let max_line_distance_for_naively_paired_lines =
env::get_env_var("DELTA_EXPERIMENTAL_MAX_LINE_DISTANCE_FOR_NAIVELY_PAIRED_LINES")
Expand Down Expand Up @@ -297,6 +300,7 @@ impl From<cli::Opt> for Config {
),
line_numbers_zero_style: styles["line-numbers-zero-style"],
line_buffer_size: opt.line_buffer_size,
styles_map,
max_line_distance: opt.max_line_distance,
max_line_distance_for_naively_paired_lines,
max_line_length: match (opt.side_by_side, wrap_max_lines_plus1) {
Expand Down Expand Up @@ -396,6 +400,28 @@ fn make_blame_palette(blame_palette: Option<String>, is_light_mode: bool) -> Vec
}
}

fn make_styles_map(opt: &cli::Opt) -> Option<HashMap<style::AnsiTermStyleEqualityKey, Style>> {
if let Some(styles_map_str) = &opt.map_styles {
let mut styles_map = HashMap::new();
for pair_str in styles_map_str.split(',') {
let mut style_strs = pair_str.split("=>").map(|s| s.trim());
if let (Some(from_str), Some(to_str)) = (style_strs.next(), style_strs.next()) {
let key = style::ansi_term_style_equality_key(
Style::from_str(from_str, None, None, true, opt.git_config.as_ref())
.ansi_term_style,
);
styles_map.insert(
key,
Style::from_str(to_str, None, None, true, opt.git_config.as_ref()),
);
}
}
Some(styles_map)
} else {
None
}
}

/// Did the user supply `option` on the command line?
pub fn user_supplied_option(option: &str, arg_matches: &clap::ArgMatches) -> bool {
arg_matches.occurrences_of(option) > 0
Expand Down
1 change: 1 addition & 0 deletions src/options/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ pub fn set_options(
inspect_raw_lines,
keep_plus_minus_markers,
line_buffer_size,
map_styles,
max_line_distance,
max_line_length,
// Hack: minus-style must come before minus-*emph-style because the latter default
Expand Down
Loading

0 comments on commit 16d9deb

Please sign in to comment.