| 1 | use std::collections::{HashMap, HashSet, VecDeque}; |
| 2 | use std::convert::TryInto; |
| 3 | use std::result::Result; |
| 4 | use std::str::FromStr; |
| 5 | |
| 6 | use bat::assets::HighlightingAssets; |
| 7 | use console::Term; |
| 8 | |
| 9 | use crate::cli; |
| 10 | use crate::config; |
| 11 | use crate::env::DeltaEnv; |
| 12 | use crate::errors::*; |
| 13 | use crate::fatal; |
| 14 | use crate::features; |
| 15 | use crate::git_config::GitConfig; |
| 16 | use crate::options::option_value::{OptionValue, ProvenancedOptionValue}; |
| 17 | use crate::options::theme; |
| 18 | use crate::utils::bat::output::PagingMode; |
| 19 | |
| 20 | macro_rules! set_options { |
| 21 | ([$( $field_ident:ident ),* ], |
| 22 | $opt:expr, $builtin_features:expr, $git_config:expr, $arg_matches:expr, $expected_option_name_map:expr, $check_names:expr) => { |
| 23 | let mut option_names = HashSet::new(); |
| 24 | $( |
| 25 | let field_name = stringify!($field_ident); |
| 26 | let option_name = &$expected_option_name_map[field_name]; |
| 27 | if !$crate::config::user_supplied_option(&field_name, $arg_matches) { |
| 28 | if let Some(value) = $crate::options::get::get_option_value( |
| 29 | option_name, |
| 30 | &$builtin_features, |
| 31 | $opt, |
| 32 | $git_config |
| 33 | ) { |
| 34 | $opt.$field_ident = value; |
| 35 | } |
| 36 | } |
| 37 | if $check_names { |
| 38 | option_names.insert(option_name.as_str()); |
| 39 | } |
| 40 | )* |
| 41 | if $check_names { |
| 42 | option_names.extend(&[ |
| 43 | "24-bit-color", |
| 44 | "diff-highlight", // Does not exist as a flag on config |
| 45 | "diff-so-fancy", // Does not exist as a flag on config |
| 46 | "detect-dark-light", // Does not exist as a flag on config |
| 47 | "features", // Processed differently |
| 48 | // Set prior to the rest |
| 49 | "no-gitconfig", |
| 50 | "dark", |
| 51 | "light", |
| 52 | "syntax-theme", |
| 53 | ]); |
| 54 | let expected_option_names: HashSet<_> = $expected_option_name_map |
| 55 | .values() |
| 56 | .map(String::as_str) |
| 57 | .collect(); |
| 58 | |
| 59 | if option_names != expected_option_names { |
| 60 | $crate::config::delta_unreachable( |
| 61 | &format!("Error processing options.\nUnhandled names: {:?}\nInvalid names: {:?}.\n", |
| 62 | &expected_option_names - &option_names, |
| 63 | &option_names - &expected_option_names)); |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | pub fn set_options( |
| 70 | opt: &mut cli::Opt, |
| 71 | git_config: &mut Option<GitConfig>, |
| 72 | arg_matches: &clap::ArgMatches, |
| 73 | assets: HighlightingAssets, |
| 74 | ) { |
| 75 | if let Some(git_config) = git_config { |
| 76 | if opt.no_gitconfig { |
| 77 | git_config.enabled = false; |
| 78 | } |
| 79 | } |
| 80 | opt.navigate = opt.navigate || opt.env.navigate.is_some(); |
| 81 | if opt.syntax_theme.is_none() { |
| 82 | opt.syntax_theme.clone_from(&opt.env.bat_theme); |
| 83 | } |
| 84 | |
| 85 | let option_names = cli::Opt::get_argument_and_option_names(); |
| 86 | |
| 87 | // Set features |
| 88 | let mut builtin_features = features::make_builtin_features(); |
| 89 | |
| 90 | // --color-only is used for interactive.diffFilter (git add -p) and side-by-side cannot be used |
| 91 | // there (does not emit lines in 1-1 correspondence with raw git output). See #274. |
| 92 | if config::user_supplied_option("color_only", arg_matches) { |
| 93 | builtin_features.remove("side-by-side"); |
| 94 | } |
| 95 | |
| 96 | let features = gather_features(opt, &builtin_features, git_config); |
| 97 | opt.features = Some(features.join(" ")); |
| 98 | |
| 99 | // Set light, dark, and syntax-theme. |
| 100 | set__light__dark__syntax_theme__options(opt, git_config, arg_matches, &option_names); |
| 101 | |
| 102 | // HACK: make minus-line styles have syntax-highlighting iff side-by-side. |
| 103 | if features.contains(&"side-by-side".to_string()) { |
| 104 | let prefix = "normal "; |
| 105 | if !config::user_supplied_option("minus_style", arg_matches) |
| 106 | && opt.minus_style.starts_with(prefix) |
| 107 | { |
| 108 | opt.minus_style = format!("syntax {}", &opt.minus_style[prefix.len()..]); |
| 109 | } |
| 110 | if !config::user_supplied_option("minus_emph_style", arg_matches) |
| 111 | && opt.minus_emph_style.starts_with(prefix) |
| 112 | { |
| 113 | opt.minus_emph_style = format!("syntax {}", &opt.minus_emph_style[prefix.len()..]); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | // Handle options which default to an arbitrary git config value. |
| 118 | // TODO: incorporate this logic into the set_options macro. |
| 119 | if !config::user_supplied_option("whitespace_error_style", arg_matches) { |
| 120 | opt.whitespace_error_style = if let Some(git_config) = git_config { |
| 121 | git_config.get::<String>("color.diff.whitespace") |
| 122 | } else { |
| 123 | None |
| 124 | } |
| 125 | .unwrap_or_else(|| "magenta reverse".to_string()) |
| 126 | } |
| 127 | |
| 128 | set_options!( |
| 129 | [ |
| 130 | blame_code_style, |
| 131 | blame_format, |
| 132 | blame_separator_format, |
| 133 | blame_palette, |
| 134 | blame_separator_style, |
| 135 | blame_timestamp_format, |
| 136 | blame_timestamp_output_format, |
| 137 | color_only, |
| 138 | config, |
| 139 | commit_decoration_style, |
| 140 | commit_regex, |
| 141 | commit_style, |
| 142 | default_language, |
| 143 | diff_args, |
| 144 | diff_stat_align_width, |
| 145 | file_added_label, |
| 146 | file_copied_label, |
| 147 | file_decoration_style, |
| 148 | file_modified_label, |
| 149 | file_removed_label, |
| 150 | file_renamed_label, |
| 151 | file_regex_replacement, |
| 152 | right_arrow, |
| 153 | hunk_label, |
| 154 | file_style, |
| 155 | grep_context_line_style, |
| 156 | grep_file_style, |
| 157 | grep_header_decoration_style, |
| 158 | grep_header_file_style, |
| 159 | grep_output_type, |
| 160 | grep_line_number_style, |
| 161 | grep_match_line_style, |
| 162 | grep_match_word_style, |
| 163 | grep_separator_symbol, |
| 164 | hunk_header_decoration_style, |
| 165 | hunk_header_file_style, |
| 166 | hunk_header_line_number_style, |
| 167 | hunk_header_style, |
| 168 | hyperlinks, |
| 169 | hyperlinks_commit_link_format, |
| 170 | hyperlinks_file_link_format, |
| 171 | inline_hint_style, |
| 172 | inspect_raw_lines, |
| 173 | keep_plus_minus_markers, |
| 174 | line_buffer_size, |
| 175 | map_styles, |
| 176 | max_line_distance, |
| 177 | max_line_length, |
| 178 | max_syntax_length, |
| 179 | // Hack: minus-style must come before minus-*emph-style because the latter default |
| 180 | // dynamically to the value of the former. |
| 181 | merge_conflict_begin_symbol, |
| 182 | merge_conflict_end_symbol, |
| 183 | merge_conflict_ours_diff_header_decoration_style, |
| 184 | merge_conflict_ours_diff_header_style, |
| 185 | merge_conflict_theirs_diff_header_decoration_style, |
| 186 | merge_conflict_theirs_diff_header_style, |
| 187 | minus_style, |
| 188 | minus_emph_style, |
| 189 | minus_empty_line_marker_style, |
| 190 | minus_non_emph_style, |
| 191 | minus_non_emph_style, |
| 192 | navigate, |
| 193 | navigate_regex, |
| 194 | line_fill_method, |
| 195 | line_numbers, |
| 196 | line_numbers_left_format, |
| 197 | line_numbers_left_style, |
| 198 | line_numbers_minus_style, |
| 199 | line_numbers_plus_style, |
| 200 | line_numbers_right_format, |
| 201 | line_numbers_right_style, |
| 202 | line_numbers_zero_style, |
| 203 | pager, |
| 204 | paging_mode, |
| 205 | parse_ansi, |
| 206 | // Hack: plus-style must come before plus-*emph-style because the latter default |
| 207 | // dynamically to the value of the former. |
| 208 | plus_style, |
| 209 | plus_emph_style, |
| 210 | plus_empty_line_marker_style, |
| 211 | plus_non_emph_style, |
| 212 | raw, |
| 213 | relative_paths, |
| 214 | show_colors, |
| 215 | show_themes, |
| 216 | side_by_side, |
| 217 | wrap_max_lines, |
| 218 | wrap_right_prefix_symbol, |
| 219 | wrap_right_percent, |
| 220 | wrap_right_symbol, |
| 221 | wrap_left_symbol, |
| 222 | tab_width, |
| 223 | tokenization_regex, |
| 224 | true_color, |
| 225 | whitespace_error_style, |
| 226 | width, |
| 227 | zero_style |
| 228 | ], |
| 229 | opt, |
| 230 | builtin_features, |
| 231 | git_config, |
| 232 | arg_matches, |
| 233 | &option_names, |
| 234 | true |
| 235 | ); |
| 236 | |
| 237 | // Setting ComputedValues |
| 238 | set_widths_and_isatty(opt); |
| 239 | set_true_color(opt); |
| 240 | theme::set__color_mode__syntax_theme__syntax_set(opt, assets); |
| 241 | opt.computed.inspect_raw_lines = |
| 242 | cli::InspectRawLines::from_str(&opt.inspect_raw_lines).unwrap(); |
| 243 | opt.computed.paging_mode = parse_paging_mode(&opt.paging_mode); |
| 244 | |
| 245 | // --color-only is used for interactive.diffFilter (git add -p). side-by-side, and |
| 246 | // **-decoration-style cannot be used there (does not emit lines in 1-1 correspondence with raw git output). |
| 247 | // See #274. |
| 248 | if opt.color_only { |
| 249 | opt.side_by_side = false; |
| 250 | opt.file_decoration_style = "none".to_string(); |
| 251 | opt.commit_decoration_style = "none".to_string(); |
| 252 | opt.hunk_header_decoration_style = "none".to_string(); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | #[allow(non_snake_case)] |
| 257 | fn set__light__dark__syntax_theme__options( |
| 258 | opt: &mut cli::Opt, |
| 259 | git_config: &mut Option<GitConfig>, |
| 260 | arg_matches: &clap::ArgMatches, |
| 261 | option_names: &HashMap<String, String>, |
| 262 | ) { |
| 263 | let validate_light_and_dark = |opt: &cli::Opt| { |
| 264 | if opt.light && opt.dark { |
| 265 | fatal("--light and --dark cannot be used together."); |
| 266 | } |
| 267 | }; |
| 268 | let empty_builtin_features = HashMap::new(); |
| 269 | validate_light_and_dark(opt); |
| 270 | if !(opt.light || opt.dark) { |
| 271 | set_options!( |
| 272 | [dark, light], |
| 273 | opt, |
| 274 | &empty_builtin_features, |
| 275 | git_config, |
| 276 | arg_matches, |
| 277 | option_names, |
| 278 | false |
| 279 | ); |
| 280 | } |
| 281 | validate_light_and_dark(opt); |
| 282 | set_options!( |
| 283 | [syntax_theme], |
| 284 | opt, |
| 285 | &empty_builtin_features, |
| 286 | git_config, |
| 287 | arg_matches, |
| 288 | option_names, |
| 289 | false |
| 290 | ); |
| 291 | } |
| 292 | |
| 293 | // Features are processed differently from all other options. The role of this function is to |
| 294 | // collect all configuration related to features and summarize it as a single list |
| 295 | // (space-separated string) of enabled features. The list is arranged in order of increasing |
| 296 | // priority in the sense that, when searching for a option value, one starts at the right-hand end |
| 297 | // and moves leftward, examining each feature in turn until a feature that associates a value with |
| 298 | // the option name is encountered. This search is documented in |
| 299 | // `get_option_value::get_option_value`. |
| 300 | // |
| 301 | // The feature list comprises features deriving from the following sources, listed in order of |
| 302 | // decreasing priority: |
| 303 | // |
| 304 | // 1. Suppose the command-line has `--features "a b"`. Then |
| 305 | // - `b`, followed by b's "ordered descendents" |
| 306 | // - `a`, followed by a's "ordered descendents" |
| 307 | // |
| 308 | // 2. Suppose the command line enables two builtin features via `--navigate --diff-so-fancy`. Then |
| 309 | // - `diff-so-fancy` |
| 310 | // - `navigate` |
| 311 | // |
| 312 | // 3. Suppose the main [delta] section has `features = d e`. Then |
| 313 | // - `e`, followed by e's "ordered descendents" |
| 314 | // - `d`, followed by d's "ordered descendents" |
| 315 | // |
| 316 | // 4. Suppose the main [delta] section has `diff-highlight = true` followed by `raw = true`. |
| 317 | // Then |
| 318 | // - `diff-highlight` |
| 319 | // - `raw` |
| 320 | // |
| 321 | // The "ordered descendents" of a feature `f` is a list of features obtained via a pre-order |
| 322 | // traversal of the feature tree rooted at `f`. This tree arises because it is allowed for a |
| 323 | // feature to contain a (key, value) pair that itself enables features. |
| 324 | // |
| 325 | // If a feature has already been included at higher priority, and is encountered again, it is |
| 326 | // ignored. |
| 327 | // |
| 328 | // Thus, for example: |
| 329 | // |
| 330 | // delta --features "my-navigate-settings" --navigate => "navigate my-navigate-settings" |
| 331 | // |
| 332 | // In the following configuration, the feature names indicate their priority, with `a` having |
| 333 | // highest priority: |
| 334 | // |
| 335 | // delta --g --features "d a" |
| 336 | // |
| 337 | // [delta "a"] |
| 338 | // features = c b |
| 339 | // |
| 340 | // [delta "d"] |
| 341 | // features = f e |
| 342 | fn gather_features( |
| 343 | opt: &mut cli::Opt, |
| 344 | builtin_features: &HashMap<String, features::BuiltinFeature>, |
| 345 | git_config: &Option<GitConfig>, |
| 346 | ) -> Vec<String> { |
| 347 | let from_env_var = &opt.env.features; |
| 348 | let from_args = opt.features.as_deref().unwrap_or(""); |
| 349 | let input_features: Vec<&str> = match from_env_var.as_deref() { |
| 350 | Some(from_env_var) if from_env_var.starts_with('+') => from_env_var[1..] |
| 351 | .split_whitespace() |
| 352 | .chain(split_feature_string(from_args)) |
| 353 | .collect(), |
| 354 | Some(from_env_var) => { |
| 355 | opt.features = Some(from_env_var.to_string()); |
| 356 | split_feature_string(from_env_var).collect() |
| 357 | } |
| 358 | None => split_feature_string(from_args).collect(), |
| 359 | }; |
| 360 | |
| 361 | let mut features = VecDeque::new(); |
| 362 | |
| 363 | // Gather features from command line. |
| 364 | if let Some(git_config) = git_config { |
| 365 | for feature in input_features { |
| 366 | gather_features_recursively(feature, &mut features, builtin_features, opt, git_config); |
| 367 | } |
| 368 | } else { |
| 369 | for feature in input_features { |
| 370 | features.push_front(feature.to_string()); |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | // Gather builtin feature flags supplied on command line. |
| 375 | // TODO: Iterate over programmatically-obtained names of builtin features. |
| 376 | if opt.raw { |
| 377 | gather_builtin_features_recursively("raw", &mut features, builtin_features, opt); |
| 378 | } |
| 379 | if opt.color_only { |
| 380 | gather_builtin_features_recursively("color-only", &mut features, builtin_features, opt); |
| 381 | } |
| 382 | if opt.diff_highlight { |
| 383 | gather_builtin_features_recursively("diff-highlight", &mut features, builtin_features, opt); |
| 384 | } |
| 385 | if opt.diff_so_fancy { |
| 386 | gather_builtin_features_recursively("diff-so-fancy", &mut features, builtin_features, opt); |
| 387 | } |
| 388 | if opt.hyperlinks { |
| 389 | gather_builtin_features_recursively("hyperlinks", &mut features, builtin_features, opt); |
| 390 | } |
| 391 | if opt.line_numbers { |
| 392 | gather_builtin_features_recursively("line-numbers", &mut features, builtin_features, opt); |
| 393 | } |
| 394 | if opt.navigate { |
| 395 | gather_builtin_features_recursively("navigate", &mut features, builtin_features, opt); |
| 396 | } |
| 397 | if opt.side_by_side { |
| 398 | gather_builtin_features_recursively("side-by-side", &mut features, builtin_features, opt); |
| 399 | } |
| 400 | |
| 401 | if let Some(git_config) = git_config { |
| 402 | // Gather features from [delta] section if --features was not passed. |
| 403 | if opt.features.is_none() { |
| 404 | if let Some(feature_string) = git_config.get::<String>("delta.features") { |
| 405 | for feature in split_feature_string(&feature_string) { |
| 406 | gather_features_recursively( |
| 407 | feature, |
| 408 | &mut features, |
| 409 | builtin_features, |
| 410 | opt, |
| 411 | git_config, |
| 412 | ) |
| 413 | } |
| 414 | } |
| 415 | } |
| 416 | // Always gather builtin feature flags from [delta] section. |
| 417 | gather_builtin_features_from_flags_in_gitconfig( |
| 418 | "delta", |
| 419 | &mut features, |
| 420 | builtin_features, |
| 421 | opt, |
| 422 | git_config, |
| 423 | ); |
| 424 | } |
| 425 | |
| 426 | Vec::<String>::from(features) |
| 427 | } |
| 428 | |
| 429 | /// Add to feature list `features` all features in the tree rooted at `feature`. |
| 430 | fn gather_features_recursively( |
| 431 | feature: &str, |
| 432 | features: &mut VecDeque<String>, |
| 433 | builtin_features: &HashMap<String, features::BuiltinFeature>, |
| 434 | opt: &cli::Opt, |
| 435 | git_config: &GitConfig, |
| 436 | ) { |
| 437 | if builtin_features.contains_key(feature) { |
| 438 | gather_builtin_features_recursively(feature, features, builtin_features, opt); |
| 439 | } else { |
| 440 | features.push_front(feature.to_string()); |
| 441 | } |
| 442 | if let Some(child_features) = git_config.get::<String>(&format!("delta.{feature}.features")) { |
| 443 | for child_feature in split_feature_string(&child_features) { |
| 444 | if !features.contains(&child_feature.to_string()) { |
| 445 | gather_features_recursively( |
| 446 | child_feature, |
| 447 | features, |
| 448 | builtin_features, |
| 449 | opt, |
| 450 | git_config, |
| 451 | ) |
| 452 | } |
| 453 | } |
| 454 | } |
| 455 | gather_builtin_features_from_flags_in_gitconfig( |
| 456 | &format!("delta.{feature}"), |
| 457 | features, |
| 458 | builtin_features, |
| 459 | opt, |
| 460 | git_config, |
| 461 | ); |
| 462 | } |
| 463 | |
| 464 | /// Look for builtin features requested via boolean feature flags (as opposed to via a "features" |
| 465 | /// list) in a custom feature section in git config and add them to the features list. |
| 466 | fn gather_builtin_features_from_flags_in_gitconfig( |
| 467 | git_config_key: &str, |
| 468 | features: &mut VecDeque<String>, |
| 469 | builtin_features: &HashMap<String, features::BuiltinFeature>, |
| 470 | opt: &cli::Opt, |
| 471 | git_config: &GitConfig, |
| 472 | ) { |
| 473 | for child_feature in builtin_features.keys() { |
| 474 | if let Some(true) = git_config.get::<bool>(&format!("{git_config_key}.{child_feature}")) { |
| 475 | gather_builtin_features_recursively(child_feature, features, builtin_features, opt); |
| 476 | } |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | /// Add to feature list `features` all builtin features in the tree rooted at `builtin_feature`. A |
| 481 | /// builtin feature is a named collection of (option-name, value) pairs. This tree arises because |
| 482 | /// those option names might include (a) a "features" list, and (b) boolean feature flags. I.e. the |
| 483 | /// children of a node in the tree are features in (a) and (b). (In both cases the features |
| 484 | /// referenced will be other builtin features, since a builtin feature is determined at compile |
| 485 | /// time and therefore cannot know of the existence of a non-builtin custom features in gitconfig). |
| 486 | fn gather_builtin_features_recursively( |
| 487 | feature: &str, |
| 488 | features: &mut VecDeque<String>, |
| 489 | builtin_features: &HashMap<String, features::BuiltinFeature>, |
| 490 | opt: &cli::Opt, |
| 491 | ) { |
| 492 | let feature_string = feature.to_string(); |
| 493 | if features.contains(&feature_string) { |
| 494 | return; |
| 495 | } |
| 496 | features.push_front(feature_string); |
| 497 | if let Some(feature_data) = builtin_features.get(feature) { |
| 498 | if let Some(child_features_fn) = feature_data.get("features") { |
| 499 | if let ProvenancedOptionValue::DefaultValue(OptionValue::String(features_string)) = |
| 500 | child_features_fn(opt, &None) |
| 501 | { |
| 502 | for child_feature in split_feature_string(&features_string) { |
| 503 | gather_builtin_features_recursively( |
| 504 | child_feature, |
| 505 | features, |
| 506 | builtin_features, |
| 507 | opt, |
| 508 | ); |
| 509 | } |
| 510 | } |
| 511 | } |
| 512 | for child_feature in builtin_features.keys() { |
| 513 | if let Some(child_features_fn) = feature_data.get(child_feature) { |
| 514 | if let ProvenancedOptionValue::DefaultValue(OptionValue::Boolean(true)) = |
| 515 | child_features_fn(opt, &None) |
| 516 | { |
| 517 | gather_builtin_features_recursively( |
| 518 | child_feature, |
| 519 | features, |
| 520 | builtin_features, |
| 521 | opt, |
| 522 | ); |
| 523 | } |
| 524 | } |
| 525 | } |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | fn split_feature_string(features: &str) -> impl Iterator<Item = &str> { |
| 530 | features.split_whitespace().rev() |
| 531 | } |
| 532 | |
| 533 | impl FromStr for cli::InspectRawLines { |
| 534 | type Err = Error; |
| 535 | fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 536 | match s.to_lowercase().as_str() { |
| 537 | "true" => Ok(Self::True), |
| 538 | "false" => Ok(Self::False), |
| 539 | _ => { |
| 540 | fatal(format!( |
| 541 | r#"Invalid value for inspect-raw-lines option: {s}. Valid values are "true", and "false"."#, |
| 542 | )); |
| 543 | } |
| 544 | } |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | fn parse_paging_mode(paging_mode_string: &str) -> PagingMode { |
| 549 | match paging_mode_string.to_lowercase().as_str() { |
| 550 | "always" => PagingMode::Always, |
| 551 | "never" => PagingMode::Never, |
| 552 | "auto" => PagingMode::QuitIfOneScreen, |
| 553 | _ => { |
| 554 | fatal(format!( |
| 555 | "Invalid value for --paging option: {paging_mode_string} (valid values are \"always\", \"never\", and \"auto\")", |
| 556 | )); |
| 557 | } |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | fn parse_width_specifier(width_arg: &str, terminal_width: usize) -> Result<usize, String> { |
| 562 | let width_arg = width_arg.trim(); |
| 563 | |
| 564 | let parse = |width: &str, must_be_negative, subexpression| -> Result<isize, String> { |
| 565 | let remove_spaces = |s: &str| s.chars().filter(|c| c != &' ').collect::<String>(); |
| 566 | match remove_spaces(width).parse() { |
| 567 | Ok(val) if must_be_negative && val > 0 => Err(()), |
| 568 | Err(_) => Err(()), |
| 569 | Ok(ok) => Ok(ok), |
| 570 | } |
| 571 | .map_err(|_| { |
| 572 | let pos = if must_be_negative { " negative" } else { "n" }; |
| 573 | let subexpr = if subexpression { |
| 574 | format!(" (from {width_arg:?})") |
| 575 | } else { |
| 576 | "".into() |
| 577 | }; |
| 578 | format!("{width:?}{subexpr} is not a{pos} integer") |
| 579 | }) |
| 580 | }; |
| 581 | |
| 582 | let width = match width_arg.find('-') { |
| 583 | None => parse(width_arg, false, false)?.try_into().unwrap(), |
| 584 | Some(0) => (terminal_width as isize + parse(width_arg, true, false)?) |
| 585 | .try_into() |
| 586 | .map_err(|_| { |
| 587 | format!( |
| 588 | "the current terminal width of {} minus {} is negative", |
| 589 | terminal_width, |
| 590 | &width_arg[1..].trim(), |
| 591 | ) |
| 592 | })?, |
| 593 | Some(index) => { |
| 594 | let a = parse(&width_arg[0..index], false, true)?; |
| 595 | let b = parse(&width_arg[index..], true, true)?; |
| 596 | (a + b) |
| 597 | .try_into() |
| 598 | .map_err(|_| format!("expression {width_arg:?} is not positive"))? |
| 599 | } |
| 600 | }; |
| 601 | |
| 602 | Ok(width) |
| 603 | } |
| 604 | |
| 605 | fn set_widths_and_isatty(opt: &mut cli::Opt) { |
| 606 | let term_stdout = Term::stdout(); |
| 607 | opt.computed.stdout_is_term = term_stdout.is_term(); |
| 608 | |
| 609 | // If one extra character for e.g. `less --status-column` is required use "-1" |
| 610 | // as an argument, also see #41, #10, #115 and #727. |
| 611 | opt.computed.available_terminal_width = |
| 612 | crate::utils::workarounds::windows_msys2_width_fix(term_stdout.size(), &term_stdout); |
| 613 | |
| 614 | let (decorations_width, background_color_extends_to_terminal_width) = match opt.width.as_deref() |
| 615 | { |
| 616 | Some("variable") => (cli::Width::Variable, false), |
| 617 | Some(width) => { |
| 618 | let width = parse_width_specifier(width, opt.computed.available_terminal_width) |
| 619 | .unwrap_or_else(|err| fatal(format!("Invalid value for width: {err}"))); |
| 620 | (cli::Width::Fixed(width), true) |
| 621 | } |
| 622 | None => { |
| 623 | #[cfg(test)] |
| 624 | { |
| 625 | // instead of passing `--width=..` to all tests, set it here: |
| 626 | (cli::Width::Fixed(tests::TERMINAL_WIDTH_IN_TESTS), true) |
| 627 | } |
| 628 | #[cfg(not(test))] |
| 629 | { |
| 630 | ( |
| 631 | cli::Width::Fixed(opt.computed.available_terminal_width), |
| 632 | true, |
| 633 | ) |
| 634 | } |
| 635 | } |
| 636 | }; |
| 637 | opt.computed.decorations_width = decorations_width; |
| 638 | opt.computed.background_color_extends_to_terminal_width = |
| 639 | background_color_extends_to_terminal_width; |
| 640 | } |
| 641 | |
| 642 | fn set_true_color(opt: &mut cli::Opt) { |
| 643 | if opt.true_color == "auto" { |
| 644 | // It's equal to its default, so the user might be using the deprecated |
| 645 | // --24-bit-color option. |
| 646 | if let Some(_24_bit_color) = opt._24_bit_color.as_ref() { |
| 647 | opt.true_color.clone_from(_24_bit_color); |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | opt.computed.true_color = match opt.true_color.as_ref() { |
| 652 | "always" => true, |
| 653 | "never" => false, |
| 654 | "auto" => is_truecolor_terminal(&opt.env), |
| 655 | _ => { |
| 656 | fatal(format!( |
| 657 | "Invalid value for --true-color option: {} (valid values are \"always\", \"never\", and \"auto\")", |
| 658 | opt.true_color |
| 659 | )); |
| 660 | } |
| 661 | }; |
| 662 | } |
| 663 | |
| 664 | fn is_truecolor_terminal(env: &DeltaEnv) -> bool { |
| 665 | env.colorterm |
| 666 | .as_ref() |
| 667 | .map(|colorterm| colorterm == "truecolor" || colorterm == "24bit") |
| 668 | .unwrap_or(false) |
| 669 | } |
| 670 | |
| 671 | #[cfg(test)] |
| 672 | pub mod tests { |
| 673 | use std::fs::remove_file; |
| 674 | |
| 675 | use crate::cli; |
| 676 | use crate::tests::integration_test_utils; |
| 677 | use crate::utils::bat::output::PagingMode; |
| 678 | |
| 679 | pub const TERMINAL_WIDTH_IN_TESTS: usize = 43; |
| 680 | |
| 681 | #[test] |
| 682 | fn test_options_can_be_set_in_git_config() { |
| 683 | // In general the values here are not the default values. However there are some exceptions |
| 684 | // since e.g. color-only = true (non-default) forces side-by-side = false (default). |
| 685 | let git_config_contents = b" |
| 686 | [delta] |
| 687 | color-only = false |
| 688 | commit-decoration-style = black black |
| 689 | commit-style = black black |
| 690 | dark = false |
| 691 | default-language = rs |
| 692 | diff-highlight = true |
| 693 | diff-so-fancy = true |
| 694 | features = xxxyyyzzz |
| 695 | file-added-label = xxxyyyzzz |
| 696 | file-decoration-style = black black |
| 697 | file-modified-label = xxxyyyzzz |
| 698 | file-removed-label = xxxyyyzzz |
| 699 | file-renamed-label = xxxyyyzzz |
| 700 | file-transformation = s/foo/bar/ |
| 701 | right-arrow = xxxyyyzzz |
| 702 | file-style = black black |
| 703 | hunk-header-decoration-style = black black |
| 704 | hunk-header-style = black black |
| 705 | keep-plus-minus-markers = true |
| 706 | light = true |
| 707 | line-numbers = true |
| 708 | line-numbers-left-format = xxxyyyzzz |
| 709 | line-numbers-left-style = black black |
| 710 | line-numbers-minus-style = black black |
| 711 | line-numbers-plus-style = black black |
| 712 | line-numbers-right-format = xxxyyyzzz |
| 713 | line-numbers-right-style = black black |
| 714 | line-numbers-zero-style = black black |
| 715 | max-line-distance = 77 |
| 716 | max-line-length = 77 |
| 717 | minus-emph-style = black black |
| 718 | minus-empty-line-marker-style = black black |
| 719 | minus-non-emph-style = black black |
| 720 | minus-style = black black |
| 721 | navigate = true |
| 722 | navigate-regex = xxxyyyzzz |
| 723 | paging = never |
| 724 | plus-emph-style = black black |
| 725 | plus-empty-line-marker-style = black black |
| 726 | plus-non-emph-style = black black |
| 727 | plus-style = black black |
| 728 | raw = true |
| 729 | side-by-side = true |
| 730 | syntax-theme = xxxyyyzzz |
| 731 | tabs = 77 |
| 732 | true-color = never |
| 733 | whitespace-error-style = black black |
| 734 | width = 77 |
| 735 | word-diff-regex = xxxyyyzzz |
| 736 | zero-style = black black |
| 737 | # no-gitconfig |
| 738 | "; |
| 739 | let git_config_path = "delta__test_options_can_be_set_in_git_config.gitconfig"; |
| 740 | |
| 741 | let opt = integration_test_utils::make_options_from_args_and_git_config( |
| 742 | &[], |
| 743 | Some(git_config_contents), |
| 744 | Some(git_config_path), |
| 745 | ); |
| 746 | |
| 747 | assert_eq!(opt.true_color, "never"); |
| 748 | assert!(!opt.color_only); |
| 749 | assert_eq!(opt.commit_decoration_style, "black black"); |
| 750 | assert_eq!(opt.commit_style, "black black"); |
| 751 | assert!(!opt.dark); |
| 752 | assert_eq!(opt.default_language, "rs".to_owned()); |
| 753 | // TODO: should set_options not be called on any feature flags? |
| 754 | // assert_eq!(opt.diff_highlight, true); |
| 755 | // assert_eq!(opt.diff_so_fancy, true); |
| 756 | assert!(opt |
| 757 | .features |
| 758 | .unwrap() |
| 759 | .split_whitespace() |
| 760 | .any(|s| s == "xxxyyyzzz")); |
| 761 | assert_eq!(opt.file_added_label, "xxxyyyzzz"); |
| 762 | assert_eq!(opt.file_decoration_style, "black black"); |
| 763 | assert_eq!(opt.file_modified_label, "xxxyyyzzz"); |
| 764 | assert_eq!(opt.file_removed_label, "xxxyyyzzz"); |
| 765 | assert_eq!(opt.file_renamed_label, "xxxyyyzzz"); |
| 766 | assert_eq!(opt.right_arrow, "xxxyyyzzz"); |
| 767 | assert_eq!(opt.file_style, "black black"); |
| 768 | assert_eq!(opt.file_regex_replacement, Some("s/foo/bar/".to_string())); |
| 769 | assert_eq!(opt.hunk_header_decoration_style, "black black"); |
| 770 | assert_eq!(opt.hunk_header_style, "black black"); |
| 771 | assert!(opt.keep_plus_minus_markers); |
| 772 | assert!(opt.light); |
| 773 | assert!(opt.line_numbers); |
| 774 | assert_eq!(opt.line_numbers_left_format, "xxxyyyzzz"); |
| 775 | assert_eq!(opt.line_numbers_left_style, "black black"); |
| 776 | assert_eq!(opt.line_numbers_minus_style, "black black"); |
| 777 | assert_eq!(opt.line_numbers_plus_style, "black black"); |
| 778 | assert_eq!(opt.line_numbers_right_format, "xxxyyyzzz"); |
| 779 | assert_eq!(opt.line_numbers_right_style, "black black"); |
| 780 | assert_eq!(opt.line_numbers_zero_style, "black black"); |
| 781 | assert_eq!(opt.max_line_distance, 77.0); |
| 782 | assert_eq!(opt.max_line_length, 77); |
| 783 | assert_eq!(opt.minus_emph_style, "black black"); |
| 784 | assert_eq!(opt.minus_empty_line_marker_style, "black black"); |
| 785 | assert_eq!(opt.minus_non_emph_style, "black black"); |
| 786 | assert_eq!(opt.minus_style, "black black"); |
| 787 | assert!(opt.navigate); |
| 788 | assert_eq!(opt.navigate_regex, Some("xxxyyyzzz".to_string())); |
| 789 | assert_eq!(opt.paging_mode, "never"); |
| 790 | assert_eq!(opt.plus_emph_style, "black black"); |
| 791 | assert_eq!(opt.plus_empty_line_marker_style, "black black"); |
| 792 | assert_eq!(opt.plus_non_emph_style, "black black"); |
| 793 | assert_eq!(opt.plus_style, "black black"); |
| 794 | assert!(opt.raw); |
| 795 | assert!(opt.side_by_side); |
| 796 | assert_eq!(opt.syntax_theme, Some("xxxyyyzzz".to_string())); |
| 797 | assert_eq!(opt.tab_width, 77); |
| 798 | assert_eq!(opt.true_color, "never"); |
| 799 | assert_eq!(opt.whitespace_error_style, "black black"); |
| 800 | assert_eq!(opt.width, Some("77".to_string())); |
| 801 | assert_eq!(opt.tokenization_regex, "xxxyyyzzz"); |
| 802 | assert_eq!(opt.zero_style, "black black"); |
| 803 | |
| 804 | assert_eq!(opt.computed.paging_mode, PagingMode::Never); |
| 805 | |
| 806 | remove_file(git_config_path).unwrap(); |
| 807 | } |
| 808 | |
| 809 | #[test] |
| 810 | fn test_width_in_git_config_is_honored() { |
| 811 | let git_config_contents = b" |
| 812 | [delta] |
| 813 | features = my-width-feature |
| 814 | |
| 815 | [delta \"my-width-feature\"] |
| 816 | width = variable |
| 817 | "; |
| 818 | let git_config_path = "delta__test_width_in_git_config_is_honored.gitconfig"; |
| 819 | |
| 820 | let opt = integration_test_utils::make_options_from_args_and_git_config( |
| 821 | &[], |
| 822 | Some(git_config_contents), |
| 823 | Some(git_config_path), |
| 824 | ); |
| 825 | |
| 826 | assert_eq!(opt.computed.decorations_width, cli::Width::Variable); |
| 827 | |
| 828 | remove_file(git_config_path).unwrap(); |
| 829 | } |
| 830 | |
| 831 | #[test] |
| 832 | fn test_parse_width_specifier() { |
| 833 | use super::parse_width_specifier; |
| 834 | let term_width = 12; |
| 835 | |
| 836 | let assert_failure_containing = |x, errmsg| { |
| 837 | assert!(parse_width_specifier(x, term_width) |
| 838 | .unwrap_err() |
| 839 | .contains(errmsg)); |
| 840 | }; |
| 841 | |
| 842 | assert_failure_containing("", "is not an integer"); |
| 843 | assert_failure_containing("foo", "is not an integer"); |
| 844 | assert_failure_containing("123foo", "is not an integer"); |
| 845 | assert_failure_containing("+12bar", "is not an integer"); |
| 846 | assert_failure_containing("-456bar", "is not a negative integer"); |
| 847 | |
| 848 | assert_failure_containing("-13", "minus 13 is negative"); |
| 849 | assert_failure_containing(" - 13 ", "minus 13 is negative"); |
| 850 | assert_failure_containing("12-13", "expression"); |
| 851 | assert_failure_containing(" 12 - 13 ", "expression \"12 - 13\" is not"); |
| 852 | assert_failure_containing("12+foo", "is not an integer"); |
| 853 | assert_failure_containing( |
| 854 | " 12 - bar ", |
| 855 | "\"- bar\" (from \"12 - bar\") is not a negative integer", |
| 856 | ); |
| 857 | |
| 858 | assert_eq!(parse_width_specifier("1", term_width).unwrap(), 1); |
| 859 | assert_eq!(parse_width_specifier(" 1 ", term_width).unwrap(), 1); |
| 860 | assert_eq!(parse_width_specifier("-2", term_width).unwrap(), 10); |
| 861 | assert_eq!(parse_width_specifier(" - 2", term_width).unwrap(), 10); |
| 862 | assert_eq!(parse_width_specifier("-12", term_width).unwrap(), 0); |
| 863 | assert_eq!(parse_width_specifier(" - 12 ", term_width).unwrap(), 0); |
| 864 | assert_eq!(parse_width_specifier(" 2 - 2 ", term_width).unwrap(), 0); |
| 865 | } |
| 866 | } |