This repository has been archived on 2024-10-16. You can view files and clone it, but cannot push or open issues or pull requests.
numf/src/main.rs

51 lines
1.3 KiB
Rust
Raw Normal View History

2024-05-10 15:55:24 +02:00
//! # numf
//!
//! This binary should just take any amount of numbers and print them out formatted to some other
//! system.
use std::io::Read;
use std::process::exit;
use clap::Parser;
2024-05-10 15:55:24 +02:00
mod format;
use format::*;
2024-05-10 15:55:24 +02:00
2024-05-10 13:57:36 +02:00
fn main() {
// try to read from stdin first, appending the numbers we read to the FormatOptions
let mut args: Vec<String> = std::env::args_os()
.map(|x| x.into_string().unwrap())
.collect();
let mut stdin_nums = Vec::new();
match std::io::stdin().lock().read_to_end(&mut stdin_nums) {
Ok(_) => {
let whole: String = match String::from_utf8(stdin_nums) {
Ok(r) => r,
Err(e) => {
eprintln!("stdin for this program only accepts text: {e:#?}");
exit(1);
}
};
let whole = whole.replace('\n', "");
for s in whole.split(' ') {
args.push(s.to_string());
}
}
Err(e) => {
eprintln!("could not read from stdin: {e:#?}");
exit(2);
}
};
let options = FormatOptions::parse_from(args);
2024-05-10 15:55:24 +02:00
let mut out: Vec<String> = Vec::new();
for num in options.numbers() {
out.push(options.format().format(*num, &options));
2024-05-10 15:55:24 +02:00
}
for o in out {
println!("{o}")
}
}