when writing Measurements to a CSV, format them in a machine-readable manner

i haven't tested the ones which contains commas -- hopefully the CSV
encoder deals with these :-)
This commit is contained in:
2022-08-10 01:22:31 -07:00
parent 8a3a64face
commit 4fe8be8951
3 changed files with 17 additions and 5 deletions

View File

@@ -73,7 +73,7 @@ impl Measurement {
&self.name &self.name
} }
pub fn str_value(&self) -> String { pub fn pretty_print(&self) -> String {
use MeasurementValue::*; use MeasurementValue::*;
match self.value { match self.value {
Field(v) => format!("{}{}", v, self.unit), 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. /// retrieve the float value of this measurement -- if it's of float type.
/// useful for tests /// useful for tests
pub fn get_float(&self) -> Option<f32> { pub fn get_float(&self) -> Option<f32> {

View File

@@ -295,7 +295,7 @@ impl<'a, S: AbstractSim> RenderSteps<'a, S> {
} }
fn render_measurements(&mut self) { fn render_measurements(&mut self) {
for (meas_no, m) in meas::eval_multiple(self.sim, &self.meas).into_iter().enumerate() { 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() { for (i, c) in meas_string.chars().enumerate() {
let glyph = BASIC_FONTS.get(c) let glyph = BASIC_FONTS.get(c)
.or_else(|| GREEK_FONTS.get(c)) .or_else(|| GREEK_FONTS.get(c))
@@ -454,7 +454,7 @@ impl<S: AbstractSim> Renderer<S> for ColorTermRenderer {
for m in measurements { for m in measurements {
// Measurements can be slow to compute // Measurements can be slow to compute
stdout.flush().unwrap(); stdout.flush().unwrap();
let meas_string = m.str_value(); let meas_string = m.pretty_print();
stdout.queue(cursor::MoveDown(1)).unwrap(); stdout.queue(cursor::MoveDown(1)).unwrap();
stdout.queue(cursor::MoveToColumn(1)).unwrap(); stdout.queue(cursor::MoveToColumn(1)).unwrap();
stdout.queue(PrintStyledContent(style(meas_string))).unwrap(); stdout.queue(PrintStyledContent(style(meas_string))).unwrap();
@@ -739,7 +739,7 @@ impl<S: AbstractSim> Renderer<S> for CsvRenderer {
}, },
CsvState::Writing(writer) => writer, 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(); writer.flush().unwrap();
*lock = Some(CsvState::Writing(writer)); *lock = Some(CsvState::Writing(writer));
} }

View File

@@ -21,7 +21,7 @@ fn main() {
loop { loop {
for meas in frame.measurements() { for meas in frame.measurements() {
print!("\"{}\",", meas.str_value()); print!("\"{}\",", meas.machine_readable().replace(",", "\\,"));
} }
println!(""); println!("");