diff options
author | Catherine Noll <noll.catherine@gmail.com> | 2021-03-27 21:16:40 -0400 |
---|---|---|
committer | Catherine Noll <noll.catherine@gmail.com> | 2021-03-27 22:20:24 -0400 |
commit | d83c438597d29b60c7a4236417e12fa52e67c78c (patch) | |
tree | bfe68a9a941d8b3c5812d63ae7debccdfe97b16b | |
parent | a1e42432f4413b54249700e59494e261f5718f3a (diff) |
Define theme by presence of "dark" or "light" entries in git config,
and give the ability to filter themes shown with --show-themes by
dark or light, using the --dark and --light flags
-rw-r--r-- | src/cli.rs | 4 | ||||
-rw-r--r-- | src/main.rs | 22 | ||||
-rw-r--r-- | src/options/get.rs | 27 | ||||
-rw-r--r-- | themes.gitconfig | 7 |
4 files changed, 39 insertions, 21 deletions
@@ -201,11 +201,13 @@ https://github.com/dandavison/delta/issues. pub struct Opt { /// Use default colors appropriate for a light terminal background. For more control, see the /// style options and --syntax-theme. + /// When passed in with --show-themes, only light themes will be displayed. #[structopt(long = "light")] pub light: bool, /// Use default colors appropriate for a dark terminal background. For more control, see the /// style options and --syntax-theme. + /// When passed in with --show-themes, only dark themes will be displayed. #[structopt(long = "dark")] pub dark: bool, @@ -272,6 +274,8 @@ pub struct Opt { /// Show all available delta themes, each with an example of highlighted diff output. /// If diff output is supplied on standard input then this will be used for the demo. For /// example: `git show --color=always | delta --show-themes`. + /// When used with the --dark or --light command line arguments, will only show dark or + /// light themes, respectively. #[structopt(long = "show-themes")] pub show_themes: bool, diff --git a/src/main.rs b/src/main.rs index fb4628ed..dcc6ed48 100644 --- a/src/main.rs +++ b/src/main.rs @@ -68,7 +68,7 @@ fn run_app() -> std::io::Result<i32> { show_syntax_themes()?; return Ok(0); } else if opt.show_themes { - show_themes()?; + show_themes(opt.dark, opt.light)?; return Ok(0); } @@ -312,7 +312,7 @@ where } } -fn show_themes() -> std::io::Result<()> { +fn show_themes(dark: bool, light: bool) -> std::io::Result<()> { use bytelines::ByteLines; use sample_diff::DIFF; use std::io::BufReader; @@ -337,17 +337,23 @@ fn show_themes() -> std::io::Result<()> { let writer = output_type.handle().unwrap(); for theme in &get_themes(git_config::GitConfig::try_create()) { - writeln!(writer, "\n\nTheme: {}\n", title_style.paint(theme))?; let opt = cli::Opt::from_iter_and_git_config(&["", "", "--features", &theme], &mut git_config); + let is_dark_theme = opt.dark; + let is_light_theme = opt.light; 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), - _ => eprintln!("{}", error), + if (dark && is_dark_theme) || (light && is_light_theme) || (!dark && !light) { + writeln!(writer, "\n\nTheme: {}\n", title_style.paint(theme))?; + + if let Err(error) = delta(ByteLines::new(BufReader::new(&input[0..])), writer, &config) + { + match error.kind() { + ErrorKind::BrokenPipe => process::exit(0), + _ => eprintln!("{}", error), + } } - }; + } } Ok(()) diff --git a/src/options/get.rs b/src/options/get.rs index 58209f5d..a1bbecae 100644 --- a/src/options/get.rs +++ b/src/options/get.rs @@ -41,7 +41,7 @@ where } lazy_static! { - static ref GIT_CONFIG_THEME_REGEX: Regex = Regex::new(r"^delta\.(.+)\.is-theme$").unwrap(); + 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> { @@ -53,9 +53,10 @@ pub fn get_themes(git_config: Option<git_config::GitConfig>) -> Vec<String> { 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) + if !themes.contains(&name) { + themes.push(name) + } } } } @@ -304,15 +305,20 @@ pub mod tests { #[test] fn test_get_themes_from_config() { let git_config_contents = b" -[delta \"yes\"] +[delta \"dark-theme\"] max-line-distance = 0.6 - is-theme = true + dark = true -[delta \"not-a-theme\"] +[delta \"light-theme\"] + max-line-distance = 0.6 + light = true + +[delta \"light-and-dark-theme\"] max-line-distance = 0.6 - is-theme = false + light = true + dark = true -[delta \"has-no-theme-entry\"] +[delta \"not-a-theme\"] max-line-distance = 0.6 "; let git_config_path = "delta__test_get_themes_git_config.gitconfig"; @@ -325,7 +331,10 @@ pub mod tests { let themes = get_themes(git_config); - assert_eq!(themes, ["yes"]); + assert_eq!( + themes, + ["dark-theme", "light-theme", "light-and-dark-theme"] + ); remove_file(git_config_path).unwrap(); } diff --git a/themes.gitconfig b/themes.gitconfig index 3dd4ba1a..68783e9c 100644 --- a/themes.gitconfig +++ b/themes.gitconfig @@ -49,10 +49,10 @@ file-modified-label = [M] file-removed-label = [-] file-renamed-label = [R] - is-theme = true [delta "villsau"] # author: https://github.com/torarnv + dark = true syntax-theme = OneHalfDark line-numbers = false hunk-header-style = file line-number syntax @@ -70,11 +70,11 @@ 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 commit-decoration-style = bold box ul + dark = true file-style = omit file-decoration-style = none hunk-header-style = file line-number syntax @@ -92,11 +92,11 @@ plus-style = syntax "#001a00" plus-emph-style = syntax "#003300" syntax-theme = Nord - is-theme = true [delta "tangara-chilensis"] # author: https://github.com/clnoll commit-decoration-style = bold box ul "#34fd50" + dark = true file-style = omit file-decoration-style = none hunk-header-style = file line-number syntax @@ -115,4 +115,3 @@ plus-emph-style = syntax "#03a4ff" side-by-side = true syntax-theme = Vibrant Sunburst - is-theme = true |