Include theme_preview file in binary

This commit is contained in:
sharkdp 2018-08-28 20:12:45 +02:00 committed by David Peter
parent 6f67444c99
commit 8cacd9b432
5 changed files with 45 additions and 29 deletions

View File

@ -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<Option<&'a str>>,
pub files: Vec<InputFile<'a>>,
/// 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<Option<&str>> {
fn files(&self) -> Vec<InputFile> {
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<OutputComponents> {

View File

@ -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())

View File

@ -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<P: Printer>(
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<BufRead> = 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)?;

View File

@ -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()));

View File

@ -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, '┼')?;