From 8cacd9b43209c87404f587ae1f0ec3245771074a Mon Sep 17 00:00:00 2001 From: sharkdp Date: Tue, 28 Aug 2018 20:12:45 +0200 Subject: [PATCH] Include theme_preview file in binary --- src/app.rs | 19 +++++++++++++------ src/assets.rs | 9 ++++++--- src/controller.rs | 13 ++++++++----- src/main.rs | 4 ++-- src/printer.rs | 29 ++++++++++++++++------------- 5 files changed, 45 insertions(+), 29 deletions(-) diff --git a/src/app.rs b/src/app.rs index f3142a16..4eacba8f 100644 --- a/src/app.rs +++ b/src/app.rs @@ -22,10 +22,17 @@ pub enum PagingMode { Never, } +#[derive(Debug, Clone, Copy, PartialEq)] +pub enum InputFile<'a> { + StdIn, + Ordinary(&'a str), + ThemePreviewFile, +} + #[derive(Clone)] pub struct Config<'a> { /// List of files to print - pub files: Vec>, + pub files: Vec>, /// The explicitly configured language, if any pub language: Option<&'a str>, @@ -341,7 +348,7 @@ impl App { paging_mode: match self.matches.value_of("paging") { Some("always") => PagingMode::Always, Some("never") => PagingMode::Never, - Some("auto") | _ => if files.contains(&None) { + Some("auto") | _ => if files.contains(&InputFile::StdIn) { // If we are reading from stdin, only enable paging if we write to an // interactive terminal and if we do not *read* from an interactive // terminal. @@ -373,19 +380,19 @@ impl App { }) } - fn files(&self) -> Vec> { + fn files(&self) -> Vec { self.matches .values_of("FILE") .map(|values| { values .map(|filename| { if filename == "-" { - None + InputFile::StdIn } else { - Some(filename) + InputFile::Ordinary(filename) } }).collect() - }).unwrap_or_else(|| vec![None]) // read from stdin (None) if no args are given + }).unwrap_or_else(|| vec![InputFile::StdIn]) } fn output_components(&self) -> Result { diff --git a/src/assets.rs b/src/assets.rs index 12a42166..3e3211b3 100644 --- a/src/assets.rs +++ b/src/assets.rs @@ -11,6 +11,8 @@ use syntect::parsing::{SyntaxDefinition, SyntaxSet}; #[cfg(unix)] use std::os::unix::fs::FileTypeExt; +use app::InputFile; + lazy_static! { static ref PROJECT_DIRS: ProjectDirs = ProjectDirs::from("", "", crate_name!()).expect("Could not get home directory"); @@ -164,10 +166,10 @@ impl HighlightingAssets { } } - pub fn get_syntax(&self, language: Option<&str>, filename: Option<&str>) -> &SyntaxDefinition { + pub fn get_syntax(&self, language: Option<&str>, filename: InputFile) -> &SyntaxDefinition { let syntax = match (language, filename) { (Some(language), _) => self.syntax_set.find_syntax_by_token(language), - (None, Some(filename)) => { + (None, InputFile::Ordinary(filename)) => { #[cfg(not(unix))] let may_read_from_file = true; @@ -186,7 +188,8 @@ impl HighlightingAssets { None } } - (None, None) => None, + (None, InputFile::StdIn) => None, + (_, InputFile::ThemePreviewFile) => self.syntax_set.find_syntax_by_name("Rust"), }; syntax.unwrap_or_else(|| self.syntax_set.find_syntax_plain_text()) diff --git a/src/controller.rs b/src/controller.rs index 8f2bfbf4..46d15aa0 100644 --- a/src/controller.rs +++ b/src/controller.rs @@ -1,7 +1,7 @@ use std::fs::File; use std::io::{self, BufRead, BufReader, Write}; -use app::Config; +use app::{Config, InputFile}; use assets::HighlightingAssets; use errors::*; use line_range::LineRange; @@ -41,17 +41,20 @@ impl<'b> Controller<'b> { Ok(no_errors) } - fn print_file( + fn print_file<'a, P: Printer>( &self, printer: &mut P, writer: &mut Write, - filename: Option<&str>, + filename: InputFile<'a>, ) -> Result<()> { let stdin = io::stdin(); { + let theme_preview_file = include_bytes!("../assets/theme_preview.rs"); + let reader: Box = match filename { - None => Box::new(stdin.lock()), - Some(filename) => Box::new(BufReader::new(File::open(filename)?)), + InputFile::StdIn => Box::new(stdin.lock()), + InputFile::Ordinary(filename) => Box::new(BufReader::new(File::open(filename)?)), + InputFile::ThemePreviewFile => Box::new(&theme_preview_file[..]), }; printer.print_header(writer, filename)?; diff --git a/src/main.rs b/src/main.rs index e7c534aa..445b55da 100644 --- a/src/main.rs +++ b/src/main.rs @@ -36,7 +36,7 @@ use std::process; use ansi_term::Colour::Green; use ansi_term::Style; -use app::{App, Config}; +use app::{App, Config, InputFile}; use assets::{clear_assets, config_dir, HighlightingAssets}; use controller::Controller; use style::{OutputComponent, OutputComponents}; @@ -136,7 +136,7 @@ pub fn list_themes(assets: &HighlightingAssets, cfg: &Config) { let mut config = cfg.clone(); let mut style = HashSet::new(); style.insert(OutputComponent::Plain); - config.files = vec![Some("assets/theme_preview.rs")]; + config.files = vec![InputFile::ThemePreviewFile]; config.output_components = OutputComponents(style); for (theme, _) in themes.iter() { println!("Theme: {}\n", Style::new().bold().paint(theme.to_string())); diff --git a/src/printer.rs b/src/printer.rs index 835df002..afa2ac8b 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -10,7 +10,7 @@ use console::AnsiCodeIterator; use syntect::easy::HighlightLines; use syntect::highlighting::Theme; -use app::Config; +use app::{Config, InputFile}; use assets::HighlightingAssets; use decorations::{Decoration, GridBorderDecoration, LineChangesDecoration, LineNumberDecoration}; use diff::get_git_diff; @@ -20,7 +20,7 @@ use style::OutputWrap; use terminal::{as_terminal_escaped, to_ansi_color}; pub trait Printer { - fn print_header(&mut self, handle: &mut Write, filename: Option<&str>) -> Result<()>; + fn print_header(&mut self, handle: &mut Write, file: InputFile) -> Result<()>; fn print_footer(&mut self, handle: &mut Write) -> Result<()>; fn print_line( &mut self, @@ -40,7 +40,7 @@ impl SimplePrinter { } impl Printer for SimplePrinter { - fn print_header(&mut self, _handle: &mut Write, _filename: Option<&str>) -> Result<()> { + fn print_header(&mut self, _handle: &mut Write, _file: InputFile) -> Result<()> { Ok(()) } @@ -73,7 +73,7 @@ pub struct InteractivePrinter<'a> { } impl<'a> InteractivePrinter<'a> { - pub fn new(config: &'a Config, assets: &'a HighlightingAssets, filename: Option<&str>) -> Self { + pub fn new(config: &'a Config, assets: &'a HighlightingAssets, file: InputFile) -> Self { let theme = assets.get_theme(&config.theme); let colors = if config.colored_output { @@ -113,10 +113,13 @@ impl<'a> InteractivePrinter<'a> { } // Get the Git modifications - let line_changes = filename.and_then(|file| get_git_diff(file)); + let line_changes = match file { + InputFile::Ordinary(filename) => get_git_diff(filename), + _ => None, + }; // Determine the type of syntax for highlighting - let syntax = assets.get_syntax(config.language, filename); + let syntax = assets.get_syntax(config.language, file); let highlighter = HighlightLines::new(syntax, theme); InteractivePrinter { @@ -148,7 +151,7 @@ impl<'a> InteractivePrinter<'a> { } impl<'a> Printer for InteractivePrinter<'a> { - fn print_header(&mut self, handle: &mut Write, filename: Option<&str>) -> Result<()> { + fn print_header(&mut self, handle: &mut Write, file: InputFile) -> Result<()> { if !self.config.output_components.header() { return Ok(()); } @@ -168,12 +171,12 @@ impl<'a> Printer for InteractivePrinter<'a> { write!(handle, "{}", " ".repeat(self.panel_width))?; } - writeln!( - handle, - "{}{}", - filename.map_or("", |_| "File: "), - self.colors.filename.paint(filename.unwrap_or("STDIN")) - )?; + let (prefix, name) = match file { + InputFile::Ordinary(filename) => ("File: ", filename), + _ => ("", "STDIN"), + }; + + writeln!(handle, "{}{}", prefix, self.colors.filename.paint(name))?; if self.config.output_components.grid() { self.print_horizontal_line(handle, '┼')?;