1
0
mirror of https://github.com/sharkdp/bat synced 2026-08-04 19:01:44 +00:00

Completely refactor 'input' module

This commit is contained in:
sharkdp
2020-04-22 16:27:34 +02:00
committed by David Peter
parent b4d54106fe
commit 590960f7f5
9 changed files with 233 additions and 200 deletions
+16 -21
View File
@@ -15,10 +15,11 @@ use console::Term;
use bat::{
config::{
Config, HighlightedLineRanges, Input, LineRange, LineRanges, MappingTarget, OrdinaryFile,
PagingMode, StyleComponent, StyleComponents, SyntaxMapping, WrappingMode,
Config, HighlightedLineRanges, LineRange, LineRanges, MappingTarget, PagingMode,
StyleComponent, StyleComponents, SyntaxMapping, WrappingMode,
},
errors::*,
input::Input,
HighlightingAssets,
};
@@ -83,13 +84,7 @@ impl App {
if self.matches.occurrences_of("plain") > 1 {
// If we have -pp as an option when in auto mode, the pager should be disabled.
PagingMode::Never
} else if inputs.iter().any(|f| {
if let Input::StdIn(None) = f {
true
} else {
false
}
}) {
} else if inputs.iter().any(Input::is_stdin) {
// If we are reading from stdin, only enable paging if we write to an
// interactive terminal and if we do not *read* from an interactive
// terminal.
@@ -251,9 +246,9 @@ impl App {
let files: Option<Vec<&OsStr>> = self.matches.values_of_os("FILE").map(|vs| vs.collect());
if files.is_none() {
return Ok(vec![Input::StdIn(
filenames_or_none.nth(0).unwrap().map(|f| f.to_owned()),
)]);
let mut input = Input::stdin();
input.set_provided_name(filenames_or_none.nth(0).unwrap_or(None));
return Ok(vec![input]);
}
let files_or_none: Box<dyn Iterator<Item = _>> = match files {
Some(ref files) => Box::new(files.into_iter().map(|name| Some(*name))),
@@ -261,16 +256,16 @@ impl App {
};
let mut file_input = Vec::new();
for (input, name) in files_or_none.zip(filenames_or_none) {
if let Some(input) = input {
if input.to_str().unwrap_or_default() == "-" {
file_input.push(Input::StdIn(name.map(|n| n.to_owned())));
for (filepath, provided_name) in files_or_none.zip(filenames_or_none) {
if let Some(filepath) = filepath {
if filepath.to_str().unwrap_or_default() == "-" {
let mut input = Input::stdin();
input.set_provided_name(provided_name);
file_input.push(input);
} else {
let mut ofile = OrdinaryFile::from_path(input);
if let Some(path) = name {
ofile.set_provided_path(path);
}
file_input.push(Input::Ordinary(ofile))
let mut input = Input::ordinary_file(filepath);
input.set_provided_name(provided_name);
file_input.push(input);
}
}
}
+4 -5
View File
@@ -26,8 +26,9 @@ use clap::crate_version;
use directories::PROJECT_DIRS;
use bat::{
config::{Config, Input, OrdinaryFile, StyleComponent, StyleComponents},
config::{Config, StyleComponent, StyleComponents},
errors::*,
input::Input,
Controller, HighlightingAssets,
};
@@ -134,7 +135,7 @@ pub fn list_themes(cfg: &Config) -> Result<()> {
)?;
config.theme = theme.to_string();
Controller::new(&config, &assets)
.run(vec![Input::ThemePreviewFile])
.run(vec![Input::theme_preview_file()])
.ok();
writeln!(stdout)?;
}
@@ -167,9 +168,7 @@ fn run() -> Result<bool> {
run_cache_subcommand(cache_matches)?;
Ok(true)
} else {
let inputs = vec![Input::Ordinary(OrdinaryFile::from_path(OsStr::new(
"cache",
)))];
let inputs = vec![Input::ordinary_file(OsStr::new("cache"))];
let config = app.config(&inputs)?;
run_controller(inputs, &config)