2018-05-10 10:36:09 +00:00
|
|
|
use directories::ProjectDirs;
|
|
|
|
use errors::*;
|
|
|
|
use std::borrow::Cow;
|
|
|
|
use std::fs::{self, File};
|
2018-05-16 19:22:16 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
2018-05-10 10:36:09 +00:00
|
|
|
use syntect::dumps::{dump_to_file, from_binary, from_reader};
|
|
|
|
use syntect::highlighting::{Theme, ThemeSet};
|
2018-05-18 10:30:30 +00:00
|
|
|
use syntect::parsing::{SyntaxDefinition, SyntaxSet};
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
use std::os::unix::fs::FileTypeExt;
|
2018-05-10 10:36:09 +00:00
|
|
|
|
|
|
|
lazy_static! {
|
2018-08-18 18:44:25 +00:00
|
|
|
static ref PROJECT_DIRS: ProjectDirs =
|
|
|
|
ProjectDirs::from("", "", crate_name!()).expect("Could not get home directory");
|
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-05-16 19:22:16 +00:00
|
|
|
pub fn from_files(dir: Option<&Path>) -> Result<Self> {
|
|
|
|
let source_dir = dir.unwrap_or_else(|| PROJECT_DIRS.config_dir());
|
2018-05-10 10:36:09 +00:00
|
|
|
|
2018-05-16 19:22:16 +00:00
|
|
|
let theme_dir = source_dir.join("themes");
|
2018-05-23 09:31:55 +00:00
|
|
|
let theme_set = ThemeSet::load_from_folder(&theme_dir).chain_err(|| {
|
|
|
|
format!(
|
|
|
|
"Could not load themes from '{}'",
|
|
|
|
theme_dir.to_string_lossy()
|
|
|
|
)
|
|
|
|
})?;
|
2018-05-10 10:36:09 +00:00
|
|
|
let mut syntax_set = SyntaxSet::new();
|
2018-05-16 19:22:16 +00:00
|
|
|
let syntax_dir = source_dir.join("syntaxes");
|
|
|
|
if !syntax_dir.exists() {
|
|
|
|
return Err(format!(
|
|
|
|
"Could not load syntaxes from '{}'",
|
|
|
|
syntax_dir.to_string_lossy()
|
|
|
|
).into());
|
|
|
|
}
|
2018-05-25 20:12:30 +00:00
|
|
|
syntax_set.load_syntaxes(syntax_dir, true)?;
|
2018-05-10 10:36:09 +00:00
|
|
|
syntax_set.load_plain_text_syntax();
|
|
|
|
|
|
|
|
Ok(HighlightingAssets {
|
|
|
|
syntax_set,
|
|
|
|
theme_set,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
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-05-16 20:23:53 +00:00
|
|
|
let mut syntax_set: SyntaxSet =
|
|
|
|
from_reader(syntax_set_file).chain_err(|| "Could not parse cached syntax set")?;
|
2018-05-10 10:36:09 +00:00
|
|
|
syntax_set.link_syntaxes();
|
|
|
|
|
|
|
|
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-05-16 20:23:53 +00:00
|
|
|
let theme_set: ThemeSet =
|
|
|
|
from_reader(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,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_binary() -> Self {
|
2018-05-16 19:22:16 +00:00
|
|
|
let mut syntax_set: SyntaxSet = from_binary(include_bytes!("../assets/syntaxes.bin"));
|
2018-05-10 10:36:09 +00:00
|
|
|
syntax_set.link_syntaxes();
|
2018-05-16 19:22:16 +00:00
|
|
|
let theme_set: ThemeSet = from_binary(include_bytes!("../assets/themes.bin"));
|
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());
|
|
|
|
let _ = fs::create_dir(target_dir);
|
|
|
|
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
|
|
|
|
);
|
|
|
|
&self.theme_set.themes["Default"]
|
|
|
|
}
|
|
|
|
}
|
2018-07-21 06:26:24 +00:00
|
|
|
}
|
|
|
|
|
2018-05-19 09:46:41 +00:00
|
|
|
pub fn get_syntax(&self, language: Option<&str>, filename: Option<&str>) -> &SyntaxDefinition {
|
2018-05-18 10:30:30 +00:00
|
|
|
let syntax = match (language, filename) {
|
|
|
|
(Some(language), _) => self.syntax_set.find_syntax_by_token(language),
|
|
|
|
(None, Some(filename)) => {
|
|
|
|
#[cfg(not(unix))]
|
|
|
|
let may_read_from_file = true;
|
|
|
|
|
|
|
|
// Do not peek at the file (to determine the syntax) if it is a FIFO because they can
|
|
|
|
// only be read once.
|
|
|
|
#[cfg(unix)]
|
2018-05-19 09:46:41 +00:00
|
|
|
let may_read_from_file = !fs::metadata(filename)
|
|
|
|
.map(|m| m.file_type().is_fifo())
|
|
|
|
.unwrap_or(false);
|
2018-05-18 10:30:30 +00:00
|
|
|
|
|
|
|
if may_read_from_file {
|
2018-05-19 09:46:41 +00:00
|
|
|
self.syntax_set
|
|
|
|
.find_syntax_for_file(filename)
|
|
|
|
.unwrap_or(None)
|
2018-05-18 10:30:30 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(None, None) => None,
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
|
|
|
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");
|
|
|
|
}
|