2
0
mirror of https://github.com/sharkdp/bat synced 2024-11-16 21:25:56 +00:00

Extract syntax finding to assets module

This commit is contained in:
Ezinwa Okpoechi 2018-05-18 12:30:30 +02:00 committed by David Peter
parent 882931a77b
commit 247dfbee83
2 changed files with 37 additions and 30 deletions

View File

@ -6,7 +6,10 @@ use std::io;
use std::path::{Path, PathBuf};
use syntect::dumps::{dump_to_file, from_binary, from_reader};
use syntect::highlighting::{Theme, ThemeSet};
use syntect::parsing::SyntaxSet;
use syntect::parsing::{SyntaxDefinition, SyntaxSet};
#[cfg(unix)]
use std::os::unix::fs::FileTypeExt;
lazy_static! {
static ref PROJECT_DIRS: ProjectDirs = ProjectDirs::from("", "", crate_name!());
@ -124,6 +127,34 @@ impl HighlightingAssets {
)
})?)
}
pub fn get_syntax(
&self,
language: Option<&str>,
filename: Option<&str>,
) -> Result<&SyntaxDefinition> {
let syntax = match (language, filename) {
(Some(language), _) => self.syntax_set.find_syntax_by_token(language),
(None, Some(filename)) => {
#[cfg(not(unix))]
let may_read_from_file = true;
// Do not peek at the file (to determine the syntax) if it is a FIFO because they can
// only be read once.
#[cfg(unix)]
let may_read_from_file = !fs::metadata(filename)?.file_type().is_fifo();
if may_read_from_file {
self.syntax_set.find_syntax_for_file(filename)?
} else {
None
}
}
(None, None) => None,
};
Ok(syntax.unwrap_or_else(|| self.syntax_set.find_syntax_plain_text()))
}
}
pub fn theme_set_path() -> PathBuf {

View File

@ -30,17 +30,14 @@ use std::io::{self, BufRead, BufReader, Write};
use std::path::Path;
use std::process::{self, Child, Command, Stdio};
#[cfg(unix)]
use std::os::unix::fs::FileTypeExt;
use ansi_term::Colour::{Fixed, Green, Red, White, Yellow};
use ansi_term::Style;
use syntect::easy::HighlightLines;
use syntect::highlighting::Theme;
use syntect::parsing::SyntaxSet;
use syntect::parsing::SyntaxDefinition;
use app::{App, Config};
use app::App;
use assets::{config_dir, syntax_set_path, theme_set_path, HighlightingAssets};
use diff::get_git_diff;
use printer::Printer;
@ -124,9 +121,8 @@ impl Colors {
}
fn print_file(
config: &Config,
theme: &Theme,
syntax_set: &SyntaxSet,
syntax: &SyntaxDefinition,
printer: &mut Printer,
filename: Option<&str>,
) -> Result<()> {
@ -137,27 +133,6 @@ fn print_file(
Some(filename) => Box::new(BufReader::new(File::open(filename)?)),
};
let syntax = match (config.language, filename) {
(Some(language), _) => syntax_set.find_syntax_by_token(language),
(None, Some(filename)) => {
#[cfg(not(unix))]
let may_read_from_file = true;
// Do not peek at the file (to determine the syntax) if it is a FIFO because they can
// only be read once.
#[cfg(unix)]
let may_read_from_file = !fs::metadata(filename)?.file_type().is_fifo();
if may_read_from_file {
syntax_set.find_syntax_for_file(filename)?
} else {
None
}
}
(None, None) => None,
};
let syntax = syntax.unwrap_or_else(|| syntax_set.find_syntax_plain_text());
let mut highlighter = HighlightLines::new(syntax, theme);
printer.print_header(filename)?;
@ -274,8 +249,9 @@ fn run() -> Result<()> {
for file in &config.files {
printer.line_changes = file.and_then(|filename| get_git_diff(filename));
let syntax = assets.get_syntax(config.language, *file)?;
print_file(&config, theme, &assets.syntax_set, &mut printer, *file)?;
print_file(theme, &syntax, &mut printer, *file)?;
}
}
}