diff --git a/src/datatype.rs b/src/datatype.rs index c64b2a0..f96c995 100644 --- a/src/datatype.rs +++ b/src/datatype.rs @@ -126,13 +126,14 @@ pub fn format_strings( vec_col: &[&str], lower_column_width: usize, upper_column_width: usize, + sigfig: i64, ) -> Vec { let ellipsis = '\u{2026}'; let strings_and_fracts: Vec<(String, usize, usize)> = vec_col .iter() .map(|&string| format_if_na(string)) - .map(|string| format_if_num(&string)) + .map(|string| format_if_num(&string, sigfig)) .map(|string| { // the string, and the length of its fractional digits if any let (lhs, rhs) = if is_double(&string) { @@ -213,9 +214,9 @@ pub fn format_if_na(text: &str) -> String { string.to_string() } -pub fn format_if_num(text: &str) -> String { +pub fn format_if_num(text: &str, sigfig: i64) -> String { if let Ok(val) = text.parse::() { - sigfig::DecimalSplits { val, sigfig: 3 }.final_string() + sigfig::DecimalSplits { val, sigfig }.final_string() } else { text.to_string() } diff --git a/src/datatype/sigfig.rs b/src/datatype/sigfig.rs index 2c4e38e..5474986 100644 --- a/src/datatype/sigfig.rs +++ b/src/datatype/sigfig.rs @@ -203,14 +203,11 @@ pub fn get_final_string(x: f64, lhs: f64, rhs: f64, neg: bool, sigfig: i64) -> S let split = total_clone.split('.'); let vec: Vec<&str> = split.collect(); let len_to_take_lhs = vec[0].len(); // point -> +1 to sigfig - let len_to_take_rhs = ((sigfig + 1) as usize) - len_to_take_lhs; - if vec[1].len() > (sigfig - 2) as usize { - let len_to_take = len_to_take_lhs + len_to_take_rhs + 1; // +1 for the space the neg sign takes - total_string[..len_to_take].to_string() - } else { - let len_to_take = len_to_take_lhs + len_to_take_rhs; - total_string[..len_to_take].to_string() - } + // The plus one at the end stands for the '.' character as lhs doesn't include it + let len_to_take_rhs = + std::cmp::min((sigfig as usize) - len_to_take_lhs + 1, vec[1].len()) + 1; + let len_to_take = len_to_take_lhs + len_to_take_rhs; + total_string[..len_to_take].to_string() } else { //concatonate: //(lhs) diff --git a/src/main.rs b/src/main.rs index 7183344..a1381bd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -108,13 +108,13 @@ struct Cli { help = "The delimiter separating the columns." )] delimiter: Option, - //#[structopt( - // short = "sig", - // long = "sigfig", - // default_value = "3", - // help = "Significant Digits. Default 3. (Comming Soon!)" - //)] - //sigfig: usize, + #[structopt( + short = "g", + long = "sigfig", + default_value = "5", + help = "Significant Digits. Default 3." + )] + sigfig: i64, #[structopt( short = "d", long = "debug-mode", @@ -159,7 +159,7 @@ fn main() { let term_tuple = size().unwrap(); let opt = Cli::from_args(); let color_option = opt.color; - //let sigfig = opt.sigfig; + let sigfig = opt.sigfig; let debug_mode = opt.debug_mode; let is_title_defined = opt.title.chars().count() > 0; let is_footer_defined = opt.title.chars().count() > 0; @@ -371,7 +371,7 @@ fn main() { // vector of formatted values let vf: Vec> = v .iter() - .map(|col| datatype::format_strings(col, lower_column_width, upper_column_width)) + .map(|col| datatype::format_strings(col, lower_column_width, upper_column_width, sigfig)) .collect(); if debug_mode { @@ -711,6 +711,7 @@ mod tests { &v[i], col_largest_width_post_proc[i], col_largest_width_post_proc[i], + 3, ); } @@ -788,6 +789,7 @@ mod tests { &v[i], col_largest_width_post_proc[i], col_largest_width_post_proc[i], + 3, ); } @@ -820,6 +822,7 @@ mod tests { &v[i], col_largest_width_post_proc[i], col_largest_width_post_proc[i], + 3, ); }