on master help sign in
Replace Input::stdin_as_file with bat-application functions
commit9d08c0102eba2a6a2e613f2bc9cf3083252f580b authorEthan P <eth-p+git@hidden.email> dateWed 27 May 2020, 16:25 (6 years ago) parenta335754
4 files changed+27−15
src/bin/bat/app.rs+6−4 src/bin/bat/input.rs+20 src/bin/bat/main.rs+1 src/input.rs−11
src/bin/bat/app.rs+6−4
@@ -13,6 +13,7 @@ use clap::ArgMatches;
1313
1414use console::Term;
1515
16+use crate::input::{new_file_input, new_stdin_input};
1617use bat::{
1718 assets::HighlightingAssets,
1819 config::{Config, VisibleLines},
@@ -257,8 +258,9 @@ impl App {
257258 let files: Option<Vec<&OsStr>> = self.matches.values_of_os("FILE").map(|vs| vs.collect());
258259
259260 if files.is_none() {
260− let input = Input::stdin_as_file(filenames_or_none.next().unwrap_or(None));
261− return Ok(vec![input]);
261+ return Ok(vec![new_stdin_input(
262+ filenames_or_none.next().unwrap_or(None),
263+ )]);
262264 }
263265 let files_or_none: Box<dyn Iterator<Item = _>> = match files {
264266 Some(ref files) => Box::new(files.iter().map(|name| Some(*name))),
@@ -269,9 +271,9 @@ impl App {
269271 for (filepath, provided_name) in files_or_none.zip(filenames_or_none) {
270272 if let Some(filepath) = filepath {
271273 if filepath.to_str().unwrap_or_default() == "-" {
272− file_input.push(Input::stdin_as_file(provided_name));
274+ file_input.push(new_stdin_input(provided_name));
273275 } else {
274− file_input.push(Input::ordinary_file(filepath).with_name(provided_name));
276+ file_input.push(new_file_input(filepath, provided_name));
275277 }
276278 }
277279 }
src/bin/bat/input.rsnew file+20
@@ -0,0 +1,20 @@
1+use bat::input::Input;
2+use std::ffi::OsStr;
3+
4+pub fn new_file_input<'a>(file: &'a OsStr, name: Option<&'a OsStr>) -> Input<'a> {
5+ named(Input::ordinary_file(file), name.or_else(|| Some(file)))
6+}
7+
8+pub fn new_stdin_input(name: Option<&OsStr>) -> Input {
9+ named(Input::stdin(), name)
10+}
11+
12+fn named<'a>(input: Input<'a>, name: Option<&OsStr>) -> Input<'a> {
13+ if let Some(provided_name) = name {
14+ let mut input = input.with_name(Some(provided_name));
15+ input.description_mut().set_kind(Some("File".to_owned()));
16+ input
17+ } else {
18+ input
19+ }
20+}
src/bin/bat/main.rs+1
@@ -6,6 +6,7 @@ mod assets;
66mod clap_app;
77mod config;
88mod directories;
9+mod input;
910
1011use std::collections::HashSet;
1112use std::ffi::OsStr;
src/input.rs−11
@@ -125,17 +125,6 @@ impl<'a> Input<'a> {
125125 }
126126 }
127127
128− pub fn stdin_as_file(name: Option<impl AsRef<OsStr>>) -> Self {
129− match name {
130− None => Input::stdin(),
131− Some(name) => {
132− let mut input = Input::stdin().with_name(Some(name.as_ref()));
133− input.description.kind = Some("File".to_owned());
134− input
135− }
136− }
137− }
138−
139128 pub fn from_reader(reader: Box<dyn Read + 'a>) -> Self {
140129 let kind = InputKind::CustomReader(reader);
141130 Input {