mirror of
https://github.com/sharkdp/bat.git
synced 2025-01-10 23:58:30 +01:00
Include theme_preview file in binary
This commit is contained in:
parent
6f67444c99
commit
8cacd9b432
19
src/app.rs
19
src/app.rs
@ -22,10 +22,17 @@ pub enum PagingMode {
|
|||||||
Never,
|
Never,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||||
|
pub enum InputFile<'a> {
|
||||||
|
StdIn,
|
||||||
|
Ordinary(&'a str),
|
||||||
|
ThemePreviewFile,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Config<'a> {
|
pub struct Config<'a> {
|
||||||
/// List of files to print
|
/// List of files to print
|
||||||
pub files: Vec<Option<&'a str>>,
|
pub files: Vec<InputFile<'a>>,
|
||||||
|
|
||||||
/// The explicitly configured language, if any
|
/// The explicitly configured language, if any
|
||||||
pub language: Option<&'a str>,
|
pub language: Option<&'a str>,
|
||||||
@ -341,7 +348,7 @@ impl App {
|
|||||||
paging_mode: match self.matches.value_of("paging") {
|
paging_mode: match self.matches.value_of("paging") {
|
||||||
Some("always") => PagingMode::Always,
|
Some("always") => PagingMode::Always,
|
||||||
Some("never") => PagingMode::Never,
|
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
|
// 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
|
// interactive terminal and if we do not *read* from an interactive
|
||||||
// terminal.
|
// terminal.
|
||||||
@ -373,19 +380,19 @@ impl App {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn files(&self) -> Vec<Option<&str>> {
|
fn files(&self) -> Vec<InputFile> {
|
||||||
self.matches
|
self.matches
|
||||||
.values_of("FILE")
|
.values_of("FILE")
|
||||||
.map(|values| {
|
.map(|values| {
|
||||||
values
|
values
|
||||||
.map(|filename| {
|
.map(|filename| {
|
||||||
if filename == "-" {
|
if filename == "-" {
|
||||||
None
|
InputFile::StdIn
|
||||||
} else {
|
} else {
|
||||||
Some(filename)
|
InputFile::Ordinary(filename)
|
||||||
}
|
}
|
||||||
}).collect()
|
}).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> {
|
fn output_components(&self) -> Result<OutputComponents> {
|
||||||
|
@ -11,6 +11,8 @@ use syntect::parsing::{SyntaxDefinition, SyntaxSet};
|
|||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
use std::os::unix::fs::FileTypeExt;
|
use std::os::unix::fs::FileTypeExt;
|
||||||
|
|
||||||
|
use app::InputFile;
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
static ref PROJECT_DIRS: ProjectDirs =
|
static ref PROJECT_DIRS: ProjectDirs =
|
||||||
ProjectDirs::from("", "", crate_name!()).expect("Could not get home directory");
|
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) {
|
let syntax = match (language, filename) {
|
||||||
(Some(language), _) => self.syntax_set.find_syntax_by_token(language),
|
(Some(language), _) => self.syntax_set.find_syntax_by_token(language),
|
||||||
(None, Some(filename)) => {
|
(None, InputFile::Ordinary(filename)) => {
|
||||||
#[cfg(not(unix))]
|
#[cfg(not(unix))]
|
||||||
let may_read_from_file = true;
|
let may_read_from_file = true;
|
||||||
|
|
||||||
@ -186,7 +188,8 @@ impl HighlightingAssets {
|
|||||||
None
|
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())
|
syntax.unwrap_or_else(|| self.syntax_set.find_syntax_plain_text())
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::{self, BufRead, BufReader, Write};
|
use std::io::{self, BufRead, BufReader, Write};
|
||||||
|
|
||||||
use app::Config;
|
use app::{Config, InputFile};
|
||||||
use assets::HighlightingAssets;
|
use assets::HighlightingAssets;
|
||||||
use errors::*;
|
use errors::*;
|
||||||
use line_range::LineRange;
|
use line_range::LineRange;
|
||||||
@ -41,17 +41,20 @@ impl<'b> Controller<'b> {
|
|||||||
Ok(no_errors)
|
Ok(no_errors)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn print_file<P: Printer>(
|
fn print_file<'a, P: Printer>(
|
||||||
&self,
|
&self,
|
||||||
printer: &mut P,
|
printer: &mut P,
|
||||||
writer: &mut Write,
|
writer: &mut Write,
|
||||||
filename: Option<&str>,
|
filename: InputFile<'a>,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let stdin = io::stdin();
|
let stdin = io::stdin();
|
||||||
{
|
{
|
||||||
|
let theme_preview_file = include_bytes!("../assets/theme_preview.rs");
|
||||||
|
|
||||||
let reader: Box<BufRead> = match filename {
|
let reader: Box<BufRead> = match filename {
|
||||||
None => Box::new(stdin.lock()),
|
InputFile::StdIn => Box::new(stdin.lock()),
|
||||||
Some(filename) => Box::new(BufReader::new(File::open(filename)?)),
|
InputFile::Ordinary(filename) => Box::new(BufReader::new(File::open(filename)?)),
|
||||||
|
InputFile::ThemePreviewFile => Box::new(&theme_preview_file[..]),
|
||||||
};
|
};
|
||||||
|
|
||||||
printer.print_header(writer, filename)?;
|
printer.print_header(writer, filename)?;
|
||||||
|
@ -36,7 +36,7 @@ use std::process;
|
|||||||
use ansi_term::Colour::Green;
|
use ansi_term::Colour::Green;
|
||||||
use ansi_term::Style;
|
use ansi_term::Style;
|
||||||
|
|
||||||
use app::{App, Config};
|
use app::{App, Config, InputFile};
|
||||||
use assets::{clear_assets, config_dir, HighlightingAssets};
|
use assets::{clear_assets, config_dir, HighlightingAssets};
|
||||||
use controller::Controller;
|
use controller::Controller;
|
||||||
use style::{OutputComponent, OutputComponents};
|
use style::{OutputComponent, OutputComponents};
|
||||||
@ -136,7 +136,7 @@ pub fn list_themes(assets: &HighlightingAssets, cfg: &Config) {
|
|||||||
let mut config = cfg.clone();
|
let mut config = cfg.clone();
|
||||||
let mut style = HashSet::new();
|
let mut style = HashSet::new();
|
||||||
style.insert(OutputComponent::Plain);
|
style.insert(OutputComponent::Plain);
|
||||||
config.files = vec![Some("assets/theme_preview.rs")];
|
config.files = vec![InputFile::ThemePreviewFile];
|
||||||
config.output_components = OutputComponents(style);
|
config.output_components = OutputComponents(style);
|
||||||
for (theme, _) in themes.iter() {
|
for (theme, _) in themes.iter() {
|
||||||
println!("Theme: {}\n", Style::new().bold().paint(theme.to_string()));
|
println!("Theme: {}\n", Style::new().bold().paint(theme.to_string()));
|
||||||
|
@ -10,7 +10,7 @@ use console::AnsiCodeIterator;
|
|||||||
use syntect::easy::HighlightLines;
|
use syntect::easy::HighlightLines;
|
||||||
use syntect::highlighting::Theme;
|
use syntect::highlighting::Theme;
|
||||||
|
|
||||||
use app::Config;
|
use app::{Config, InputFile};
|
||||||
use assets::HighlightingAssets;
|
use assets::HighlightingAssets;
|
||||||
use decorations::{Decoration, GridBorderDecoration, LineChangesDecoration, LineNumberDecoration};
|
use decorations::{Decoration, GridBorderDecoration, LineChangesDecoration, LineNumberDecoration};
|
||||||
use diff::get_git_diff;
|
use diff::get_git_diff;
|
||||||
@ -20,7 +20,7 @@ use style::OutputWrap;
|
|||||||
use terminal::{as_terminal_escaped, to_ansi_color};
|
use terminal::{as_terminal_escaped, to_ansi_color};
|
||||||
|
|
||||||
pub trait Printer {
|
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_footer(&mut self, handle: &mut Write) -> Result<()>;
|
||||||
fn print_line(
|
fn print_line(
|
||||||
&mut self,
|
&mut self,
|
||||||
@ -40,7 +40,7 @@ impl SimplePrinter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Printer for 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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -73,7 +73,7 @@ pub struct InteractivePrinter<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> 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 theme = assets.get_theme(&config.theme);
|
||||||
|
|
||||||
let colors = if config.colored_output {
|
let colors = if config.colored_output {
|
||||||
@ -113,10 +113,13 @@ impl<'a> InteractivePrinter<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get the Git modifications
|
// 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
|
// 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);
|
let highlighter = HighlightLines::new(syntax, theme);
|
||||||
|
|
||||||
InteractivePrinter {
|
InteractivePrinter {
|
||||||
@ -148,7 +151,7 @@ impl<'a> InteractivePrinter<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Printer for 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() {
|
if !self.config.output_components.header() {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
@ -168,12 +171,12 @@ impl<'a> Printer for InteractivePrinter<'a> {
|
|||||||
write!(handle, "{}", " ".repeat(self.panel_width))?;
|
write!(handle, "{}", " ".repeat(self.panel_width))?;
|
||||||
}
|
}
|
||||||
|
|
||||||
writeln!(
|
let (prefix, name) = match file {
|
||||||
handle,
|
InputFile::Ordinary(filename) => ("File: ", filename),
|
||||||
"{}{}",
|
_ => ("", "STDIN"),
|
||||||
filename.map_or("", |_| "File: "),
|
};
|
||||||
self.colors.filename.paint(filename.unwrap_or("STDIN"))
|
|
||||||
)?;
|
writeln!(handle, "{}{}", prefix, self.colors.filename.paint(name))?;
|
||||||
|
|
||||||
if self.config.output_components.grid() {
|
if self.config.output_components.grid() {
|
||||||
self.print_horizontal_line(handle, '┼')?;
|
self.print_horizontal_line(handle, '┼')?;
|
||||||
|
Loading…
Reference in New Issue
Block a user