mirror of
https://github.com/sharkdp/bat
synced 2026-07-29 18:01:43 +00:00
Separate inputs from config
This commit is contained in:
+9
-5
@@ -73,8 +73,7 @@ impl App {
|
||||
Ok(clap_app::build_app(interactive_output).get_matches_from(args))
|
||||
}
|
||||
|
||||
pub fn config(&self) -> Result<Config> {
|
||||
let files = self.files()?;
|
||||
pub fn config(&self, inputs: &[InputFile]) -> Result<Config> {
|
||||
let style_components = self.style_components()?;
|
||||
|
||||
let paging_mode = match self.matches.value_of("paging") {
|
||||
@@ -84,7 +83,13 @@ 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 files.contains(&InputFile::StdIn(None)) {
|
||||
} else if inputs.iter().any(|f| {
|
||||
if let InputFile::StdIn(None) = f {
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}) {
|
||||
// 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.
|
||||
@@ -170,7 +175,6 @@ impl App {
|
||||
loop_through: !(self.interactive_output
|
||||
|| self.matches.value_of("color") == Some("always")
|
||||
|| self.matches.value_of("decorations") == Some("always")),
|
||||
files,
|
||||
tab_width: self
|
||||
.matches
|
||||
.value_of("tabs")
|
||||
@@ -222,7 +226,7 @@ impl App {
|
||||
})
|
||||
}
|
||||
|
||||
fn files(&self) -> Result<Vec<InputFile>> {
|
||||
pub fn inputs(&self) -> Result<Vec<InputFile>> {
|
||||
// verify equal length of file-names and input FILEs
|
||||
match self.matches.values_of("file-name") {
|
||||
Some(ref filenames)
|
||||
|
||||
+11
-9
@@ -120,7 +120,6 @@ pub fn list_themes(cfg: &Config) -> Result<()> {
|
||||
let mut config = cfg.clone();
|
||||
let mut style = HashSet::new();
|
||||
style.insert(StyleComponent::Plain);
|
||||
config.files = vec![InputFile::ThemePreviewFile];
|
||||
config.style_components = StyleComponents(style);
|
||||
|
||||
let stdout = io::stdout();
|
||||
@@ -134,7 +133,9 @@ pub fn list_themes(cfg: &Config) -> Result<()> {
|
||||
Style::new().bold().paint(theme.to_string())
|
||||
)?;
|
||||
config.theme = theme.to_string();
|
||||
let _controller = Controller::new(&config, &assets).run();
|
||||
Controller::new(&config, &assets)
|
||||
.run(vec![InputFile::ThemePreviewFile])
|
||||
.ok();
|
||||
writeln!(stdout)?;
|
||||
}
|
||||
} else {
|
||||
@@ -146,10 +147,10 @@ pub fn list_themes(cfg: &Config) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn run_controller(config: &Config) -> Result<bool> {
|
||||
fn run_controller(inputs: Vec<InputFile>, config: &Config) -> Result<bool> {
|
||||
let assets = assets_from_cache_or_binary()?;
|
||||
let controller = Controller::new(&config, &assets);
|
||||
controller.run()
|
||||
controller.run(inputs)
|
||||
}
|
||||
|
||||
/// Returns `Err(..)` upon fatal errors. Otherwise, returns `Ok(true)` on full success and
|
||||
@@ -166,16 +167,17 @@ fn run() -> Result<bool> {
|
||||
run_cache_subcommand(cache_matches)?;
|
||||
Ok(true)
|
||||
} else {
|
||||
let mut config = app.config()?;
|
||||
config.files = vec![InputFile::Ordinary(OrdinaryFile::from_path(OsStr::new(
|
||||
let inputs = vec![InputFile::Ordinary(OrdinaryFile::from_path(OsStr::new(
|
||||
"cache",
|
||||
)))];
|
||||
let mut config = app.config(&inputs)?;
|
||||
|
||||
run_controller(&config)
|
||||
run_controller(inputs, &config)
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
let config = app.config()?;
|
||||
let inputs = app.inputs()?;
|
||||
let config = app.config(&inputs)?;
|
||||
|
||||
if app.matches.is_present("list-languages") {
|
||||
list_languages(&config)?;
|
||||
@@ -196,7 +198,7 @@ fn run() -> Result<bool> {
|
||||
writeln!(io::stdout(), "{}", cache_dir())?;
|
||||
Ok(true)
|
||||
} else {
|
||||
run_controller(&config)
|
||||
run_controller(inputs, &config)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user