summaryrefslogtreecommitdiffstats
path: root/src/bat
diff options
context:
space:
mode:
authordata-pup <16364986+data-pup@users.noreply.github.com>2019-12-23 20:39:48 -0500
committerdata-pup <16364986+data-pup@users.noreply.github.com>2019-12-23 20:45:13 -0500
commit19c2db538760694feaf194db9d59e1435c83ca9e (patch)
tree7fd125bea74b665b9064d35d7146c00935dceb16 /src/bat
parent13e39b1de9ef0a8324b882fade04eeff65241f40 (diff)
read themes from bat's cache as well
Diffstat (limited to 'src/bat')
-rw-r--r--src/bat/assets.rs45
-rw-r--r--src/bat/dirs.rs42
-rw-r--r--src/bat/mod.rs1
3 files changed, 85 insertions, 3 deletions
diff --git a/src/bat/assets.rs b/src/bat/assets.rs
index b8bd4561..f436fef1 100644
--- a/src/bat/assets.rs
+++ b/src/bat/assets.rs
@@ -1,14 +1,19 @@
// Based on code from https://github.com/sharkdp/bat a1b9334a44a2c652f52dddaa83dbacba57372468
// See src/bat/LICENSE
-use std::io::{self, Write};
+use std::fs::File;
+use std::io::{self, BufReader, Write};
+use std::path::PathBuf;
use ansi_term::Colour::Green;
use ansi_term::Style;
-use syntect::dumps::from_binary;
+use syntect::dumps::{from_binary, from_reader};
use syntect::highlighting::ThemeSet;
use syntect::parsing::SyntaxSet;
+use crate::bat::dirs::PROJECT_DIRS;
+use crate::errors::*;
+
pub struct HighlightingAssets {
pub syntax_set: SyntaxSet,
pub theme_set: ThemeSet,
@@ -16,7 +21,7 @@ pub struct HighlightingAssets {
impl HighlightingAssets {
pub fn new() -> Self {
- Self::from_binary()
+ Self::from_cache().unwrap_or_else(|_| Self::from_binary())
}
fn get_integrated_syntaxset() -> SyntaxSet {
@@ -27,6 +32,32 @@ impl HighlightingAssets {
from_binary(include_bytes!("../../assets/themes.bin"))
}
+ 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()
+ )
+ })?;
+ let syntax_set: SyntaxSet = from_reader(BufReader::new(syntax_set_file))
+ .chain_err(|| "Could not parse cached syntax set")?;
+
+ let theme_set_file = File::open(&theme_set_path).chain_err(|| {
+ format!(
+ "Could not load cached theme set '{}'",
+ theme_set_path.to_string_lossy()
+ )
+ })?;
+ let theme_set: ThemeSet = from_reader(BufReader::new(theme_set_file))
+ .chain_err(|| "Could not parse cached theme set")?;
+
+ Ok(HighlightingAssets {
+ syntax_set,
+ theme_set,
+ })
+ }
+
fn from_binary() -> Self {
let syntax_set = Self::get_integrated_syntaxset();
let theme_set = Self::get_integrated_themeset();
@@ -38,6 +69,14 @@ impl HighlightingAssets {
}
}
+fn theme_set_path() -> PathBuf {
+ PROJECT_DIRS.cache_dir().join("themes.bin")
+}
+
+fn syntax_set_path() -> PathBuf {
+ PROJECT_DIRS.cache_dir().join("syntaxes.bin")
+}
+
pub fn list_languages() -> std::io::Result<()> {
let assets = HighlightingAssets::new();
let mut languages = assets
diff --git a/src/bat/dirs.rs b/src/bat/dirs.rs
new file mode 100644
index 00000000..26c49b20
--- /dev/null
+++ b/src/bat/dirs.rs
@@ -0,0 +1,42 @@
+// Based on code from https://github.com/sharkdp/bat e981e974076a926a38f124b7d8746de2ca5f0a28
+// See src/bat/LICENSE
+
+use dirs as dirs_rs;
+use lazy_static::lazy_static;
+use std::path::{Path, PathBuf};
+
+#[cfg(target_os = "macos")]
+use std::env;
+
+/// Wrapper for 'dirs' that treats MacOS more like Linux, by following the XDG specification.
+/// This means that the `XDG_CACHE_HOME` and `XDG_CONFIG_HOME` environment variables are
+/// checked first. The fallback directories are `~/.cache/bat` and `~/.config/bat`, respectively.
+pub struct BatProjectDirs {
+ cache_dir: PathBuf,
+}
+
+impl BatProjectDirs {
+ fn new() -> Option<BatProjectDirs> {
+ #[cfg(target_os = "macos")]
+ let cache_dir_op = env::var_os("XDG_CACHE_HOME")
+ .map(PathBuf::from)
+ .filter(|p| p.is_absolute())
+ .or_else(|| dirs_rs::home_dir().map(|d| d.join(".cache")));
+
+ #[cfg(not(target_os = "macos"))]
+ let cache_dir_op = dirs_rs::cache_dir();
+
+ let cache_dir = cache_dir_op.map(|d| d.join("bat"))?;
+
+ Some(BatProjectDirs { cache_dir })
+ }
+
+ pub fn cache_dir(&self) -> &Path {
+ &self.cache_dir
+ }
+}
+
+lazy_static! {
+ pub static ref PROJECT_DIRS: BatProjectDirs =
+ BatProjectDirs::new().expect("Could not get home directory");
+}
diff --git a/src/bat/mod.rs b/src/bat/mod.rs
index e26ac304..88673f26 100644
--- a/src/bat/mod.rs
+++ b/src/bat/mod.rs
@@ -1,2 +1,3 @@
pub mod assets;
+pub mod dirs;
pub mod output;