summaryrefslogtreecommitdiffstats
path: root/src/ui.rs
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2020-12-08 11:25:39 +0100
committerMatthias Beyer <mail@beyermatthias.de>2020-12-08 11:25:39 +0100
commite195a937625ad20e94466cba819e35055e51834e (patch)
tree25e73221cefa227612db354ee638ea51351ff15d /src/ui.rs
parentbbebc19ce4e33430884f12d3e04b66abf3183dfe (diff)
Move script highlighting to helper function
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
Diffstat (limited to 'src/ui.rs')
-rw-r--r--src/ui.rs60
1 files changed, 59 insertions, 1 deletions
diff --git a/src/ui.rs b/src/ui.rs
index 59fd90c..cb44c49 100644
--- a/src/ui.rs
+++ b/src/ui.rs
@@ -7,8 +7,13 @@ use std::path::Path;
use anyhow::Error;
use anyhow::Result;
use anyhow::anyhow;
-use log::error;
use handlebars::Handlebars;
+use itertools::Itertools;
+use log::error;
+use syntect::easy::HighlightLines;
+use syntect::highlighting::{ThemeSet, Style};
+use syntect::parsing::SyntaxSet;
+use syntect::util::{as_24_bit_terminal_escaped, LinesWithEndings};
use crate::package::Package;
use crate::package::ScriptBuilder;
@@ -107,3 +112,56 @@ fn print_package(out: &mut dyn Write,
.and_then(|r| writeln!(out, "{}", r).map_err(Error::from))
}
+pub fn script_to_printable(script: &str,
+ highlight: bool,
+ highlight_theme: Option<&String>,
+ line_numbers: bool)
+ -> Result<String>
+{
+ if highlight {
+ if let Some(configured_theme) = highlight_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(script)
+ .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);
+
+ let output = LinesWithEndings::from(script)
+ .enumerate()
+ .map(|(i, line)| {
+ let ranges: Vec<(Style, &str)> = h.highlight(line, &ps);
+ if line_numbers {
+ format!("{:>4} | {}", i, as_24_bit_terminal_escaped(&ranges[..], true))
+ } else {
+ as_24_bit_terminal_escaped(&ranges[..], true)
+ }
+ })
+ .join("");
+
+ Ok(output)
+ } else {
+ Err(anyhow!("Highlighting for script enabled, but no theme configured"))
+ }
+ } else {
+ if line_numbers {
+ Ok({
+ script.lines()
+ .enumerate()
+ .map(|(i, s)| format!("{:>4} | {}", i, s))
+ .join("\n")
+ })
+ } else {
+ Ok(script.to_string())
+ }
+ }
+}
+