diff --git a/crates/coremem/src/meas.rs b/crates/coremem/src/meas.rs index e766e2f..7d581f2 100644 --- a/crates/coremem/src/meas.rs +++ b/crates/coremem/src/meas.rs @@ -73,7 +73,7 @@ impl Measurement { &self.name } - pub fn str_value(&self) -> String { + pub fn pretty_print(&self) -> String { use MeasurementValue::*; match self.value { Field(v) => format!("{}{}", v, self.unit), @@ -87,6 +87,18 @@ impl Measurement { } } + /// format the Measurement in a way that could be parseable later. + /// one major use case for this is in dumping the type to a CSV. + pub fn machine_readable(&self) -> String { + use MeasurementValue::*; + match self.value { + Field(v) => format!("{},{},{}", v.x(), v.y(), v.z()), + Float(f) => f.to_string(), + Int(u) => u.to_string(), + Dim(v) => format!("{},{},{}", v.x(), v.y(), v.z()), + } + } + /// retrieve the float value of this measurement -- if it's of float type. /// useful for tests pub fn get_float(&self) -> Option { diff --git a/crates/coremem/src/render.rs b/crates/coremem/src/render.rs index 53df5d3..7ae7e46 100644 --- a/crates/coremem/src/render.rs +++ b/crates/coremem/src/render.rs @@ -295,7 +295,7 @@ impl<'a, S: AbstractSim> RenderSteps<'a, S> { } fn render_measurements(&mut self) { for (meas_no, m) in meas::eval_multiple(self.sim, &self.meas).into_iter().enumerate() { - let meas_string = m.str_value(); + let meas_string = m.pretty_print(); for (i, c) in meas_string.chars().enumerate() { let glyph = BASIC_FONTS.get(c) .or_else(|| GREEK_FONTS.get(c)) @@ -454,7 +454,7 @@ impl Renderer for ColorTermRenderer { for m in measurements { // Measurements can be slow to compute stdout.flush().unwrap(); - let meas_string = m.str_value(); + let meas_string = m.pretty_print(); stdout.queue(cursor::MoveDown(1)).unwrap(); stdout.queue(cursor::MoveToColumn(1)).unwrap(); stdout.queue(PrintStyledContent(style(meas_string))).unwrap(); @@ -739,7 +739,7 @@ impl Renderer for CsvRenderer { }, CsvState::Writing(writer) => writer, }; - writer.write_record(row.iter().map(|m| m.str_value())).unwrap(); + writer.write_record(row.iter().map(|m| m.machine_readable())).unwrap(); writer.flush().unwrap(); *lock = Some(CsvState::Writing(writer)); } diff --git a/crates/post/src/bin/csv.rs b/crates/post/src/bin/csv.rs index d29007f..a400259 100644 --- a/crates/post/src/bin/csv.rs +++ b/crates/post/src/bin/csv.rs @@ -21,7 +21,7 @@ fn main() { loop { for meas in frame.measurements() { - print!("\"{}\",", meas.str_value()); + print!("\"{}\",", meas.machine_readable().replace(",", "\\,")); } println!("");