diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2020-11-13 18:33:41 +0100 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2020-11-13 18:33:51 +0100 |
commit | 20cb64970dae30bacc58fa7af8ded258142e316a (patch) | |
tree | 4db2c316d70a6d111dcfaa44cf946393fd2eac0f | |
parent | 2dee992fe19cc32a2a769b9674c7876245518e5f (diff) |
Make highlighting optional
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
-rw-r--r-- | config.toml | 4 | ||||
-rw-r--r-- | src/commands/db.rs | 43 | ||||
-rw-r--r-- | src/config/not_validated.rs | 29 | ||||
-rw-r--r-- | src/config/util.rs | 4 |
4 files changed, 41 insertions, 39 deletions
diff --git a/config.toml b/config.toml index 6dfbbff..add5c22 100644 --- a/config.toml +++ b/config.toml @@ -17,13 +17,15 @@ progress_format = "[{elapsed_precise}] ({percent:>3}%): {bar:40.cyan/blue} | {ms # a container. # # Valid values: -# "base16-ocean.dark" (default if key is not present) +# "base16-ocean.dark" # "base16-eighties.dark" # "base16-mocha.dark" # "base16-ocean.light" # "InspiredGitHub" # "Solarized (dark)" # "Solarized (light)" +# +# If the value is not set, highlighting is disabled. script_highlight_theme = "Solarized (dark)" # The format to print the found packages with. diff --git a/src/commands/db.rs b/src/commands/db.rs index 7aa808c..6c373b3 100644 --- a/src/commands/db.rs +++ b/src/commands/db.rs @@ -13,6 +13,10 @@ use diesel::JoinOnDsl; use diesel::QueryDsl; use diesel::RunQueryDsl; use itertools::Itertools; +use syntect::easy::HighlightLines; +use syntect::highlighting::{ThemeSet, Style}; +use syntect::parsing::SyntaxSet; +use syntect::util::{as_24_bit_terminal_escaped, LinesWithEndings}; use crate::config::Configuration; use crate::db::DbConnectionConfig; @@ -327,26 +331,25 @@ fn job<'a>(conn_cfg: DbConnectionConfig, config: &Configuration<'a>, matches: &A script_len = format!("{:<4}", data.0.script_text.len()).cyan(), log_len = format!("{:<4}", data.0.log_text.len()).cyan(), script_text = if show_script { - use syntect::easy::HighlightLines; - use syntect::parsing::SyntaxSet; - use syntect::highlighting::{ThemeSet, Style}; - use syntect::util::{as_24_bit_terminal_escaped, LinesWithEndings}; - - // Load these once at the start of your program - let ps = SyntaxSet::load_defaults_newlines(); - let ts = ThemeSet::load_defaults(); - - let syntax = ps.find_syntax_by_first_line(&data.0.script_text).ok_or_else(|| anyhow!("Failed to load syntax for highlighting script"))?; - - let theme = ts.themes.get(configured_theme) - .ok_or_else(|| anyhow!("Theme not available: {}", configured_theme))?; - let mut h = HighlightLines::new(syntax, &theme); - LinesWithEndings::from(&data.0.script_text) - .map(|line| { - let ranges: Vec<(Style, &str)> = h.highlight(line, &ps); - as_24_bit_terminal_escaped(&ranges[..], true) - }) - .join("\n") + if let Some(configured_theme) = configured_theme { + // Load these once at the start of your program + let ps = SyntaxSet::load_defaults_newlines(); + let ts = ThemeSet::load_defaults(); + + let syntax = ps.find_syntax_by_first_line(&data.0.script_text).ok_or_else(|| anyhow!("Failed to load syntax for highlighting script"))?; + + let theme = ts.themes.get(configured_theme) + .ok_or_else(|| anyhow!("Theme not available: {}", configured_theme))?; + let mut h = HighlightLines::new(syntax, &theme); + LinesWithEndings::from(&data.0.script_text) + .map(|line| { + let ranges: Vec<(Style, &str)> = h.highlight(line, &ps); + as_24_bit_terminal_escaped(&ranges[..], true) + }) + .join("\n") + } else { + data.0.script_text.clone() + } } else { String::from("<script hidden>") }, diff --git a/src/config/not_validated.rs b/src/config/not_validated.rs index 456cc2a..87bb816 100644 --- a/src/config/not_validated.rs +++ b/src/config/not_validated.rs @@ -24,9 +24,8 @@ pub struct NotValidatedConfiguration { #[getset(get = "pub")] package_print_format: String, - #[serde(default = "default_script_highlight_theme")] #[getset(get = "pub")] - script_highlight_theme: String, + script_highlight_theme: Option<String>, #[serde(rename = "releases")] releases_directory: String, @@ -71,18 +70,20 @@ pub struct NotValidatedConfiguration { impl<'reg> NotValidatedConfiguration { pub fn validate(self) -> Result<Configuration<'reg>> { // TODO: Implement proper validation - let allowed_theme_present = [ - "base16-ocean.dark", - "base16-eighties.dark", - "base16-mocha.dark", - "base16-ocean.light", - "InspiredGitHub", - "Solarized (dark)", - "Solarized (light)", - ].into_iter().any(|allowed_theme| self.script_highlight_theme == *allowed_theme); - - if !allowed_theme_present { - return Err(anyhow!("Theme not known: {}", self.script_highlight_theme)) + if let Some(configured_theme) = self.script_highlight_theme.as_ref() { + let allowed_theme_present = [ + "base16-ocean.dark", + "base16-eighties.dark", + "base16-mocha.dark", + "base16-ocean.light", + "InspiredGitHub", + "Solarized (dark)", + "Solarized (light)", + ].into_iter().any(|allowed_theme| configured_theme == *allowed_theme); + + if !allowed_theme_present { + return Err(anyhow!("Theme not known: {}", configured_theme)) + } } let hb = { diff --git a/src/config/util.rs b/src/config/util.rs index 38939f0..17aeadf 100644 --- a/src/config/util.rs +++ b/src/config/util.rs @@ -15,7 +15,3 @@ pub fn default_package_print_format() -> String { "#)) } -pub fn default_script_highlight_theme() -> String { - String::from("base16-ocean.dark") -} - |