on master help sign in
Include theme_preview file in binary
commit8cacd9b43209c87404f587ae1f0ec3245771074a authorsharkdp <davidpeter@web.de> dateTue 28 Aug 2018, 20:12 (8 years ago) parent6f67444
5 files changed+45−29
src/app.rs+13−6 src/assets.rs+6−3 src/controller.rs+8−5 src/main.rs+2−2 src/printer.rs+16−13
src/app.rs+13−6
@@ -22,10 +22,17 @@ pub enum PagingMode {
2222 Never,
2323}
2424
25+#[derive(Debug, Clone, Copy, PartialEq)]
26+pub enum InputFile<'a> {
27+ StdIn,
28+ Ordinary(&'a str),
29+ ThemePreviewFile,
30+}
31+
2532#[derive(Clone)]
2633pub struct Config<'a> {
2734 /// List of files to print
28− pub files: Vec<Option<&'a str>>,
35+ pub files: Vec<InputFile<'a>>,
2936
3037 /// The explicitly configured language, if any
3138 pub language: Option<&'a str>,
@@ -341,7 +348,7 @@ impl App {
341348 paging_mode: match self.matches.value_of("paging") {
342349 Some("always") => PagingMode::Always,
343350 Some("never") => PagingMode::Never,
344− Some("auto") | _ => if files.contains(&None) {
351+ Some("auto") | _ => if files.contains(&InputFile::StdIn) {
345352 // If we are reading from stdin, only enable paging if we write to an
346353 // interactive terminal and if we do not *read* from an interactive
347354 // terminal.
@@ -373,19 +380,19 @@ impl App {
373380 })
374381 }
375382
376− fn files(&self) -> Vec<Option<&str>> {
383+ fn files(&self) -> Vec<InputFile> {
377384 self.matches
378385 .values_of("FILE")
379386 .map(|values| {
380387 values
381388 .map(|filename| {
382389 if filename == "-" {
383− None
390+ InputFile::StdIn
384391 } else {
385− Some(filename)
392+ InputFile::Ordinary(filename)
386393 }
387394 }).collect()
388− }).unwrap_or_else(|| vec![None]) // read from stdin (None) if no args are given
395+ }).unwrap_or_else(|| vec![InputFile::StdIn])
389396 }
390397
391398 fn output_components(&self) -> Result<OutputComponents> {
src/assets.rs+6−3
@@ -11,6 +11,8 @@ use syntect::parsing::{SyntaxDefinition, SyntaxSet};
1111#[cfg(unix)]
1212use std::os::unix::fs::FileTypeExt;
1313
14+use app::InputFile;
15+
1416lazy_static! {
1517 static ref PROJECT_DIRS: ProjectDirs =
1618 ProjectDirs::from("", "", crate_name!()).expect("Could not get home directory");
@@ -164,10 +166,10 @@ impl HighlightingAssets {
164166 }
165167 }
166168
167− pub fn get_syntax(&self, language: Option<&str>, filename: Option<&str>) -> &SyntaxDefinition {
169+ pub fn get_syntax(&self, language: Option<&str>, filename: InputFile) -> &SyntaxDefinition {
168170 let syntax = match (language, filename) {
169171 (Some(language), _) => self.syntax_set.find_syntax_by_token(language),
170− (None, Some(filename)) => {
172+ (None, InputFile::Ordinary(filename)) => {
171173 #[cfg(not(unix))]
172174 let may_read_from_file = true;
173175
@@ -186,7 +188,8 @@ impl HighlightingAssets {
186188 None
187189 }
188190 }
189− (None, None) => None,
191+ (None, InputFile::StdIn) => None,
192+ (_, InputFile::ThemePreviewFile) => self.syntax_set.find_syntax_by_name("Rust"),
190193 };
191194
192195 syntax.unwrap_or_else(|| self.syntax_set.find_syntax_plain_text())
src/controller.rs+8−5
@@ -1,7 +1,7 @@
11use std::fs::File;
22use std::io::{self, BufRead, BufReader, Write};
33
4−use app::Config;
4+use app::{Config, InputFile};
55use assets::HighlightingAssets;
66use errors::*;
77use line_range::LineRange;
@@ -41,17 +41,20 @@ impl<'b> Controller<'b> {
4141 Ok(no_errors)
4242 }
4343
44− fn print_file<P: Printer>(
44+ fn print_file<'a, P: Printer>(
4545 &self,
4646 printer: &mut P,
4747 writer: &mut Write,
48− filename: Option<&str>,
48+ filename: InputFile<'a>,
4949 ) -> Result<()> {
5050 let stdin = io::stdin();
5151 {
52+ let theme_preview_file = include_bytes!("../assets/theme_preview.rs");
53+
5254 let reader: Box<BufRead> = match filename {
53− None => Box::new(stdin.lock()),
54− Some(filename) => Box::new(BufReader::new(File::open(filename)?)),
55+ InputFile::StdIn => Box::new(stdin.lock()),
56+ InputFile::Ordinary(filename) => Box::new(BufReader::new(File::open(filename)?)),
57+ InputFile::ThemePreviewFile => Box::new(&theme_preview_file[..]),
5558 };
5659
5760 printer.print_header(writer, filename)?;
src/main.rs+2−2
@@ -36,7 +36,7 @@ use std::process;
3636use ansi_term::Colour::Green;
3737use ansi_term::Style;
3838
39−use app::{App, Config};
39+use app::{App, Config, InputFile};
4040use assets::{clear_assets, config_dir, HighlightingAssets};
4141use controller::Controller;
4242use style::{OutputComponent, OutputComponents};
@@ -136,7 +136,7 @@ pub fn list_themes(assets: &HighlightingAssets, cfg: &Config) {
136136 let mut config = cfg.clone();
137137 let mut style = HashSet::new();
138138 style.insert(OutputComponent::Plain);
139− config.files = vec![Some("assets/theme_preview.rs")];
139+ config.files = vec![InputFile::ThemePreviewFile];
140140 config.output_components = OutputComponents(style);
141141 for (theme, _) in themes.iter() {
142142 println!("Theme: {}\n", Style::new().bold().paint(theme.to_string()));
src/printer.rs+16−13
@@ -10,7 +10,7 @@ use console::AnsiCodeIterator;
1010use syntect::easy::HighlightLines;
1111use syntect::highlighting::Theme;
1212
13−use app::Config;
13+use app::{Config, InputFile};
1414use assets::HighlightingAssets;
1515use decorations::{Decoration, GridBorderDecoration, LineChangesDecoration, LineNumberDecoration};
1616use diff::get_git_diff;
@@ -20,7 +20,7 @@ use style::OutputWrap;
2020use terminal::{as_terminal_escaped, to_ansi_color};
2121
2222pub trait Printer {
23− fn print_header(&mut self, handle: &mut Write, filename: Option<&str>) -> Result<()>;
23+ fn print_header(&mut self, handle: &mut Write, file: InputFile) -> Result<()>;
2424 fn print_footer(&mut self, handle: &mut Write) -> Result<()>;
2525 fn print_line(
2626 &mut self,
@@ -40,7 +40,7 @@ impl SimplePrinter {
4040}
4141
4242impl Printer for SimplePrinter {
43− fn print_header(&mut self, _handle: &mut Write, _filename: Option<&str>) -> Result<()> {
43+ fn print_header(&mut self, _handle: &mut Write, _file: InputFile) -> Result<()> {
4444 Ok(())
4545 }
4646
@@ -73,7 +73,7 @@ pub struct InteractivePrinter<'a> {
7373}
7474
7575impl<'a> InteractivePrinter<'a> {
76− pub fn new(config: &'a Config, assets: &'a HighlightingAssets, filename: Option<&str>) -> Self {
76+ pub fn new(config: &'a Config, assets: &'a HighlightingAssets, file: InputFile) -> Self {
7777 let theme = assets.get_theme(&config.theme);
7878
7979 let colors = if config.colored_output {
@@ -113,10 +113,13 @@ impl<'a> InteractivePrinter<'a> {
113113 }
114114
115115 // Get the Git modifications
116− let line_changes = filename.and_then(|file| get_git_diff(file));
116+ let line_changes = match file {
117+ InputFile::Ordinary(filename) => get_git_diff(filename),
118+ _ => None,
119+ };
117120
118121 // Determine the type of syntax for highlighting
119− let syntax = assets.get_syntax(config.language, filename);
122+ let syntax = assets.get_syntax(config.language, file);
120123 let highlighter = HighlightLines::new(syntax, theme);
121124
122125 InteractivePrinter {
@@ -148,7 +151,7 @@ impl<'a> InteractivePrinter<'a> {
148151}
149152
150153impl<'a> Printer for InteractivePrinter<'a> {
151− fn print_header(&mut self, handle: &mut Write, filename: Option<&str>) -> Result<()> {
154+ fn print_header(&mut self, handle: &mut Write, file: InputFile) -> Result<()> {
152155 if !self.config.output_components.header() {
153156 return Ok(());
154157 }
@@ -168,12 +171,12 @@ impl<'a> Printer for InteractivePrinter<'a> {
168171 write!(handle, "{}", " ".repeat(self.panel_width))?;
169172 }
170173
171− writeln!(
172− handle,
173− "{}{}",
174− filename.map_or("", |_| "File: "),
175− self.colors.filename.paint(filename.unwrap_or("STDIN"))
176− )?;
174+ let (prefix, name) = match file {
175+ InputFile::Ordinary(filename) => ("File: ", filename),
176+ _ => ("", "STDIN"),
177+ };
178+
179+ writeln!(handle, "{}{}", prefix, self.colors.filename.paint(name))?;
177180
178181 if self.config.output_components.grid() {
179182 self.print_horizontal_line(handle, '┼')?;