on master help sign in
Major restructuring of theme/syntax handling
commit06b7be7ee90a336bd1f51deea1e90f357cb01916 authorsharkdp <davidpeter@web.de> dateSat 21 Mar 2020, 16:48 (7 years ago) parentb1b8add
10 files changed+84−72
examples/simple.rs+1−1 src/assets.rs+8−45 src/bin/bat/assets.rs+37 src/bin/bat/config.rs+1−1 src/dirs.rs → src/bin/bat/directories.rs+7−5 src/bin/bat/main.rs+12−6 src/lib.rs−4 src/line_range.rs+16−8 tests/no_duplicate_extensions.rs+1−1 tests/syntax_detection.rs+1−1
examples/simple.rs+1−1
@@ -29,7 +29,7 @@ fn main() {
2929 theme: "1337".into(),
3030 ..Default::default()
3131 };
32− let assets = HighlightingAssets::new();
32+ let assets = HighlightingAssets::from_binary();
3333
3434 Controller::new(&config, &assets).run().expect("no errors");
3535}
src/assets.rs+8−45
@@ -1,15 +1,12 @@
1−use std::borrow::Cow;
21use std::collections::BTreeMap;
32use std::fs::{self, File};
43use std::io::BufReader;
5−use std::path::{Path, PathBuf};
4+use std::path::Path;
65
76use syntect::dumps::{dump_to_file, from_binary, from_reader};
87use syntect::highlighting::{Theme, ThemeSet};
98use syntect::parsing::{SyntaxReference, SyntaxSet, SyntaxSetBuilder};
109
11−use crate::dirs::PROJECT_DIRS;
12−
1310use crate::errors::*;
1411use crate::inputfile::{InputFile, InputFileReader};
1512use crate::syntax_mapping::SyntaxMapping;
@@ -23,13 +20,7 @@ pub struct HighlightingAssets {
2320}
2421
2522impl HighlightingAssets {
26− pub fn new() -> Self {
27− Self::from_cache().unwrap_or_else(|_| Self::from_binary())
28− }
29−
30− pub fn from_files(dir: Option<&Path>, start_empty: bool) -> Result<Self> {
31− let source_dir = dir.unwrap_or_else(|| PROJECT_DIRS.config_dir());
32−
23+ pub fn from_files(source_dir: &Path, start_empty: bool) -> Result<Self> {
3324 let mut theme_set = if start_empty {
3425 ThemeSet {
3526 themes: BTreeMap::new(),
@@ -72,12 +63,11 @@ impl HighlightingAssets {
7263 })
7364 }
7465
75− fn from_cache() -> Result<Self> {
76− let theme_set_path = theme_set_path();
77− let syntax_set_file = File::open(&syntax_set_path()).chain_err(|| {
66+ pub fn from_cache(theme_set_path: &Path, syntax_set_path: &Path) -> Result<Self> {
67+ let syntax_set_file = File::open(syntax_set_path).chain_err(|| {
7868 format!(
7969 "Could not load cached syntax set '{}'",
80− syntax_set_path().to_string_lossy()
70+ syntax_set_path.to_string_lossy()
8171 )
8272 })?;
8373 let syntax_set: SyntaxSet = from_reader(BufReader::new(syntax_set_file))
@@ -106,7 +96,7 @@ impl HighlightingAssets {
10696 from_binary(include_bytes!("../assets/themes.bin"))
10797 }
10898
109− fn from_binary() -> Self {
99+ pub fn from_binary() -> Self {
110100 let syntax_set = Self::get_integrated_syntaxset();
111101 let theme_set = Self::get_integrated_themeset();
112102
@@ -116,8 +106,7 @@ impl HighlightingAssets {
116106 }
117107 }
118108
119− pub fn save(&self, dir: Option<&Path>) -> Result<()> {
120− let target_dir = dir.unwrap_or_else(|| PROJECT_DIRS.cache_dir());
109+ pub fn save(&self, target_dir: &Path) -> Result<()> {
121110 let _ = fs::create_dir_all(target_dir);
122111 let theme_set_path = target_dir.join("themes.bin");
123112 let syntax_set_path = target_dir.join("syntaxes.bin");
@@ -203,30 +192,4 @@ impl HighlightingAssets {
203192
204193 syntax.unwrap_or_else(|| self.syntax_set.find_syntax_plain_text())
205194 }
206−}
207−
208−fn theme_set_path() -> PathBuf {
209− PROJECT_DIRS.cache_dir().join("themes.bin")
210−}
211−
212−fn syntax_set_path() -> PathBuf {
213− PROJECT_DIRS.cache_dir().join("syntaxes.bin")
214−}
215−
216−pub fn config_dir() -> Cow<'static, str> {
217− PROJECT_DIRS.config_dir().to_string_lossy()
218−}
219−
220−pub fn cache_dir() -> Cow<'static, str> {
221− PROJECT_DIRS.cache_dir().to_string_lossy()
222−}
223−
224−pub fn clear_assets() {
225− print!("Clearing theme set cache ... ");
226− fs::remove_file(theme_set_path()).ok();
227− println!("okay");
228−
229− print!("Clearing syntax set cache ... ");
230− fs::remove_file(syntax_set_path()).ok();
231− println!("okay");
232−}
195+}
src/bin/bat/assets.rsnew file+37
@@ -0,0 +1,37 @@
1+use std::borrow::Cow;
2+use std::fs;
3+use std::path::PathBuf;
4+
5+use crate::directories::PROJECT_DIRS;
6+
7+use bat::assets::HighlightingAssets;
8+
9+fn theme_set_path() -> PathBuf {
10+ PROJECT_DIRS.cache_dir().join("themes.bin")
11+}
12+
13+fn syntax_set_path() -> PathBuf {
14+ PROJECT_DIRS.cache_dir().join("syntaxes.bin")
15+}
16+
17+pub fn config_dir() -> Cow<'static, str> {
18+ PROJECT_DIRS.config_dir().to_string_lossy()
19+}
20+
21+pub fn cache_dir() -> Cow<'static, str> {
22+ PROJECT_DIRS.cache_dir().to_string_lossy()
23+}
24+
25+pub fn clear_assets() {
26+ print!("Clearing theme set cache ... ");
27+ fs::remove_file(theme_set_path()).ok();
28+ println!("okay");
29+
30+ print!("Clearing syntax set cache ... ");
31+ fs::remove_file(syntax_set_path()).ok();
32+ println!("okay");
33+}
34+
35+pub fn assets_from_cache_or_binary() -> HighlightingAssets {
36+ HighlightingAssets::from_cache(&theme_set_path(), &syntax_set_path()).unwrap_or(HighlightingAssets::from_binary())
37+}
src/bin/bat/config.rs+1−1
@@ -5,7 +5,7 @@ use std::path::PathBuf;
55
66use shell_words;
77
8−use bat::dirs::PROJECT_DIRS;
8+use crate::directories::PROJECT_DIRS;
99
1010pub fn config_file() -> PathBuf {
1111 env::var("BAT_CONFIG_PATH")
src/dirs.rs → src/bin/bat/directories.rsrenamed+7−5
@@ -1,7 +1,9 @@
1−use crate::dirs_rs;
21use std::env;
32use std::path::{Path, PathBuf};
43
4+use dirs;
5+use lazy_static::lazy_static;
6+
57/// Wrapper for 'dirs' that treats MacOS more like Linux, by following the XDG specification.
68/// This means that the `XDG_CACHE_HOME` and `XDG_CONFIG_HOME` environment variables are
79/// checked first. The fallback directories are `~/.cache/bat` and `~/.config/bat`, respectively.
@@ -18,10 +20,10 @@ impl BatProjectDirs {
1820 let config_dir_op = env::var_os("XDG_CONFIG_HOME")
1921 .map(PathBuf::from)
2022 .filter(|p| p.is_absolute())
21− .or_else(|| dirs_rs::home_dir().map(|d| d.join(".config")));
23+ .or_else(|| dirs::home_dir().map(|d| d.join(".config")));
2224
2325 #[cfg(not(target_os = "macos"))]
24− let config_dir_op = dirs_rs::config_dir();
26+ let config_dir_op = dirs::config_dir();
2527
2628 let config_dir = config_dir_op.map(|d| d.join("bat"))?;
2729
@@ -42,10 +44,10 @@ impl BatProjectDirs {
4244 let cache_dir_op = env::var_os("XDG_CACHE_HOME")
4345 .map(PathBuf::from)
4446 .filter(|p| p.is_absolute())
45− .or_else(|| dirs_rs::home_dir().map(|d| d.join(".cache")));
47+ .or_else(|| dirs::home_dir().map(|d| d.join(".cache")));
4648
4749 #[cfg(not(target_os = "macos"))]
48− let cache_dir_op = dirs_rs::cache_dir();
50+ let cache_dir_op = dirs::cache_dir();
4951
5052 cache_dir_op.map(|d| d.join("bat"))
5153 }
src/bin/bat/main.rs+12−6
@@ -4,9 +4,13 @@
44#[macro_use]
55extern crate clap;
66
7+extern crate dirs as dirs_rs;
8+
79mod app;
810mod clap_app;
911mod config;
12+mod directories;
13+mod assets;
1014
1115use std::collections::HashSet;
1216use std::ffi::OsStr;
@@ -19,10 +23,12 @@ use ansi_term::Colour::Green;
1923use ansi_term::Style;
2024
2125use crate::{app::App, config::config_file};
26+use directories::PROJECT_DIRS;
27+use assets::{cache_dir, clear_assets, config_dir, assets_from_cache_or_binary};
2228use bat::controller::Controller;
2329
2430use bat::{
25− assets::{cache_dir, clear_assets, config_dir, HighlightingAssets},
31+ assets::HighlightingAssets,
2632 errors::*,
2733 inputfile::InputFile,
2834 style::{OutputComponent, OutputComponents},
@@ -31,8 +37,8 @@ use bat::{
3137
3238fn run_cache_subcommand(matches: &clap::ArgMatches) -> Result<()> {
3339 if matches.is_present("build") {
34− let source_dir = matches.value_of("source").map(Path::new);
35− let target_dir = matches.value_of("target").map(Path::new);
40+ let source_dir = matches.value_of("source").map(Path::new).unwrap_or_else(|| PROJECT_DIRS.config_dir());
41+ let target_dir = matches.value_of("target").map(Path::new).unwrap_or_else(|| PROJECT_DIRS.cache_dir());
3642
3743 let blank = matches.is_present("blank");
3844
@@ -46,7 +52,7 @@ fn run_cache_subcommand(matches: &clap::ArgMatches) -> Result<()> {
4652}
4753
4854pub fn list_languages(config: &Config) -> Result<()> {
49− let assets = HighlightingAssets::new();
55+ let assets = assets_from_cache_or_binary();
5056 let mut languages = assets
5157 .syntax_set
5258 .syntaxes()
@@ -109,7 +115,7 @@ pub fn list_languages(config: &Config) -> Result<()> {
109115}
110116
111117pub fn list_themes(cfg: &Config) -> Result<()> {
112− let assets = HighlightingAssets::new();
118+ let assets = assets_from_cache_or_binary();
113119 let themes = &assets.theme_set.themes;
114120 let mut config = cfg.clone();
115121 let mut style = HashSet::new();
@@ -141,7 +147,7 @@ pub fn list_themes(cfg: &Config) -> Result<()> {
141147}
142148
143149fn run_controller(config: &Config) -> Result<bool> {
144− let assets = HighlightingAssets::new();
150+ let assets = assets_from_cache_or_binary();
145151 let controller = Controller::new(&config, &assets);
146152 controller.run()
147153}
src/lib.rs−4
@@ -4,9 +4,6 @@
44#[macro_use]
55extern crate error_chain;
66
7−#[macro_use]
8−extern crate lazy_static;
9−
107extern crate ansi_term;
118extern crate atty;
129extern crate console;
@@ -22,7 +19,6 @@ pub mod assets;
2219pub mod controller;
2320mod decorations;
2421mod diff;
25−pub mod dirs;
2622pub mod inputfile;
2723mod less;
2824pub mod line_range;
src/line_range.rs+16−8
@@ -6,20 +6,22 @@ pub struct LineRange {
66 pub upper: usize,
77}
88
9−impl LineRange {
10− pub fn from(range_raw: &str) -> Result<LineRange> {
11− LineRange::parse_range(range_raw)
12− }
13−
14− pub fn new() -> LineRange {
9+impl Default for LineRange {
10+ fn default() -> LineRange {
1511 LineRange {
1612 lower: usize::min_value(),
1713 upper: usize::max_value(),
1814 }
1915 }
16+}
17+
18+impl LineRange {
19+ pub fn from(range_raw: &str) -> Result<LineRange> {
20+ LineRange::parse_range(range_raw)
21+ }
2022
2123 pub fn parse_range(range_raw: &str) -> Result<LineRange> {
22− let mut new_range = LineRange::new();
24+ let mut new_range = LineRange::default();
2325
2426 if range_raw.bytes().nth(0).ok_or("Empty line range")? == b':' {
2527 new_range.upper = range_raw[1..].parse()?;
@@ -103,12 +105,18 @@ pub enum RangeCheckResult {
103105 AfterLastRange,
104106}
105107
106−#[derive(Debug, Clone, Default)]
108+#[derive(Debug, Clone)]
107109pub struct LineRanges {
108110 ranges: Vec<LineRange>,
109111 largest_upper_bound: usize,
110112}
111113
114+impl Default for LineRanges {
115+ fn default() -> Self {
116+ LineRanges::from(vec![LineRange { lower: 0, upper: 0 }])
117+ }
118+}
119+
112120impl LineRanges {
113121 pub fn from(ranges: Vec<LineRange>) -> LineRanges {
114122 let largest_upper_bound = ranges
tests/no_duplicate_extensions.rs+1−1
@@ -19,7 +19,7 @@ fn no_duplicate_extensions() {
1919 "sass",
2020 ];
2121
22− let assets = HighlightingAssets::new();
22+ let assets = HighlightingAssets::from_binary();
2323
2424 let mut extensions = HashSet::new();
2525
tests/syntax_detection.rs+1−1
@@ -18,7 +18,7 @@ struct SyntaxDetectionTest {
1818impl SyntaxDetectionTest {
1919 fn new() -> Self {
2020 SyntaxDetectionTest {
21− assets: HighlightingAssets::new(),
21+ assets: HighlightingAssets::from_binary(),
2222 syntax_mapping: SyntaxMapping::new(),
2323 temp_dir: TempDir::new("bat_syntax_detection_tests")
2424 .expect("creation of temporary directory"),