diff --git a/src/datatype.rs b/src/datatype.rs index 471de48..69135c0 100644 --- a/src/datatype.rs +++ b/src/datatype.rs @@ -203,23 +203,8 @@ pub fn format_if_na(text: &str) -> String { } pub fn format_if_num(text: &str) -> String { - if is_double(text) { - let xf = text.to_string().parse::().unwrap(); - let x = sigfig::DecimalSplits { val: xf, sigfig: 3 }; - let list = sigfig::DecimalSplitsList { - val: x.value(), - sigfig: x.sig_fig(), - neg: x.neg(), - lhs: x.lhs(), - rhs: x.rhs(), - dec: x.dec(), - final_string: x.final_string(), - rhs_string_len: x.rhs_string_len(x.final_string()), - sigfig_index_lhs_or_rhs: x.sigfig_index_lhs_or_rhs(), - sigfig_index_from: x.sigfig_index_from(), - sigfig_index_to: x.sigfig_index_to(), - }; - list.final_string + if let Ok(val) = text.parse::() { + sigfig::DecimalSplits { val, sigfig: 3 }.final_string() } else { text.to_string() } diff --git a/src/datatype/sigfig.rs b/src/datatype/sigfig.rs index de217fe..d5a7326 100644 --- a/src/datatype/sigfig.rs +++ b/src/datatype/sigfig.rs @@ -1,4 +1,4 @@ -use core::str; +use core::{convert::AsRef, str}; // The general logic and return values in this file were learned from the GNU R package pillar in the sigfig.R file. // A special thanks to the great code quality from Hadley Wickham, Jim Hester, and krlmlr @@ -81,13 +81,6 @@ impl DecimalSplits { self.sig_fig(), ) } - pub fn rhs_string_len(&self, string_final_string: String) -> usize { - string_final_string - .split('.') - .nth(1) - .map(|decimals| decimals.len()) - .unwrap_or(0) - } pub fn sigfig_index_lhs_or_rhs(&self) -> Option { sigfig_index_lhs_or_rhs(&self.final_string(), self.sig_fig()) } @@ -99,6 +92,18 @@ impl DecimalSplits { } } +pub fn rhs_string_len(string_final_string: S) -> usize +where + S: AsRef, +{ + string_final_string + .as_ref() + .split('.') + .nth(1) + .map(|decimals| decimals.len()) + .unwrap_or_default() +} + #[derive(Debug)] pub struct DecimalSplitsList { pub val: f64, @@ -378,7 +383,7 @@ fn test_f12345() { rhs: x.rhs(), dec: x.dec(), final_string: x.final_string(), - rhs_string_len: x.rhs_string_len(x.final_string()), + rhs_string_len: rhs_string_len(x.final_string()), sigfig_index_lhs_or_rhs: x.sigfig_index_lhs_or_rhs(), sigfig_index_from: x.sigfig_index_from(), sigfig_index_to: x.sigfig_index_to(), @@ -448,7 +453,7 @@ fn test_f100() { rhs: x.rhs(), dec: x.dec(), final_string: x.final_string(), - rhs_string_len: x.rhs_string_len(x.final_string()), + rhs_string_len: rhs_string_len(x.final_string()), sigfig_index_lhs_or_rhs: x.sigfig_index_lhs_or_rhs(), sigfig_index_from: x.sigfig_index_from(), sigfig_index_to: x.sigfig_index_to(), @@ -519,7 +524,7 @@ fn test_fn100() { rhs: x.rhs(), dec: x.dec(), final_string: x.final_string(), - rhs_string_len: x.rhs_string_len(x.final_string()), + rhs_string_len: rhs_string_len(x.final_string()), sigfig_index_lhs_or_rhs: x.sigfig_index_lhs_or_rhs(), sigfig_index_from: x.sigfig_index_from(), sigfig_index_to: x.sigfig_index_to(), @@ -589,7 +594,7 @@ fn test_fn12345() { rhs: x.rhs(), dec: x.dec(), final_string: x.final_string(), - rhs_string_len: x.rhs_string_len(x.final_string()), + rhs_string_len: rhs_string_len(x.final_string()), sigfig_index_lhs_or_rhs: x.sigfig_index_lhs_or_rhs(), sigfig_index_from: x.sigfig_index_from(), sigfig_index_to: x.sigfig_index_to(), @@ -661,7 +666,7 @@ fn test_long_double() { rhs: x.rhs(), dec: x.dec(), final_string: x.final_string(), - rhs_string_len: x.rhs_string_len(x.final_string()), + rhs_string_len: rhs_string_len(x.final_string()), sigfig_index_lhs_or_rhs: x.sigfig_index_lhs_or_rhs(), sigfig_index_from: x.sigfig_index_from(), sigfig_index_to: x.sigfig_index_to(), @@ -704,7 +709,7 @@ fn test_bug75() { rhs: x.rhs(), dec: x.dec(), final_string: x.final_string(), - rhs_string_len: x.rhs_string_len(x.final_string()), + rhs_string_len: rhs_string_len(x.final_string()), sigfig_index_lhs_or_rhs: x.sigfig_index_lhs_or_rhs(), sigfig_index_from: x.sigfig_index_from(), sigfig_index_to: x.sigfig_index_to(), diff --git a/src/main.rs b/src/main.rs index bb1f962..992ee35 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,7 +19,7 @@ use toml; Example Usage: wget https://raw.githubusercontent.com/tidyverse/ggplot2/master/data-raw/diamonds.csv cat diamonds.csv | head -n 35 | tv - + Configuration File Support: An example config is printed to make it easy to copy/paste to `tv.toml`. The config (tv.toml) location is dependent on OS: @@ -45,7 +45,7 @@ use toml; #meta_color = [64, 179, 162] ## header_color = [R,G,B] color for column headers #header_color = [232, 168, 124] - ## std_color = [R,G,B] color for standard cell data values + ## std_color = [R,G,B] color for standard cell data values #std_color = [133, 205, 202] ## na_color = [R,G,B] color for NA values #na_color = [226, 125, 95] @@ -262,21 +262,6 @@ fn main() { nord_na_color, ), }; - fn get_color_from_config(a: &toml::value::Array) -> [u8; 3] { - let i32_array: [u8; 3] = a - .clone() - .iter() - .map(|v| { - v.as_integer() - .expect("Not an integer") - .try_into() - .expect("Does not fit in a `i32`") - }) - .collect::>() - .try_into() - .expect("Not 3 elements"); - i32_array - } let is_color_defined = opt.color > 0; let meta_color = match (&config, is_color_defined) { (Some(x), false) => get_color_from_config(&x.clone().meta_color), @@ -385,23 +370,6 @@ fn main() { vp.push(row); } - // how wide will the print be? - fn get_num_cols_to_print(cols: usize, vp: Vec>, term_tuple: (u16, u16)) -> usize { - let mut last = 0; - let mut j = format!("{: <6}", ""); - for col in 0..cols { - let text = vp[0].get(col).unwrap().to_string(); - j.push_str(&text); - let total_width = j.chars().count(); - let term_width = term_tuple.0 as usize; - if total_width > term_width { - break; - } - last = col + 1; - } - last - } - let num_cols_to_print = get_num_cols_to_print(cols, vp.clone(), term_tuple); // color @@ -525,6 +493,39 @@ fn main() { println!(); } // end main +fn get_color_from_config(a: &toml::value::Array) -> [u8; 3] { + let i32_array: [u8; 3] = a + .clone() + .iter() + .map(|v| { + v.as_integer() + .expect("Not an integer") + .try_into() + .expect("Does not fit in a `i32`") + }) + .collect::>() + .try_into() + .expect("Not 3 elements"); + i32_array +} + +// how wide will the print be? +fn get_num_cols_to_print(cols: usize, vp: Vec>, term_tuple: (u16, u16)) -> usize { + let mut last = 0; + let mut j = format!("{: <6}", ""); + for col in 0..cols { + let text = vp[0].get(col).unwrap().to_string(); + j.push_str(&text); + let total_width = j.chars().count(); + let term_width = term_tuple.0 as usize; + if total_width > term_width { + break; + } + last = col + 1; + } + last +} + fn build_reader(opt: &Cli) -> Result>, std::io::Error> { let source: Box = if let Some(path) = opt.file.clone() { let file = File::open(path)?;