diff options
author | Dan Davison <dandavison7@gmail.com> | 2020-06-30 08:31:03 -0400 |
---|---|---|
committer | Dan Davison <dandavison7@gmail.com> | 2020-06-30 08:46:00 -0400 |
commit | eb202089a504098380f5454fb5c8858917148fd7 (patch) | |
tree | 7f7afb907b7b710b50321095668bb2ce419a443c /src/config.rs | |
parent | 1ea2c4e7af6c3a6e2165a14979458972d03ebda1 (diff) |
Add more computed values to options struct
Diffstat (limited to 'src/config.rs')
-rw-r--r-- | src/config.rs | 96 |
1 files changed, 22 insertions, 74 deletions
diff --git a/src/config.rs b/src/config.rs index b479cb41..cfa03be8 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,8 +1,6 @@ -use std::cmp::min; use std::path::PathBuf; use std::process; -use console::Term; use regex::Regex; use structopt::clap; use syntect::highlighting::Style as SyntectStyle; @@ -16,15 +14,10 @@ use crate::delta::State; use crate::env; use crate::style::Style; -pub enum Width { - Fixed(usize), - Variable, -} - pub struct Config { pub background_color_extends_to_terminal_width: bool, pub commit_style: Style, - pub decorations_width: Width, + pub decorations_width: cli::Width, pub file_added_label: String, pub file_modified_label: String, pub file_removed_label: String, @@ -80,47 +73,6 @@ impl Config { impl From<cli::Opt> for Config { fn from(opt: cli::Opt) -> Self { - let paging_mode = match opt.paging_mode.as_ref() { - "always" => PagingMode::Always, - "never" => PagingMode::Never, - "auto" => PagingMode::QuitIfOneScreen, - _ => { - eprintln!( - "Invalid value for --paging option: {} (valid values are \"always\", \"never\", and \"auto\")", - opt.paging_mode - ); - process::exit(1); - } - }; - - let true_color = match opt.true_color.as_ref() { - "always" => true, - "never" => false, - "auto" => is_truecolor_terminal(), - _ => { - eprintln!( - "Invalid value for --24-bit-color option: {} (valid values are \"always\", \"never\", and \"auto\")", - opt.true_color - ); - process::exit(1); - } - }; - - // Allow one character in case e.g. `less --status-column` is in effect. See #41 and #10. - let available_terminal_width = (Term::stdout().size().1 - 1) as usize; - let (decorations_width, background_color_extends_to_terminal_width) = - match opt.width.as_deref() { - Some("variable") => (Width::Variable, false), - Some(width) => { - let width = width.parse().unwrap_or_else(|_| { - eprintln!("Could not parse width as a positive integer: {:?}", width); - process::exit(1); - }); - (Width::Fixed(min(width, available_terminal_width)), true) - } - None => (Width::Fixed(available_terminal_width), true), - }; - let ( minus_style, minus_emph_style, @@ -132,10 +84,10 @@ impl From<cli::Opt> for Config { plus_non_emph_style, plus_empty_line_marker_style, whitespace_error_style, - ) = make_hunk_styles(&opt, true_color); + ) = make_hunk_styles(&opt); let (commit_style, file_style, hunk_header_style) = - make_commit_file_hunk_header_styles(&opt, true_color); + make_commit_file_hunk_header_styles(&opt); let ( line_numbers_minus_style, @@ -143,7 +95,7 @@ impl From<cli::Opt> for Config { line_numbers_plus_style, line_numbers_left_style, line_numbers_right_style, - ) = make_line_number_styles(&opt, true_color); + ) = make_line_number_styles(&opt); let max_line_distance_for_naively_paired_lines = env::get_env_var("DELTA_EXPERIMENTAL_MAX_LINE_DISTANCE_FOR_NAIVELY_PAIRED_LINES") @@ -161,9 +113,11 @@ impl From<cli::Opt> for Config { }); Self { - background_color_extends_to_terminal_width, + background_color_extends_to_terminal_width: opt + .computed + .background_color_extends_to_terminal_width, commit_style, - decorations_width, + decorations_width: opt.computed.decorations_width, file_added_label: opt.file_added_label, file_modified_label: opt.file_modified_label, file_removed_label: opt.file_removed_label, @@ -189,7 +143,7 @@ impl From<cli::Opt> for Config { line_numbers_right_format: opt.line_numbers_right_format, line_numbers_right_style, line_numbers_zero_style, - paging_mode, + paging_mode: opt.computed.paging_mode, plus_emph_style, plus_empty_line_marker_style, plus_file: opt.plus_file.map(|s| s.clone()), @@ -201,7 +155,7 @@ impl From<cli::Opt> for Config { syntax_theme: opt.computed.syntax_theme, tab_width: opt.tab_width, tokenization_regex, - true_color, + true_color: opt.computed.true_color, whitespace_error_style, zero_style, } @@ -210,7 +164,6 @@ impl From<cli::Opt> for Config { fn make_hunk_styles<'a>( opt: &'a cli::Opt, - true_color: bool, ) -> ( Style, Style, @@ -223,12 +176,14 @@ fn make_hunk_styles<'a>( Style, Style, ) { + let is_light_mode = opt.computed.is_light_mode; + let true_color = opt.computed.true_color; let minus_style = Style::from_str( &opt.minus_style, Some(Style::from_colors( None, Some(color::get_minus_background_color_default( - opt.computed.is_light_mode, + is_light_mode, true_color, )), )), @@ -242,7 +197,7 @@ fn make_hunk_styles<'a>( Some(Style::from_colors( None, Some(color::get_minus_emph_background_color_default( - opt.computed.is_light_mode, + is_light_mode, true_color, )), )), @@ -266,7 +221,7 @@ fn make_hunk_styles<'a>( Some(Style::from_colors( None, Some(color::get_minus_background_color_default( - opt.computed.is_light_mode, + is_light_mode, true_color, )), )), @@ -282,7 +237,7 @@ fn make_hunk_styles<'a>( Some(Style::from_colors( None, Some(color::get_plus_background_color_default( - opt.computed.is_light_mode, + is_light_mode, true_color, )), )), @@ -296,7 +251,7 @@ fn make_hunk_styles<'a>( Some(Style::from_colors( None, Some(color::get_plus_emph_background_color_default( - opt.computed.is_light_mode, + is_light_mode, true_color, )), )), @@ -320,7 +275,7 @@ fn make_hunk_styles<'a>( Some(Style::from_colors( None, Some(color::get_plus_background_color_default( - opt.computed.is_light_mode, + is_light_mode, true_color, )), )), @@ -346,10 +301,8 @@ fn make_hunk_styles<'a>( ) } -fn make_line_number_styles<'a>( - opt: &'a cli::Opt, - true_color: bool, -) -> (Style, Style, Style, Style, Style) { +fn make_line_number_styles<'a>(opt: &'a cli::Opt) -> (Style, Style, Style, Style, Style) { + let true_color = opt.computed.true_color; let line_numbers_left_style = Style::from_str(&opt.line_numbers_left_style, None, None, true_color, false); @@ -374,7 +327,8 @@ fn make_line_number_styles<'a>( ) } -fn make_commit_file_hunk_header_styles(opt: &cli::Opt, true_color: bool) -> (Style, Style, Style) { +fn make_commit_file_hunk_header_styles(opt: &cli::Opt) -> (Style, Style, Style) { + let true_color = opt.computed.true_color; ( Style::from_str_with_handling_of_special_decoration_attributes_and_respecting_deprecated_foreground_color_arg( &opt.commit_style, @@ -416,9 +370,3 @@ pub fn delta_unreachable(message: &str) -> ! { ); process::exit(1); } - -fn is_truecolor_terminal() -> bool { - env::get_env_var("COLORTERM") - .map(|colorterm| colorterm == "truecolor" || colorterm == "24bit") - .unwrap_or(false) -} |