Skip to content

Commit

Permalink
Merge pull request #15 from dandavison/14-no-syntax-highlighting
Browse files Browse the repository at this point in the history
Support --theme=none
  • Loading branch information
dandavison authored Oct 1, 2019
2 parents e2b6da0 + a6d16c3 commit 2396737
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
6 changes: 3 additions & 3 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -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);
Expand All @@ -157,7 +157,7 @@ pub fn process_command_line_arguments<'a>(
process::exit(1);
}
}
None => (),
_ => (),
};

let terminal_width = Term::stdout().size().1 as usize;
Expand Down
15 changes: 12 additions & 3 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand Down
17 changes: 10 additions & 7 deletions src/paint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)]
Expand Down

0 comments on commit 2396737

Please sign in to comment.