diff options
author | Catherine Noll <noll.catherine@gmail.com> | 2021-03-21 23:03:10 -0400 |
---|---|---|
committer | Catherine Noll <noll.catherine@gmail.com> | 2021-03-27 18:55:29 -0400 |
commit | cbbc1aab01db4caa24872fd402567d1fc8ee49c3 (patch) | |
tree | 1f5f28176e8a28e9c870259d78d48a1a0e93c80a | |
parent | 85f591b75297c085ba321e869b1d3a63a0d4837d (diff) |
Get themes from gitconfig instead of hardcoding
-rw-r--r-- | src/git_config/mod.rs | 2 | ||||
-rw-r--r-- | src/main.rs | 39 | ||||
-rw-r--r-- | themes.gitconfig | 4 |
3 files changed, 34 insertions, 11 deletions
diff --git a/src/git_config/mod.rs b/src/git_config/mod.rs index 827446e1..13b494b9 100644 --- a/src/git_config/mod.rs +++ b/src/git_config/mod.rs @@ -12,7 +12,7 @@ use std::process; use lazy_static::lazy_static; pub struct GitConfig { - config: git2::Config, + pub config: git2::Config, config_from_env_var: HashMap<String, String>, pub enabled: bool, pub repo: Option<git2::Repository>, diff --git a/src/main.rs b/src/main.rs index 35edb03c..a90d4871 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,6 +27,8 @@ mod style; mod syntect_color; mod tests; +use lazy_static::lazy_static; +use regex::Regex; use std::io::{self, ErrorKind, Read, Write}; use std::path::PathBuf; use std::process; @@ -311,12 +313,29 @@ where } } -const THEMES: [&'static str; 4] = [ - "collared-trogon", - "tangara-chilensis", - "villsau", - "woolly-mammoth", -]; +lazy_static! { + static ref GIT_CONFIG_THEME_REGEX: Regex = Regex::new(r"^delta\.(.+)\.is-theme$").unwrap(); +} + +fn get_themes() -> Vec<String> { + let _git_config = git_config::GitConfig::try_create(); + 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 entry_value = entry.value().unwrap(); + dbg!(entry_name, entry_value); + if entry_value == "true" { + let caps = GIT_CONFIG_THEME_REGEX.captures(entry_name); + if let Some(caps) = caps { + // need to check value i.e. whether is_theme = false + let name = caps.get(1).map_or("", |m| m.as_str()).to_string(); + themes.push(name) + } + } + } + themes +} fn show_themes() -> std::io::Result<()> { use bytelines::ByteLines; @@ -337,17 +356,17 @@ fn show_themes() -> std::io::Result<()> { &["", "", "--navigate", "--show-themes"], &mut git_config, ); - let mut output_type = OutputType::from_mode(PagingMode::Always, None, &config::Config::from(opt)).unwrap(); let title_style = ansi_term::Style::new().bold(); let writer = output_type.handle().unwrap(); - for theme in &THEMES { - writeln!(writer, "\n\nTheme: {}\n", title_style.paint(*theme))?; + for theme in &get_themes() { + writeln!(writer, "\n\nTheme: {}\n", title_style.paint(theme))?; let opt = - cli::Opt::from_iter_and_git_config(&["", "", "--features", theme], &mut git_config); + cli::Opt::from_iter_and_git_config(&["", "", "--features", &theme], &mut git_config); let config = config::Config::from(opt); + if let Err(error) = delta(ByteLines::new(BufReader::new(&input[0..])), writer, &config) { match error.kind() { ErrorKind::BrokenPipe => process::exit(0), diff --git a/themes.gitconfig b/themes.gitconfig index 13960e32..3dd4ba1a 100644 --- a/themes.gitconfig +++ b/themes.gitconfig @@ -49,6 +49,7 @@ file-modified-label = [M] file-removed-label = [-] file-renamed-label = [R] + is-theme = true [delta "villsau"] # author: https://github.com/torarnv @@ -69,6 +70,7 @@ plus-emph-style = bold green 22 plus-empty-line-marker-style = normal "#002800" whitespace-error-style = reverse red + is-theme = true [delta "collared-trogon"] # author: https://github.com/clnoll @@ -90,6 +92,7 @@ plus-style = syntax "#001a00" plus-emph-style = syntax "#003300" syntax-theme = Nord + is-theme = true [delta "tangara-chilensis"] # author: https://github.com/clnoll @@ -112,3 +115,4 @@ plus-emph-style = syntax "#03a4ff" side-by-side = true syntax-theme = Vibrant Sunburst + is-theme = true |