diff options
author | Vla Mai <[email protected]> | 2019-04-01 23:37:15 +0100 |
---|---|---|
committer | Vla Mai <[email protected]> | 2019-04-01 23:37:15 +0100 |
commit | 4d1e44fd4c6237228c6d366ba371d9c88a9311e1 (patch) | |
tree | df37b8932b4b01dafd58a328f2177c10b132f04d /src | |
parent | 3546c82fb9cd2495340adbd6aadc4099d293e83e (diff) |
add print function
Diffstat (limited to 'src')
-rw-r--r-- | src/main.rs | 12 |
1 files changed, 8 insertions, 4 deletions
diff --git a/src/main.rs b/src/main.rs index 5219c6d..94cbc1e 100644 --- a/src/main.rs +++ b/src/main.rs | |||
@@ -49,7 +49,7 @@ fn main() { | |||
49 | if CONFIGURATION.input.len() > 0 { | 49 | if CONFIGURATION.input.len() > 0 { |
50 | let evaled = eval_math_expression(&CONFIGURATION.input[..]); | 50 | let evaled = eval_math_expression(&CONFIGURATION.input[..]); |
51 | match evaled { | 51 | match evaled { |
52 | Ok(ans) => println!("{:.*}", CONFIGURATION.fix, ans), | 52 | Ok(ans) => pprint(ans), |
53 | Err(e) => { | 53 | Err(e) => { |
54 | eprintln!("{}", handler(e)); | 54 | eprintln!("{}", handler(e)); |
55 | std::process::exit(1); | 55 | std::process::exit(1); |
@@ -74,7 +74,7 @@ fn main() { | |||
74 | rl.add_history_entry(line.as_ref()); | 74 | rl.add_history_entry(line.as_ref()); |
75 | let evaled = eval_math_expression(&line[..]); | 75 | let evaled = eval_math_expression(&line[..]); |
76 | match evaled { | 76 | match evaled { |
77 | Ok(ans) => println!("{:.*}", CONFIGURATION.fix, ans), | 77 | Ok(ans) => pprint(ans), |
78 | Err(e) => println!("{}", handler(e)), | 78 | Err(e) => println!("{}", handler(e)), |
79 | }; | 79 | }; |
80 | }, | 80 | }, |
@@ -96,6 +96,10 @@ fn main() { | |||
96 | } | 96 | } |
97 | } | 97 | } |
98 | 98 | ||
99 | fn pprint(ans: f64) { | ||
100 | println!("{a:width$}", width = CONFIGURATION.fix, a = ans) | ||
101 | } | ||
102 | |||
99 | fn parse_arguments() -> Configuration { | 103 | fn parse_arguments() -> Configuration { |
100 | let config = App::new(env!("CARGO_PKG_NAME")) | 104 | let config = App::new(env!("CARGO_PKG_NAME")) |
101 | .version(env!("CARGO_PKG_VERSION")) | 105 | .version(env!("CARGO_PKG_VERSION")) |
@@ -119,7 +123,7 @@ fn parse_arguments() -> Configuration { | |||
119 | let mut input = String::new(); | 123 | let mut input = String::new(); |
120 | if let Some(i) = config.value_of("INPUT") { | 124 | if let Some(i) = config.value_of("INPUT") { |
121 | input.push_str(i); | 125 | input.push_str(i); |
122 | }; | 126 | }; |
123 | Configuration { | 127 | Configuration { |
124 | radian_mode: config.is_present("radian"), | 128 | radian_mode: config.is_present("radian"), |
125 | fix: config.value_of("fix") | 129 | fix: config.value_of("fix") |
@@ -166,7 +170,7 @@ fn eval_math_expression(input: &str) -> Result<f64, CalcError> { | |||
166 | Ok(evaled) | 170 | Ok(evaled) |
167 | } | 171 | } |
168 | 172 | ||
169 | #[cfg(test)] | 173 | #[cfg(test)] |
170 | mod tests { | 174 | mod tests { |
171 | use super::*; | 175 | use super::*; |
172 | 176 | ||