From cbbc1aab01db4caa24872fd402567d1fc8ee49c3 Mon Sep 17 00:00:00 2001 From: Catherine Noll Date: Sun, 21 Mar 2021 23:03:10 -0400 Subject: Get themes from gitconfig instead of hardcoding --- src/git_config/mod.rs | 2 +- src/main.rs | 39 +++++++++++++++++++++++++++++---------- 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, pub enabled: bool, pub repo: Option, 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 { + let _git_config = git_config::GitConfig::try_create(); + let mut themes: Vec = 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 -- cgit v1.2.3