mirror of
https://github.com/sharkdp/bat.git
synced 2025-02-22 04:48:48 +00:00
Replace Input::stdin_as_file with bat-application functions
This commit is contained in:
parent
a3357547ea
commit
9d08c0102e
@ -13,6 +13,7 @@ use clap::ArgMatches;
|
|||||||
|
|
||||||
use console::Term;
|
use console::Term;
|
||||||
|
|
||||||
|
use crate::input::{new_file_input, new_stdin_input};
|
||||||
use bat::{
|
use bat::{
|
||||||
assets::HighlightingAssets,
|
assets::HighlightingAssets,
|
||||||
config::{Config, VisibleLines},
|
config::{Config, VisibleLines},
|
||||||
@ -257,8 +258,9 @@ impl App {
|
|||||||
let files: Option<Vec<&OsStr>> = self.matches.values_of_os("FILE").map(|vs| vs.collect());
|
let files: Option<Vec<&OsStr>> = self.matches.values_of_os("FILE").map(|vs| vs.collect());
|
||||||
|
|
||||||
if files.is_none() {
|
if files.is_none() {
|
||||||
let input = Input::stdin_as_file(filenames_or_none.next().unwrap_or(None));
|
return Ok(vec![new_stdin_input(
|
||||||
return Ok(vec![input]);
|
filenames_or_none.next().unwrap_or(None),
|
||||||
|
)]);
|
||||||
}
|
}
|
||||||
let files_or_none: Box<dyn Iterator<Item = _>> = match files {
|
let files_or_none: Box<dyn Iterator<Item = _>> = match files {
|
||||||
Some(ref files) => Box::new(files.iter().map(|name| Some(*name))),
|
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) {
|
for (filepath, provided_name) in files_or_none.zip(filenames_or_none) {
|
||||||
if let Some(filepath) = filepath {
|
if let Some(filepath) = filepath {
|
||||||
if filepath.to_str().unwrap_or_default() == "-" {
|
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 {
|
} 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
20
src/bin/bat/input.rs
Normal 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
|
||||||
|
}
|
||||||
|
}
|
@ -6,6 +6,7 @@ mod assets;
|
|||||||
mod clap_app;
|
mod clap_app;
|
||||||
mod config;
|
mod config;
|
||||||
mod directories;
|
mod directories;
|
||||||
|
mod input;
|
||||||
|
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
use std::ffi::OsStr;
|
use std::ffi::OsStr;
|
||||||
|
11
src/input.rs
11
src/input.rs
@ -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 {
|
pub fn from_reader(reader: Box<dyn Read + 'a>) -> Self {
|
||||||
let kind = InputKind::CustomReader(reader);
|
let kind = InputKind::CustomReader(reader);
|
||||||
Input {
|
Input {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user