2018-05-10 10:36:09 +00:00
|
|
|
use std::borrow::Cow;
|
2018-08-20 19:12:52 +00:00
|
|
|
use std::collections::BTreeMap;
|
2018-05-10 10:36:09 +00:00
|
|
|
use std::fs::{self, File};
|
2018-10-09 19:18:40 +00:00
|
|
|
use std::io::BufReader;
|
2018-05-16 19:22:16 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
2018-10-09 19:18:40 +00:00
|
|
|
|
2018-05-10 10:36:09 +00:00
|
|
|
use syntect::dumps::{dump_to_file, from_binary, from_reader};
|
|
|
|
use syntect::highlighting::{Theme, ThemeSet};
|
2018-10-09 19:18:40 +00:00
|
|
|
use syntect::parsing::{SyntaxReference, SyntaxSet, SyntaxSetBuilder};
|
|
|
|
|
2019-03-08 10:48:22 +00:00
|
|
|
use crate::dirs::PROJECT_DIRS;
|
2018-05-18 10:30:30 +00:00
|
|
|
|
2019-03-08 10:48:22 +00:00
|
|
|
use crate::errors::*;
|
|
|
|
use crate::inputfile::{InputFile, InputFileReader};
|
|
|
|
use crate::syntax_mapping::SyntaxMapping;
|
2018-08-28 18:12:45 +00:00
|
|
|
|
2018-08-20 19:39:21 +00:00
|
|
|
pub const BAT_THEME_DEFAULT: &str = "Monokai Extended";
|
|
|
|
|
2018-05-10 10:36:09 +00:00
|
|
|
pub struct HighlightingAssets {
|
|
|
|
pub syntax_set: SyntaxSet,
|
|
|
|
pub theme_set: ThemeSet,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl HighlightingAssets {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self::from_cache().unwrap_or_else(|_| Self::from_binary())
|
|
|
|
}
|
|
|
|
|
2018-08-20 19:12:52 +00:00
|
|
|
pub fn from_files(dir: Option<&Path>, start_empty: bool) -> Result<Self> {
|
2018-05-16 19:22:16 +00:00
|
|
|
let source_dir = dir.unwrap_or_else(|| PROJECT_DIRS.config_dir());
|
2018-05-10 10:36:09 +00:00
|
|
|
|
2018-10-09 19:18:40 +00:00
|
|
|
let mut theme_set = if start_empty {
|
|
|
|
ThemeSet {
|
|
|
|
themes: BTreeMap::new(),
|
|
|
|
}
|
2018-08-20 19:12:52 +00:00
|
|
|
} else {
|
2018-10-09 19:18:40 +00:00
|
|
|
Self::get_integrated_themeset()
|
2018-08-20 19:12:52 +00:00
|
|
|
};
|
2018-08-19 08:49:09 +00:00
|
|
|
|
2018-05-16 19:22:16 +00:00
|
|
|
let theme_dir = source_dir.join("themes");
|
2018-08-19 08:49:09 +00:00
|
|
|
|
2018-10-09 19:18:40 +00:00
|
|
|
let res = theme_set.add_from_folder(&theme_dir);
|
2019-03-08 10:46:49 +00:00
|
|
|
if res.is_err() {
|
2018-08-19 08:49:09 +00:00
|
|
|
println!(
|
|
|
|
"No themes were found in '{}', using the default set",
|
2018-05-23 09:31:55 +00:00
|
|
|
theme_dir.to_string_lossy()
|
2018-08-19 08:49:09 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-10-09 19:18:40 +00:00
|
|
|
let mut syntax_set_builder = if start_empty {
|
|
|
|
let mut builder = SyntaxSetBuilder::new();
|
|
|
|
builder.add_plain_text_syntax();
|
|
|
|
builder
|
|
|
|
} else {
|
|
|
|
Self::get_integrated_syntaxset().into_builder()
|
|
|
|
};
|
|
|
|
|
2018-05-16 19:22:16 +00:00
|
|
|
let syntax_dir = source_dir.join("syntaxes");
|
2018-08-19 08:49:09 +00:00
|
|
|
if syntax_dir.exists() {
|
2018-10-09 19:18:40 +00:00
|
|
|
syntax_set_builder.add_from_folder(syntax_dir, true)?;
|
2018-08-19 08:49:09 +00:00
|
|
|
} else {
|
|
|
|
println!(
|
|
|
|
"No syntaxes were found in '{}', using the default set.",
|
2018-05-16 19:22:16 +00:00
|
|
|
syntax_dir.to_string_lossy()
|
2018-08-19 08:49:09 +00:00
|
|
|
);
|
2018-05-16 19:22:16 +00:00
|
|
|
}
|
2018-05-10 10:36:09 +00:00
|
|
|
|
2018-10-09 19:18:40 +00:00
|
|
|
Ok(HighlightingAssets {
|
|
|
|
syntax_set: syntax_set_builder.build(),
|
|
|
|
theme_set,
|
|
|
|
})
|
2018-05-10 10:36:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn from_cache() -> Result<Self> {
|
|
|
|
let theme_set_path = theme_set_path();
|
|
|
|
let syntax_set_file = File::open(&syntax_set_path()).chain_err(|| {
|
|
|
|
format!(
|
|
|
|
"Could not load cached syntax set '{}'",
|
|
|
|
syntax_set_path().to_string_lossy()
|
|
|
|
)
|
|
|
|
})?;
|
2018-10-09 19:18:40 +00:00
|
|
|
let syntax_set: SyntaxSet = from_reader(BufReader::new(syntax_set_file))
|
|
|
|
.chain_err(|| "Could not parse cached syntax set")?;
|
2018-05-10 10:36:09 +00:00
|
|
|
|
|
|
|
let theme_set_file = File::open(&theme_set_path).chain_err(|| {
|
|
|
|
format!(
|
|
|
|
"Could not load cached theme set '{}'",
|
|
|
|
theme_set_path.to_string_lossy()
|
|
|
|
)
|
|
|
|
})?;
|
2018-10-09 19:18:40 +00:00
|
|
|
let theme_set: ThemeSet = from_reader(BufReader::new(theme_set_file))
|
|
|
|
.chain_err(|| "Could not parse cached theme set")?;
|
2018-05-10 10:36:09 +00:00
|
|
|
|
|
|
|
Ok(HighlightingAssets {
|
|
|
|
syntax_set,
|
|
|
|
theme_set,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-10-09 19:18:40 +00:00
|
|
|
fn get_integrated_syntaxset() -> SyntaxSet {
|
|
|
|
from_binary(include_bytes!("../assets/syntaxes.bin"))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_integrated_themeset() -> ThemeSet {
|
|
|
|
from_binary(include_bytes!("../assets/themes.bin"))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_binary() -> Self {
|
|
|
|
let syntax_set = Self::get_integrated_syntaxset();
|
|
|
|
let theme_set = Self::get_integrated_themeset();
|
2018-05-10 10:36:09 +00:00
|
|
|
|
|
|
|
HighlightingAssets {
|
|
|
|
syntax_set,
|
|
|
|
theme_set,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-16 19:22:16 +00:00
|
|
|
pub fn save(&self, dir: Option<&Path>) -> Result<()> {
|
|
|
|
let target_dir = dir.unwrap_or_else(|| PROJECT_DIRS.cache_dir());
|
2019-05-30 15:47:40 +00:00
|
|
|
let _ = fs::create_dir_all(target_dir);
|
2018-05-16 19:22:16 +00:00
|
|
|
let theme_set_path = target_dir.join("themes.bin");
|
|
|
|
let syntax_set_path = target_dir.join("syntaxes.bin");
|
2018-05-10 10:36:09 +00:00
|
|
|
|
|
|
|
print!(
|
|
|
|
"Writing theme set to {} ... ",
|
|
|
|
theme_set_path.to_string_lossy()
|
|
|
|
);
|
2018-05-16 20:23:53 +00:00
|
|
|
dump_to_file(&self.theme_set, &theme_set_path).chain_err(|| {
|
|
|
|
format!(
|
|
|
|
"Could not save theme set to {}",
|
|
|
|
theme_set_path.to_string_lossy()
|
2018-05-10 10:36:09 +00:00
|
|
|
)
|
|
|
|
})?;
|
|
|
|
println!("okay");
|
|
|
|
|
|
|
|
print!(
|
|
|
|
"Writing syntax set to {} ... ",
|
|
|
|
syntax_set_path.to_string_lossy()
|
|
|
|
);
|
2018-05-16 20:23:53 +00:00
|
|
|
dump_to_file(&self.syntax_set, &syntax_set_path).chain_err(|| {
|
|
|
|
format!(
|
|
|
|
"Could not save syntax set to {}",
|
|
|
|
syntax_set_path.to_string_lossy()
|
2018-05-10 10:36:09 +00:00
|
|
|
)
|
|
|
|
})?;
|
|
|
|
println!("okay");
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-07-23 19:38:45 +00:00
|
|
|
pub fn get_theme(&self, theme: &str) -> &Theme {
|
|
|
|
match self.theme_set.themes.get(theme) {
|
|
|
|
Some(theme) => theme,
|
|
|
|
None => {
|
|
|
|
use ansi_term::Colour::Yellow;
|
|
|
|
eprintln!(
|
|
|
|
"{}: Unknown theme '{}', using default.",
|
|
|
|
Yellow.paint("[bat warning]"),
|
|
|
|
theme
|
|
|
|
);
|
2018-08-20 19:39:21 +00:00
|
|
|
&self.theme_set.themes[BAT_THEME_DEFAULT]
|
2018-07-23 19:38:45 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-21 06:26:24 +00:00
|
|
|
}
|
|
|
|
|
2018-10-07 11:26:50 +00:00
|
|
|
pub fn get_syntax(
|
|
|
|
&self,
|
|
|
|
language: Option<&str>,
|
|
|
|
filename: InputFile,
|
|
|
|
reader: &mut InputFileReader,
|
2018-10-17 20:30:09 +00:00
|
|
|
mapping: &SyntaxMapping,
|
2018-10-09 19:18:40 +00:00
|
|
|
) -> &SyntaxReference {
|
2018-05-18 10:30:30 +00:00
|
|
|
let syntax = match (language, filename) {
|
|
|
|
(Some(language), _) => self.syntax_set.find_syntax_by_token(language),
|
2018-08-28 18:12:45 +00:00
|
|
|
(None, InputFile::Ordinary(filename)) => {
|
2018-10-07 11:26:50 +00:00
|
|
|
let path = Path::new(filename);
|
|
|
|
let file_name = path.file_name().and_then(|n| n.to_str()).unwrap_or("");
|
|
|
|
let extension = path.extension().and_then(|x| x.to_str()).unwrap_or("");
|
2018-10-17 20:30:09 +00:00
|
|
|
|
|
|
|
let file_name = mapping.replace(file_name);
|
|
|
|
let extension = mapping.replace(extension);
|
|
|
|
|
2018-10-07 11:26:50 +00:00
|
|
|
let ext_syntax = self
|
|
|
|
.syntax_set
|
2018-10-17 20:30:09 +00:00
|
|
|
.find_syntax_by_extension(&file_name)
|
|
|
|
.or_else(|| self.syntax_set.find_syntax_by_extension(&extension));
|
2018-10-07 11:26:50 +00:00
|
|
|
let line_syntax = if ext_syntax.is_none() {
|
2018-10-07 11:47:54 +00:00
|
|
|
String::from_utf8(reader.first_line.clone())
|
2018-10-07 11:26:50 +00:00
|
|
|
.ok()
|
|
|
|
.and_then(|l| self.syntax_set.find_syntax_by_first_line(&l))
|
2018-05-18 10:30:30 +00:00
|
|
|
} else {
|
|
|
|
None
|
2018-10-07 11:26:50 +00:00
|
|
|
};
|
2019-03-08 10:46:49 +00:00
|
|
|
|
|
|
|
ext_syntax.or(line_syntax)
|
2018-05-18 10:30:30 +00:00
|
|
|
}
|
2018-10-07 12:31:23 +00:00
|
|
|
(None, InputFile::StdIn) => String::from_utf8(reader.first_line.clone())
|
|
|
|
.ok()
|
|
|
|
.and_then(|l| self.syntax_set.find_syntax_by_first_line(&l)),
|
2018-08-28 18:12:45 +00:00
|
|
|
(_, InputFile::ThemePreviewFile) => self.syntax_set.find_syntax_by_name("Rust"),
|
2018-05-18 10:30:30 +00:00
|
|
|
};
|
|
|
|
|
2018-05-19 09:46:41 +00:00
|
|
|
syntax.unwrap_or_else(|| self.syntax_set.find_syntax_plain_text())
|
2018-05-18 10:30:30 +00:00
|
|
|
}
|
2018-05-10 10:36:09 +00:00
|
|
|
}
|
|
|
|
|
2018-05-21 20:29:03 +00:00
|
|
|
fn theme_set_path() -> PathBuf {
|
2018-05-16 19:22:16 +00:00
|
|
|
PROJECT_DIRS.cache_dir().join("themes.bin")
|
2018-05-10 10:36:09 +00:00
|
|
|
}
|
|
|
|
|
2018-05-21 20:29:03 +00:00
|
|
|
fn syntax_set_path() -> PathBuf {
|
2018-05-16 19:22:16 +00:00
|
|
|
PROJECT_DIRS.cache_dir().join("syntaxes.bin")
|
2018-05-10 10:36:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn config_dir() -> Cow<'static, str> {
|
|
|
|
PROJECT_DIRS.config_dir().to_string_lossy()
|
|
|
|
}
|
2018-05-21 20:29:03 +00:00
|
|
|
|
2019-02-07 20:19:20 +00:00
|
|
|
pub fn cache_dir() -> Cow<'static, str> {
|
|
|
|
PROJECT_DIRS.cache_dir().to_string_lossy()
|
|
|
|
}
|
|
|
|
|
2018-05-21 20:29:03 +00:00
|
|
|
pub fn clear_assets() {
|
|
|
|
print!("Clearing theme set cache ... ");
|
|
|
|
fs::remove_file(theme_set_path()).ok();
|
|
|
|
println!("okay");
|
|
|
|
|
|
|
|
print!("Clearing syntax set cache ... ");
|
|
|
|
fs::remove_file(syntax_set_path()).ok();
|
|
|
|
println!("okay");
|
|
|
|
}
|