summaryrefslogtreecommitdiffstats
path: root/src/config.rs
diff options
context:
space:
mode:
authorDan Davison <dandavison7@gmail.com>2020-06-22 11:07:09 -0400
committerDan Davison <dandavison7@gmail.com>2020-06-22 12:05:37 -0400
commit83e020d43e143852aebe1c64e15edd2ba4b13756 (patch)
tree57d523665c16077c6ad6839f561d2b66d79c6a7c /src/config.rs
parentf635d87938a0b3d74e90e8e0e9aadb65e7d3bb93 (diff)
Refactor: separate Opt construction from Config construction
Diffstat (limited to 'src/config.rs')
-rw-r--r--src/config.rs119
1 files changed, 51 insertions, 68 deletions
diff --git a/src/config.rs b/src/config.rs
index e05d7b8c..3ee7c85a 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -4,7 +4,7 @@ use std::process;
use console::Term;
use regex::Regex;
-use structopt::{clap, StructOpt};
+use structopt::clap;
use syntect::highlighting::Style as SyntectStyle;
use syntect::highlighting::Theme as SyntaxTheme;
use syntect::parsing::SyntaxSet;
@@ -15,9 +15,6 @@ use crate::cli;
use crate::color;
use crate::delta::State;
use crate::env;
-use crate::git_config::GitConfig;
-use crate::rewrite_options;
-use crate::set_options;
use crate::style::Style;
use crate::syntax_theme;
@@ -77,20 +74,6 @@ pub struct Config {
}
impl Config {
- pub fn from_args(args: &[&str], git_config: &mut Option<GitConfig>) -> Self {
- Self::from_arg_matches(cli::Opt::clap().get_matches_from(args), git_config)
- }
-
- pub fn from_arg_matches(
- arg_matches: clap::ArgMatches,
- git_config: &mut Option<GitConfig>,
- ) -> Self {
- let mut opt = cli::Opt::from_clap(&arg_matches);
- set_options::set_options(&mut opt, git_config, &arg_matches);
- rewrite_options::apply_rewrite_rules(&mut opt, &arg_matches);
- Self::from(opt)
- }
-
pub fn get_style(&self, state: &State) -> &Style {
match state {
State::CommitMeta => &self.commit_style,
@@ -101,56 +84,6 @@ impl Config {
}
}
-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()) {
- return;
- }
- let is_light_syntax_theme = syntax_theme::is_light_theme(&syntax_theme);
- if is_light_syntax_theme && opt.dark {
- eprintln!(
- "{} is a light syntax theme, but you supplied --dark. \
- If you use --syntax-theme, you do not need to supply --light or --dark.",
- syntax_theme
- );
- process::exit(1);
- } else if !is_light_syntax_theme && opt.light {
- eprintln!(
- "{} is a dark syntax theme, but you supplied --light. \
- If you use --syntax-theme, you do not need to supply --light or --dark.",
- syntax_theme
- );
- process::exit(1);
- }
- }
- }
-}
-
-/// Did the user supply `option` on the command line?
-pub fn user_supplied_option(option: &str, arg_matches: &clap::ArgMatches) -> bool {
- arg_matches.occurrences_of(option) > 0
-}
-
-pub fn unreachable(message: &str) -> ! {
- eprintln!(
- "{} This should not be possible. \
- Please report the bug at https://github.com/dandavison/delta/issues.",
- message
- );
- process::exit(1);
-}
-
-fn is_truecolor_terminal() -> bool {
- env::get_env_var("COLORTERM")
- .map(|colorterm| colorterm == "truecolor" || colorterm == "24bit")
- .unwrap_or(false)
-}
-
impl From<cli::Opt> for Config {
fn from(opt: cli::Opt) -> Self {
let assets = HighlightingAssets::new();
@@ -539,6 +472,56 @@ pub fn make_navigate_regexp(config: &Config) -> String {
)
}
+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()) {
+ return;
+ }
+ let is_light_syntax_theme = syntax_theme::is_light_theme(&syntax_theme);
+ if is_light_syntax_theme && opt.dark {
+ eprintln!(
+ "{} is a light syntax theme, but you supplied --dark. \
+ If you use --syntax-theme, you do not need to supply --light or --dark.",
+ syntax_theme
+ );
+ process::exit(1);
+ } else if !is_light_syntax_theme && opt.light {
+ eprintln!(
+ "{} is a dark syntax theme, but you supplied --light. \
+ If you use --syntax-theme, you do not need to supply --light or --dark.",
+ syntax_theme
+ );
+ process::exit(1);
+ }
+ }
+ }
+}
+
+/// Did the user supply `option` on the command line?
+pub fn user_supplied_option(option: &str, arg_matches: &clap::ArgMatches) -> bool {
+ arg_matches.occurrences_of(option) > 0
+}
+
+pub fn unreachable(message: &str) -> ! {
+ eprintln!(
+ "{} This should not be possible. \
+ Please report the bug at https://github.com/dandavison/delta/issues.",
+ message
+ );
+ process::exit(1);
+}
+
+fn is_truecolor_terminal() -> bool {
+ env::get_env_var("COLORTERM")
+ .map(|colorterm| colorterm == "truecolor" || colorterm == "24bit")
+ .unwrap_or(false)
+}
+
#[cfg(test)]
mod tests {
use super::*;