From 247dfbee83098f8542b1a5e130d31bcf3a972e79 Mon Sep 17 00:00:00 2001 From: Ezinwa Okpoechi Date: Fri, 18 May 2018 12:30:30 +0200 Subject: [PATCH] Extract syntax finding to assets module --- src/assets.rs | 33 ++++++++++++++++++++++++++++++++- src/main.rs | 34 +++++----------------------------- 2 files changed, 37 insertions(+), 30 deletions(-) diff --git a/src/assets.rs b/src/assets.rs index 0d4de133..22f870de 100644 --- a/src/assets.rs +++ b/src/assets.rs @@ -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 { diff --git a/src/main.rs b/src/main.rs index 0c0cda19..104c1b27 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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)?; } } }