diff options
Diffstat (limited to 'src/options')
-rw-r--r-- | src/options/get.rs | 61 | ||||
-rw-r--r-- | src/options/set.rs | 1 | ||||
-rw-r--r-- | src/options/theme.rs | 2 |
3 files changed, 63 insertions, 1 deletions
diff --git a/src/options/get.rs b/src/options/get.rs index a36987ca..b907ee88 100644 --- a/src/options/get.rs +++ b/src/options/get.rs @@ -1,3 +1,5 @@ +use lazy_static::lazy_static; +use regex::Regex; use std::collections::HashMap; use crate::cli; @@ -38,6 +40,27 @@ where T::get_option_value(option_name, builtin_features, opt, git_config) } +lazy_static! { + static ref GIT_CONFIG_THEME_REGEX: Regex = Regex::new(r"^delta\.(.+)\.(light|dark)$").unwrap(); +} + +pub fn get_themes(git_config: Option<git_config::GitConfig>) -> Vec<String> { + let mut themes: Vec<String> = Vec::new(); + for e in &git_config.unwrap().config.entries(None).unwrap() { + let entry = e.unwrap(); + let entry_name = entry.name().unwrap(); + let caps = GIT_CONFIG_THEME_REGEX.captures(entry_name); + if let Some(caps) = caps { + let name = caps.get(1).map_or("", |m| m.as_str()).to_string(); + if !themes.contains(&name) { + themes.push(name) + } + } + } + themes.sort_by(|a, b| a.to_lowercase().cmp(&b.to_lowercase())); + themes +} + pub trait GetOptionValue { fn get_option_value( option_name: &str, @@ -115,6 +138,7 @@ pub mod tests { use std::env; use std::fs::remove_file; + use crate::options::get::get_themes; use crate::tests::integration_test_utils::integration_test_utils; // TODO: the followig tests are collapsed into one since they all set the same env var and thus @@ -275,4 +299,41 @@ pub mod tests { remove_file(git_config_path).unwrap(); } + + #[test] + fn test_get_themes_from_config() { + let git_config_contents = r#" +[delta "dark-theme"] + max-line-distance = 0.6 + dark = true + +[delta "light-theme"] + max-line-distance = 0.6 + light = true + +[delta "light-and-dark-theme"] + max-line-distance = 0.6 + light = true + dark = true + +[delta "not-a-theme"] + max-line-distance = 0.6 +"#; + let git_config_path = "delta__test_get_themes_git_config.gitconfig"; + + let git_config = Some(integration_test_utils::make_git_config( + git_config_contents.as_bytes(), + git_config_path, + false, + )); + + let themes = get_themes(git_config); + + assert_eq!( + themes, + ["dark-theme", "light-and-dark-theme", "light-theme",] + ); + + remove_file(git_config_path).unwrap(); + } } diff --git a/src/options/set.rs b/src/options/set.rs index 354eeea6..e5c1edf0 100644 --- a/src/options/set.rs +++ b/src/options/set.rs @@ -169,6 +169,7 @@ pub fn set_options( plus_empty_line_marker_style, plus_non_emph_style, raw, + show_themes, side_by_side, tab_width, tokenization_regex, diff --git a/src/options/theme.rs b/src/options/theme.rs index ae831202..420c5be5 100644 --- a/src/options/theme.rs +++ b/src/options/theme.rs @@ -66,7 +66,7 @@ fn is_no_syntax_highlighting_syntax_theme_name(theme_name: &str) -> bool { /// chosen for a light or dark terminal background. (`bat` has no equivalent.) /// /// Basically: -/// 1. The theme is specified by the `--theme` option. If this isn't supplied then it is specified +/// 1. The theme is specified by the `--syntax-theme` option. If this isn't supplied then it is specified /// by the `BAT_THEME` environment variable. /// 2. Light vs dark mode is specified by the `--light` or `--dark` options. If these aren't /// supplied then it is inferred from the chosen theme. |