diff options
author | Dan Davison <dandavison7@gmail.com> | 2021-11-24 06:58:26 -0500 |
---|---|---|
committer | Dan Davison <dandavison7@gmail.com> | 2021-11-24 07:04:20 -0500 |
commit | dba418057eccf5b64586d8b58a878256e53dff63 (patch) | |
tree | ad20541de6832f7bbde88b58d5b6ba593201ee54 | |
parent | 4f4cd1493ee5a6260136cdce4d3bfd4aa69db4b7 (diff) |
Support style names in --map-styles
-rw-r--r-- | src/config.rs | 24 | ||||
-rw-r--r-- | src/parse_styles.rs | 60 |
2 files changed, 48 insertions, 36 deletions
diff --git a/src/config.rs b/src/config.rs index efe6adb0..5e5dad41 100644 --- a/src/config.rs +++ b/src/config.rs @@ -159,7 +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 styles_map = parse_styles::parse_styles_map(&opt); let max_line_distance_for_naively_paired_lines = env::get_env_var("DELTA_EXPERIMENTAL_MAX_LINE_DISTANCE_FOR_NAIVELY_PAIRED_LINES") @@ -400,28 +400,6 @@ 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 diff --git a/src/parse_styles.rs b/src/parse_styles.rs index 39e436f7..d45d08e7 100644 --- a/src/parse_styles.rs +++ b/src/parse_styles.rs @@ -54,6 +54,26 @@ pub fn parse_styles(opt: &cli::Opt) -> HashMap<String, Style> { resolved_styles } +pub fn parse_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 from_style = parse_as_style_or_reference_to_git_config(from_str, opt); + let to_style = parse_as_style_or_reference_to_git_config(to_str, opt); + styles_map.insert( + style::ansi_term_style_equality_key(from_style.ansi_term_style), + to_style, + ); + } + } + Some(styles_map) + } else { + None + } +} + fn resolve_style_references( edges: HashMap<&str, StyleReference>, opt: &cli::Opt, @@ -83,19 +103,8 @@ fn resolve_style_references( break; } None => { - if let Some(git_config) = &opt.git_config { - match git_config.get::<String>(&format!("delta.{}", node)) { - Some(s) => { - let style = Style::from_git_str(&s); - resolved_styles - .extend(visited.iter().map(|node| (node.to_string(), style))); - break; - } - _ => fatal(format!("Style not found: {}", node)), - } - } else { - fatal(format!("Style not found: {}", node)); - } + let style = parse_as_reference_to_git_config(node, opt); + resolved_styles.extend(visited.iter().map(|node| (node.to_string(), style))); } } } @@ -103,6 +112,31 @@ fn resolve_style_references( resolved_styles } +fn parse_as_style_or_reference_to_git_config(style_string: &str, opt: &cli::Opt) -> Style { + match style_from_str(style_string, None, None, true, opt.git_config.as_ref()) { + StyleReference::Reference(style_ref) => parse_as_reference_to_git_config(&style_ref, opt), + StyleReference::Style(style) => style, + } +} + +fn parse_as_reference_to_git_config(style_string: &str, opt: &cli::Opt) -> Style { + if let Some(git_config) = &opt.git_config { + let git_config_key = format!("delta.{}", style_string); + match git_config.get::<String>(&git_config_key) { + Some(s) => Style::from_git_str(&s), + _ => fatal(format!( + "Style key not found in git config: {}", + git_config_key + )), + } + } else { + fatal(format!( + "Style not found (git config unavailable): {}", + style_string + )); + } +} + fn make_hunk_styles<'a>(opt: &'a cli::Opt, styles: &'a mut HashMap<&str, StyleReference>) { let is_light_mode = opt.computed.is_light_mode; let true_color = opt.computed.true_color; |