Skip to content

Commit

Permalink
General cleanup (#93)
Browse files Browse the repository at this point in the history
* Take AsRef<str> instead of String in DecimalSplits::rhs_string_len

* Move fn declarations outside of main function scope

* Move rhs_string_len into separate non-member function

It doesn't use `self` at all, it doesn't need to be a member function.

* Remove unused DecimalSplitsList assignment

It's just unnecessary computation at the moment.
  • Loading branch information
jacobmischka authored Oct 9, 2021
1 parent ebbe731 commit ccf9aca
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 65 deletions.
19 changes: 2 additions & 17 deletions src/datatype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<f64>().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::<f64>() {
sigfig::DecimalSplits { val, sigfig: 3 }.final_string()
} else {
text.to_string()
}
Expand Down
33 changes: 19 additions & 14 deletions src/datatype/sigfig.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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<bool> {
sigfig_index_lhs_or_rhs(&self.final_string(), self.sig_fig())
}
Expand All @@ -99,6 +92,18 @@ impl DecimalSplits {
}
}

pub fn rhs_string_len<S>(string_final_string: S) -> usize
where
S: AsRef<str>,
{
string_final_string
.as_ref()
.split('.')
.nth(1)
.map(|decimals| decimals.len())
.unwrap_or_default()
}

#[derive(Debug)]
pub struct DecimalSplitsList {
pub val: f64,
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down
69 changes: 35 additions & 34 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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]
Expand Down Expand Up @@ -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::<Vec<_>>()
.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),
Expand Down Expand Up @@ -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<Vec<String>>, 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
Expand Down Expand Up @@ -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::<Vec<_>>()
.try_into()
.expect("Not 3 elements");
i32_array
}

// how wide will the print be?
fn get_num_cols_to_print(cols: usize, vp: Vec<Vec<String>>, 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<Reader<Box<dyn Read>>, std::io::Error> {
let source: Box<dyn Read> = if let Some(path) = opt.file.clone() {
let file = File::open(path)?;
Expand Down

0 comments on commit ccf9aca

Please sign in to comment.