generated from PlexSheep/rs-base
Compare commits
No commits in common. "bf4e7b00d72341184a130685856db988f30e1463" and "09a7fa22cf428000667a8ed978fd82ea0391395d" have entirely different histories.
bf4e7b00d7
...
09a7fa22cf
2 changed files with 25 additions and 53 deletions
|
@ -68,21 +68,10 @@ pub struct FormatOptions {
|
|||
#[arg(short = 'z', long)]
|
||||
/// format to base32
|
||||
base32: bool,
|
||||
#[clap(value_parser=numf_parser::<NumberType>, required=false)]
|
||||
#[clap(value_parser=numf_parser::<NumberType>, required=true)]
|
||||
/// at least one number that should be formatted
|
||||
///
|
||||
/// Any of the [Formats](Format::format) are supported, but the prefixes are needed for formats
|
||||
/// other than decimal.
|
||||
///
|
||||
/// Formats:
|
||||
///
|
||||
/// - '0x' - Hexadecimal
|
||||
/// - '0b' - Binary
|
||||
/// - '0o' - Octal
|
||||
/// - '0s' - Base64
|
||||
/// - '032s' - Base32
|
||||
///
|
||||
/// The numbers may be left empty at first, if numbers are provided with the stdin.
|
||||
/// supports either base 10 or base 16 inputs (with 0xaaaa)
|
||||
numbers: Vec<NumberType>,
|
||||
}
|
||||
|
||||
|
@ -153,11 +142,6 @@ impl FormatOptions {
|
|||
pub fn set_prefix(&mut self, value: bool) {
|
||||
self.prefix = value;
|
||||
}
|
||||
|
||||
/// manually add a number
|
||||
pub fn push_number(&mut self, value: NumberType) {
|
||||
self.numbers.push(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for FormatOptions {
|
||||
|
|
58
src/main.rs
58
src/main.rs
|
@ -3,53 +3,41 @@
|
|||
//! This binary should just take any amount of numbers and print them out formatted to some other
|
||||
//! system.
|
||||
|
||||
use std::io::{IsTerminal, Read};
|
||||
use std::io::Read;
|
||||
use std::process::exit;
|
||||
|
||||
use clap::{CommandFactory, Parser};
|
||||
use clap::Parser;
|
||||
|
||||
mod format;
|
||||
use format::*;
|
||||
use numf::format::numf_parser;
|
||||
|
||||
fn main() {
|
||||
// try to read from stdin first, appending the numbers we read to the FormatOptions
|
||||
let mut options = FormatOptions::parse();
|
||||
let mut args: Vec<String> = std::env::args_os()
|
||||
.map(|x| x.into_string().unwrap())
|
||||
.collect();
|
||||
let mut stdin_nums = Vec::new();
|
||||
let stdin = std::io::stdin();
|
||||
if !stdin.is_terminal() {
|
||||
match 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 split = whole.split_whitespace();
|
||||
for s in split {
|
||||
let number = match numf_parser(s) {
|
||||
Ok(n) => n,
|
||||
Err(e) => {
|
||||
eprintln!("could not parse number from stdin: {e:#?}");
|
||||
exit(2);
|
||||
}
|
||||
};
|
||||
options.push_number(number)
|
||||
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 split = whole.split_whitespace();
|
||||
for s in split {
|
||||
args.push(s.to_string());
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("could not read from stdin: {e:#?}");
|
||||
exit(2);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("could not read from stdin: {e:#?}");
|
||||
exit(2);
|
||||
}
|
||||
};
|
||||
|
||||
if options.numbers().is_empty() {
|
||||
format!("{}", FormatOptions::command().render_usage());
|
||||
exit(1);
|
||||
}
|
||||
let options = FormatOptions::parse_from(args);
|
||||
|
||||
let mut out: Vec<String> = Vec::new();
|
||||
|
||||
|
|
Reference in a new issue