diff --git a/src/bin/bat/app.rs b/src/bin/bat/app.rs index b97d3ab8..432b8c57 100644 --- a/src/bin/bat/app.rs +++ b/src/bin/bat/app.rs @@ -13,6 +13,7 @@ use clap::ArgMatches; use console::Term; +use crate::input::{new_file_input, new_stdin_input}; use bat::{ assets::HighlightingAssets, config::{Config, VisibleLines}, @@ -257,8 +258,9 @@ impl App { let files: Option> = self.matches.values_of_os("FILE").map(|vs| vs.collect()); if files.is_none() { - let input = Input::stdin_as_file(filenames_or_none.next().unwrap_or(None)); - return Ok(vec![input]); + return Ok(vec![new_stdin_input( + filenames_or_none.next().unwrap_or(None), + )]); } let files_or_none: Box> = match files { Some(ref files) => Box::new(files.iter().map(|name| Some(*name))), @@ -269,9 +271,9 @@ impl App { for (filepath, provided_name) in files_or_none.zip(filenames_or_none) { if let Some(filepath) = filepath { if filepath.to_str().unwrap_or_default() == "-" { - file_input.push(Input::stdin_as_file(provided_name)); + file_input.push(new_stdin_input(provided_name)); } else { - file_input.push(Input::ordinary_file(filepath).with_name(provided_name)); + file_input.push(new_file_input(filepath, provided_name)); } } } diff --git a/src/bin/bat/input.rs b/src/bin/bat/input.rs new file mode 100644 index 00000000..9b596274 --- /dev/null +++ b/src/bin/bat/input.rs @@ -0,0 +1,20 @@ +use bat::input::Input; +use std::ffi::OsStr; + +pub fn new_file_input<'a>(file: &'a OsStr, name: Option<&'a OsStr>) -> Input<'a> { + named(Input::ordinary_file(file), name.or_else(|| Some(file))) +} + +pub fn new_stdin_input(name: Option<&OsStr>) -> Input { + named(Input::stdin(), name) +} + +fn named<'a>(input: Input<'a>, name: Option<&OsStr>) -> Input<'a> { + if let Some(provided_name) = name { + let mut input = input.with_name(Some(provided_name)); + input.description_mut().set_kind(Some("File".to_owned())); + input + } else { + input + } +} diff --git a/src/bin/bat/main.rs b/src/bin/bat/main.rs index 00aa71de..4847db9a 100644 --- a/src/bin/bat/main.rs +++ b/src/bin/bat/main.rs @@ -6,6 +6,7 @@ mod assets; mod clap_app; mod config; mod directories; +mod input; use std::collections::HashSet; use std::ffi::OsStr; diff --git a/src/input.rs b/src/input.rs index 04a3e5bb..c5478e39 100644 --- a/src/input.rs +++ b/src/input.rs @@ -125,17 +125,6 @@ impl<'a> Input<'a> { } } - pub fn stdin_as_file(name: Option>) -> Self { - match name { - None => Input::stdin(), - Some(name) => { - let mut input = Input::stdin().with_name(Some(name.as_ref())); - input.description.kind = Some("File".to_owned()); - input - } - } - } - pub fn from_reader(reader: Box) -> Self { let kind = InputKind::CustomReader(reader); Input {