Read inputs from stdin when applicable

This commit is contained in:
Collin Styles
2022-09-23 23:17:04 -07:00
committed by andy.boot
parent 0a67191054
commit e858f9e976
2 changed files with 20 additions and 5 deletions

View File

@@ -131,5 +131,5 @@ pub fn build_cli() -> Command<'static> {
.long("si")
.help("print sizes in powers of 1000 (e.g., 1.1G)")
)
.arg(Arg::new("inputs").multiple_occurrences(true).default_value("."))
.arg(Arg::new("inputs").multiple_occurrences(true))
}

View File

@@ -11,6 +11,7 @@ mod utils;
use crate::cli::build_cli;
use std::collections::HashSet;
use std::io::BufRead;
use std::process;
use sysinfo::{System, SystemExt};
@@ -91,14 +92,28 @@ fn get_regex_value(maybe_value: Option<Values>) -> Vec<Regex> {
.collect()
}
// Returns a list of lines from stdin or `None` if there's nothing to read
fn get_lines_from_stdin() -> Option<Vec<String>> {
atty::isnt(atty::Stream::Stdin).then(|| {
std::io::stdin()
.lock()
.lines()
.collect::<Result<_, _>>()
.expect("Error reading from stdin")
})
}
fn main() {
let options = build_cli().get_matches();
let config = get_config();
let stdin_lines = get_lines_from_stdin();
let target_dirs = options
.values_of("inputs")
.expect("Should be a default value here")
.collect();
let target_dirs = match options.values_of("inputs") {
Some(values) => values.collect(),
None => stdin_lines.as_ref().map_or(vec!["."], |lines| {
lines.iter().map(String::as_str).collect()
}),
};
let summarize_file_types = options.is_present("types");