diff --git a/Cargo.toml b/Cargo.toml index 6c04f92..87bec4a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "lowcharts" -version = "0.2.0" +version = "0.3.1" authors = ["JuanLeon Lahoz "] edition = "2018" description = "Tool to draw low-resolution graphs in terminal" diff --git a/src/plot/xy.rs b/src/plot/xy.rs index fdd579c..ce5db8b 100644 --- a/src/plot/xy.rs +++ b/src/plot/xy.rs @@ -44,7 +44,12 @@ impl fmt::Display for XyPlot { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.stats)?; let _step = (self.stats.max - self.stats.min) / self.height as f64; - let y_width = format!("{:.3}", self.stats.max).len(); + let y_width = self + .y_axis + .iter() + .map(|v| format!("{:.3}", v).len()) + .max() + .unwrap(); let mut newvec = self.y_axis.to_vec(); newvec.reverse(); print_line(f, &self.x_axis, newvec[0]..f64::INFINITY, y_width)?; @@ -72,7 +77,7 @@ fn print_line( writeln!( f, "[{}] {}", - Blue.paint(format!("{y:.*}", y_width, y = range.start.to_string())), + Blue.paint(format!("{:>width$.3}", range.start, width = y_width)), Red.paint(row), ) } @@ -104,9 +109,9 @@ mod tests { plot.load(&[-1.0, 0.0, 1.0, 2.0, 3.0, 4.0, -1.0]); Paint::disable(); let display = format!("{}", plot); - assert!(display.contains("[3] ● ")); - assert!(display.contains("[2] ")); - assert!(display.contains("[1] ● ")); - assert!(display.contains("[-1] ● ●")); + assert!(display.contains("[ 3.000] ● ")); + assert!(display.contains("[ 2.000] ")); + assert!(display.contains("[ 1.000] ● ")); + assert!(display.contains("[-1.000] ● ●")); } } diff --git a/tests/integration_tests.rs b/tests/integration_tests.rs index 75aa606..e52e9f8 100644 --- a/tests/integration_tests.rs +++ b/tests/integration_tests.rs @@ -112,10 +112,10 @@ fn test_plot() { .assert() .success() .stdout(predicate::str::contains("Samples = 4; Min = 1; Max = 4\n")) - .stdout(predicate::str::contains("\n[3.25] ●")) - .stdout(predicate::str::contains("\n[2.5] ●")) - .stdout(predicate::str::contains("\n[1.75] ●")) - .stdout(predicate::str::contains("\n[1] ●")) + .stdout(predicate::str::contains("\n[3.250] ●")) + .stdout(predicate::str::contains("\n[2.500] ●")) + .stdout(predicate::str::contains("\n[1.750] ●")) + .stdout(predicate::str::contains("\n[1.000] ●")) .stderr(predicate::str::contains("[DEBUG] Cannot parse float")); } Err(_) => assert!(false, "Could not create temp file"),