| 1 | use std::collections::HashMap; |
| 2 | |
| 3 | use crate::cli; |
| 4 | use crate::features; |
| 5 | use crate::git_config::{self, GitConfigGet}; |
| 6 | use crate::options::option_value::{OptionValue, ProvenancedOptionValue}; |
| 7 | use ProvenancedOptionValue::*; |
| 8 | |
| 9 | // Look up a value of type `T` associated with `option name`. The search rules are: |
| 10 | // |
| 11 | // 1. If there is a value associated with `option_name` in the main [delta] git config |
| 12 | // section, then stop searching and return that value (steps 2 and 3 are not executed at all). |
| 13 | // |
| 14 | // 2. For each feature in the ordered list of enabled features: |
| 15 | // |
| 16 | // 2.1 Look-up the value, treating `feature` as a custom feature. |
| 17 | // I.e., if there is a value associated with `option_name` in a git config section |
| 18 | // named [delta "`feature`"] then stop searching and return that value. |
| 19 | // |
| 20 | // 2.2 Look-up the value, treating `feature` as a builtin feature. |
| 21 | // I.e., if there is a value (not a default value) associated with `option_name` in a |
| 22 | // builtin feature named `feature`, then stop searching and return that value. |
| 23 | // Otherwise, record the default value and continue searching. |
| 24 | // |
| 25 | // 3. Return the last default value that was encountered. |
| 26 | pub fn get_option_value<T>( |
| 27 | option_name: &str, |
| 28 | builtin_features: &HashMap<String, features::BuiltinFeature>, |
| 29 | opt: &cli::Opt, |
| 30 | git_config: &mut Option<git_config::GitConfig>, |
| 31 | ) -> Option<T> |
| 32 | where |
| 33 | T: GitConfigGet, |
| 34 | T: GetOptionValue, |
| 35 | T: From<OptionValue>, |
| 36 | T: Into<OptionValue>, |
| 37 | { |
| 38 | T::get_option_value(option_name, builtin_features, opt, git_config) |
| 39 | } |
| 40 | |
| 41 | static GIT_CONFIG_THEME_REGEX: &str = r"^delta\.(.+)\.(light|dark)$"; |
| 42 | |
| 43 | pub fn get_themes(git_config: Option<git_config::GitConfig>) -> Vec<String> { |
| 44 | let mut themes: Vec<String> = Vec::new(); |
| 45 | let git_config = git_config.unwrap(); |
| 46 | git_config.for_each(GIT_CONFIG_THEME_REGEX, |name, _| { |
| 47 | if let Some(name) = name.strip_prefix("delta.") { |
| 48 | if let Some((name, _)) = name.rsplit_once('.') { |
| 49 | let name = name.to_owned(); |
| 50 | if !themes.contains(&name) { |
| 51 | themes.push(name); |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | }); |
| 56 | themes.sort_by_key(|a| a.to_lowercase()); |
| 57 | themes |
| 58 | } |
| 59 | |
| 60 | pub trait GetOptionValue { |
| 61 | fn get_option_value( |
| 62 | option_name: &str, |
| 63 | builtin_features: &HashMap<String, features::BuiltinFeature>, |
| 64 | opt: &cli::Opt, |
| 65 | git_config: &mut Option<git_config::GitConfig>, |
| 66 | ) -> Option<Self> |
| 67 | where |
| 68 | Self: Sized, |
| 69 | Self: GitConfigGet, |
| 70 | Self: From<OptionValue>, |
| 71 | Self: Into<OptionValue>, |
| 72 | { |
| 73 | if let Some(git_config) = git_config { |
| 74 | if let Some(value) = git_config.get::<Self>(&format!("delta.{option_name}")) { |
| 75 | return Some(value); |
| 76 | } |
| 77 | } |
| 78 | if let Some(features) = &opt.features { |
| 79 | for feature in features.split_whitespace().rev() { |
| 80 | match Self::get_provenanced_value_for_feature( |
| 81 | option_name, |
| 82 | feature, |
| 83 | builtin_features, |
| 84 | opt, |
| 85 | git_config, |
| 86 | ) { |
| 87 | Some(GitConfigValue(value)) | Some(DefaultValue(value)) => { |
| 88 | return Some(value.into()); |
| 89 | } |
| 90 | None => {} |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | None |
| 95 | } |
| 96 | |
| 97 | /// Return the value, or default value, associated with `option_name` under feature name |
| 98 | /// `feature`. This may refer to a custom feature, or a builtin feature, or both. Only builtin |
| 99 | /// features have defaults. See `GetOptionValue::get_option_value`. |
| 100 | fn get_provenanced_value_for_feature( |
| 101 | option_name: &str, |
| 102 | feature: &str, |
| 103 | builtin_features: &HashMap<String, features::BuiltinFeature>, |
| 104 | opt: &cli::Opt, |
| 105 | git_config: &mut Option<git_config::GitConfig>, |
| 106 | ) -> Option<ProvenancedOptionValue> |
| 107 | where |
| 108 | Self: Sized, |
| 109 | Self: GitConfigGet, |
| 110 | Self: Into<OptionValue>, |
| 111 | { |
| 112 | if let Some(git_config) = git_config { |
| 113 | if let Some(value) = git_config.get::<Self>(&format!("delta.{feature}.{option_name}")) { |
| 114 | return Some(GitConfigValue(value.into())); |
| 115 | } |
| 116 | } |
| 117 | if let Some(builtin_feature) = builtin_features.get(feature) { |
| 118 | if let Some(value_function) = builtin_feature.get(option_name) { |
| 119 | return Some(value_function(opt, git_config)); |
| 120 | } |
| 121 | } |
| 122 | None |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | impl GetOptionValue for Option<String> {} |
| 127 | impl GetOptionValue for String {} |
| 128 | impl GetOptionValue for bool {} |
| 129 | impl GetOptionValue for f64 {} |
| 130 | impl GetOptionValue for usize {} |
| 131 | |
| 132 | #[cfg(test)] |
| 133 | pub mod tests { |
| 134 | use std::fs::remove_file; |
| 135 | |
| 136 | use crate::cli::Opt; |
| 137 | use crate::env::DeltaEnv; |
| 138 | use crate::options::get::get_themes; |
| 139 | use crate::tests::integration_test_utils; |
| 140 | |
| 141 | // fn generic<T>(_s: SGen<T>) {} |
| 142 | fn _test_env_var_overrides_git_config_generic( |
| 143 | git_config_contents: &[u8], |
| 144 | git_config_path: &str, |
| 145 | env_value: String, |
| 146 | fn_cmp_before: &dyn Fn(Opt), |
| 147 | fn_cmp_after: &dyn Fn(Opt), |
| 148 | ) { |
| 149 | let opt = integration_test_utils::make_options_from_args_and_git_config( |
| 150 | &[], |
| 151 | Some(git_config_contents), |
| 152 | Some(git_config_path), |
| 153 | ); |
| 154 | fn_cmp_before(opt); |
| 155 | |
| 156 | let opt = integration_test_utils::make_options_from_args_and_git_config_honoring_env_var_with_custom_env( |
| 157 | DeltaEnv { |
| 158 | git_config_parameters: Some(env_value), |
| 159 | ..DeltaEnv::default() |
| 160 | }, |
| 161 | &[], |
| 162 | Some(git_config_contents), |
| 163 | Some(git_config_path), |
| 164 | ); |
| 165 | fn_cmp_after(opt); |
| 166 | |
| 167 | remove_file(git_config_path).unwrap(); |
| 168 | } |
| 169 | #[test] |
| 170 | fn test_env_var_overrides_git_config_simple_string() { |
| 171 | let git_config_contents = b" |
| 172 | [delta] |
| 173 | plus-style = blue |
| 174 | "; |
| 175 | let git_config_path = "delta__test_simple_string_env_var_overrides_git_config.gitconfig"; |
| 176 | _test_env_var_overrides_git_config_generic( |
| 177 | git_config_contents, |
| 178 | git_config_path, |
| 179 | "'delta.plus-style=green'".into(), |
| 180 | &|opt: Opt| assert_eq!(opt.plus_style, "blue"), |
| 181 | &|opt: Opt| assert_eq!(opt.plus_style, "green"), |
| 182 | ); |
| 183 | } |
| 184 | |
| 185 | #[test] |
| 186 | fn test_env_var_overrides_git_config_complex_string() { |
| 187 | let git_config_contents = br##" |
| 188 | [delta] |
| 189 | minus-style = red bold ul "#ffeeee" |
| 190 | "##; |
| 191 | let git_config_path = "delta__test_complex_string_env_var_overrides_git_config.gitconfig"; |
| 192 | _test_env_var_overrides_git_config_generic( |
| 193 | git_config_contents, |
| 194 | git_config_path, |
| 195 | r##"'delta.minus-style=magenta italic ol "#aabbcc"'"##.into(), |
| 196 | &|opt: Opt| assert_eq!(opt.minus_style, r##"red bold ul #ffeeee"##), |
| 197 | &|opt: Opt| assert_eq!(opt.minus_style, r##"magenta italic ol "#aabbcc""##,), |
| 198 | ); |
| 199 | } |
| 200 | |
| 201 | #[test] |
| 202 | fn test_env_var_overrides_git_config_option_string() { |
| 203 | let git_config_contents = b" |
| 204 | [delta] |
| 205 | plus-style = blue |
| 206 | "; |
| 207 | let git_config_path = "delta__test_option_string_env_var_overrides_git_config.gitconfig"; |
| 208 | _test_env_var_overrides_git_config_generic( |
| 209 | git_config_contents, |
| 210 | git_config_path, |
| 211 | "'delta.plus-style=green'".into(), |
| 212 | &|opt: Opt| assert_eq!(opt.plus_style, "blue"), |
| 213 | &|opt: Opt| assert_eq!(opt.plus_style, "green"), |
| 214 | ); |
| 215 | } |
| 216 | |
| 217 | #[test] |
| 218 | fn test_env_var_overrides_git_config_bool() { |
| 219 | let git_config_contents = b" |
| 220 | [delta] |
| 221 | side-by-side = true |
| 222 | "; |
| 223 | let git_config_path = "delta__test_bool_env_var_overrides_git_config.gitconfig"; |
| 224 | _test_env_var_overrides_git_config_generic( |
| 225 | git_config_contents, |
| 226 | git_config_path, |
| 227 | "'delta.side-by-side=false'".into(), |
| 228 | &|opt: Opt| assert!(opt.side_by_side), |
| 229 | &|opt: Opt| assert!(!opt.side_by_side), |
| 230 | ); |
| 231 | } |
| 232 | |
| 233 | #[test] |
| 234 | fn test_env_var_overrides_git_config_int() { |
| 235 | let git_config_contents = b" |
| 236 | [delta] |
| 237 | max-line-length = 1 |
| 238 | "; |
| 239 | let git_config_path = "delta__test_int_env_var_overrides_git_config.gitconfig"; |
| 240 | _test_env_var_overrides_git_config_generic( |
| 241 | git_config_contents, |
| 242 | git_config_path, |
| 243 | "'delta.max-line-length=2'".into(), |
| 244 | &|opt: Opt| assert_eq!(opt.max_line_length, 1), |
| 245 | &|opt: Opt| assert_eq!(opt.max_line_length, 2), |
| 246 | ); |
| 247 | } |
| 248 | |
| 249 | #[test] |
| 250 | fn test_env_var_overrides_git_config_float() { |
| 251 | let git_config_contents = b" |
| 252 | [delta] |
| 253 | max-line-distance = 0.6 |
| 254 | "; |
| 255 | let git_config_path = "delta__test_float_env_var_overrides_git_config.gitconfig"; |
| 256 | _test_env_var_overrides_git_config_generic( |
| 257 | git_config_contents, |
| 258 | git_config_path, |
| 259 | "'delta.max-line-distance=0.7'".into(), |
| 260 | &|opt: Opt| assert_eq!(opt.max_line_distance, 0.6), |
| 261 | &|opt: Opt| assert_eq!(opt.max_line_distance, 0.7), |
| 262 | ); |
| 263 | } |
| 264 | |
| 265 | #[test] |
| 266 | fn test_delta_features_env_var() { |
| 267 | let git_config_contents = b" |
| 268 | [delta] |
| 269 | features = feature-from-gitconfig |
| 270 | "; |
| 271 | let git_config_path = "delta__test_delta_features_env_var.gitconfig"; |
| 272 | |
| 273 | let opt = integration_test_utils::make_options_from_args_and_git_config( |
| 274 | &[], |
| 275 | Some(git_config_contents), |
| 276 | Some(git_config_path), |
| 277 | ); |
| 278 | assert_eq!(opt.features.unwrap(), "feature-from-gitconfig"); |
| 279 | assert!(!opt.side_by_side); |
| 280 | |
| 281 | let opt = integration_test_utils::make_options_from_args_and_git_config_with_custom_env( |
| 282 | DeltaEnv { |
| 283 | features: Some("side-by-side".into()), |
| 284 | ..DeltaEnv::default() |
| 285 | }, |
| 286 | &[], |
| 287 | Some(git_config_contents), |
| 288 | Some(git_config_path), |
| 289 | ); |
| 290 | // `line-numbers` is a builtin feature induced by side-by-side |
| 291 | assert_eq!(opt.features.unwrap(), "line-numbers side-by-side"); |
| 292 | assert!(opt.side_by_side); |
| 293 | |
| 294 | let opt = integration_test_utils::make_options_from_args_and_git_config_with_custom_env( |
| 295 | DeltaEnv { |
| 296 | features: Some("+side-by-side".into()), |
| 297 | ..DeltaEnv::default() |
| 298 | }, |
| 299 | &[], |
| 300 | Some(git_config_contents), |
| 301 | Some(git_config_path), |
| 302 | ); |
| 303 | assert_eq!( |
| 304 | opt.features.unwrap(), |
| 305 | "feature-from-gitconfig line-numbers side-by-side" |
| 306 | ); |
| 307 | assert!(opt.side_by_side); |
| 308 | |
| 309 | remove_file(git_config_path).unwrap(); |
| 310 | } |
| 311 | |
| 312 | #[test] |
| 313 | fn test_get_themes_from_config() { |
| 314 | let git_config_contents = r#" |
| 315 | [delta "dark-theme"] |
| 316 | max-line-distance = 0.6 |
| 317 | dark = true |
| 318 | |
| 319 | [delta "light-theme"] |
| 320 | max-line-distance = 0.6 |
| 321 | light = true |
| 322 | |
| 323 | [delta "light-and-dark-theme"] |
| 324 | max-line-distance = 0.6 |
| 325 | light = true |
| 326 | dark = true |
| 327 | |
| 328 | [delta "Uppercase-Theme"] |
| 329 | light = true |
| 330 | |
| 331 | [delta "not-a-theme"] |
| 332 | max-line-distance = 0.6 |
| 333 | "#; |
| 334 | let git_config_path = "delta__test_get_themes_git_config.gitconfig"; |
| 335 | |
| 336 | let git_config = Some(integration_test_utils::make_git_config( |
| 337 | &DeltaEnv::default(), |
| 338 | git_config_contents.as_bytes(), |
| 339 | git_config_path, |
| 340 | false, |
| 341 | )); |
| 342 | |
| 343 | let themes = get_themes(git_config); |
| 344 | |
| 345 | assert_eq!( |
| 346 | themes, |
| 347 | [ |
| 348 | "dark-theme", |
| 349 | "light-and-dark-theme", |
| 350 | "light-theme", |
| 351 | "Uppercase-Theme" |
| 352 | ] |
| 353 | ); |
| 354 | |
| 355 | remove_file(git_config_path).unwrap(); |
| 356 | } |
| 357 | } |