gnuplot_wrapper/
gnuplot.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
use std::process::{Child, Command, Stdio};

use crate::{
    process::{gnuplot_live_process::GnuplotLiveProcess, gnuplot_process::GnuplotProcess, gnuplot_script_process::GnuplorScriptProcess},
    script::Script,
};

/// Entry point
pub struct Gnuplot {
    command: Command,
}

impl Gnuplot {
    /// Create new instance with default command "gnuplot"
    pub fn new() -> Self {
        return Self::new_with_path_str("gnuplot");
    }

    /// Create new instance
    pub fn new_with_path_str(path: &str) -> Self {
        let mut command: Command = Command::new(path.to_string());
        command.arg("-p");
        return Gnuplot { command: command };
    }

    /// Create new instance
    pub fn new_with_path(path: &String) -> Self {
        return Self::new_with_path_str(&path);
    }

    /// Run gnuplot in live mode
    pub fn run_live(&mut self) -> GnuplotLiveProcess {
        let child: Child = self
            .command
            .stdin(Stdio::piped())
            .spawn()
            .expect("Error when running gnuplot");
        return GnuplotLiveProcess::new(child);
    }

    /// Run script in gnuplot
    pub fn execute_script(&mut self, script: Script) -> GnuplorScriptProcess {
        let path = script.get_path().ok_or("Script is not saved!").unwrap();

        let child: Child = self
            .command
            .arg(path)
            .spawn()
            .expect("Error when running gnuplot");
        return GnuplorScriptProcess::new(child);
    }
}