diff options
author | Dan Davison <dandavison7@gmail.com> | 2020-06-29 19:46:55 -0400 |
---|---|---|
committer | Dan Davison <dandavison7@gmail.com> | 2020-06-30 08:00:30 -0400 |
commit | 32b7bdc066eee243171114ff7952ed573a410fda (patch) | |
tree | 045363116f8bf46e99d241522a747d9a0ba95888 /src/options | |
parent | a733af869f35e7f00380483865abf42033699c5a (diff) |
Set the light/dark/theme option values early
Diffstat (limited to 'src/options')
-rw-r--r-- | src/options/preprocess.rs | 10 | ||||
-rw-r--r-- | src/options/set.rs | 72 |
2 files changed, 60 insertions, 22 deletions
diff --git a/src/options/preprocess.rs b/src/options/preprocess.rs index 818470f1..7fb7accd 100644 --- a/src/options/preprocess.rs +++ b/src/options/preprocess.rs @@ -5,7 +5,11 @@ use crate::cli; use crate::env; use crate::syntax_theme; -pub fn preprocess_options(opt: &mut cli::Opt, assets: HighlightingAssets) { +#[allow(non_snake_case)] +pub fn set__is_light_mode__syntax_theme__syntax_set( + opt: &mut cli::Opt, + assets: HighlightingAssets, +) { _check_validity(&opt, &assets); let syntax_theme_name_from_bat_theme = env::get_env_var("BAT_THEME"); let (is_light_mode, syntax_theme_name) = syntax_theme::get_is_light_mode_and_theme_name( @@ -26,10 +30,6 @@ pub fn preprocess_options(opt: &mut cli::Opt, assets: HighlightingAssets) { } fn _check_validity(opt: &cli::Opt, assets: &HighlightingAssets) { - if opt.light && opt.dark { - eprintln!("--light and --dark cannot be used together."); - process::exit(1); - } if let Some(ref syntax_theme) = opt.syntax_theme { if !syntax_theme::is_no_syntax_highlighting_theme_name(&syntax_theme) { if !assets.theme_set.themes.contains_key(syntax_theme.as_str()) { diff --git a/src/options/set.rs b/src/options/set.rs index c17023b9..339ceffc 100644 --- a/src/options/set.rs +++ b/src/options/set.rs @@ -1,16 +1,19 @@ use std::collections::{HashSet, VecDeque}; +use std::process; use structopt::clap; +use crate::bat::assets::HighlightingAssets; use crate::cli; use crate::config; use crate::features; use crate::git_config; +use crate::options::preprocess; macro_rules! set_options { ([$( ($option_name:expr, $field_ident:ident) ),* ], - $opt:expr, $builtin_features:expr, $git_config:expr, $arg_matches:expr) => { + $opt:expr, $builtin_features:expr, $git_config:expr, $arg_matches:expr, $check_names:expr) => { let mut option_names = HashSet::new(); $( if !$crate::config::user_supplied_option($option_name, $arg_matches) { @@ -22,29 +25,34 @@ macro_rules! set_options { ) { $opt.$field_ident = value; } - }; - option_names.insert($option_name); + } + if $check_names { + option_names.insert($option_name); + } )* - option_names.extend(&[ - "diff-highlight", // Does not exist as a flag on config - "diff-so-fancy", // Does not exist as a flag on config - "features", - "no-gitconfig", - ]); - let expected_option_names = $crate::cli::Opt::get_option_or_flag_names(); - if option_names != expected_option_names { - $crate::config::delta_unreachable( - &format!("Error processing options.\nUnhandled names: {:?}\nInvalid names: {:?}.\n", - &expected_option_names - &option_names, - &option_names - &expected_option_names)); + if $check_names { + option_names.extend(&[ + "diff-highlight", // Does not exist as a flag on config + "diff-so-fancy", // Does not exist as a flag on config + "features", + "no-gitconfig", + ]); + let expected_option_names = $crate::cli::Opt::get_option_or_flag_names(); + if option_names != expected_option_names { + $crate::config::delta_unreachable( + &format!("Error processing options.\nUnhandled names: {:?}\nInvalid names: {:?}.\n", + &expected_option_names - &option_names, + &option_names - &expected_option_names)); + } } - }; + } } pub fn set_options( opt: &mut cli::Opt, git_config: &mut Option<git_config::GitConfig>, arg_matches: &clap::ArgMatches, + assets: HighlightingAssets, ) { if let Some(git_config) = git_config { if opt.no_gitconfig { @@ -69,6 +77,35 @@ pub fn set_options( .unwrap_or_else(|| "magenta reverse".to_string()) } + // Set light, dark and syntax-theme + let validate_light_and_dark = |opt: &cli::Opt| { + if opt.light && opt.dark { + eprintln!("--light and --dark cannot be used together."); + process::exit(1); + } + }; + validate_light_and_dark(&opt); + if !(opt.light || opt.dark) { + set_options!( + [("dark", dark), ("light", light)], + opt, + builtin_features, + git_config, + arg_matches, + false + ); + } + validate_light_and_dark(&opt); + set_options!( + [("syntax-theme", syntax_theme)], + opt, + builtin_features, + git_config, + arg_matches, + false + ); + preprocess::set__is_light_mode__syntax_theme__syntax_set(opt, assets); + set_options!( [ ("24-bit-color", true_color), @@ -124,7 +161,8 @@ pub fn set_options( opt, builtin_features, git_config, - arg_matches + arg_matches, + true ); } |