33use argparse_rs:: * ;
44use gnuplot:: * ;
55use std:: env;
6+ use std:: path:: Path ;
67
78#[ derive( Copy , Clone ) ]
89pub struct BetterIterator < ' l , T : ' l >
@@ -41,8 +42,11 @@ impl<'l, T: 'l> BetterIteratorExt<'l, T> for &'l [T]
4142pub 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
4852impl 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}
0 commit comments