From a62cd828b9d59c563d1d92630ea786e442c58475 Mon Sep 17 00:00:00 2001 From: Pavel Sountsov Date: Sat, 26 Oct 2024 12:58:04 -0700 Subject: [PATCH] Add a flag to save examples to images, for testing. Also, disable the echoing by default. --- gnuplot/examples/common.rs | 51 +++++++++++++++++++++++++++++++++--- gnuplot/examples/example1.rs | 6 ----- 2 files changed, 47 insertions(+), 10 deletions(-) diff --git a/gnuplot/examples/common.rs b/gnuplot/examples/common.rs index d8e4ee7e..e0ed74ca 100644 --- a/gnuplot/examples/common.rs +++ b/gnuplot/examples/common.rs @@ -3,6 +3,7 @@ use argparse_rs::*; use gnuplot::*; use std::env; +use std::path::Path; #[derive(Copy, Clone)] pub struct BetterIterator<'l, T: 'l> @@ -41,8 +42,11 @@ impl<'l, T: 'l> BetterIteratorExt<'l, T> for &'l [T] pub struct Common { pub no_show: bool, + pub save_png: bool, pub term: Option, pub extension: String, + pub output_dir: String, + pub echo: bool, } impl Common @@ -70,13 +74,37 @@ impl Common ArgType::Option, ); args.add_opt( - "extension", + "output-dir", None, + 'o', + false, + "output directory.", + ArgType::Option, + ); + args.add_opt( + "extension", + Some("out"), 'e', false, "specify what extension the output file should have. Default: 'out'", ArgType::Option, ); + args.add_opt( + "save-png", + Some("false"), + 's', + false, + "render the plots to images.", + ArgType::Flag, + ); + args.add_opt( + "echo", + Some("false"), + 'g', + false, + "echo gnuplot commands.", + ArgType::Flag, + ); let res = args.parse(arg_vec.iter()).unwrap(); @@ -87,21 +115,36 @@ impl Common } Some(Common { + output_dir: res.get("output-dir").unwrap_or("".into()), no_show: res.get("no-show").unwrap(), + save_png: res.get("save-png").unwrap(), + echo: res.get("echo").unwrap_or(false), term: res.get::("terminal").map(|s| s.to_string()), - extension: res.get::("extension").unwrap_or("out".to_string()), + extension: res.get::("extension").unwrap(), }) } pub fn show(&self, fg: &mut Figure, filename: &str) { + let out_path = Path::new(&self.output_dir).join(filename); self.term.as_ref().map(|t| { - fg.set_terminal(&t, &format!("{}.{}", filename, self.extension)); + fg.set_terminal( + &t, + out_path.with_extension(&self.extension).to_str().unwrap(), + ); }); if !self.no_show { fg.show().unwrap(); } - fg.echo_to_file(&format!("{}.gnuplot", filename)); + if self.save_png + { + fg.save_to_png(out_path.with_extension("png").to_str().unwrap(), 800, 600) + .unwrap(); + } + if self.echo + { + fg.echo_to_file(out_path.with_extension("gnuplot").to_str().unwrap()); + } } } diff --git a/gnuplot/examples/example1.rs b/gnuplot/examples/example1.rs index 60a1f65b..f5a28d6e 100644 --- a/gnuplot/examples/example1.rs +++ b/gnuplot/examples/example1.rs @@ -91,12 +91,6 @@ fn example(c: Common) c.show(&mut fg, "example1_1"); - if !c.no_show - { - fg.save_to_pdf("example1_1.pdf", 3.5, 3.5).unwrap(); - fg.save_to_png("example1_1.png", 256, 256).unwrap(); - } - let mut fg = Figure::new(); fg.axes2d()