From f8d0956893a0a6e42f9caddbb8e54a25c74d1166 Mon Sep 17 00:00:00 2001 From: sharkdp Date: Tue, 21 Apr 2020 21:19:06 +0200 Subject: [PATCH] Rename InputFile => Input --- src/assets.rs | 32 +++++++++++++-------------- src/bin/bat/app.rs | 16 +++++++------- src/bin/bat/main.rs | 10 ++++----- src/config.rs | 4 ++-- src/controller.rs | 26 +++++++++++----------- src/{inputfile.rs => input.rs} | 28 ++++++++++++------------ src/lib.rs | 2 +- src/pretty_printer.rs | 8 +++---- src/printer.rs | 40 ++++++++++++++++------------------ 9 files changed, 82 insertions(+), 84 deletions(-) rename src/{inputfile.rs => input.rs} (84%) diff --git a/src/assets.rs b/src/assets.rs index e4e93363..55bf3ef0 100644 --- a/src/assets.rs +++ b/src/assets.rs @@ -10,7 +10,7 @@ use syntect::parsing::{SyntaxReference, SyntaxSet, SyntaxSetBuilder}; use crate::assets_metadata::AssetsMetadata; use crate::errors::*; -use crate::inputfile::{InputFile, InputFileReader}; +use crate::input::{Input, InputReader}; use crate::syntax_mapping::{MappingTarget, SyntaxMapping}; #[derive(Debug)] @@ -188,13 +188,13 @@ impl HighlightingAssets { pub(crate) fn get_syntax( &self, language: Option<&str>, - file: &InputFile, - reader: &mut InputFileReader, + file: &Input, + reader: &mut InputReader, mapping: &SyntaxMapping, ) -> &SyntaxReference { let syntax = match (language, file) { (Some(language), _) => self.syntax_set.find_syntax_by_token(language), - (None, InputFile::Ordinary(ofile)) => { + (None, Input::Ordinary(ofile)) => { let path = Path::new(ofile.provided_path()); let line_syntax = self.get_first_line_syntax(reader); @@ -212,14 +212,14 @@ impl HighlightingAssets { } } } - (None, InputFile::StdIn(None)) => String::from_utf8(reader.first_line.clone()) + (None, Input::StdIn(None)) => String::from_utf8(reader.first_line.clone()) .ok() .and_then(|l| self.syntax_set.find_syntax_by_first_line(&l)), - (None, InputFile::StdIn(Some(file_name))) => self + (None, Input::StdIn(Some(file_name))) => self .get_extension_syntax(&file_name) .or(self.get_first_line_syntax(reader)), - (_, InputFile::ThemePreviewFile) => self.syntax_set.find_syntax_by_name("Rust"), - (None, InputFile::FromReader(s, _)) => None, + (_, Input::ThemePreviewFile) => self.syntax_set.find_syntax_by_name("Rust"), + (None, Input::FromReader(_, _)) => unimplemented!(), }; syntax.unwrap_or_else(|| self.syntax_set.find_syntax_plain_text()) @@ -238,7 +238,7 @@ impl HighlightingAssets { }) } - fn get_first_line_syntax(&self, reader: &mut InputFileReader) -> Option<&SyntaxReference> { + fn get_first_line_syntax(&self, reader: &mut InputReader) -> Option<&SyntaxReference> { String::from_utf8(reader.first_line.clone()) .ok() .and_then(|l| self.syntax_set.find_syntax_by_first_line(&l)) @@ -249,7 +249,7 @@ impl HighlightingAssets { mod tests { use super::*; - use crate::inputfile::OrdinaryFile; + use crate::input::OrdinaryFile; use std::ffi::{OsStr, OsString}; use std::fs::File; @@ -281,11 +281,11 @@ mod tests { writeln!(temp_file, "{}", first_line).unwrap(); } - let input_file = InputFile::Ordinary(OrdinaryFile::from_path(file_path.as_os_str())); + let input = Input::Ordinary(OrdinaryFile::from_path(file_path.as_os_str())); let syntax = self.assets.get_syntax( None, - &input_file, - &mut input_file.get_reader(io::stdin().lock()).unwrap(), + &input, + &mut input.get_reader(io::stdin().lock()).unwrap(), &self.syntax_mapping, ); @@ -305,11 +305,11 @@ mod tests { } fn syntax_for_stdin_with_content(&self, file_name: &str, content: &[u8]) -> String { - let input_file = InputFile::StdIn(Some(OsString::from(file_name))); + let input = Input::StdIn(Some(OsString::from(file_name))); let syntax = self.assets.get_syntax( None, - &input_file, - &mut input_file.get_reader(content).unwrap(), + &input, + &mut input.get_reader(content).unwrap(), &self.syntax_mapping, ); syntax.name.clone() diff --git a/src/bin/bat/app.rs b/src/bin/bat/app.rs index 2fc1d8c4..569e7b47 100644 --- a/src/bin/bat/app.rs +++ b/src/bin/bat/app.rs @@ -15,8 +15,8 @@ use console::Term; use bat::{ config::{ - Config, HighlightedLineRanges, InputFile, LineRange, LineRanges, MappingTarget, - OrdinaryFile, PagingMode, StyleComponent, StyleComponents, SyntaxMapping, WrappingMode, + Config, HighlightedLineRanges, Input, LineRange, LineRanges, MappingTarget, OrdinaryFile, + PagingMode, StyleComponent, StyleComponents, SyntaxMapping, WrappingMode, }, errors::*, HighlightingAssets, @@ -73,7 +73,7 @@ impl App { Ok(clap_app::build_app(interactive_output).get_matches_from(args)) } - pub fn config(&self, inputs: &[InputFile]) -> Result { + pub fn config(&self, inputs: &[Input]) -> Result { let style_components = self.style_components()?; let paging_mode = match self.matches.value_of("paging") { @@ -84,7 +84,7 @@ impl App { // If we have -pp as an option when in auto mode, the pager should be disabled. PagingMode::Never } else if inputs.iter().any(|f| { - if let InputFile::StdIn(None) = f { + if let Input::StdIn(None) = f { true } else { false @@ -226,7 +226,7 @@ impl App { }) } - pub fn inputs(&self) -> Result> { + pub fn inputs(&self) -> Result> { // verify equal length of file-names and input FILEs match self.matches.values_of("file-name") { Some(ref filenames) @@ -251,7 +251,7 @@ impl App { let files: Option> = self.matches.values_of_os("FILE").map(|vs| vs.collect()); if files.is_none() { - return Ok(vec![InputFile::StdIn( + return Ok(vec![Input::StdIn( filenames_or_none.nth(0).unwrap().map(|f| f.to_owned()), )]); } @@ -264,13 +264,13 @@ impl App { for (input, name) in files_or_none.zip(filenames_or_none) { if let Some(input) = input { if input.to_str().unwrap_or_default() == "-" { - file_input.push(InputFile::StdIn(name.map(|n| n.to_owned()))); + file_input.push(Input::StdIn(name.map(|n| n.to_owned()))); } else { let mut ofile = OrdinaryFile::from_path(input); if let Some(path) = name { ofile.set_provided_path(path); } - file_input.push(InputFile::Ordinary(ofile)) + file_input.push(Input::Ordinary(ofile)) } } } diff --git a/src/bin/bat/main.rs b/src/bin/bat/main.rs index 88ad2c7c..924461d8 100644 --- a/src/bin/bat/main.rs +++ b/src/bin/bat/main.rs @@ -26,7 +26,7 @@ use clap::crate_version; use directories::PROJECT_DIRS; use bat::{ - config::{Config, InputFile, OrdinaryFile, StyleComponent, StyleComponents}, + config::{Config, Input, OrdinaryFile, StyleComponent, StyleComponents}, errors::*, Controller, HighlightingAssets, }; @@ -134,7 +134,7 @@ pub fn list_themes(cfg: &Config) -> Result<()> { )?; config.theme = theme.to_string(); Controller::new(&config, &assets) - .run(vec![InputFile::ThemePreviewFile]) + .run(vec![Input::ThemePreviewFile]) .ok(); writeln!(stdout)?; } @@ -147,7 +147,7 @@ pub fn list_themes(cfg: &Config) -> Result<()> { Ok(()) } -fn run_controller(inputs: Vec, config: &Config) -> Result { +fn run_controller(inputs: Vec, config: &Config) -> Result { let assets = assets_from_cache_or_binary()?; let controller = Controller::new(&config, &assets); controller.run(inputs) @@ -167,10 +167,10 @@ fn run() -> Result { run_cache_subcommand(cache_matches)?; Ok(true) } else { - let inputs = vec![InputFile::Ordinary(OrdinaryFile::from_path(OsStr::new( + let inputs = vec![Input::Ordinary(OrdinaryFile::from_path(OsStr::new( "cache", )))]; - let mut config = app.config(&inputs)?; + let config = app.config(&inputs)?; run_controller(inputs, &config) } diff --git a/src/config.rs b/src/config.rs index 42ff6bdc..960ac63e 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,5 +1,5 @@ -pub use crate::inputfile::InputFile; -pub use crate::inputfile::OrdinaryFile; +pub use crate::input::Input; +pub use crate::input::OrdinaryFile; pub use crate::line_range::{HighlightedLineRanges, LineRange, LineRanges}; pub use crate::style::{StyleComponent, StyleComponents}; pub use crate::syntax_mapping::{MappingTarget, SyntaxMapping}; diff --git a/src/controller.rs b/src/controller.rs index f227446e..f9cf78ac 100644 --- a/src/controller.rs +++ b/src/controller.rs @@ -5,7 +5,7 @@ use crate::config::Config; #[cfg(feature = "paging")] use crate::config::PagingMode; use crate::errors::*; -use crate::inputfile::{InputFile, InputFileReader}; +use crate::input::{Input, InputReader}; use crate::line_range::{LineRanges, RangeCheckResult}; use crate::output::OutputType; use crate::printer::{InteractivePrinter, Printer, SimplePrinter}; @@ -20,13 +20,13 @@ impl<'b> Controller<'b> { Controller { config, assets } } - pub fn run(&self, inputs: Vec) -> Result { + pub fn run(&self, inputs: Vec) -> Result { self.run_with_error_handler(inputs, default_error_handler) } pub fn run_with_error_handler( &self, - inputs: Vec, + inputs: Vec, handle_error: impl Fn(&Error), ) -> Result { let mut output_type; @@ -39,7 +39,7 @@ impl<'b> Controller<'b> { let mut paging_mode = self.config.paging_mode; if self.config.paging_mode != PagingMode::Never { let call_pager = inputs.iter().any(|file| { - if let InputFile::Ordinary(ofile) = file { + if let Input::Ordinary(ofile) = file { return Path::new(ofile.provided_path()).exists(); } else { return true; @@ -60,8 +60,8 @@ impl<'b> Controller<'b> { let writer = output_type.handle()?; let mut no_errors: bool = true; - for input_file in inputs.into_iter() { - match input_file.get_reader(io::stdin().lock()) { + for input in inputs.into_iter() { + match input.get_reader(io::stdin().lock()) { Err(error) => { handle_error(&error); no_errors = false; @@ -69,15 +69,15 @@ impl<'b> Controller<'b> { Ok(mut reader) => { let result = if self.config.loop_through { let mut printer = SimplePrinter::new(); - self.print_file(reader, &mut printer, writer, &input_file) + self.print_file(reader, &mut printer, writer, &input) } else { let mut printer = InteractivePrinter::new( &self.config, &self.assets, - &input_file, + &input, &mut reader, ); - self.print_file(reader, &mut printer, writer, &input_file) + self.print_file(reader, &mut printer, writer, &input) }; if let Err(error) = result { @@ -93,13 +93,13 @@ impl<'b> Controller<'b> { fn print_file<'a, P: Printer>( &self, - reader: InputFileReader, + reader: InputReader, printer: &mut P, writer: &mut dyn Write, - input_file: &InputFile, + input: &Input, ) -> Result<()> { if !reader.first_line.is_empty() || self.config.style_components.header() { - printer.print_header(writer, input_file)?; + printer.print_header(writer, input)?; } if !reader.first_line.is_empty() { @@ -114,7 +114,7 @@ impl<'b> Controller<'b> { &self, printer: &mut P, writer: &mut dyn Write, - mut reader: InputFileReader, + mut reader: InputReader, line_ranges: &LineRanges, ) -> Result<()> { let mut line_buffer = Vec::new(); diff --git a/src/inputfile.rs b/src/input.rs similarity index 84% rename from src/inputfile.rs rename to src/input.rs index 1a9a3a4e..7b2c371f 100644 --- a/src/inputfile.rs +++ b/src/input.rs @@ -8,14 +8,14 @@ use crate::errors::*; const THEME_PREVIEW_FILE: &[u8] = include_bytes!("../assets/theme_preview.rs"); -pub struct InputFileReader<'a> { +pub struct InputReader<'a> { inner: Box, pub(crate) first_line: Vec, pub(crate) content_type: Option, } -impl<'a> InputFileReader<'a> { - fn new(mut reader: R) -> InputFileReader<'a> { +impl<'a> InputReader<'a> { + fn new(mut reader: R) -> InputReader<'a> { let mut first_line = vec![]; reader.read_until(b'\n', &mut first_line).ok(); @@ -29,7 +29,7 @@ impl<'a> InputFileReader<'a> { reader.read_until(0x00, &mut first_line).ok(); } - InputFileReader { + InputReader { inner: Box::new(reader), first_line, content_type, @@ -77,18 +77,18 @@ impl OrdinaryFile { } } -pub enum InputFile { +pub enum Input { StdIn(Option), Ordinary(OrdinaryFile), FromReader(Box, Option), ThemePreviewFile, } -impl InputFile { - pub(crate) fn get_reader<'a, R: BufRead + 'a>(&self, stdin: R) -> Result> { +impl Input { + pub(crate) fn get_reader<'a, R: BufRead + 'a>(&self, stdin: R) -> Result> { match self { - InputFile::StdIn(_) => Ok(InputFileReader::new(stdin)), - InputFile::Ordinary(ofile) => { + Input::StdIn(_) => Ok(InputReader::new(stdin)), + Input::Ordinary(ofile) => { let file = File::open(&ofile.path) .map_err(|e| format!("'{}': {}", ofile.path.to_string_lossy(), e))?; @@ -98,10 +98,10 @@ impl InputFile { ); } - Ok(InputFileReader::new(BufReader::new(file))) + Ok(InputReader::new(BufReader::new(file))) } - InputFile::ThemePreviewFile => Ok(InputFileReader::new(THEME_PREVIEW_FILE)), - InputFile::FromReader(reader, _) => unimplemented!(), //Ok(InputFileReader::new(BufReader::new(reader))), + Input::ThemePreviewFile => Ok(InputReader::new(THEME_PREVIEW_FILE)), + Input::FromReader(_, _) => unimplemented!(), //Ok(InputReader::new(BufReader::new(reader))), } } } @@ -109,7 +109,7 @@ impl InputFile { #[test] fn basic() { let content = b"#!/bin/bash\necho hello"; - let mut reader = InputFileReader::new(&content[..]); + let mut reader = InputReader::new(&content[..]); assert_eq!(b"#!/bin/bash\n", &reader.first_line[..]); @@ -138,7 +138,7 @@ fn basic() { #[test] fn utf16le() { let content = b"\xFF\xFE\x73\x00\x0A\x00\x64\x00"; - let mut reader = InputFileReader::new(&content[..]); + let mut reader = InputReader::new(&content[..]); assert_eq!(b"\xFF\xFE\x73\x00\x0A\x00", &reader.first_line[..]); diff --git a/src/lib.rs b/src/lib.rs index 07f6d191..1352cb2b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,7 +8,7 @@ pub(crate) mod controller; mod decorations; mod diff; pub mod errors; -pub(crate) mod inputfile; +pub(crate) mod input; mod less; pub(crate) mod line_range; mod output; diff --git a/src/pretty_printer.rs b/src/pretty_printer.rs index 03774397..a517de0f 100644 --- a/src/pretty_printer.rs +++ b/src/pretty_printer.rs @@ -2,7 +2,7 @@ use std::ffi::OsStr; use crate::{ config::{ - Config, HighlightedLineRanges, InputFile, LineRanges, OrdinaryFile, StyleComponents, + Config, HighlightedLineRanges, Input, LineRanges, OrdinaryFile, StyleComponents, SyntaxMapping, WrappingMode, }, errors::Result, @@ -13,7 +13,7 @@ use crate::{ use crate::config::PagingMode; pub struct PrettyPrinter<'a> { - inputs: Vec, + inputs: Vec, config: Config<'a>, assets: HighlightingAssets, } @@ -35,7 +35,7 @@ impl<'a> PrettyPrinter<'a> { /// Add a file which should be pretty-printed pub fn file(&mut self, path: &OsStr) -> &mut Self { self.inputs - .push(InputFile::Ordinary(OrdinaryFile::from_path(path))); + .push(Input::Ordinary(OrdinaryFile::from_path(path))); self } @@ -47,7 +47,7 @@ impl<'a> PrettyPrinter<'a> { { for path in paths { self.inputs - .push(InputFile::Ordinary(OrdinaryFile::from_path(path.as_ref()))); + .push(Input::Ordinary(OrdinaryFile::from_path(path.as_ref()))); } self } diff --git a/src/printer.rs b/src/printer.rs index 8e582f80..b61c8063 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -27,14 +27,14 @@ use crate::decorations::{Decoration, GridBorderDecoration, LineNumberDecoration} #[cfg(feature = "git")] use crate::diff::{get_git_diff, LineChanges}; use crate::errors::*; -use crate::inputfile::{InputFile, InputFileReader}; +use crate::input::{Input, InputReader}; use crate::line_range::RangeCheckResult; use crate::preprocessor::{expand_tabs, replace_nonprintable}; use crate::terminal::{as_terminal_escaped, to_ansi_color}; use crate::wrap::WrappingMode; pub trait Printer { - fn print_header(&mut self, handle: &mut dyn Write, file: &InputFile) -> Result<()>; + fn print_header(&mut self, handle: &mut dyn Write, file: &Input) -> Result<()>; fn print_footer(&mut self, handle: &mut dyn Write) -> Result<()>; fn print_snip(&mut self, handle: &mut dyn Write) -> Result<()>; @@ -57,7 +57,7 @@ impl SimplePrinter { } impl Printer for SimplePrinter { - fn print_header(&mut self, _handle: &mut dyn Write, _file: &InputFile) -> Result<()> { + fn print_header(&mut self, _handle: &mut dyn Write, _file: &Input) -> Result<()> { Ok(()) } @@ -101,8 +101,8 @@ impl<'a> InteractivePrinter<'a> { pub fn new( config: &'a Config, assets: &'a HighlightingAssets, - file: &InputFile, - reader: &mut InputFileReader, + file: &Input, + reader: &mut InputReader, ) -> Self { let theme = assets.get_theme(&config.theme); @@ -160,7 +160,7 @@ impl<'a> InteractivePrinter<'a> { #[cfg(feature = "git")] { if config.style_components.changes() { - if let InputFile::Ordinary(ofile) = file { + if let Input::Ordinary(ofile) = file { line_changes = get_git_diff(ofile.provided_path()); } } @@ -230,23 +230,23 @@ impl<'a> InteractivePrinter<'a> { } impl<'a> Printer for InteractivePrinter<'a> { - fn print_header(&mut self, handle: &mut dyn Write, file: &InputFile) -> Result<()> { + fn print_header(&mut self, handle: &mut dyn Write, file: &Input) -> Result<()> { if !self.config.style_components.header() { if Some(ContentType::BINARY) == self.content_type && !self.config.show_nonprintable { let input = match file { - InputFile::Ordinary(ofile) => { + Input::Ordinary(ofile) => { format!("file '{}'", &ofile.provided_path().to_string_lossy()) } - InputFile::StdIn(Some(name)) => format!( + Input::StdIn(Some(name)) => format!( "STDIN (with name '{}')", name.to_string_lossy().into_owned() ), - InputFile::StdIn(None) => "STDIN".to_owned(), - InputFile::ThemePreviewFile => "".to_owned(), - InputFile::FromReader(_, Some(name)) => { + Input::StdIn(None) => "STDIN".to_owned(), + Input::ThemePreviewFile => "".to_owned(), + Input::FromReader(_, Some(name)) => { format!("file '{}'", name.to_string_lossy()) } - InputFile::FromReader(_, None) => "READER".to_owned(), + Input::FromReader(_, None) => "READER".to_owned(), }; writeln!( @@ -281,19 +281,17 @@ impl<'a> Printer for InteractivePrinter<'a> { } let (prefix, name) = match file { - InputFile::Ordinary(ofile) => ( + Input::Ordinary(ofile) => ( "File: ", Cow::from(ofile.provided_path().to_string_lossy().to_owned()), ), - InputFile::StdIn(Some(name)) => { + Input::StdIn(Some(name)) => ("File: ", Cow::from(name.to_string_lossy().to_owned())), + Input::StdIn(None) => ("File: ", Cow::from("STDIN".to_owned())), + Input::ThemePreviewFile => ("", Cow::from("")), + Input::FromReader(_, Some(name)) => { ("File: ", Cow::from(name.to_string_lossy().to_owned())) } - InputFile::StdIn(None) => ("File: ", Cow::from("STDIN".to_owned())), - InputFile::ThemePreviewFile => ("", Cow::from("")), - InputFile::FromReader(_, Some(name)) => { - ("File: ", Cow::from(name.to_string_lossy().to_owned())) - } - InputFile::FromReader(_, None) => ("File: ", Cow::from("READER".to_owned())), + Input::FromReader(_, None) => ("File: ", Cow::from("READER".to_owned())), }; let mode = match self.content_type {