diff --git a/src/cli.rs b/src/cli.rs index b4b46b9..15c5384 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -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)) } diff --git a/src/main.rs b/src/main.rs index 942a414..8381524 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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) -> Vec { .collect() } +// Returns a list of lines from stdin or `None` if there's nothing to read +fn get_lines_from_stdin() -> Option> { + atty::isnt(atty::Stream::Stdin).then(|| { + std::io::stdin() + .lock() + .lines() + .collect::>() + .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");