1use directories::ProjectDirs;
4use std::fs::{self, File};
7use syntect::dumps::{dump_to_file, from_binary, from_reader};
8use syntect::highlighting::{Theme, ThemeSet};
9use syntect::parsing::SyntaxSet;
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())
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(|_| {
34 "Could not load themes from '{}'",
35 theme_dir.to_string_lossy()
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 {
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(|| {
55 "Could not load cached syntax set '{}'",
56 syntax_set_path().to_string_lossy()
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")
62 syntax_set.link_syntaxes();
64 let theme_set_file = File::open(&theme_set_path).chain_err(|| {
66 "Could not load cached theme set '{}'",
67 theme_set_path.to_string_lossy()
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 {
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"));
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();
97 "Writing theme set to {} ... ",
98 theme_set_path.to_string_lossy()
100 dump_to_file(&self.theme_set, &theme_set_path).map_err(|_| {
102 io::ErrorKind::Other,
104 "Could not save theme set to {}",
105 theme_set_path.to_string_lossy()
112 "Writing syntax set to {} ... ",
113 syntax_set_path.to_string_lossy()
115 dump_to_file(&self.syntax_set, &syntax_set_path).map_err(|_| {
117 io::ErrorKind::Other,
119 "Could not save syntax set to {}",
120 syntax_set_path.to_string_lossy()
129 pub fn get_theme(&self, theme: &str) -> Result<&Theme> {
130 Ok(self.theme_set.themes.get(theme).ok_or_else(|| {
132 io::ErrorKind::Other,
133 format!("Could not find '{}' theme", theme),
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()
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list