gnuplot_wrapper/
gnuplot.rs

1use std::process::{Child, Command, Stdio};
2
3use crate::{
4    process::{gnuplot_live_process::GnuplotLiveProcess, gnuplot_process::GnuplotProcess, gnuplot_script_process::GnuplorScriptProcess},
5    script::Script,
6};
7
8/// Entry point
9pub struct Gnuplot {
10    command: Command,
11}
12
13impl Gnuplot {
14    /// Create new instance with default command "gnuplot"
15    pub fn new() -> Self {
16        return Self::new_with_path_str("gnuplot");
17    }
18
19    /// Create new instance
20    pub fn new_with_path_str(path: &str) -> Self {
21        let mut command: Command = Command::new(path.to_string());
22        command.arg("-p");
23        return Gnuplot { command: command };
24    }
25
26    /// Create new instance
27    pub fn new_with_path(path: &String) -> Self {
28        return Self::new_with_path_str(&path);
29    }
30
31    /// Run gnuplot in live mode
32    pub fn run_live(&mut self) -> GnuplotLiveProcess {
33        let child: Child = self
34            .command
35            .stdin(Stdio::piped())
36            .spawn()
37            .expect("Error when running gnuplot");
38        return GnuplotLiveProcess::new(child);
39    }
40
41    /// Run script in gnuplot
42    pub fn execute_script(&mut self, script: Script) -> GnuplorScriptProcess {
43        let path = script.get_path().ok_or("Script is not saved!").unwrap();
44
45        let child: Child = self
46            .command
47            .arg(path)
48            .spawn()
49            .expect("Error when running gnuplot");
50        return GnuplorScriptProcess::new(child);
51    }
52}