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:
@@ -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<f32> {
|
||||
|
@@ -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<S: AbstractSim> Renderer<S> 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<S: AbstractSim> Renderer<S> 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));
|
||||
}
|
||||
|
@@ -21,7 +21,7 @@ fn main() {
|
||||
|
||||
loop {
|
||||
for meas in frame.measurements() {
|
||||
print!("\"{}\",", meas.str_value());
|
||||
print!("\"{}\",", meas.machine_readable().replace(",", "\\,"));
|
||||
}
|
||||
println!("");
|
||||
|
||||
|
Reference in New Issue
Block a user