summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/gitconfig.rs336
-rw-r--r--src/main.rs1
-rw-r--r--src/preset.rs172
-rw-r--r--src/rewrite.rs165
4 files changed, 346 insertions, 328 deletions
diff --git a/src/gitconfig.rs b/src/gitconfig.rs
index aa020a4d..54902050 100644
--- a/src/gitconfig.rs
+++ b/src/gitconfig.rs
@@ -1,223 +1,205 @@
-// TODO: Add tests and parameterize over types more cleanly.
+use std::collections::HashMap;
+use std::process;
+
+use crate::cli;
+use crate::preset::{self, GetValueFunctionFromBuiltinPreset};
+
+// A type T implementing this trait gains a static method allowing an option value of type T to be
+// sought, obeying delta's standard rules for looking up option values. It is implemented for T in
+// {String, bool, i64}.
+pub trait GetOptionValue {
+ // If the value for option name n was not supplied on the command line, then a search is performed
+ // as follows. The first value encountered is used:
+ //
+ // 1. For each preset p (moving right to left through the listed presets):
+ // 1.1 The value of n under p interpreted as a user-supplied preset (i.e. git config value
+ // delta.$p.$n)
+ // 1.2 The value for n under p interpreted as a builtin preset
+ // 3. The value for n in the main git config section for delta (i.e. git config value delta.$n)
+ fn get_option_value(
+ option_name: &str,
+ builtin_presets: &HashMap<String, preset::BuiltinPreset<String>>,
+ opt: &cli::Opt,
+ git_config: &mut Option<git2::Config>,
+ ) -> Option<Self>
+ where
+ Self: Sized,
+ Self: GitConfigGet,
+ Self: GetValueFunctionFromBuiltinPreset,
+ {
+ if let Some(presets) = &opt.presets {
+ for preset in presets.to_lowercase().split_whitespace().rev() {
+ if let Some(value) = Self::get_option_value_for_preset(
+ option_name,
+ &preset,
+ &builtin_presets,
+ opt,
+ git_config,
+ ) {
+ return Some(value);
+ }
+ }
+ }
+ if let Some(git_config) = git_config {
+ let git_config = git_config.snapshot().unwrap_or_else(|err| {
+ eprintln!("Failed to read git config: {}", err);
+ process::exit(1)
+ });
+ if let Some(value) =
+ git_config_get::<Self>(&format!("delta.{}", option_name), git_config)
+ {
+ return Some(value);
+ }
+ }
+ None
+ }
+
+ fn get_option_value_for_preset(
+ option_name: &str,
+ preset: &str,
+ builtin_presets: &HashMap<String, preset::BuiltinPreset<String>>,
+ opt: &cli::Opt,
+ git_config: &mut Option<git2::Config>,
+ ) -> Option<Self>
+ where
+ Self: Sized,
+ Self: GitConfigGet,
+ Self: GetValueFunctionFromBuiltinPreset,
+ {
+ if let Some(git_config) = git_config {
+ let git_config = git_config.snapshot().unwrap_or_else(|err| {
+ eprintln!("Failed to read git config: {}", err);
+ process::exit(1)
+ });
+ if let Some(value) =
+ git_config_get::<Self>(&format!("delta.{}.{}", preset, option_name), git_config)
+ {
+ return Some(value);
+ }
+ }
+ if let Some(builtin_preset) = builtin_presets.get(preset) {
+ if let Some(value_function) =
+ Self::get_value_function_from_builtin_preset(option_name, builtin_preset)
+ {
+ return Some(value_function(opt, &git_config));
+ }
+ }
+ return None;
+ }
+}
+
+impl GetOptionValue for String {}
+impl GetOptionValue for bool {}
+impl GetOptionValue for i64 {}
+
+pub trait GitConfigGet {
+ fn git_config_get(key: &str, git_config: &git2::Config) -> Option<Self>
+ where
+ Self: Sized;
+}
+
+impl GitConfigGet for String {
+ fn git_config_get(key: &str, git_config: &git2::Config) -> Option<Self> {
+ git_config.get_string(key).ok()
+ }
+}
+
+impl GitConfigGet for bool {
+ fn git_config_get(key: &str, git_config: &git2::Config) -> Option<Self> {
+ git_config.get_bool(key).ok()
+ }
+}
+
+impl GitConfigGet for i64 {
+ fn git_config_get(key: &str, git_config: &git2::Config) -> Option<Self> {
+ git_config.get_i64(key).ok()
+ }
+}
+
+fn git_config_get<T>(key: &str, git_config: git2::Config) -> Option<T>
+where
+ T: GitConfigGet,
+{
+ T::git_config_get(key, &git_config)
+}
#[macro_use]
mod set_options {
- /// If `opt_name` was not supplied on the command line, then change its value to one of the
- /// following in order of precedence:
- /// 1. The entry for it in the section of gitconfig corresponding to the active presets (if
- /// presets disagree over an option value, the preset named last in the presets string has
- /// priority).
- /// 2. The entry for it in the main delta section of gitconfig, if there is one.
- /// 3. The default value passed to this macro (which may be the current value).
+ // set_options<T> implementations
macro_rules! set_options__string {
- ([$( ($opt_name:expr, $field_ident:ident, $keys:expr, $default:expr) ),* ],
+ ([$( ($option_name:expr, $field_ident:ident) ),* ],
$opt:expr, $arg_matches:expr, $git_config:expr) => {
+ let builtin_presets = $crate::preset::make_builtin_presets(); // TODO: move up the stack
$(
- if !$crate::config::user_supplied_option($opt_name, $arg_matches) {
- $opt.$field_ident =
- $crate::gitconfig::git_config_get::_string($keys, $git_config)
- .unwrap_or_else(|| $default.to_string());
+ if !$crate::config::user_supplied_option($option_name, $arg_matches) {
+ if let Some(value) = String::get_option_value($option_name, &builtin_presets, $opt, $git_config) {
+ $opt.$field_ident = value;
+ }
};
)*
- };
+ };
}
macro_rules! set_options__option_string {
- ([$( ($opt_name:expr, $field_ident:ident, $keys:expr, $default:expr) ),* ],
+ ([$( ($option_name:expr, $field_ident:ident) ),* ],
$opt:expr, $arg_matches:expr, $git_config:expr) => {
+ let builtin_presets = $crate::preset::make_builtin_presets(); // TODO: move up the stack
$(
- if !$crate::config::user_supplied_option($opt_name, $arg_matches) {
- $opt.$field_ident = $crate::gitconfig::git_config_get::_string($keys, $git_config)
- .or_else(|| $default.map(str::to_string));
+ if !$crate::config::user_supplied_option($option_name, $arg_matches) {
+ if let Some(value) = String::get_option_value($option_name, &builtin_presets, $opt, $git_config) {
+ $opt.$field_ident = Some(value);
+ }
};
)*
- };
+ };
}
macro_rules! set_options__bool {
- ([$( ($opt_name:expr, $field_ident:ident, $keys:expr, $default:expr) ),* ],
+ ([$( ($option_name:expr, $field_ident:ident) ),* ],
$opt:expr, $arg_matches:expr, $git_config:expr) => {
+ let builtin_presets = $crate::preset::make_builtin_presets(); // TODO: move up the stack
$(
- if !$crate::config::user_supplied_option($opt_name, $arg_matches) {
- $opt.$field_ident =
- $crate::gitconfig::git_config_get::_bool($keys, $git_config)
- .unwrap_or_else(|| $default);
+ if !$crate::config::user_supplied_option($option_name, $arg_matches) {
+ if let Some(value) = bool::get_option_value($option_name, &builtin_presets, $opt, $git_config) {
+ $opt.$field_ident = value;
+ }
};
)*
- };
+ };
}
macro_rules! set_options__f64 {
- ([$( ($opt_name:expr, $field_ident:ident, $keys:expr, $default:expr) ),* ],
+ ([$( ($option_name:expr, $field_ident:ident) ),* ],
$opt:expr, $arg_matches:expr, $git_config:expr) => {
+ let builtin_presets = $crate::preset::make_builtin_presets(); // TODO: move up the stack
$(
- if !$crate::config::user_supplied_option($opt_name, $arg_matches) {
- $opt.$field_ident = match $crate::gitconfig::git_config_get::_string($keys, $git_config) {
- Some(s) => s.parse::<f64>().unwrap_or($default),
- None => $default,
+ if !$crate::config::user_supplied_option($option_name, $arg_matches) {
+ if let Some(value) = String::get_option_value($option_name, &builtin_presets, $opt, $git_config) {
+ if let Some(value) = value.parse::<f64>().ok(){
+ $opt.$field_ident = value;
+ }
}
};
)*
- };
+ };
}
macro_rules! set_options__usize {
- ([$( ($opt_name:expr, $field_ident:ident, $keys:expr, $default:expr) ),* ],
+ ([$( ($option_name:expr, $field_ident:ident) ),* ],
$opt:expr, $arg_matches:expr, $git_config:expr) => {
+ let builtin_presets = $crate::preset::make_builtin_presets(); // TODO: move up the stack
$(
- if !$crate::config::user_supplied_option($opt_name, $arg_matches) {
- $opt.$field_ident = match $crate::gitconfig::git_config_get::_i64($keys, $git_config) {
- Some(int) => int as usize,
- None => $default,
+ if !$crate::config::user_supplied_option($option_name, $arg_matches) {
+ if let Some(value) = i64::get_option_value($option_name, &builtin_presets, $opt, $git_config) {
+ $opt.$field_ident = value as usize;
}
};
)*
- };
- }
-}
-
-#[macro_use]
-mod set_delta_options {
- // set_delta_options<T> implementations
-
- macro_rules! set_delta_options__string {
- ([$( ($option_name:expr, $field_ident:ident) ),* ],
- $opt:expr, $arg_matches:expr, $git_config:expr) => {
- set_options__string!([
- $(
- ($option_name,
- $field_ident,
- $crate::gitconfig::make_git_config_keys_for_delta($option_name, $opt.presets.as_deref()),
- &$opt.$field_ident)
- ),*
- ],
- $opt,
- $arg_matches,
- $git_config);
- };
- }
-
- macro_rules! set_delta_options__option_string {
- ([$( ($option_name:expr, $field_ident:ident) ),* ],
- $opt:expr, $arg_matches:expr, $git_config:expr) => {
- set_options__option_string!([
- $(
- ($option_name,
- $field_ident,
- $crate::gitconfig::make_git_config_keys_for_delta($option_name, $opt.presets.as_deref()),
- $opt.$field_ident.as_deref())
- ),*
- ],
- $opt,
- $arg_matches,
- $git_config);
- };
- }
-
- macro_rules! set_delta_options__bool {
- ([$( ($option_name:expr, $field_ident:ident) ),* ],
- $opt:expr, $arg_matches:expr, $git_config:expr) => {
- set_options__bool!([
- $(
- ($option_name,
- $field_ident,
- $crate::gitconfig::make_git_config_keys_for_delta($option_name, $opt.presets.as_deref()),
- $opt.$field_ident)
- ),*
- ],
- $opt,
- $arg_matches,
- $git_config);
- };
- }
-
- macro_rules! set_delta_options__f64 {
- ([$( ($option_name:expr, $field_ident:ident) ),* ],
- $opt:expr, $arg_matches:expr, $git_config:expr) => {
- set_options__f64!([
- $(
- ($option_name,
- $field_ident,
- $crate::gitconfig::make_git_config_keys_for_delta($option_name, $opt.presets.as_deref()),
- $opt.$field_ident)
- ),*
- ],
- $opt,
- $arg_matches,
- $git_config);
- };
- }
-
- macro_rules! set_delta_options__usize {
- ([$( ($option_name:expr, $field_ident:ident) ),* ],
- $opt:expr, $arg_matches:expr, $git_config:expr) => {
- set_options__usize!([
- $(
- ($option_name,
- $field_ident,
- $crate::gitconfig::make_git_config_keys_for_delta($option_name, $opt.presets.as_deref()),
- $opt.$field_ident)
- ),*
- ],
- $opt,
- $arg_matches,
- $git_config);
};
}
}
-pub mod git_config_get {
- use git2;
-
- macro_rules! _git_config_get {
- ($keys:expr, $git_config:expr, $getter:ident) => {
- match $git_config {
- Some(git_config) => {
- let git_config = git_config.snapshot().unwrap();
- for key in $keys {
- let entry = git_config.$getter(&key);
- if let Ok(entry) = entry {
- return Some(entry);
- }
- }
- None
- }
- None => None,
- }
- };
- }
-
- /// Get String value from gitconfig
- pub fn _string(keys: Vec<String>, git_config: &mut Option<git2::Config>) -> Option<String> {
- _git_config_get!(keys, git_config, get_string)
- }
-
- /// Get bool value from gitconfig
- pub fn _bool(keys: Vec<String>, git_config: &mut Option<git2::Config>) -> Option<bool> {
- _git_config_get!(keys, git_config, get_bool)
- }
-
- /// Get i64 value from gitconfig
- pub fn _i64(keys: Vec<String>, git_config: &mut Option<git2::Config>) -> Option<i64> {
- _git_config_get!(keys, git_config, get_i64)
- }
-}
-
-pub fn make_git_config_keys_for_delta(key: &str, presets: Option<&str>) -> Vec<String> {
- match presets {
- Some(presets) => {
- let mut keys = Vec::new();
- for preset in presets.split_whitespace().rev() {
- keys.push(format!("delta.{}.{}", preset, key));
- }
- keys.push(format!("delta.{}", key));
- keys
- }
- None => vec![format!("delta.{}", key)],
- }
-}
-
#[cfg(test)]
mod tests {
use std::fs::{remove_file, File};
diff --git a/src/main.rs b/src/main.rs
index b290c32a..1c95224d 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -16,6 +16,7 @@ mod env;
mod gitconfig;
mod paint;
mod parse;
+mod preset;
mod rewrite;
mod style;
mod syntax_theme;
diff --git a/src/preset.rs b/src/preset.rs
new file mode 100644
index 00000000..c941811a
--- /dev/null
+++ b/src/preset.rs
@@ -0,0 +1,172 @@
+use std::collections::HashMap;
+
+use crate::cli;
+
+type PresetValueFunction<T> = Box<dyn Fn(&cli::Opt, &Option<git2::Config>) -> T>;
+pub type BuiltinPreset<T> = HashMap<String, PresetValueFunction<T>>;
+
+pub trait GetValueFunctionFromBuiltinPreset {
+ fn get_value_function_from_builtin_preset<'a>(
+ _option_name: &str,
+ _builtin_preset: &'a BuiltinPreset<String>,
+ ) -> Option<&'a PresetValueFunction<Self>>
+ where
+ Self: Sized,
+ {
+ None
+ }
+}
+
+impl GetValueFunctionFromBuiltinPreset for String {
+ fn get_value_function_from_builtin_preset<'a>(
+ option_name: &str,
+ builtin_preset: &'a BuiltinPreset<String>,
+ ) -> Option<&'a PresetValueFunction<String>> {
+ builtin_preset.get(option_name)
+ }
+}
+
+impl GetValueFunctionFromBuiltinPreset for bool {}
+impl GetValueFunctionFromBuiltinPreset for i64 {}
+
+// Construct a 2-level hash map: (preset name) -> (option name) -> (value function). A value
+// function is a function that takes an Opt struct, and a git Config struct, and returns the value
+// for the option.
+pub fn make_builtin_presets() -> HashMap<String, BuiltinPreset<String>> {
+ vec![
+ (
+ "diff-highlight".to_string(),
+ make_diff_highlight_preset().into_iter().collect(),
+ ),
+ (
+ "diff-so-fancy".to_string(),
+ make_diff_so_fancy_preset().into_iter().collect(),
+ ),
+ ]
+ .into_iter()
+ .collect()
+}
+
+fn _make_diff_highlight_preset<'a>(bold: bool) -> Vec<(String, PresetValueFunction<String>)> {
+ vec![
+ (
+ "minus-style".to_string(),
+ Box::new(move |_opt: &cli::Opt, git_config: &Option<git2::Config>| {
+ match git_config {
+ Some(git_config) => git_config.get_string("color.diff.old").ok(),
+ None => None,
+ }
+ .unwrap_or_else(|| (if bold { "bold red" } else { "red" }).to_string())
+ }),
+ ),
+ (
+ "minus-non-emph-style".to_string(),
+ Box::new(|opt: &cli::Opt, git_config: &Option<git2::Config>| {
+ match git_config {
+ Some(git_config) => {
+ git_config.get_string("color.diff-highlight.oldNormal").ok()
+ }
+ None => None,
+ }
+ .unwrap_or_else(|| opt.minus_style.clone())
+ }),
+ ),
+ (
+ "minus-emph-style".to_string(),
+ Box::new(|opt: &cli::Opt, git_config: &Option<git2::Config>| {
+ match git_config {
+ Some(git_config) => git_config
+ .get_string("color.diff-highlight.oldHighlight")
+ .ok(),
+ None => None,
+ }
+ .unwrap_or_else(|| format!("{} reverse", opt.minus_style))
+ }),
+ ),
+ (
+ "zero-style".to_string(),
+ Box::new(|_opt: &cli::Opt, _git_config: &Option<git2::Config>| "normal".to_string()),
+ ),
+ (
+ "plus-style".to_string(),
+ Box::new(move |_opt: &cli::Opt, git_config: &Option<git2::Config>| {
+ match git_config {
+ Some(git_config) => git_config.get_string("color.diff.new").ok(),
+ None => None,
+ }
+ .unwrap_or_else(|| (if bold { "bold green" } else { "green" }).to_string())
+ }),
+ ),
+ (
+ "plus-non-emph-style".to_string(),
+ Box::new(|opt: &cli::Opt, git_config: &Option<git2::Config>| {
+ match git_config {
+ Some(git_config) => {
+ git_config.get_string("color.diff-highlight.newNormal").ok()
+ }
+ None => None,
+ }
+ .unwrap_or_else(|| opt.plus_style.clone())
+ }),
+ ),
+ (
+ "plus-emph-style".to_string(),
+ Box::new(|opt: &cli::Opt, git_config: &Option<git2::Config>| {
+ match git_config {
+ Some(git_config) => git_config
+ .get_string("color.diff-highlight.newHighlight")
+ .ok(),
+ None => None,
+ }
+ .unwrap_or_else(|| format!("{} reverse", opt.plus_style))
+ }),
+ ),
+ ]
+}
+
+fn make_diff_highlight_preset() -> Vec<(String, PresetValueFunction<String>)> {
+ _make_diff_highlight_preset(false)
+}
+
+fn make_diff_so_fancy_preset() -> Vec<(String, PresetValueFunction<String>)> {
+ let mut preset = _make_diff_highlight_preset(true);
+ preset.push((
+ "commit-style".to_string(),
+ Box::new(|_opt: &cli::Opt, _git_config: &Option<git2::Config>| "bold yellow".to_string()),
+ ));
+ preset.push((
+ "commit-decoration-style".to_string(),
+ Box::new(|_opt: &cli::Opt, _git_config: &Option<git2::Config>| "none".to_string()),
+ ));
+ preset.push((
+ "file-style".to_string(),
+ Box::new(|_opt: &cli::Opt, git_config: &Option<git2::Config>| {
+ match git_config {
+ Some(git_config) => git_config.get_string("color.diff.meta").ok(),
+ None => None,
+ }
+ .unwrap_or_else(|| "11".to_string())
+ }),
+ ));
+ preset.push((
+ "file-decoration-style".to_string(),
+ Box::new(|_opt: &cli::Opt, _git_config: &Option<git2::Config>| {
+ "bold yellow ul ol".to_string()
+ }),
+ ));
+ preset.push((
+ "hunk-header-style".to_string(),
+ Box::new(|_opt: &cli::Opt, git_config: &Option<git2::Config>| {
+ match git_config {
+ Some(git_config) => git_config.get_string("color.diff.frag").ok(),
+ None => None,
+ }
+ .unwrap_or_else(|| "bold syntax".to_string())
+ }),
+ ));
+ preset.push((
+ "hunk-header-decoration-style".to_string(),
+ Box::new(|_opt: &cli::Opt, _git_config: &Option<git2::Config>| "magenta box".to_string()),
+ ));
+ preset
+}
diff --git a/src/rewrite.rs b/src/rewrite.rs
index d21ea2c2..2490083d 100644
--- a/src/rewrite.rs
+++ b/src/rewrite.rs
@@ -8,6 +8,7 @@ use structopt::clap;
use crate::cli;
use crate::config::user_supplied_option;
+use crate::gitconfig::GetOptionValue;
pub fn apply_rewrite_rules(
opt: &mut cli::Opt,
@@ -20,8 +21,6 @@ pub fn apply_rewrite_rules(
rewrite_options_to_implement_deprecated_hunk_style_option(opt);
rewrite_options_to_implement_deprecated_theme_option(opt, &arg_matches);
rewrite_options_to_implement_color_only(opt);
- rewrite_options_to_implement_diff_highlight_emulation(opt, &arg_matches, git_config);
- rewrite_options_to_implement_diff_so_fancy_emulation(opt, &arg_matches, git_config);
rewrite_options_to_implement_navigate(opt, &arg_matches);
}
@@ -48,8 +47,8 @@ fn rewrite_options_to_honor_git_config(
return;
}
// --presets must be set first
- set_delta_options__option_string!([("presets", presets)], opt, arg_matches, git_config);
- set_delta_options__bool!(
+ set_options__option_string!([("presets", presets)], opt, arg_matches, git_config);
+ set_options__bool!(
[
("light", light),
("dark", dark),
@@ -62,13 +61,13 @@ fn rewrite_options_to_honor_git_config(
arg_matches,
git_config
);
- set_delta_options__f64!(
+ set_options__f64!(
[("max-line-distance", max_line_distance)],
opt,
arg_matches,
git_config
);
- set_delta_options__string!(
+ set_options__string!(
[
("commit-decoration-style", commit_decoration_style),
("commit-style", commit_style),
@@ -80,9 +79,11 @@ fn rewrite_options_to_honor_git_config(
("file-style", file_style),
("hunk-header-decoration-style", hunk_header_decoration_style),
("hunk-header-style", hunk_header_style),
+ // Hack: minus-style must come before minus-*emph-style because the latter default
+ // dynamically to the value of the former.
+ ("minus-style", minus_style),
("minus-emph-style", minus_emph_style),
("minus-non-emph-style", minus_non_emph_style),
- ("minus-style", minus_style),
("number-minus-format", number_minus_format),
("number-minus-format-style", number_minus_format_style),
("number-minus-style", number_minus_style),
@@ -90,9 +91,11 @@ fn rewrite_options_to_honor_git_config(
("number-plus-format-style", number_plus_format_style),
("number-plus-style", number_plus_style),
("paging-mode", paging_mode),
+ // Hack: plus-style must come before plus-*emph-style because the latter default
+ // dynamically to the value of the former.
+ ("plus-style", plus_style),
("plus-emph-style", plus_emph_style),
("plus-non-emph-style", plus_non_emph_style),
- ("plus-style", plus_style),
("true-color", true_color),
("word-diff-regex", tokenization_regex),
("zero-style", zero_style)
@@ -101,134 +104,13 @@ fn rewrite_options_to_honor_git_config(
arg_matches,
git_config
);
- set_delta_options__option_string!(
+ set_options__option_string!(
[("syntax_theme", syntax_theme), ("width", width)],
opt,
arg_matches,
git_config
);
- set_delta_options__usize!([("tabs", tab_width)], opt, arg_matches, git_config);
-}
-
-/// Implement --presets=diff-highlight
-fn rewrite_options_to_implement_diff_highlight_emulation(
- opt: &mut cli::Opt,
- arg_matches: &clap::ArgMatches,
- git_config: &mut Option<git2::Config>,
-) {
- _rewrite_options_to_implement_diff_highlight_emulation(opt, arg_matches, git_config, false)
-}
-
-fn _rewrite_options_to_implement_diff_highlight_emulation(
- opt: &mut cli::Opt,
- arg_matches: &clap::ArgMatches,
- git_config: &mut Option<git2::Config>,
- bold: bool,
-) {
- if !(has_preset("diff-highlight", opt.presets.as_deref())
- || has_preset("diff-so-fancy", opt.presets.as_deref()))
- {
- return;
- }
- set_options__string!(
- [
- (
- "minus-style",
- minus_style,
- vec!["color.diff.old".to_string()],
- if bold { "bold red" } else { "red" }
- ),
- (
- "minus-non-emph-style",
- minus_non_emph_style,
- vec!["color.diff-highlight.oldNormal".to_string()],
- &opt.minus_style
- ),
- (
- "minus-emph-style",
- minus_emph_style,
- vec!["color.diff-highlight.oldHighlight".to_string()],
- &format!("{} reverse", opt.minus_style)
- ),
- ("zero-style", zero_style, vec![], "normal"),
- (
- "plus-style",
- plus_style,
- vec!["color.diff.new".to_string()],
- if bold { "bold green" } else { "green" }
- ),
- (
- "plus-non-emph-style",
- plus_non_emph_style,
- vec!["color.diff-highlight.newNormal".to_string()],
- &opt.plus_style
- ),
- (
- "plus-emph-style",
- plus_emph_style,
- vec!["color.diff-highlight.newHighlight".to_string()],
- &format!("{} reverse", opt.plus_style)
- )
- ],
- opt,
- arg_matches,
- git_config
- );
-}
-
-/// Implement --presets=diff-so-fancy
-fn rewrite_options_to_implement_diff_so_fancy_emulation(
- opt: &mut cli::Opt,
- arg_matches: &clap::ArgMatches,
- git_config: &mut Option<git2::Config>,
-) {
- if !has_preset("diff-so-fancy", opt.presets.as_deref()) {
- return;
- }
- _rewrite_options_to_implement_diff_highlight_emulation(opt, arg_matches, git_config, true);
- set_options__string!(
- [
- (
- "commit-style",
- commit_style,
- vec!["color.diff.commit".to_string()],
- "bold yellow"
- ),
- (
- "file-style",
- file_style,
- vec!["color.diff.meta".to_string()],
- "11"
- ),
- (
- "hunk-header-style",
- hunk_header_style,
- vec!["color.diff.frag".to_string()],
- "bold syntax"
- ),
- (
- "commit-decoration-style",
- commit_decoration_style,
- vec![],
- "none"
- ),
- (
- "file-decoration-style",
- file_decoration_style,
- vec![],
- "bold yellow ul ol"
- ),
- (
- "hunk-header-decoration-style",
- hunk_header_decoration_style,
- vec![],
- "magenta box"
- )
- ],
- opt,
- arg_matches,
- git_config
- );
+ set_options__usize!([("tabs", tab_width)], opt, arg_matches, git_config);
}
/// Implement --navigate
@@ -410,13 +292,6 @@ fn _get_rewritten_minus_plus_style_string(
}
}
-fn has_preset(preset: &str, presets: Option<&str>) -> bool {
- match presets.map(str::to_lowercase).as_deref() {
- Some(presets) => presets.split_whitespace().any(|s| s == preset),
- None => false,
- }
-}
-
#[cfg(test)]
mod tests {
use std::ffi::OsString;
@@ -424,19 +299,7 @@ mod tests {
use structopt::{clap, StructOpt};
use crate::cli;
- use crate::rewrite::{apply_rewrite_rules, has_preset};
-
- #[test]
- fn test_has_preset() {
- assert!(!has_preset("a", Some("")));
- assert!(has_preset("a", Some("a")));
- assert!(has_preset("a", Some("a b")));
- assert!(has_preset("b", Some("a b")));
- assert!(has_preset(
- "diff-so-fancy",
- Some("diff-so-fancy some-other-preset")
- ));
- }
+ use crate::rewrite::apply_rewrite_rules;
#[test]
fn test_default_is_stable_under_rewrites() {