diff options
author | Dan Davison <dandavison7@gmail.com> | 2020-06-06 21:43:45 -0400 |
---|---|---|
committer | Dan Davison <dandavison7@gmail.com> | 2020-06-06 21:43:45 -0400 |
commit | e39da2aaa68099ab004184d9b2e6f31cffd0be3d (patch) | |
tree | 41db5341b137381cf16b67c704b918393aff0729 | |
parent | c887a9960466654b9fde56661d924ccc600c0af5 (diff) |
Use 'syntax_theme' instead of 'theme' everywhere
-rw-r--r-- | HomeBrewFormula/git-delta.rb | 2 | ||||
-rw-r--r-- | src/cli.rs | 50 | ||||
-rw-r--r-- | src/config.rs | 63 | ||||
-rw-r--r-- | src/gitconfig.rs | 10 | ||||
-rw-r--r-- | src/main.rs | 28 | ||||
-rw-r--r-- | src/paint.rs | 8 | ||||
-rw-r--r-- | src/rewrite.rs | 2 | ||||
-rw-r--r-- | src/syntax_theme.rs (renamed from src/theme.rs) | 0 | ||||
-rw-r--r-- | src/tests/integration_test_utils.rs | 2 | ||||
-rw-r--r-- | src/tests/test_example_diffs.rs | 6 |
10 files changed, 88 insertions, 83 deletions
diff --git a/HomeBrewFormula/git-delta.rb b/HomeBrewFormula/git-delta.rb index 25d8cc86..ffae7289 100644 --- a/HomeBrewFormula/git-delta.rb +++ b/HomeBrewFormula/git-delta.rb @@ -20,6 +20,6 @@ class GitDelta < Formula end test do - shell_output "#{bin}/delta --list-themes" + shell_output "#{bin}/delta --list-syntax-themes" end end @@ -10,7 +10,7 @@ use crate::bat::output::PagingMode; use crate::config; use crate::env; use crate::rewrite; -use crate::theme; +use crate::syntax_theme; #[derive(StructOpt, Clone, Debug, PartialEq)] #[structopt( @@ -119,12 +119,12 @@ https://github.com/dandavison/delta/issues. " )] pub struct Opt { - #[structopt(long = "theme", env = "BAT_THEME")] - /// The code syntax highlighting theme to use. Use --list-themes to demo available themes. If - /// the theme is not set using this option, it will be taken from the BAT_THEME environment - /// variable, if that contains a valid theme name. --theme=none disables all syntax - /// highlighting. - pub theme: Option<String>, + #[structopt(long = "syntax-theme", env = "BAT_THEME")] + /// The code syntax-highlighting theme to use. Use --list-syntax-themes to demo available + /// themes. If the syntax-highlighting theme is not set using this option, it will be taken + /// from the BAT_THEME environment variable, if that contains a valid theme name. + /// --syntax-theme=none disables all syntax highlighting. + pub syntax_theme: Option<String>, /// Use default colors appropriate for a light terminal background. For more control, see the /// style options. @@ -264,14 +264,14 @@ pub struct Opt { pub list_languages: bool, /// List available syntax-highlighting color themes. - #[structopt(long = "list-theme-names")] - pub list_theme_names: bool, + #[structopt(long = "list-syntax-theme-names")] + pub list_syntax_theme_names: bool, - /// List available syntax highlighting themes, each with an example of highlighted diff output. + /// List available syntax-highlighting 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 --list-themes`. - #[structopt(long = "list-themes")] - pub list_themes: bool, + /// example: `git show --color=always | delta --list-syntax-themes`. + #[structopt(long = "list-syntax-themes")] + pub list_syntax_themes: bool, /// The maximum distance between two lines for them to be inferred to be homologous. Homologous /// line pairs are highlighted according to the deletion and insertion operations transforming @@ -409,24 +409,24 @@ fn _check_validity(opt: &Opt, assets: &HighlightingAssets) { eprintln!("--light and --dark cannot be used together."); process::exit(1); } - if let Some(ref theme) = opt.theme { - if !theme::is_no_syntax_highlighting_theme_name(&theme) { - if !assets.theme_set.themes.contains_key(theme.as_str()) { + 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_theme = theme::is_light_theme(&theme); - if is_light_theme && opt.dark { + let is_light_syntax_theme = syntax_theme::is_light_theme(&syntax_theme); + if is_light_syntax_theme && opt.dark { eprintln!( - "{} is a light theme, but you supplied --dark. \ - If you use --theme, you do not need to supply --light or --dark.", - theme + "{} 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_theme && opt.light { + } else if !is_light_syntax_theme && opt.light { eprintln!( - "{} is a dark theme, but you supplied --light. \ - If you use --theme, you do not need to supply --light or --dark.", - theme + "{} 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); } diff --git a/src/config.rs b/src/config.rs index 0de51a04..4c522ea8 100644 --- a/src/config.rs +++ b/src/config.rs @@ -4,7 +4,7 @@ use std::process; use console::Term; use syntect::highlighting::Style as SyntectStyle; -use syntect::highlighting::{Theme, ThemeSet}; +use syntect::highlighting::{Theme as SyntaxTheme, ThemeSet as SyntaxThemeSet}; use syntect::parsing::SyntaxSet; use crate::bat::output::PagingMode; @@ -13,7 +13,7 @@ use crate::color; use crate::delta::State; use crate::env; use crate::style::Style; -use crate::theme; +use crate::syntax_theme; pub enum Width { Fixed(usize), @@ -24,7 +24,6 @@ pub struct Config<'a> { pub background_color_extends_to_terminal_width: bool, pub commit_style: Style, pub decorations_width: Width, - pub dummy_theme: Theme, pub file_added_label: String, pub file_modified_label: String, pub file_removed_label: String, @@ -48,10 +47,11 @@ pub struct Config<'a> { pub plus_line_marker: &'a str, pub plus_non_emph_style: Style, pub plus_style: Style, + pub syntax_dummy_theme: SyntaxTheme, pub syntax_set: SyntaxSet, + pub syntax_theme: Option<SyntaxTheme>, + pub syntax_theme_name: String, pub tab_width: usize, - pub theme: Option<Theme>, - pub theme_name: String, pub true_color: bool, pub zero_style: Style, } @@ -80,7 +80,7 @@ impl<'a> Config<'a> { pub fn get_config<'a>( opt: cli::Opt, syntax_set: SyntaxSet, - theme_set: ThemeSet, + syntax_theme_set: SyntaxThemeSet, true_color: bool, paging_mode: PagingMode, ) -> Config<'a> { @@ -99,12 +99,12 @@ pub fn get_config<'a>( None => (Width::Fixed(available_terminal_width), true), }; - let theme_name_from_bat_pager = env::get_env_var("BAT_THEME"); - let (is_light_mode, theme_name) = theme::get_is_light_mode_and_theme_name( - opt.theme.as_ref(), - theme_name_from_bat_pager.as_ref(), + let syntax_theme_name_from_bat_theme = env::get_env_var("BAT_THEME"); + let (is_light_mode, syntax_theme_name) = syntax_theme::get_is_light_mode_and_theme_name( + opt.syntax_theme.as_ref(), + syntax_theme_name_from_bat_theme.as_ref(), opt.light, - &theme_set, + &syntax_theme_set, ); let ( @@ -120,12 +120,12 @@ pub fn get_config<'a>( let (commit_style, file_style, hunk_header_style) = make_commit_file_hunk_header_styles(&opt, true_color); - let theme = if theme::is_no_syntax_highlighting_theme_name(&theme_name) { + let syntax_theme = if syntax_theme::is_no_syntax_highlighting_theme_name(&syntax_theme_name) { None } else { - Some(theme_set.themes[&theme_name].clone()) + Some(syntax_theme_set.themes[&syntax_theme_name].clone()) }; - let dummy_theme = theme_set.themes.values().next().unwrap().clone(); + let syntax_dummy_theme = syntax_theme_set.themes.values().next().unwrap().clone(); let minus_line_marker = if opt.keep_plus_minus_markers { "-" @@ -147,7 +147,6 @@ pub fn get_config<'a>( background_color_extends_to_terminal_width, commit_style, decorations_width, - dummy_theme, file_added_label: opt.file_added_label, file_modified_label: opt.file_modified_label, file_removed_label: opt.file_removed_label, @@ -171,10 +170,11 @@ pub fn get_config<'a>( plus_line_marker, plus_non_emph_style, plus_style, + syntax_dummy_theme, syntax_set, + syntax_theme, + syntax_theme_name, tab_width: opt.tab_width, - theme, - theme_name, true_color, zero_style, } @@ -307,20 +307,20 @@ mod tests { #[test] #[ignore] - fn test_theme_selection() { + fn test_syntax_theme_selection() { #[derive(PartialEq)] enum Mode { Light, Dark, }; for ( - theme_option, + syntax_theme_option, bat_theme_env_var, mode_option, // (--light, --dark) - expected_theme, + expected_syntax_theme, expected_mode, ) in vec![ - (None, "", None, theme::DEFAULT_DARK_THEME, Mode::Dark), + (None, "", None, syntax_theme::DEFAULT_DARK_THEME, Mode::Dark), (Some("GitHub".to_string()), "", None, "GitHub", Mode::Light), ( Some("GitHub".to_string()), @@ -334,28 +334,28 @@ mod tests { None, "<not set>", None, - theme::DEFAULT_DARK_THEME, + syntax_theme::DEFAULT_DARK_THEME, Mode::Dark, ), ( None, "", Some(Mode::Light), - theme::DEFAULT_LIGHT_THEME, + syntax_theme::DEFAULT_LIGHT_THEME, Mode::Light, ), ( None, "", Some(Mode::Dark), - theme::DEFAULT_DARK_THEME, + syntax_theme::DEFAULT_DARK_THEME, Mode::Dark, ), ( None, "<@@@@@>", Some(Mode::Light), - theme::DEFAULT_LIGHT_THEME, + syntax_theme::DEFAULT_LIGHT_THEME, Mode::Light, ), (None, "1337", Some(Mode::Light), "1337", Mode::Light), @@ -375,7 +375,7 @@ mod tests { } let is_true_color = true; let mut options = integration_test_utils::get_command_line_options(); - options.theme = theme_option; + options.syntax_theme = syntax_theme_option; match mode_option { Some(Mode::Light) => { options.light = true; @@ -391,11 +391,14 @@ mod tests { } } let config = cli::process_command_line_arguments(options, None); - assert_eq!(config.theme_name, expected_theme); - if theme::is_no_syntax_highlighting_theme_name(expected_theme) { - assert!(config.theme.is_none()) + assert_eq!(config.syntax_theme_name, expected_syntax_theme); + if syntax_theme::is_no_syntax_highlighting_theme_name(expected_syntax_theme) { + assert!(config.syntax_theme.is_none()) } else { - assert_eq!(config.theme.unwrap().name.as_ref().unwrap(), expected_theme); + assert_eq!( + config.syntax_theme.unwrap().name.as_ref().unwrap(), + expected_syntax_theme + ); } assert_eq!( config.minus_style.ansi_term_style.background.unwrap(), diff --git a/src/gitconfig.rs b/src/gitconfig.rs index 328433e8..5544bb62 100644 --- a/src/gitconfig.rs +++ b/src/gitconfig.rs @@ -90,7 +90,7 @@ mod set_delta_options { $( ($option_name, $field_ident, - $crate::gitconfig::make_git_config_keys_for_delta($option_name, $opt.theme.as_deref()), + $crate::gitconfig::make_git_config_keys_for_delta($option_name, $opt.syntax_theme.as_deref()), &$opt.$field_ident) ),* ], @@ -107,7 +107,7 @@ mod set_delta_options { $( ($option_name, $field_ident, - $crate::gitconfig::make_git_config_keys_for_delta($option_name, $opt.theme.as_deref()), + $crate::gitconfig::make_git_config_keys_for_delta($option_name, $opt.syntax_theme.as_deref()), $opt.$field_ident.as_deref()) ),* ], @@ -124,7 +124,7 @@ mod set_delta_options { $( ($option_name, $field_ident, - $crate::gitconfig::make_git_config_keys_for_delta($option_name, $opt.theme.as_deref()), + $crate::gitconfig::make_git_config_keys_for_delta($option_name, $opt.syntax_theme.as_deref()), $opt.$field_ident) ),* ], @@ -141,7 +141,7 @@ mod set_delta_options { $( ($option_name, $field_ident, - $crate::gitconfig::make_git_config_keys_for_delta($option_name, $opt.theme.as_deref()), + $crate::gitconfig::make_git_config_keys_for_delta($option_name, $opt.syntax_theme.as_deref()), $opt.$field_ident) ),* ], @@ -158,7 +158,7 @@ mod set_delta_options { $( ($option_name, $field_ident, - $crate::gitconfig::make_git_config_keys_for_delta($option_name, $opt.theme.as_deref()), + $crate::gitconfig::make_git_config_keys_for_delta($option_name, $opt.syntax_theme.as_deref()), $opt.$field_ident) ),* ], diff --git a/src/main.rs b/src/main.rs index 14417119..f92c5e2c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,9 +18,9 @@ mod paint; mod parse; mod rewrite; mod style; +mod syntax_theme; mod syntect_color; mod tests; -mod theme; use std::io::{self, ErrorKind, Read, Write}; use std::path::PathBuf; @@ -52,11 +52,11 @@ fn main() -> std::io::Result<()> { if opt.list_languages { list_languages()?; process::exit(0); - } else if opt.list_theme_names { - list_theme_names()?; + } else if opt.list_syntax_theme_names { + list_syntax_theme_names()?; process::exit(0); - } else if opt.list_themes { - list_themes()?; + } else if opt.list_syntax_themes { + list_syntax_themes()?; process::exit(0); } @@ -145,7 +145,7 @@ fn get_painted_rgb_string(color: Color) -> String { color.paint(format!("{:?}", color)).to_string() } -fn list_themes() -> std::io::Result<()> { +fn list_syntax_themes() -> std::io::Result<()> { use bytelines::ByteLines; use std::io::BufReader; let opt = cli::Opt::from_args(); @@ -178,15 +178,17 @@ index f38589a..0f1bb83 100644 let assets = HighlightingAssets::new(); - for (theme, _) in assets.theme_set.themes.iter() { - if opt.light && !theme::is_light_theme(theme) || opt.dark && theme::is_light_theme(theme) { + for (syntax_theme, _) in assets.theme_set.themes.iter() { + if opt.light && !syntax_theme::is_light_theme(syntax_theme) + || opt.dark && syntax_theme::is_light_theme(syntax_theme) + { continue; } - writeln!(stdout, "\n\nTheme: {}\n", style.paint(theme))?; + writeln!(stdout, "\n\nTheme: {}\n", style.paint(syntax_theme))?; let config = cli::process_command_line_arguments( cli::Opt { - theme: Some(theme.to_string()), + syntax_theme: Some(syntax_theme.to_string()), file_style: "omit".to_string(), hunk_header_style: "omit".to_string(), ..opt.clone() @@ -211,7 +213,7 @@ index f38589a..0f1bb83 100644 Ok(()) } -pub fn list_theme_names() -> std::io::Result<()> { +pub fn list_syntax_theme_names() -> std::io::Result<()> { let assets = HighlightingAssets::new(); let themes = &assets.theme_set.themes; let stdout = io::stdout(); @@ -219,13 +221,13 @@ pub fn list_theme_names() -> std::io::Result<()> { writeln!(stdout, "Light themes:")?; for (theme, _) in themes.iter() { - if theme::is_light_theme(theme) { + if syntax_theme::is_light_theme(theme) { writeln!(stdout, " {}", theme)?; } } writeln!(stdout, "Dark themes:")?; for (theme, _) in themes.iter() { - if !theme::is_light_theme(theme) { + if !syntax_theme::is_light_theme(theme) { writeln!(stdout, " {}", theme)?; } } diff --git a/src/paint.rs b/src/paint.rs index b5ef0a49..bfd029ef 100644 --- a/src/paint.rs +++ b/src/paint.rs @@ -28,7 +28,7 @@ impl<'a> Painter<'a> { pub fn new(writer: &'a mut dyn Write, config: &'a config::Config) -> Self { let default_syntax = Self::get_syntax(&config.syntax_set, None); // TODO: Avoid doing this. - let dummy_highlighter = HighlightLines::new(default_syntax, &config.dummy_theme); + let dummy_highlighter = HighlightLines::new(default_syntax, &config.syntax_dummy_theme); Self { minus_lines: Vec::new(), plus_lines: Vec::new(), @@ -51,8 +51,8 @@ impl<'a> Painter<'a> { } pub fn set_highlighter(&mut self) { - if let Some(ref theme) = self.config.theme { - self.highlighter = HighlightLines::new(self.syntax, &theme) + if let Some(ref syntax_theme) = self.config.syntax_theme { + self.highlighter = HighlightLines::new(self.syntax, &syntax_theme) }; } @@ -185,7 +185,7 @@ impl<'a> Painter<'a> { } pub fn should_compute_syntax_highlighting(state: &State, config: &config::Config) -> bool { - if config.theme.is_none() { + if config.syntax_theme.is_none() { return false; } match state { diff --git a/src/rewrite.rs b/src/rewrite.rs index 84b751f2..bef63533 100644 --- a/src/rewrite.rs +++ b/src/rewrite.rs @@ -87,7 +87,7 @@ fn _rewrite_options_to_honor_git_config( arg_matches ); set_delta_options__option_string!( - [("theme", theme), ("width", width)], + [("syntax_theme", syntax_theme), ("width", width)], opt, git_config, arg_matches diff --git a/src/theme.rs b/src/syntax_theme.rs index 04db911d..04db911d 100644 --- a/src/theme.rs +++ b/src/syntax_theme.rs diff --git a/src/tests/integration_test_utils.rs b/src/tests/integration_test_utils.rs index 1b96537d..ad554097 100644 --- a/src/tests/integration_test_utils.rs +++ b/src/tests/integration_test_utils.rs @@ -13,7 +13,7 @@ pub mod integration_test_utils { pub fn get_command_line_options() -> cli::Opt { let mut opt = cli::Opt::from_iter(Vec::<OsString>::new()); - opt.theme = None; // TODO: Why does opt.theme have the value Some("")? + opt.syntax_theme = None; // TODO: Why does opt.theme have the value Some("")? opt } diff --git a/src/tests/test_example_diffs.rs b/src/tests/test_example_diffs.rs index 6f7dca22..024fc932 100644 --- a/src/tests/test_example_diffs.rs +++ b/src/tests/test_example_diffs.rs @@ -59,7 +59,7 @@ mod tests { } #[test] - fn test_unrecognized_file_type_with_theme() { + fn test_unrecognized_file_type_with_syntax_theme() { // In addition to the background color, the code has the foreground color using the default // .txt syntax under the theme. let options = integration_test_utils::get_command_line_options(); @@ -70,11 +70,11 @@ mod tests { } #[test] - fn test_unrecognized_file_type_no_theme() { + fn test_unrecognized_file_type_no_syntax_theme() { // The code has the background color only. (Since there is no theme, the code has no // foreground ansi color codes.) let mut options = integration_test_utils::get_command_line_options(); - options.theme = Some("none".to_string()); + options.syntax_theme = Some("none".to_string()); options.width = Some("variable".to_string()); let input = ADDED_FILE_INPUT.replace("a.py", "a"); let (output, config) = |