Skip to content

Commit a62cd82

Browse files
SiegeLordExSiegeLord
authored andcommitted
Add a flag to save examples to images, for testing.
Also, disable the echoing by default.
1 parent 8ca2b8b commit a62cd82

2 files changed

Lines changed: 47 additions & 10 deletions

File tree

gnuplot/examples/common.rs

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use argparse_rs::*;
44
use gnuplot::*;
55
use std::env;
6+
use std::path::Path;
67

78
#[derive(Copy, Clone)]
89
pub struct BetterIterator<'l, T: 'l>
@@ -41,8 +42,11 @@ impl<'l, T: 'l> BetterIteratorExt<'l, T> for &'l [T]
4142
pub struct Common
4243
{
4344
pub no_show: bool,
45+
pub save_png: bool,
4446
pub term: Option<String>,
4547
pub extension: String,
48+
pub output_dir: String,
49+
pub echo: bool,
4650
}
4751

4852
impl Common
@@ -70,13 +74,37 @@ impl Common
7074
ArgType::Option,
7175
);
7276
args.add_opt(
73-
"extension",
77+
"output-dir",
7478
None,
79+
'o',
80+
false,
81+
"output directory.",
82+
ArgType::Option,
83+
);
84+
args.add_opt(
85+
"extension",
86+
Some("out"),
7587
'e',
7688
false,
7789
"specify what extension the output file should have. Default: 'out'",
7890
ArgType::Option,
7991
);
92+
args.add_opt(
93+
"save-png",
94+
Some("false"),
95+
's',
96+
false,
97+
"render the plots to images.",
98+
ArgType::Flag,
99+
);
100+
args.add_opt(
101+
"echo",
102+
Some("false"),
103+
'g',
104+
false,
105+
"echo gnuplot commands.",
106+
ArgType::Flag,
107+
);
80108

81109
let res = args.parse(arg_vec.iter()).unwrap();
82110

@@ -87,21 +115,36 @@ impl Common
87115
}
88116

89117
Some(Common {
118+
output_dir: res.get("output-dir").unwrap_or("".into()),
90119
no_show: res.get("no-show").unwrap(),
120+
save_png: res.get("save-png").unwrap(),
121+
echo: res.get("echo").unwrap_or(false),
91122
term: res.get::<String>("terminal").map(|s| s.to_string()),
92-
extension: res.get::<String>("extension").unwrap_or("out".to_string()),
123+
extension: res.get::<String>("extension").unwrap(),
93124
})
94125
}
95126

96127
pub fn show(&self, fg: &mut Figure, filename: &str)
97128
{
129+
let out_path = Path::new(&self.output_dir).join(filename);
98130
self.term.as_ref().map(|t| {
99-
fg.set_terminal(&t, &format!("{}.{}", filename, self.extension));
131+
fg.set_terminal(
132+
&t,
133+
out_path.with_extension(&self.extension).to_str().unwrap(),
134+
);
100135
});
101136
if !self.no_show
102137
{
103138
fg.show().unwrap();
104139
}
105-
fg.echo_to_file(&format!("{}.gnuplot", filename));
140+
if self.save_png
141+
{
142+
fg.save_to_png(out_path.with_extension("png").to_str().unwrap(), 800, 600)
143+
.unwrap();
144+
}
145+
if self.echo
146+
{
147+
fg.echo_to_file(out_path.with_extension("gnuplot").to_str().unwrap());
148+
}
106149
}
107150
}

gnuplot/examples/example1.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,6 @@ fn example(c: Common)
9191

9292
c.show(&mut fg, "example1_1");
9393

94-
if !c.no_show
95-
{
96-
fg.save_to_pdf("example1_1.pdf", 3.5, 3.5).unwrap();
97-
fg.save_to_png("example1_1.png", 256, 256).unwrap();
98-
}
99-
10094
let mut fg = Figure::new();
10195

10296
fg.axes2d()

0 commit comments

Comments
 (0)