on fcecd97 help sign in
bat/src/assets.rs
149 lines 4.5 kB history raw
1use directories::ProjectDirs;
2use errors::*;
3use std::borrow::Cow;
4use std::fs::{self, File};
5use std::io;
6use std::path::PathBuf;
7use syntect::dumps::{dump_to_file, from_binary, from_reader};
8use syntect::highlighting::{Theme, ThemeSet};
9use syntect::parsing::SyntaxSet;
11lazy_static! {
12 static ref PROJECT_DIRS: ProjectDirs = ProjectDirs::from("", "", crate_name!());
15pub struct HighlightingAssets {
16 pub syntax_set: SyntaxSet,
17 pub theme_set: ThemeSet,
20impl HighlightingAssets {
21 pub fn new() -> Self {
22 Self::from_cache().unwrap_or_else(|_| Self::from_binary())
23 }
25 pub fn from_files() -> Result<Self> {
26 let config_dir = PROJECT_DIRS.config_dir();
28 let theme_dir = config_dir.join("themes");
30 let theme_set = ThemeSet::load_from_folder(&theme_dir).map_err(|_| {
31 io::Error::new(
32 io::ErrorKind::Other,
33 format!(
34 "Could not load themes from '{}'",
35 theme_dir.to_string_lossy()
36 ),
37 )
38 })?;
40 let mut syntax_set = SyntaxSet::new();
41 let syntax_dir = config_dir.join("syntax");
42 let _ = syntax_set.load_syntaxes(syntax_dir, true);
43 syntax_set.load_plain_text_syntax();
45 Ok(HighlightingAssets {
46 syntax_set,
47 theme_set,
48 })
49 }
51 fn from_cache() -> Result<Self> {
52 let theme_set_path = theme_set_path();
53 let syntax_set_file = File::open(&syntax_set_path()).chain_err(|| {
54 format!(
55 "Could not load cached syntax set '{}'",
56 syntax_set_path().to_string_lossy()
57 )
58 })?;
59 let mut syntax_set: SyntaxSet = from_reader(syntax_set_file).map_err(|_| {
60 io::Error::new(io::ErrorKind::Other, "Could not parse cached syntax set")
61 })?;
62 syntax_set.link_syntaxes();
64 let theme_set_file = File::open(&theme_set_path).chain_err(|| {
65 format!(
66 "Could not load cached theme set '{}'",
67 theme_set_path.to_string_lossy()
68 )
69 })?;
70 let theme_set: ThemeSet = from_reader(theme_set_file)
71 .map_err(|_| io::Error::new(io::ErrorKind::Other, "Could not parse cached theme set"))?;
73 Ok(HighlightingAssets {
74 syntax_set,
75 theme_set,
76 })
77 }
79 fn from_binary() -> Self {
80 let mut syntax_set: SyntaxSet = from_binary(include_bytes!("../assets/syntax_set"));
81 syntax_set.link_syntaxes();
82 let theme_set: ThemeSet = from_binary(include_bytes!("../assets/theme_set"));
84 HighlightingAssets {
85 syntax_set,
86 theme_set,
87 }
88 }
90 pub fn save(&self) -> Result<()> {
91 let cache_dir = PROJECT_DIRS.cache_dir();
92 let _ = fs::create_dir(cache_dir);
93 let theme_set_path = theme_set_path();
94 let syntax_set_path = syntax_set_path();
96 print!(
97 "Writing theme set to {} ... ",
98 theme_set_path.to_string_lossy()
99 );
100 dump_to_file(&self.theme_set, &theme_set_path).map_err(|_| {
101 io::Error::new(
102 io::ErrorKind::Other,
103 format!(
104 "Could not save theme set to {}",
105 theme_set_path.to_string_lossy()
106 ),
107 )
108 })?;
109 println!("okay");
111 print!(
112 "Writing syntax set to {} ... ",
113 syntax_set_path.to_string_lossy()
114 );
115 dump_to_file(&self.syntax_set, &syntax_set_path).map_err(|_| {
116 io::Error::new(
117 io::ErrorKind::Other,
118 format!(
119 "Could not save syntax set to {}",
120 syntax_set_path.to_string_lossy()
121 ),
122 )
123 })?;
124 println!("okay");
126 Ok(())
127 }
129 pub fn get_theme(&self, theme: &str) -> Result<&Theme> {
130 Ok(self.theme_set.themes.get(theme).ok_or_else(|| {
131 io::Error::new(
132 io::ErrorKind::Other,
133 format!("Could not find '{}' theme", theme),
134 )
135 })?)
136 }
139pub fn theme_set_path() -> PathBuf {
140 PROJECT_DIRS.cache_dir().join("theme_set")
143pub fn syntax_set_path() -> PathBuf {
144 PROJECT_DIRS.cache_dir().join("syntax_set")
147pub fn config_dir() -> Cow<'static, str> {
148 PROJECT_DIRS.config_dir().to_string_lossy()