mirror of
https://github.com/sharkdp/bat.git
synced 2025-03-15 15:18:45 +00:00
Split assets to separate module
This commit is contained in:
parent
01d6218148
commit
5e07eb0e35
156
src/assets.rs
Normal file
156
src/assets.rs
Normal file
@ -0,0 +1,156 @@
|
|||||||
|
use directories::ProjectDirs;
|
||||||
|
use errors::*;
|
||||||
|
use std::borrow::Cow;
|
||||||
|
use std::fs::{self, File};
|
||||||
|
use std::io;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
use syntect::dumps::{dump_to_file, from_binary, from_reader};
|
||||||
|
use syntect::highlighting::{Theme, ThemeSet};
|
||||||
|
use syntect::parsing::SyntaxSet;
|
||||||
|
|
||||||
|
lazy_static! {
|
||||||
|
static ref PROJECT_DIRS: ProjectDirs = ProjectDirs::from("", "", crate_name!());
|
||||||
|
}
|
||||||
|
|
||||||
|
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())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn from_files() -> Result<Self> {
|
||||||
|
let config_dir = PROJECT_DIRS.config_dir();
|
||||||
|
|
||||||
|
let theme_dir = config_dir.join("themes");
|
||||||
|
|
||||||
|
let theme_set = ThemeSet::load_from_folder(&theme_dir).map_err(|_| {
|
||||||
|
io::Error::new(
|
||||||
|
io::ErrorKind::Other,
|
||||||
|
format!(
|
||||||
|
"Could not load themes from '{}'",
|
||||||
|
theme_dir.to_string_lossy()
|
||||||
|
),
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
|
||||||
|
let mut syntax_set = SyntaxSet::new();
|
||||||
|
let syntax_dir = config_dir.join("syntax");
|
||||||
|
let _ = syntax_set.load_syntaxes(syntax_dir, true);
|
||||||
|
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()
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
let mut syntax_set: SyntaxSet = from_reader(syntax_set_file).map_err(|_| {
|
||||||
|
io::Error::new(
|
||||||
|
io::ErrorKind::Other,
|
||||||
|
format!("Could not parse cached syntax set"),
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
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()
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
let theme_set: ThemeSet = from_reader(theme_set_file).map_err(|_| {
|
||||||
|
io::Error::new(
|
||||||
|
io::ErrorKind::Other,
|
||||||
|
format!("Could not parse cached theme set"),
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
|
||||||
|
Ok(HighlightingAssets {
|
||||||
|
syntax_set,
|
||||||
|
theme_set,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn from_binary() -> Self {
|
||||||
|
let mut syntax_set: SyntaxSet = from_binary(include_bytes!("../assets/syntax_set"));
|
||||||
|
syntax_set.link_syntaxes();
|
||||||
|
let theme_set: ThemeSet = from_binary(include_bytes!("../assets/theme_set"));
|
||||||
|
|
||||||
|
HighlightingAssets {
|
||||||
|
syntax_set,
|
||||||
|
theme_set,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn save(&self) -> Result<()> {
|
||||||
|
let cache_dir = PROJECT_DIRS.cache_dir();
|
||||||
|
let _ = fs::create_dir(cache_dir);
|
||||||
|
let theme_set_path = theme_set_path();
|
||||||
|
let syntax_set_path = syntax_set_path();
|
||||||
|
|
||||||
|
print!(
|
||||||
|
"Writing theme set to {} ... ",
|
||||||
|
theme_set_path.to_string_lossy()
|
||||||
|
);
|
||||||
|
dump_to_file(&self.theme_set, &theme_set_path).map_err(|_| {
|
||||||
|
io::Error::new(
|
||||||
|
io::ErrorKind::Other,
|
||||||
|
format!(
|
||||||
|
"Could not save theme set to {}",
|
||||||
|
theme_set_path.to_string_lossy()
|
||||||
|
),
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
println!("okay");
|
||||||
|
|
||||||
|
print!(
|
||||||
|
"Writing syntax set to {} ... ",
|
||||||
|
syntax_set_path.to_string_lossy()
|
||||||
|
);
|
||||||
|
dump_to_file(&self.syntax_set, &syntax_set_path).map_err(|_| {
|
||||||
|
io::Error::new(
|
||||||
|
io::ErrorKind::Other,
|
||||||
|
format!(
|
||||||
|
"Could not save syntax set to {}",
|
||||||
|
syntax_set_path.to_string_lossy()
|
||||||
|
),
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
println!("okay");
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn default_theme(&self) -> Result<&Theme> {
|
||||||
|
Ok(self.theme_set.themes.get("Default").ok_or_else(|| {
|
||||||
|
io::Error::new(
|
||||||
|
io::ErrorKind::Other,
|
||||||
|
format!("Could not find 'Default' theme"),
|
||||||
|
)
|
||||||
|
})?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn theme_set_path() -> PathBuf {
|
||||||
|
PROJECT_DIRS.cache_dir().join("theme_set")
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn syntax_set_path() -> PathBuf {
|
||||||
|
PROJECT_DIRS.cache_dir().join("syntax_set")
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn config_dir() -> Cow<'static, str> {
|
||||||
|
PROJECT_DIRS.config_dir().to_string_lossy()
|
||||||
|
}
|
151
src/main.rs
151
src/main.rs
@ -17,6 +17,7 @@ extern crate directories;
|
|||||||
extern crate git2;
|
extern crate git2;
|
||||||
extern crate syntect;
|
extern crate syntect;
|
||||||
|
|
||||||
|
mod assets;
|
||||||
mod diff;
|
mod diff;
|
||||||
mod printer;
|
mod printer;
|
||||||
mod terminal;
|
mod terminal;
|
||||||
@ -24,7 +25,6 @@ mod terminal;
|
|||||||
use std::env;
|
use std::env;
|
||||||
use std::fs::{self, File};
|
use std::fs::{self, File};
|
||||||
use std::io::{self, BufRead, BufReader, Write};
|
use std::io::{self, BufRead, BufReader, Write};
|
||||||
use std::path::PathBuf;
|
|
||||||
use std::process::{self, Child, Command, Stdio};
|
use std::process::{self, Child, Command, Stdio};
|
||||||
|
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
@ -34,20 +34,15 @@ use ansi_term::Colour::{Fixed, Green, Red, White, Yellow};
|
|||||||
use ansi_term::Style;
|
use ansi_term::Style;
|
||||||
use atty::Stream;
|
use atty::Stream;
|
||||||
use clap::{App, AppSettings, Arg, ArgGroup, SubCommand};
|
use clap::{App, AppSettings, Arg, ArgGroup, SubCommand};
|
||||||
use directories::ProjectDirs;
|
|
||||||
|
|
||||||
use syntect::dumps::{dump_to_file, from_binary, from_reader};
|
|
||||||
use syntect::easy::HighlightLines;
|
use syntect::easy::HighlightLines;
|
||||||
use syntect::highlighting::{Theme, ThemeSet};
|
use syntect::highlighting::Theme;
|
||||||
use syntect::parsing::SyntaxSet;
|
use syntect::parsing::SyntaxSet;
|
||||||
|
|
||||||
|
use assets::{config_dir, syntax_set_path, theme_set_path, HighlightingAssets};
|
||||||
use diff::get_git_diff;
|
use diff::get_git_diff;
|
||||||
use printer::Printer;
|
use printer::Printer;
|
||||||
|
|
||||||
lazy_static! {
|
|
||||||
static ref PROJECT_DIRS: ProjectDirs = ProjectDirs::from("", "", crate_name!());
|
|
||||||
}
|
|
||||||
|
|
||||||
mod errors {
|
mod errors {
|
||||||
error_chain! {
|
error_chain! {
|
||||||
foreign_links {
|
foreign_links {
|
||||||
@ -224,129 +219,6 @@ fn is_truecolor_terminal() -> bool {
|
|||||||
.unwrap_or(false)
|
.unwrap_or(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
struct HighlightingAssets {
|
|
||||||
pub syntax_set: SyntaxSet,
|
|
||||||
pub theme_set: ThemeSet,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl HighlightingAssets {
|
|
||||||
fn theme_set_path() -> PathBuf {
|
|
||||||
PROJECT_DIRS.cache_dir().join("theme_set")
|
|
||||||
}
|
|
||||||
|
|
||||||
fn syntax_set_path() -> PathBuf {
|
|
||||||
PROJECT_DIRS.cache_dir().join("syntax_set")
|
|
||||||
}
|
|
||||||
|
|
||||||
fn from_files() -> Result<Self> {
|
|
||||||
let config_dir = PROJECT_DIRS.config_dir();
|
|
||||||
|
|
||||||
let theme_dir = config_dir.join("themes");
|
|
||||||
|
|
||||||
let theme_set = ThemeSet::load_from_folder(&theme_dir).map_err(|_| {
|
|
||||||
io::Error::new(
|
|
||||||
io::ErrorKind::Other,
|
|
||||||
format!(
|
|
||||||
"Could not load themes from '{}'",
|
|
||||||
theme_dir.to_string_lossy()
|
|
||||||
),
|
|
||||||
)
|
|
||||||
})?;
|
|
||||||
|
|
||||||
let mut syntax_set = SyntaxSet::new();
|
|
||||||
let syntax_dir = config_dir.join("syntax");
|
|
||||||
let _ = syntax_set.load_syntaxes(syntax_dir, true);
|
|
||||||
syntax_set.load_plain_text_syntax();
|
|
||||||
|
|
||||||
Ok(HighlightingAssets {
|
|
||||||
syntax_set,
|
|
||||||
theme_set,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
fn save(&self) -> Result<()> {
|
|
||||||
let cache_dir = PROJECT_DIRS.cache_dir();
|
|
||||||
let _ = fs::create_dir(cache_dir);
|
|
||||||
|
|
||||||
print!(
|
|
||||||
"Writing theme set to {} ... ",
|
|
||||||
Self::theme_set_path().to_string_lossy()
|
|
||||||
);
|
|
||||||
dump_to_file(&self.theme_set, &Self::theme_set_path()).map_err(|_| {
|
|
||||||
io::Error::new(
|
|
||||||
io::ErrorKind::Other,
|
|
||||||
format!(
|
|
||||||
"Could not save theme set to {}",
|
|
||||||
Self::theme_set_path().to_string_lossy()
|
|
||||||
),
|
|
||||||
)
|
|
||||||
})?;
|
|
||||||
println!("okay");
|
|
||||||
|
|
||||||
print!(
|
|
||||||
"Writing syntax set to {} ... ",
|
|
||||||
Self::syntax_set_path().to_string_lossy()
|
|
||||||
);
|
|
||||||
dump_to_file(&self.syntax_set, &Self::syntax_set_path()).map_err(|_| {
|
|
||||||
io::Error::new(
|
|
||||||
io::ErrorKind::Other,
|
|
||||||
format!(
|
|
||||||
"Could not save syntax set to {}",
|
|
||||||
Self::syntax_set_path().to_string_lossy()
|
|
||||||
),
|
|
||||||
)
|
|
||||||
})?;
|
|
||||||
println!("okay");
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn from_cache() -> Result<Self> {
|
|
||||||
let syntax_set_file = File::open(&Self::syntax_set_path()).chain_err(|| {
|
|
||||||
format!(
|
|
||||||
"Could not load cached syntax set '{}'",
|
|
||||||
Self::syntax_set_path().to_string_lossy()
|
|
||||||
)
|
|
||||||
})?;
|
|
||||||
let mut syntax_set: SyntaxSet = from_reader(syntax_set_file).map_err(|_| {
|
|
||||||
io::Error::new(
|
|
||||||
io::ErrorKind::Other,
|
|
||||||
format!("Could not parse cached syntax set"),
|
|
||||||
)
|
|
||||||
})?;
|
|
||||||
syntax_set.link_syntaxes();
|
|
||||||
|
|
||||||
let theme_set_file = File::open(&Self::theme_set_path()).chain_err(|| {
|
|
||||||
format!(
|
|
||||||
"Could not load cached theme set '{}'",
|
|
||||||
Self::theme_set_path().to_string_lossy()
|
|
||||||
)
|
|
||||||
})?;
|
|
||||||
let theme_set: ThemeSet = from_reader(theme_set_file).map_err(|_| {
|
|
||||||
io::Error::new(
|
|
||||||
io::ErrorKind::Other,
|
|
||||||
format!("Could not parse cached theme set"),
|
|
||||||
)
|
|
||||||
})?;
|
|
||||||
|
|
||||||
Ok(HighlightingAssets {
|
|
||||||
syntax_set,
|
|
||||||
theme_set,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
fn from_binary() -> Self {
|
|
||||||
let mut syntax_set: SyntaxSet = from_binary(include_bytes!("../assets/syntax_set"));
|
|
||||||
syntax_set.link_syntaxes();
|
|
||||||
let theme_set: ThemeSet = from_binary(include_bytes!("../assets/theme_set"));
|
|
||||||
|
|
||||||
HighlightingAssets {
|
|
||||||
syntax_set,
|
|
||||||
theme_set,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn run() -> Result<()> {
|
fn run() -> Result<()> {
|
||||||
let interactive_terminal = atty::is(Stream::Stdout);
|
let interactive_terminal = atty::is(Stream::Stdout);
|
||||||
|
|
||||||
@ -447,14 +319,14 @@ fn run() -> Result<()> {
|
|||||||
assets.save()?;
|
assets.save()?;
|
||||||
} else if cache_matches.is_present("clear") {
|
} else if cache_matches.is_present("clear") {
|
||||||
print!("Clearing theme set cache ... ");
|
print!("Clearing theme set cache ... ");
|
||||||
fs::remove_file(HighlightingAssets::theme_set_path())?;
|
fs::remove_file(theme_set_path())?;
|
||||||
println!("okay");
|
println!("okay");
|
||||||
|
|
||||||
print!("Clearing syntax set cache ... ");
|
print!("Clearing syntax set cache ... ");
|
||||||
fs::remove_file(HighlightingAssets::syntax_set_path())?;
|
fs::remove_file(syntax_set_path())?;
|
||||||
println!("okay");
|
println!("okay");
|
||||||
} else if cache_matches.is_present("config-dir") {
|
} else if cache_matches.is_present("config-dir") {
|
||||||
println!("{}", PROJECT_DIRS.config_dir().to_string_lossy());
|
println!("{}", config_dir());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
@ -506,15 +378,8 @@ fn run() -> Result<()> {
|
|||||||
term_width: console::Term::stdout().size().1 as usize,
|
term_width: console::Term::stdout().size().1 as usize,
|
||||||
};
|
};
|
||||||
|
|
||||||
let assets =
|
let assets = HighlightingAssets::new();
|
||||||
HighlightingAssets::from_cache().unwrap_or(HighlightingAssets::from_binary());
|
let theme = assets.default_theme()?;
|
||||||
|
|
||||||
let theme = assets.theme_set.themes.get("Default").ok_or_else(|| {
|
|
||||||
io::Error::new(
|
|
||||||
io::ErrorKind::Other,
|
|
||||||
format!("Could not find 'Default' theme"),
|
|
||||||
)
|
|
||||||
})?;
|
|
||||||
|
|
||||||
if app_matches.is_present("list-languages") {
|
if app_matches.is_present("list-languages") {
|
||||||
let languages = assets.syntax_set.syntaxes();
|
let languages = assets.syntax_set.syntaxes();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user