From d0205b71da28e4c2164e1cd2f0dd38fdc6cabc92 Mon Sep 17 00:00:00 2001 From: emma Date: Tue, 13 Feb 2024 17:48:25 -0700 Subject: [PATCH] hru(1): read stdin until EOF --- src/hru.rs | 53 +++++++++++++++++++++++++++++------------------------ 1 file changed, 29 insertions(+), 24 deletions(-) diff --git a/src/hru.rs b/src/hru.rs index f27d4ab..1e57be4 100644 --- a/src/hru.rs +++ b/src/hru.rs @@ -52,35 +52,40 @@ fn convert(input: u64) -> (f64, u32) { fn main() -> ExitCode { let argv = args().collect::>(); let mut buf = String::new(); - let _ = stdin().read_line(&mut buf); + while let Ok(_) = stdin().read_line(&mut buf) { + if buf.is_empty() { return ExitCode::SUCCESS; } - let n: u64 = match buf.trim().parse() { - Ok(f) => f, - Err(err) => { - eprintln!("{}: {}", argv[0], err); - return ExitCode::from(EX_DATAERR as u8); - }, - }; + let n: u64 = match buf.trim().parse() { + Ok(f) => { + buf.clear(); + f + }, + Err(err) => { + eprintln!("{}: {}", argv[0], err); + return ExitCode::from(EX_DATAERR as u8); + }, + }; - let (number, prefix) = convert(n); + let (number, prefix) = convert(n); - let si_prefix = format!("{}B", match prefix { - 3 => "K", - 6 => "M", - 9 => "G", - 12 => "T", - 15 => "P", - 18 => "E", - 21 => "Z", - 24 => "Y", - 27 => "R", - 30 => "Q", - _ => "", - }); + let si_prefix = format!("{}B", match prefix { + 3 => "K", + 6 => "M", + 9 => "G", + 12 => "T", + 15 => "P", + 18 => "E", + 21 => "Z", + 24 => "Y", + 27 => "R", + 30 => "Q", + _ => "", + }); - let out = ((number * 10.0).round() / 10.0).to_string(); + let out = ((number * 10.0).round() / 10.0).to_string(); - println!("{} {}", out, si_prefix); + println!("{} {}", out, si_prefix); + } ExitCode::SUCCESS }