on master help sign in
Rename InputFile => Input
commitf8d0956893a0a6e42f9caddbb8e54a25c74d1166 authorsharkdp <davidpeter@web.de> dateTue 21 Apr 2020, 21:19 (7 years ago) parent1dc328a
9 files changed+82−84
src/assets.rs+16−16 src/bin/bat/app.rs+8−8 src/bin/bat/main.rs+5−5 src/config.rs+2−2 src/controller.rs+13−13 src/inputfile.rs → src/input.rs+14−14 src/lib.rs+1−1 src/pretty_printer.rs+4−4 src/printer.rs+19−21
src/assets.rs+16−16
@@ -10,7 +10,7 @@ use syntect::parsing::{SyntaxReference, SyntaxSet, SyntaxSetBuilder};
1010
1111use crate::assets_metadata::AssetsMetadata;
1212use crate::errors::*;
13−use crate::inputfile::{InputFile, InputFileReader};
13+use crate::input::{Input, InputReader};
1414use crate::syntax_mapping::{MappingTarget, SyntaxMapping};
1515
1616#[derive(Debug)]
@@ -188,13 +188,13 @@ impl HighlightingAssets {
188188 pub(crate) fn get_syntax(
189189 &self,
190190 language: Option<&str>,
191− file: &InputFile,
192− reader: &mut InputFileReader,
191+ file: &Input,
192+ reader: &mut InputReader,
193193 mapping: &SyntaxMapping,
194194 ) -> &SyntaxReference {
195195 let syntax = match (language, file) {
196196 (Some(language), _) => self.syntax_set.find_syntax_by_token(language),
197− (None, InputFile::Ordinary(ofile)) => {
197+ (None, Input::Ordinary(ofile)) => {
198198 let path = Path::new(ofile.provided_path());
199199 let line_syntax = self.get_first_line_syntax(reader);
200200
@@ -212,14 +212,14 @@ impl HighlightingAssets {
212212 }
213213 }
214214 }
215− (None, InputFile::StdIn(None)) => String::from_utf8(reader.first_line.clone())
215+ (None, Input::StdIn(None)) => String::from_utf8(reader.first_line.clone())
216216 .ok()
217217 .and_then(|l| self.syntax_set.find_syntax_by_first_line(&l)),
218− (None, InputFile::StdIn(Some(file_name))) => self
218+ (None, Input::StdIn(Some(file_name))) => self
219219 .get_extension_syntax(&file_name)
220220 .or(self.get_first_line_syntax(reader)),
221− (_, InputFile::ThemePreviewFile) => self.syntax_set.find_syntax_by_name("Rust"),
222− (None, InputFile::FromReader(s, _)) => None,
221+ (_, Input::ThemePreviewFile) => self.syntax_set.find_syntax_by_name("Rust"),
222+ (None, Input::FromReader(_, _)) => unimplemented!(),
223223 };
224224
225225 syntax.unwrap_or_else(|| self.syntax_set.find_syntax_plain_text())
@@ -238,7 +238,7 @@ impl HighlightingAssets {
238238 })
239239 }
240240
241− fn get_first_line_syntax(&self, reader: &mut InputFileReader) -> Option<&SyntaxReference> {
241+ fn get_first_line_syntax(&self, reader: &mut InputReader) -> Option<&SyntaxReference> {
242242 String::from_utf8(reader.first_line.clone())
243243 .ok()
244244 .and_then(|l| self.syntax_set.find_syntax_by_first_line(&l))
@@ -249,7 +249,7 @@ impl HighlightingAssets {
249249mod tests {
250250 use super::*;
251251
252− use crate::inputfile::OrdinaryFile;
252+ use crate::input::OrdinaryFile;
253253
254254 use std::ffi::{OsStr, OsString};
255255 use std::fs::File;
@@ -281,11 +281,11 @@ mod tests {
281281 writeln!(temp_file, "{}", first_line).unwrap();
282282 }
283283
284− let input_file = InputFile::Ordinary(OrdinaryFile::from_path(file_path.as_os_str()));
284+ let input = Input::Ordinary(OrdinaryFile::from_path(file_path.as_os_str()));
285285 let syntax = self.assets.get_syntax(
286286 None,
287− &input_file,
288− &mut input_file.get_reader(io::stdin().lock()).unwrap(),
287+ &input,
288+ &mut input.get_reader(io::stdin().lock()).unwrap(),
289289 &self.syntax_mapping,
290290 );
291291
@@ -305,11 +305,11 @@ mod tests {
305305 }
306306
307307 fn syntax_for_stdin_with_content(&self, file_name: &str, content: &[u8]) -> String {
308− let input_file = InputFile::StdIn(Some(OsString::from(file_name)));
308+ let input = Input::StdIn(Some(OsString::from(file_name)));
309309 let syntax = self.assets.get_syntax(
310310 None,
311− &input_file,
312− &mut input_file.get_reader(content).unwrap(),
311+ &input,
312+ &mut input.get_reader(content).unwrap(),
313313 &self.syntax_mapping,
314314 );
315315 syntax.name.clone()
src/bin/bat/app.rs+8−8
@@ -15,8 +15,8 @@ use console::Term;
1515
1616use bat::{
1717 config::{
18− Config, HighlightedLineRanges, InputFile, LineRange, LineRanges, MappingTarget,
19− OrdinaryFile, PagingMode, StyleComponent, StyleComponents, SyntaxMapping, WrappingMode,
18+ Config, HighlightedLineRanges, Input, LineRange, LineRanges, MappingTarget, OrdinaryFile,
19+ PagingMode, StyleComponent, StyleComponents, SyntaxMapping, WrappingMode,
2020 },
2121 errors::*,
2222 HighlightingAssets,
@@ -73,7 +73,7 @@ impl App {
7373 Ok(clap_app::build_app(interactive_output).get_matches_from(args))
7474 }
7575
76− pub fn config(&self, inputs: &[InputFile]) -> Result<Config> {
76+ pub fn config(&self, inputs: &[Input]) -> Result<Config> {
7777 let style_components = self.style_components()?;
7878
7979 let paging_mode = match self.matches.value_of("paging") {
@@ -84,7 +84,7 @@ impl App {
8484 // If we have -pp as an option when in auto mode, the pager should be disabled.
8585 PagingMode::Never
8686 } else if inputs.iter().any(|f| {
87− if let InputFile::StdIn(None) = f {
87+ if let Input::StdIn(None) = f {
8888 true
8989 } else {
9090 false
@@ -226,7 +226,7 @@ impl App {
226226 })
227227 }
228228
229− pub fn inputs(&self) -> Result<Vec<InputFile>> {
229+ pub fn inputs(&self) -> Result<Vec<Input>> {
230230 // verify equal length of file-names and input FILEs
231231 match self.matches.values_of("file-name") {
232232 Some(ref filenames)
@@ -251,7 +251,7 @@ impl App {
251251 let files: Option<Vec<&OsStr>> = self.matches.values_of_os("FILE").map(|vs| vs.collect());
252252
253253 if files.is_none() {
254− return Ok(vec![InputFile::StdIn(
254+ return Ok(vec![Input::StdIn(
255255 filenames_or_none.nth(0).unwrap().map(|f| f.to_owned()),
256256 )]);
257257 }
@@ -264,13 +264,13 @@ impl App {
264264 for (input, name) in files_or_none.zip(filenames_or_none) {
265265 if let Some(input) = input {
266266 if input.to_str().unwrap_or_default() == "-" {
267− file_input.push(InputFile::StdIn(name.map(|n| n.to_owned())));
267+ file_input.push(Input::StdIn(name.map(|n| n.to_owned())));
268268 } else {
269269 let mut ofile = OrdinaryFile::from_path(input);
270270 if let Some(path) = name {
271271 ofile.set_provided_path(path);
272272 }
273− file_input.push(InputFile::Ordinary(ofile))
273+ file_input.push(Input::Ordinary(ofile))
274274 }
275275 }
276276 }
src/bin/bat/main.rs+5−5
@@ -26,7 +26,7 @@ use clap::crate_version;
2626use directories::PROJECT_DIRS;
2727
2828use bat::{
29− config::{Config, InputFile, OrdinaryFile, StyleComponent, StyleComponents},
29+ config::{Config, Input, OrdinaryFile, StyleComponent, StyleComponents},
3030 errors::*,
3131 Controller, HighlightingAssets,
3232};
@@ -134,7 +134,7 @@ pub fn list_themes(cfg: &Config) -> Result<()> {
134134 )?;
135135 config.theme = theme.to_string();
136136 Controller::new(&config, &assets)
137− .run(vec![InputFile::ThemePreviewFile])
137+ .run(vec![Input::ThemePreviewFile])
138138 .ok();
139139 writeln!(stdout)?;
140140 }
@@ -147,7 +147,7 @@ pub fn list_themes(cfg: &Config) -> Result<()> {
147147 Ok(())
148148}
149149
150−fn run_controller(inputs: Vec<InputFile>, config: &Config) -> Result<bool> {
150+fn run_controller(inputs: Vec<Input>, config: &Config) -> Result<bool> {
151151 let assets = assets_from_cache_or_binary()?;
152152 let controller = Controller::new(&config, &assets);
153153 controller.run(inputs)
@@ -167,10 +167,10 @@ fn run() -> Result<bool> {
167167 run_cache_subcommand(cache_matches)?;
168168 Ok(true)
169169 } else {
170− let inputs = vec![InputFile::Ordinary(OrdinaryFile::from_path(OsStr::new(
170+ let inputs = vec![Input::Ordinary(OrdinaryFile::from_path(OsStr::new(
171171 "cache",
172172 )))];
173− let mut config = app.config(&inputs)?;
173+ let config = app.config(&inputs)?;
174174
175175 run_controller(inputs, &config)
176176 }
src/config.rs+2−2
@@ -1,5 +1,5 @@
1−pub use crate::inputfile::InputFile;
2−pub use crate::inputfile::OrdinaryFile;
1+pub use crate::input::Input;
2+pub use crate::input::OrdinaryFile;
33pub use crate::line_range::{HighlightedLineRanges, LineRange, LineRanges};
44pub use crate::style::{StyleComponent, StyleComponents};
55pub use crate::syntax_mapping::{MappingTarget, SyntaxMapping};
src/controller.rs+13−13
@@ -5,7 +5,7 @@ use crate::config::Config;
55#[cfg(feature = "paging")]
66use crate::config::PagingMode;
77use crate::errors::*;
8−use crate::inputfile::{InputFile, InputFileReader};
8+use crate::input::{Input, InputReader};
99use crate::line_range::{LineRanges, RangeCheckResult};
1010use crate::output::OutputType;
1111use crate::printer::{InteractivePrinter, Printer, SimplePrinter};
@@ -20,13 +20,13 @@ impl<'b> Controller<'b> {
2020 Controller { config, assets }
2121 }
2222
23− pub fn run(&self, inputs: Vec<InputFile>) -> Result<bool> {
23+ pub fn run(&self, inputs: Vec<Input>) -> Result<bool> {
2424 self.run_with_error_handler(inputs, default_error_handler)
2525 }
2626
2727 pub fn run_with_error_handler(
2828 &self,
29− inputs: Vec<InputFile>,
29+ inputs: Vec<Input>,
3030 handle_error: impl Fn(&Error),
3131 ) -> Result<bool> {
3232 let mut output_type;
@@ -39,7 +39,7 @@ impl<'b> Controller<'b> {
3939 let mut paging_mode = self.config.paging_mode;
4040 if self.config.paging_mode != PagingMode::Never {
4141 let call_pager = inputs.iter().any(|file| {
42− if let InputFile::Ordinary(ofile) = file {
42+ if let Input::Ordinary(ofile) = file {
4343 return Path::new(ofile.provided_path()).exists();
4444 } else {
4545 return true;
@@ -60,8 +60,8 @@ impl<'b> Controller<'b> {
6060 let writer = output_type.handle()?;
6161 let mut no_errors: bool = true;
6262
63− for input_file in inputs.into_iter() {
64− match input_file.get_reader(io::stdin().lock()) {
63+ for input in inputs.into_iter() {
64+ match input.get_reader(io::stdin().lock()) {
6565 Err(error) => {
6666 handle_error(&error);
6767 no_errors = false;
@@ -69,15 +69,15 @@ impl<'b> Controller<'b> {
6969 Ok(mut reader) => {
7070 let result = if self.config.loop_through {
7171 let mut printer = SimplePrinter::new();
72− self.print_file(reader, &mut printer, writer, &input_file)
72+ self.print_file(reader, &mut printer, writer, &input)
7373 } else {
7474 let mut printer = InteractivePrinter::new(
7575 &self.config,
7676 &self.assets,
77− &input_file,
77+ &input,
7878 &mut reader,
7979 );
80− self.print_file(reader, &mut printer, writer, &input_file)
80+ self.print_file(reader, &mut printer, writer, &input)
8181 };
8282
8383 if let Err(error) = result {
@@ -93,13 +93,13 @@ impl<'b> Controller<'b> {
9393
9494 fn print_file<'a, P: Printer>(
9595 &self,
96− reader: InputFileReader,
96+ reader: InputReader,
9797 printer: &mut P,
9898 writer: &mut dyn Write,
99− input_file: &InputFile,
99+ input: &Input,
100100 ) -> Result<()> {
101101 if !reader.first_line.is_empty() || self.config.style_components.header() {
102− printer.print_header(writer, input_file)?;
102+ printer.print_header(writer, input)?;
103103 }
104104
105105 if !reader.first_line.is_empty() {
@@ -114,7 +114,7 @@ impl<'b> Controller<'b> {
114114 &self,
115115 printer: &mut P,
116116 writer: &mut dyn Write,
117− mut reader: InputFileReader,
117+ mut reader: InputReader,
118118 line_ranges: &LineRanges,
119119 ) -> Result<()> {
120120 let mut line_buffer = Vec::new();
src/inputfile.rs → src/input.rsrenamed+14−14
@@ -8,14 +8,14 @@ use crate::errors::*;
88
99const THEME_PREVIEW_FILE: &[u8] = include_bytes!("../assets/theme_preview.rs");
1010
11−pub struct InputFileReader<'a> {
11+pub struct InputReader<'a> {
1212 inner: Box<dyn BufRead + 'a>,
1313 pub(crate) first_line: Vec<u8>,
1414 pub(crate) content_type: Option<ContentType>,
1515}
1616
17−impl<'a> InputFileReader<'a> {
18− fn new<R: BufRead + 'a>(mut reader: R) -> InputFileReader<'a> {
17+impl<'a> InputReader<'a> {
18+ fn new<R: BufRead + 'a>(mut reader: R) -> InputReader<'a> {
1919 let mut first_line = vec![];
2020 reader.read_until(b'\n', &mut first_line).ok();
2121
@@ -29,7 +29,7 @@ impl<'a> InputFileReader<'a> {
2929 reader.read_until(0x00, &mut first_line).ok();
3030 }
3131
32− InputFileReader {
32+ InputReader {
3333 inner: Box::new(reader),
3434 first_line,
3535 content_type,
@@ -77,18 +77,18 @@ impl OrdinaryFile {
7777 }
7878}
7979
80−pub enum InputFile {
80+pub enum Input {
8181 StdIn(Option<OsString>),
8282 Ordinary(OrdinaryFile),
8383 FromReader(Box<dyn Read>, Option<OsString>),
8484 ThemePreviewFile,
8585}
8686
87−impl InputFile {
88− pub(crate) fn get_reader<'a, R: BufRead + 'a>(&self, stdin: R) -> Result<InputFileReader<'a>> {
87+impl Input {
88+ pub(crate) fn get_reader<'a, R: BufRead + 'a>(&self, stdin: R) -> Result<InputReader<'a>> {
8989 match self {
90− InputFile::StdIn(_) => Ok(InputFileReader::new(stdin)),
91− InputFile::Ordinary(ofile) => {
90+ Input::StdIn(_) => Ok(InputReader::new(stdin)),
91+ Input::Ordinary(ofile) => {
9292 let file = File::open(&ofile.path)
9393 .map_err(|e| format!("'{}': {}", ofile.path.to_string_lossy(), e))?;
9494
@@ -98,10 +98,10 @@ impl InputFile {
9898 );
9999 }
100100
101− Ok(InputFileReader::new(BufReader::new(file)))
101+ Ok(InputReader::new(BufReader::new(file)))
102102 }
103− InputFile::ThemePreviewFile => Ok(InputFileReader::new(THEME_PREVIEW_FILE)),
104− InputFile::FromReader(reader, _) => unimplemented!(), //Ok(InputFileReader::new(BufReader::new(reader))),
103+ Input::ThemePreviewFile => Ok(InputReader::new(THEME_PREVIEW_FILE)),
104+ Input::FromReader(_, _) => unimplemented!(), //Ok(InputReader::new(BufReader::new(reader))),
105105 }
106106 }
107107}
@@ -109,7 +109,7 @@ impl InputFile {
109109#[test]
110110fn basic() {
111111 let content = b"#!/bin/bash\necho hello";
112− let mut reader = InputFileReader::new(&content[..]);
112+ let mut reader = InputReader::new(&content[..]);
113113
114114 assert_eq!(b"#!/bin/bash\n", &reader.first_line[..]);
115115
@@ -138,7 +138,7 @@ fn basic() {
138138#[test]
139139fn utf16le() {
140140 let content = b"\xFF\xFE\x73\x00\x0A\x00\x64\x00";
141− let mut reader = InputFileReader::new(&content[..]);
141+ let mut reader = InputReader::new(&content[..]);
142142
143143 assert_eq!(b"\xFF\xFE\x73\x00\x0A\x00", &reader.first_line[..]);
144144
src/lib.rs+1−1
@@ -8,7 +8,7 @@ pub(crate) mod controller;
88mod decorations;
99mod diff;
1010pub mod errors;
11−pub(crate) mod inputfile;
11+pub(crate) mod input;
1212mod less;
1313pub(crate) mod line_range;
1414mod output;
src/pretty_printer.rs+4−4
@@ -2,7 +2,7 @@ use std::ffi::OsStr;
22
33use crate::{
44 config::{
5− Config, HighlightedLineRanges, InputFile, LineRanges, OrdinaryFile, StyleComponents,
5+ Config, HighlightedLineRanges, Input, LineRanges, OrdinaryFile, StyleComponents,
66 SyntaxMapping, WrappingMode,
77 },
88 errors::Result,
@@ -13,7 +13,7 @@ use crate::{
1313use crate::config::PagingMode;
1414
1515pub struct PrettyPrinter<'a> {
16− inputs: Vec<InputFile>,
16+ inputs: Vec<Input>,
1717 config: Config<'a>,
1818 assets: HighlightingAssets,
1919}
@@ -35,7 +35,7 @@ impl<'a> PrettyPrinter<'a> {
3535 /// Add a file which should be pretty-printed
3636 pub fn file(&mut self, path: &OsStr) -> &mut Self {
3737 self.inputs
38− .push(InputFile::Ordinary(OrdinaryFile::from_path(path)));
38+ .push(Input::Ordinary(OrdinaryFile::from_path(path)));
3939 self
4040 }
4141
@@ -47,7 +47,7 @@ impl<'a> PrettyPrinter<'a> {
4747 {
4848 for path in paths {
4949 self.inputs
50− .push(InputFile::Ordinary(OrdinaryFile::from_path(path.as_ref())));
50+ .push(Input::Ordinary(OrdinaryFile::from_path(path.as_ref())));
5151 }
5252 self
5353 }
src/printer.rs+19−21
@@ -27,14 +27,14 @@ use crate::decorations::{Decoration, GridBorderDecoration, LineNumberDecoration}
2727#[cfg(feature = "git")]
2828use crate::diff::{get_git_diff, LineChanges};
2929use crate::errors::*;
30−use crate::inputfile::{InputFile, InputFileReader};
30+use crate::input::{Input, InputReader};
3131use crate::line_range::RangeCheckResult;
3232use crate::preprocessor::{expand_tabs, replace_nonprintable};
3333use crate::terminal::{as_terminal_escaped, to_ansi_color};
3434use crate::wrap::WrappingMode;
3535
3636pub trait Printer {
37− fn print_header(&mut self, handle: &mut dyn Write, file: &InputFile) -> Result<()>;
37+ fn print_header(&mut self, handle: &mut dyn Write, file: &Input) -> Result<()>;
3838 fn print_footer(&mut self, handle: &mut dyn Write) -> Result<()>;
3939
4040 fn print_snip(&mut self, handle: &mut dyn Write) -> Result<()>;
@@ -57,7 +57,7 @@ impl SimplePrinter {
5757}
5858
5959impl Printer for SimplePrinter {
60− fn print_header(&mut self, _handle: &mut dyn Write, _file: &InputFile) -> Result<()> {
60+ fn print_header(&mut self, _handle: &mut dyn Write, _file: &Input) -> Result<()> {
6161 Ok(())
6262 }
6363
@@ -101,8 +101,8 @@ impl<'a> InteractivePrinter<'a> {
101101 pub fn new(
102102 config: &'a Config,
103103 assets: &'a HighlightingAssets,
104− file: &InputFile,
105− reader: &mut InputFileReader,
104+ file: &Input,
105+ reader: &mut InputReader,
106106 ) -> Self {
107107 let theme = assets.get_theme(&config.theme);
108108
@@ -160,7 +160,7 @@ impl<'a> InteractivePrinter<'a> {
160160 #[cfg(feature = "git")]
161161 {
162162 if config.style_components.changes() {
163− if let InputFile::Ordinary(ofile) = file {
163+ if let Input::Ordinary(ofile) = file {
164164 line_changes = get_git_diff(ofile.provided_path());
165165 }
166166 }
@@ -230,23 +230,23 @@ impl<'a> InteractivePrinter<'a> {
230230}
231231
232232impl<'a> Printer for InteractivePrinter<'a> {
233− fn print_header(&mut self, handle: &mut dyn Write, file: &InputFile) -> Result<()> {
233+ fn print_header(&mut self, handle: &mut dyn Write, file: &Input) -> Result<()> {
234234 if !self.config.style_components.header() {
235235 if Some(ContentType::BINARY) == self.content_type && !self.config.show_nonprintable {
236236 let input = match file {
237− InputFile::Ordinary(ofile) => {
237+ Input::Ordinary(ofile) => {
238238 format!("file '{}'", &ofile.provided_path().to_string_lossy())
239239 }
240− InputFile::StdIn(Some(name)) => format!(
240+ Input::StdIn(Some(name)) => format!(
241241 "STDIN (with name '{}')",
242242 name.to_string_lossy().into_owned()
243243 ),
244− InputFile::StdIn(None) => "STDIN".to_owned(),
245− InputFile::ThemePreviewFile => "".to_owned(),
246− InputFile::FromReader(_, Some(name)) => {
244+ Input::StdIn(None) => "STDIN".to_owned(),
245+ Input::ThemePreviewFile => "".to_owned(),
246+ Input::FromReader(_, Some(name)) => {
247247 format!("file '{}'", name.to_string_lossy())
248248 }
249− InputFile::FromReader(_, None) => "READER".to_owned(),
249+ Input::FromReader(_, None) => "READER".to_owned(),
250250 };
251251
252252 writeln!(
@@ -281,19 +281,17 @@ impl<'a> Printer for InteractivePrinter<'a> {
281281 }
282282
283283 let (prefix, name) = match file {
284− InputFile::Ordinary(ofile) => (
284+ Input::Ordinary(ofile) => (
285285 "File: ",
286286 Cow::from(ofile.provided_path().to_string_lossy().to_owned()),
287287 ),
288− InputFile::StdIn(Some(name)) => {
288+ Input::StdIn(Some(name)) => ("File: ", Cow::from(name.to_string_lossy().to_owned())),
289+ Input::StdIn(None) => ("File: ", Cow::from("STDIN".to_owned())),
290+ Input::ThemePreviewFile => ("", Cow::from("")),
291+ Input::FromReader(_, Some(name)) => {
289292 ("File: ", Cow::from(name.to_string_lossy().to_owned()))
290293 }
291− InputFile::StdIn(None) => ("File: ", Cow::from("STDIN".to_owned())),
292− InputFile::ThemePreviewFile => ("", Cow::from("")),
293− InputFile::FromReader(_, Some(name)) => {
294− ("File: ", Cow::from(name.to_string_lossy().to_owned()))
295− }
296− InputFile::FromReader(_, None) => ("File: ", Cow::from("READER".to_owned())),
294+ Input::FromReader(_, None) => ("File: ", Cow::from("READER".to_owned())),
297295 };
298296
299297 let mode = match self.content_type {