fix: only read from stdin if it's not a terminal #17
cargo devel CI / cargo CI (push) Successful in 1m14s Details

This commit is contained in:
Christoph J. Scherr 2024-05-13 17:01:38 +02:00
parent f93e5f6c4c
commit bf4e7b00d7
1 changed files with 26 additions and 23 deletions

View File

@ -3,7 +3,7 @@
//! This binary should just take any amount of numbers and print them out formatted to some other //! This binary should just take any amount of numbers and print them out formatted to some other
//! system. //! system.
use std::io::Read; use std::io::{IsTerminal, Read};
use std::process::exit; use std::process::exit;
use clap::{CommandFactory, Parser}; use clap::{CommandFactory, Parser};
@ -16,7 +16,9 @@ fn main() {
// try to read from stdin first, appending the numbers we read to the FormatOptions // try to read from stdin first, appending the numbers we read to the FormatOptions
let mut options = FormatOptions::parse(); let mut options = FormatOptions::parse();
let mut stdin_nums = Vec::new(); let mut stdin_nums = Vec::new();
match std::io::stdin().lock().read_to_end(&mut stdin_nums) { let stdin = std::io::stdin();
if !stdin.is_terminal() {
match stdin.lock().read_to_end(&mut stdin_nums) {
Ok(_) => { Ok(_) => {
let whole: String = match String::from_utf8(stdin_nums) { let whole: String = match String::from_utf8(stdin_nums) {
Ok(r) => r, Ok(r) => r,
@ -42,6 +44,7 @@ fn main() {
exit(2); exit(2);
} }
}; };
}
if options.numbers().is_empty() { if options.numbers().is_empty() {
format!("{}", FormatOptions::command().render_usage()); format!("{}", FormatOptions::command().render_usage());