Replace Input::stdin_as_file with bat-application functions

This commit is contained in:
Ethan P 2020-05-27 16:25:13 -07:00 committed by David Peter
parent a3357547ea
commit 9d08c0102e
4 changed files with 27 additions and 15 deletions

View File

@ -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<Vec<&OsStr>> = 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<dyn Iterator<Item = _>> = 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));
}
}
}

20
src/bin/bat/input.rs Normal file
View File

@ -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
}
}

View File

@ -6,6 +6,7 @@ mod assets;
mod clap_app;
mod config;
mod directories;
mod input;
use std::collections::HashSet;
use std::ffi::OsStr;

View File

@ -125,17 +125,6 @@ impl<'a> Input<'a> {
}
}
pub fn stdin_as_file(name: Option<impl AsRef<OsStr>>) -> 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<dyn Read + 'a>) -> Self {
let kind = InputKind::CustomReader(reader);
Input {