Skip to content

Commit

Permalink
Add a flag to save examples to images, for testing.
Browse files Browse the repository at this point in the history
Also, disable the echoing by default.
  • Loading branch information
SiegeLordEx authored and SiegeLord committed Oct 26, 2024
1 parent 8ca2b8b commit a62cd82
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 10 deletions.
51 changes: 47 additions & 4 deletions gnuplot/examples/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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<String>,
pub extension: String,
pub output_dir: String,
pub echo: bool,
}

impl Common
Expand Down Expand Up @@ -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();

Expand All @@ -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::<String>("terminal").map(|s| s.to_string()),
extension: res.get::<String>("extension").unwrap_or("out".to_string()),
extension: res.get::<String>("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());
}
}
}
6 changes: 0 additions & 6 deletions gnuplot/examples/example1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit a62cd82

Please sign in to comment.