diff options
author | Dan Davison <dandavison7@gmail.com> | 2019-10-01 19:13:01 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-10-01 19:13:01 +0100 |
commit | 2396737f0010e2f657b23f0811b76deda60c5e30 (patch) | |
tree | 434449033093044ff5abc9ff0189c840159e6901 | |
parent | e2b6da0dd429339ccfd6a67739fc1595afad41c7 (diff) | |
parent | a6d16c315f14243f23c8a59f37ad26088fb71fb4 (diff) |
Merge pull request #15 from dandavison/14-no-syntax-highlighting
Support --theme=none
-rw-r--r-- | src/cli.rs | 6 | ||||
-rw-r--r-- | src/config.rs | 15 | ||||
-rw-r--r-- | src/paint.rs | 17 |
3 files changed, 25 insertions, 13 deletions
@@ -39,7 +39,7 @@ pub struct Opt { pub plus_emph_color: Option<String>, #[structopt(long = "theme")] - /// The syntax highlighting theme to use. + /// The syntax highlighting theme to use. Use --theme=none to disable syntax highlighting. pub theme: Option<String>, #[structopt(long = "highlight-removed")] @@ -135,7 +135,7 @@ pub fn process_command_line_arguments<'a>( process::exit(1); } match &opt.theme { - Some(theme) => { + Some(theme) if theme.to_lowercase() != "none" => { if !assets.theme_set.themes.contains_key(theme.as_str()) { eprintln!("Invalid theme: '{}'", theme); process::exit(1); @@ -157,7 +157,7 @@ pub fn process_command_line_arguments<'a>( process::exit(1); } } - None => (), + _ => (), }; let terminal_width = Term::stdout().size().1 as usize; diff --git a/src/config.rs b/src/config.rs index 2151b086..e1008676 100644 --- a/src/config.rs +++ b/src/config.rs @@ -7,7 +7,7 @@ use crate::cli; use crate::style; pub struct Config<'a> { - pub theme: &'a Theme, + pub theme: Option<&'a Theme>, pub theme_name: &'a str, pub minus_style_modifier: StyleModifier, pub minus_emph_style_modifier: StyleModifier, @@ -39,7 +39,16 @@ pub fn get_config<'a>( } } }; - let is_light_theme = style::LIGHT_THEMES.contains(&theme_name); + let theme = if theme_name.to_lowercase() == "none" { + None + } else { + Some(&theme_set.themes[theme_name]) + }; + let is_light_theme = if theme.is_none() { + !opt.dark + } else { + style::LIGHT_THEMES.contains(&theme_name) + }; let minus_style_modifier = StyleModifier { background: Some(color_from_arg( @@ -94,7 +103,7 @@ pub fn get_config<'a>( }; Config { - theme: &theme_set.themes[theme_name], + theme, theme_name, minus_style_modifier, minus_emph_style_modifier, diff --git a/src/paint.rs b/src/paint.rs index bf873727..543eef95 100644 --- a/src/paint.rs +++ b/src/paint.rs @@ -25,24 +25,27 @@ impl<'a> Painter<'a> { pub fn new( writer: &'a mut Write, config: &'a config::Config, - assets: &HighlightingAssets, + assets: &'a HighlightingAssets, ) -> Self { + let dummy_highlighter = HighlightLines::new( + assets.syntax_set.find_syntax_by_extension("txt").unwrap(), + &assets.theme_set.themes[style::DEFAULT_LIGHT_THEME], + ); Self { minus_lines: Vec::new(), plus_lines: Vec::new(), output_buffer: String::new(), syntax: None, - highlighter: HighlightLines::new( - assets.syntax_set.find_syntax_by_extension("txt").unwrap(), - config.theme, - ), + highlighter: dummy_highlighter, writer, config, } } pub fn reset_highlighter(&mut self) { - self.highlighter = HighlightLines::new(self.syntax.unwrap(), self.config.theme); + if let Some(theme) = self.config.theme { + self.highlighter = HighlightLines::new(self.syntax.unwrap(), theme) + }; } pub fn paint_buffered_lines(&mut self) { @@ -165,7 +168,7 @@ impl<'a> Painter<'a> { config: &config::Config, should_syntax_highlight: bool, ) -> Vec<(Style, &'a str)> { - if should_syntax_highlight { + if should_syntax_highlight && config.theme.is_some() { highlighter.highlight(line, &config.syntax_set) } else { vec![(config.no_style, line)] |