forked from bonsai/harakit
hru(1): made it actually work
This commit is contained in:
parent
fa686eefb9
commit
1ee668aed6
68
src/hru.rs
68
src/hru.rs
@ -20,64 +20,36 @@ use std::{
|
||||
cmp::Ordering,
|
||||
env::args,
|
||||
io::stdin,
|
||||
process::exit,
|
||||
process::ExitCode,
|
||||
};
|
||||
|
||||
extern crate sysexits;
|
||||
|
||||
use sysexits::EX_SOFTWARE;
|
||||
|
||||
fn auto_c(input: u64) -> (u64, u32) {
|
||||
let mut number = input;
|
||||
let mut remainder = 0;
|
||||
let mut exp: u32 = 0;
|
||||
|
||||
if number % 10 > 0 { }
|
||||
else {
|
||||
while remainder == 0 {
|
||||
number /= 10;
|
||||
remainder = number % 10;
|
||||
exp += 1;
|
||||
}
|
||||
}
|
||||
use sysexits::EX_DATAERR;
|
||||
|
||||
fn convert(input: u64) -> (f64, u32) {
|
||||
let list: Vec<u32> = vec![3, 6, 9, 12, 15, 18, 21, 24, 27, 30];
|
||||
let mut exps = list.iter().peekable();
|
||||
let mut out = (0.0, 0_u32);
|
||||
|
||||
while let Some(i) = exps.next() {
|
||||
match exp.cmp(i) {
|
||||
if input < 1000 { return (input as f64, 0); }
|
||||
|
||||
for n in list {
|
||||
let c = 10_u64.pow(n);
|
||||
match c.cmp(&input) {
|
||||
Ordering::Less => {
|
||||
number = input;
|
||||
break;
|
||||
out = (input as f64 / c as f64, n);
|
||||
},
|
||||
Ordering::Equal => break,
|
||||
Ordering::Greater => {
|
||||
if let Some(p) = exps.peek() {
|
||||
match exp.cmp(p) {
|
||||
Ordering::Less => {
|
||||
let exp_e = 10_u64.checked_pow(exp)
|
||||
.unwrap_or_else(|| { exit(EX_SOFTWARE); });
|
||||
|
||||
let index_e = 10_u64.checked_pow(i.to_owned())
|
||||
.unwrap_or_else(|| { exit(EX_SOFTWARE); });
|
||||
|
||||
number *= exp_e / index_e;
|
||||
exp = *i;
|
||||
break;
|
||||
},
|
||||
Ordering::Greater => continue,
|
||||
_ => break,
|
||||
};
|
||||
}
|
||||
Ordering::Equal => {
|
||||
return (input as f64 / c as f64, n);
|
||||
},
|
||||
Ordering::Greater => {},
|
||||
};
|
||||
}
|
||||
|
||||
(number, exp)
|
||||
out
|
||||
}
|
||||
|
||||
|
||||
fn main() {
|
||||
fn main() -> ExitCode {
|
||||
let argv = args().collect::<Vec<String>>();
|
||||
let mut buf = String::new();
|
||||
let _ = stdin().read_line(&mut buf);
|
||||
@ -86,11 +58,11 @@ fn main() {
|
||||
Ok(f) => f,
|
||||
Err(err) => {
|
||||
eprintln!("{}: {}", argv[0], err);
|
||||
exit(1);
|
||||
return ExitCode::from(EX_DATAERR as u8);
|
||||
},
|
||||
};
|
||||
|
||||
let (number, prefix) = auto_c(n);
|
||||
let (number, prefix) = convert(n);
|
||||
|
||||
let si_prefix = format!("{}B", match prefix {
|
||||
3 => "K",
|
||||
@ -106,5 +78,9 @@ fn main() {
|
||||
_ => "",
|
||||
});
|
||||
|
||||
println!("{} {}", number.to_string(), si_prefix);
|
||||
let out = ((number * 10.0).round() / 10.0).to_string();
|
||||
|
||||
println!("{} {}", out, si_prefix);
|
||||
|
||||
ExitCode::SUCCESS
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user