Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sigfig command line parameter support #107

Merged
merged 5 commits into from Oct 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/datatype.rs
Expand Up @@ -126,13 +126,14 @@ pub fn format_strings(
vec_col: &[&str],
lower_column_width: usize,
upper_column_width: usize,
sigfig: i64,
) -> Vec<String> {
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) {
Expand Down Expand Up @@ -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::<f64>() {
sigfig::DecimalSplits { val, sigfig: 3 }.final_string()
sigfig::DecimalSplits { val, sigfig }.final_string()
} else {
text.to_string()
}
Expand Down
13 changes: 5 additions & 8 deletions src/datatype/sigfig.rs
Expand Up @@ -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)
Expand Down
21 changes: 12 additions & 9 deletions src/main.rs
Expand Up @@ -108,13 +108,13 @@ struct Cli {
help = "The delimiter separating the columns."
)]
delimiter: Option<u8>,
//#[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",
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -371,7 +371,7 @@ fn main() {
// vector of formatted values
let vf: Vec<Vec<String>> = 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 {
Expand Down Expand Up @@ -711,6 +711,7 @@ mod tests {
&v[i],
col_largest_width_post_proc[i],
col_largest_width_post_proc[i],
3,
);
}

Expand Down Expand Up @@ -788,6 +789,7 @@ mod tests {
&v[i],
col_largest_width_post_proc[i],
col_largest_width_post_proc[i],
3,
);
}

Expand Down Expand Up @@ -820,6 +822,7 @@ mod tests {
&v[i],
col_largest_width_post_proc[i],
col_largest_width_post_proc[i],
3,
);
}

Expand Down