From ef44bd662ec34b2cc7983d4afff611c759db6b81 Mon Sep 17 00:00:00 2001 From: PaddiM8 Date: Sat, 13 Jun 2020 21:41:01 +0200 Subject: [PATCH] Made the output prettier by removing repeating characters. --- kalk_cli/src/output.rs | 43 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 5 deletions(-) diff --git a/kalk_cli/src/output.rs b/kalk_cli/src/output.rs index 0e37679..ba7260a 100644 --- a/kalk_cli/src/output.rs +++ b/kalk_cli/src/output.rs @@ -16,11 +16,11 @@ pub fn eval(parser: &mut parser::Context, input: &str) { let num = if exp <= 0 { // 0 < x < 1 - format!( - "0.{}{}", - "0".repeat(exp.abs() as usize), - digits.trim_end_matches('0') + round( + format!("0.{}{}", "0".repeat(exp.abs() as usize), digits) + .trim_end_matches('0'), ) + .to_string() } else if use_sci_notation || result.fract() != 0 { // Insert the comma if there are supposed to be decimals. let mut chars: Vec = digits @@ -29,7 +29,7 @@ pub fn eval(parser: &mut parser::Context, input: &str) { .chars() .collect(); chars.insert(comma_pos, '.'); - chars.into_iter().collect::() + round(&chars.into_iter().collect::()).to_string() } else { // Regular number digits[..(exp as usize)].to_string() @@ -51,6 +51,39 @@ pub fn print_err(msg: &str) { println!("{}", Red.paint(msg)); } +fn round(value: &str) -> String { + let mut value_iter = value.chars().into_iter().rev().skip(1); + let last_char = value_iter.next().unwrap(); + let mut last_char_count = 1; + + // Find out how many repeating trailing (equal) characters there are + for c in value_iter { + if c == last_char { + last_char_count += 1; + } else { + break; + } + } + + // Don't round if there aren't that many + if last_char_count < 5 { + return value.to_string(); + } + + // Remove zeroes. + // If it's nines, round it up. + // Otherwise, only show 3 of them and then three dots: + match last_char { + '0' => value[..value.len() - last_char_count - 1].to_string(), + '9' => value.parse::().unwrap().ceil().to_string(), + _ => format!( + "{}{}...", + &value[..value.len() - last_char_count - 1], + last_char.to_string().repeat(3) + ), + } +} + fn print_calc_err(err: CalcError) { print_err(&match err { IncorrectAmountOfArguments(expected, func, got) => format!(