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,
|
cmp::Ordering,
|
||||||
env::args,
|
env::args,
|
||||||
io::stdin,
|
io::stdin,
|
||||||
process::exit,
|
process::ExitCode,
|
||||||
};
|
};
|
||||||
|
|
||||||
extern crate sysexits;
|
extern crate sysexits;
|
||||||
|
|
||||||
use sysexits::EX_SOFTWARE;
|
use sysexits::EX_DATAERR;
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
fn convert(input: u64) -> (f64, u32) {
|
||||||
let list: Vec<u32> = vec![3, 6, 9, 12, 15, 18, 21, 24, 27, 30];
|
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() {
|
if input < 1000 { return (input as f64, 0); }
|
||||||
match exp.cmp(i) {
|
|
||||||
|
for n in list {
|
||||||
|
let c = 10_u64.pow(n);
|
||||||
|
match c.cmp(&input) {
|
||||||
Ordering::Less => {
|
Ordering::Less => {
|
||||||
number = input;
|
out = (input as f64 / c as f64, n);
|
||||||
break;
|
|
||||||
},
|
},
|
||||||
Ordering::Equal => break,
|
Ordering::Equal => {
|
||||||
Ordering::Greater => {
|
return (input as f64 / c as f64, n);
|
||||||
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::Greater => {},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
(number, exp)
|
out
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn main() -> ExitCode {
|
||||||
fn main() {
|
|
||||||
let argv = args().collect::<Vec<String>>();
|
let argv = args().collect::<Vec<String>>();
|
||||||
let mut buf = String::new();
|
let mut buf = String::new();
|
||||||
let _ = stdin().read_line(&mut buf);
|
let _ = stdin().read_line(&mut buf);
|
||||||
@ -86,11 +58,11 @@ fn main() {
|
|||||||
Ok(f) => f,
|
Ok(f) => f,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
eprintln!("{}: {}", argv[0], 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 {
|
let si_prefix = format!("{}B", match prefix {
|
||||||
3 => "K",
|
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