Skip to content

Commit 5dc0d6e

Browse files
committed
New option to map raw styles encountered in input
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
1 parent 87f458a commit 5dc0d6e

File tree

8 files changed

+244
-67
lines changed

8 files changed

+244
-67
lines changed

etc/examples/72-color-moved-2.diff

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
diff --git a/file.py b/file.py
2+
index f07db74..3cb162d 100644
3+
--- a/file.py
4+
+++ b/file.py
5+
@@ -1,2 +1,2 @@
6+
-class X: pass
7+
class Y: pass
8+
+class X: pass

etc/examples/72-color-moved-3.diff

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
commit fffb6bf94087c432b6e2e29cab97bf1f8987c641
2+
Author: Dan Davison <dandavison7@gmail.com>
3+
Date: Tue Nov 23 14:51:46 2021 -0500
4+
5+
DEBUG
6+
7+
diff --git a/src/config.rs b/src/config.rs
8+
index efe6adb..9762222 100644
9+
--- a/src/config.rs
10+
+++ b/src/config.rs
11+
@@ -400,6 +400,7 @@ fn make_blame_palette(blame_palette: Option<String>, is_light_mode: bool) -> Vec
12+
+pub fn user_supplied_option(option: &str, arg_matches: &clap::ArgMatches) -> bool {
13+
@@ -416,29 +433,30 @@ fn make_styles_map(opt: &cli::Opt) -> Option<HashMap<style::AnsiTermStyleEqualit
14+
-pub fn user_supplied_option(option: &str, arg_matches: &clap::ArgMatches) -> bool {

src/cli.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,12 @@ pub struct Opt {
429429
/// (underline), 'ol' (overline), or the combination 'ul ol'.
430430
pub hunk_header_decoration_style: String,
431431

432+
#[structopt(long = "map-styles")]
433+
/// A string specifying a mapping styles encountered in raw input to desired
434+
/// output styles. An example is
435+
/// --map-styles='bold purple => red "#eeeeee", bold cyan => syntax "#eeeeee"'
436+
pub map_styles: Option<String>,
437+
432438
/// Format string for git blame commit metadata. Available placeholders are
433439
/// "{timestamp}", "{author}", and "{commit}".
434440
#[structopt(

src/config.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use crate::git_config::{GitConfig, GitConfigEntry};
2020
use crate::minusplus::MinusPlus;
2121
use crate::paint::BgFillMethod;
2222
use crate::parse_styles;
23+
use crate::style;
2324
use crate::style::Style;
2425
use crate::tests::TESTING;
2526
use crate::utils::bat::output::PagingMode;
@@ -105,6 +106,7 @@ pub struct Config {
105106
pub line_numbers_style_minusplus: MinusPlus<Style>,
106107
pub line_numbers_zero_style: Style,
107108
pub line_numbers: bool,
109+
pub styles_map: Option<HashMap<style::AnsiTermStyleEqualityKey, Style>>,
108110
pub max_line_distance_for_naively_paired_lines: f64,
109111
pub max_line_distance: f64,
110112
pub max_line_length: usize,
@@ -157,6 +159,7 @@ impl Config {
157159
impl From<cli::Opt> for Config {
158160
fn from(opt: cli::Opt) -> Self {
159161
let styles = parse_styles::parse_styles(&opt);
162+
let styles_map = make_styles_map(&opt);
160163

161164
let max_line_distance_for_naively_paired_lines =
162165
env::get_env_var("DELTA_EXPERIMENTAL_MAX_LINE_DISTANCE_FOR_NAIVELY_PAIRED_LINES")
@@ -297,6 +300,7 @@ impl From<cli::Opt> for Config {
297300
),
298301
line_numbers_zero_style: styles["line-numbers-zero-style"],
299302
line_buffer_size: opt.line_buffer_size,
303+
styles_map,
300304
max_line_distance: opt.max_line_distance,
301305
max_line_distance_for_naively_paired_lines,
302306
max_line_length: match (opt.side_by_side, wrap_max_lines_plus1) {
@@ -396,6 +400,28 @@ fn make_blame_palette(blame_palette: Option<String>, is_light_mode: bool) -> Vec
396400
}
397401
}
398402

403+
fn make_styles_map(opt: &cli::Opt) -> Option<HashMap<style::AnsiTermStyleEqualityKey, Style>> {
404+
if let Some(styles_map_str) = &opt.map_styles {
405+
let mut styles_map = HashMap::new();
406+
for pair_str in styles_map_str.split(',') {
407+
let mut style_strs = pair_str.split("=>").map(|s| s.trim());
408+
if let (Some(from_str), Some(to_str)) = (style_strs.next(), style_strs.next()) {
409+
let key = style::ansi_term_style_equality_key(
410+
Style::from_str(from_str, None, None, true, opt.git_config.as_ref())
411+
.ansi_term_style,
412+
);
413+
styles_map.insert(
414+
key,
415+
Style::from_str(to_str, None, None, true, opt.git_config.as_ref()),
416+
);
417+
}
418+
}
419+
Some(styles_map)
420+
} else {
421+
None
422+
}
423+
}
424+
399425
/// Did the user supply `option` on the command line?
400426
pub fn user_supplied_option(option: &str, arg_matches: &clap::ArgMatches) -> bool {
401427
arg_matches.occurrences_of(option) > 0

src/options/set.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ pub fn set_options(
157157
inspect_raw_lines,
158158
keep_plus_minus_markers,
159159
line_buffer_size,
160+
map_styles,
160161
max_line_distance,
161162
max_line_length,
162163
// Hack: minus-style must come before minus-*emph-style because the latter default

0 commit comments

Comments
 (0)