mirror of https://github.com/sharkdp/bat
Split modules (#86)
* Split diffing to separate module * Split assets to separate modulepull/88/head
parent
13a53d230b
commit
25cee002f9
@ -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()
|
||||
}
|
@ -0,0 +1,76 @@
|
||||
use git2::{DiffOptions, IntoCString, Repository};
|
||||
use std::collections::HashMap;
|
||||
use std::fs;
|
||||
use std::path::Path;
|
||||
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
pub enum LineChange {
|
||||
Added,
|
||||
RemovedAbove,
|
||||
RemovedBelow,
|
||||
Modified,
|
||||
}
|
||||
|
||||
pub type LineChanges = HashMap<u32, LineChange>;
|
||||
|
||||
pub fn get_git_diff(filename: &str) -> Option<LineChanges> {
|
||||
let repo = Repository::discover(&filename).ok()?;
|
||||
let path_absolute = fs::canonicalize(&filename).ok()?;
|
||||
let path_relative_to_repo = path_absolute.strip_prefix(repo.workdir()?).ok()?;
|
||||
|
||||
let mut diff_options = DiffOptions::new();
|
||||
let pathspec = path_relative_to_repo.into_c_string().ok()?;
|
||||
diff_options.pathspec(pathspec);
|
||||
diff_options.context_lines(0);
|
||||
|
||||
let diff = repo.diff_index_to_workdir(None, Some(&mut diff_options))
|
||||
.ok()?;
|
||||
|
||||
let mut line_changes: LineChanges = HashMap::new();
|
||||
|
||||
let mark_section =
|
||||
|line_changes: &mut LineChanges, start: u32, end: i32, change: LineChange| {
|
||||
for line in start..(end + 1) as u32 {
|
||||
line_changes.insert(line, change);
|
||||
}
|
||||
};
|
||||
|
||||
let _ = diff.foreach(
|
||||
&mut |_, _| true,
|
||||
None,
|
||||
Some(&mut |delta, hunk| {
|
||||
let path = delta.new_file().path().unwrap_or_else(|| Path::new(""));
|
||||
|
||||
if path_relative_to_repo != path {
|
||||
return false;
|
||||
}
|
||||
|
||||
let old_lines = hunk.old_lines();
|
||||
let new_start = hunk.new_start();
|
||||
let new_lines = hunk.new_lines();
|
||||
let new_end = (new_start + new_lines) as i32 - 1;
|
||||
|
||||
if old_lines == 0 && new_lines > 0 {
|
||||
mark_section(&mut line_changes, new_start, new_end, LineChange::Added);
|
||||
} else if new_lines == 0 && old_lines > 0 {
|
||||
if new_start == 0 {
|
||||
mark_section(&mut line_changes, 1, 1, LineChange::RemovedAbove);
|
||||
} else {
|
||||
mark_section(
|
||||
&mut line_changes,
|
||||
new_start,
|
||||
new_start as i32,
|
||||
LineChange::RemovedBelow,
|
||||
);
|
||||
}
|
||||
} else {
|
||||
mark_section(&mut line_changes, new_start, new_end, LineChange::Modified);
|
||||
}
|
||||
|
||||
true
|
||||
}),
|
||||
None,
|
||||
);
|
||||
|
||||
Some(line_changes)
|
||||
}
|
Loading…
Reference in New Issue