summaryrefslogtreecommitdiffstats
path: root/src/paint.rs
diff options
context:
space:
mode:
authorDan Davison <dandavison7@gmail.com>2019-10-17 10:20:37 -0700
committerDan Davison <dandavison7@gmail.com>2019-10-17 18:06:02 -0700
commit3af7f5a08b5e1eb321ed0f5d148ee06d78321a64 (patch)
tree22628b90d19fbfb8dd5541d2212589acdc354c39 /src/paint.rs
parent08aa08d0b86c25c2856a37c9be0a3f09a2f07e1b (diff)
Apply background colors to unrecognized file types
If a theme is selected, foreground highlighting will be highlighted as .txt. If theme=none there will be no foreground highlighting. Fixes #34
Diffstat (limited to 'src/paint.rs')
-rw-r--r--src/paint.rs27
1 files changed, 18 insertions, 9 deletions
diff --git a/src/paint.rs b/src/paint.rs
index bd7dd354..d291417a 100644
--- a/src/paint.rs
+++ b/src/paint.rs
@@ -2,7 +2,7 @@ use std::io::Write;
use syntect::easy::HighlightLines;
use syntect::highlighting::{Style, StyleModifier};
-use syntect::parsing::SyntaxReference;
+use syntect::parsing::{SyntaxReference, SyntaxSet};
use unicode_segmentation::UnicodeSegmentation;
use crate::bat::assets::HighlightingAssets;
@@ -15,7 +15,7 @@ pub struct Painter<'a> {
pub minus_lines: Vec<String>,
pub plus_lines: Vec<String>,
pub writer: &'a mut dyn Write,
- pub syntax: Option<&'a SyntaxReference>,
+ pub syntax: &'a SyntaxReference,
pub highlighter: HighlightLines<'a>,
pub config: &'a config::Config<'a>,
pub output_buffer: String,
@@ -27,24 +27,33 @@ impl<'a> Painter<'a> {
config: &'a config::Config,
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],
- );
+ let default_syntax = Self::get_syntax(config.syntax_set, None);
+ let dummy_theme = &assets.theme_set.themes[style::DEFAULT_LIGHT_THEME];
+ let dummy_highlighter = HighlightLines::new(default_syntax, dummy_theme);
Self {
minus_lines: Vec::new(),
plus_lines: Vec::new(),
output_buffer: String::new(),
- syntax: None,
+ syntax: default_syntax,
highlighter: dummy_highlighter,
writer,
config,
}
}
- pub fn reset_highlighter(&mut self) {
+ pub fn set_syntax(&mut self, extension: Option<&str>) {
+ self.syntax = Painter::get_syntax(self.config.syntax_set, extension);
+ }
+
+ fn get_syntax(syntax_set: &'a SyntaxSet, extension: Option<&str>) -> &'a SyntaxReference {
+ syntax_set
+ .find_syntax_by_extension(extension.unwrap_or("txt"))
+ .unwrap_or_else(|| Painter::get_syntax(syntax_set, Some("txt")))
+ }
+
+ pub fn set_highlighter(&mut self) {
if let Some(theme) = self.config.theme {
- self.highlighter = HighlightLines::new(self.syntax.unwrap(), theme)
+ self.highlighter = HighlightLines::new(self.syntax, theme)
};
}