gnuplot_wrapper/
script.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
53
54
55
56
57
58
use std::{fs::File, io::Write};

use crate::command::Command;

/// Gnuplot script
#[derive(Debug)]
pub struct Script {
    commands: Vec<String>,
    path: String
}

impl Script {
    /// Create script
    pub fn new(path: &str) -> Self {
        return Script {
            commands: Vec::new(),
            path: path.to_string()
        };
    }

    /// Add command
    pub fn add_raw_command(&mut self, command: &str) {
        self.commands.push(command.to_owned() + "\n");
    }

    /// Add command
    pub fn add_command(&mut self, command: impl Command) {
        self.commands.push(command.to_raw() + "\n");
    }

    /// Insert empty line in script
    pub fn br(&mut self) {
        self.commands.push("\n".to_owned());
    }

    /// Save script
    pub fn save(&self) {
        let mut file: File = File::create_new(&self.path).expect(&format!("Error when creating file: {}", self.path));
        for command in &self.commands {
            file.write_fmt(format_args!("{}", command)).expect(&format!("Error when writing to file: {}", self.path));
        }
        file.flush().expect(&format!("Error when flushing file: {}", self.path));
    }

    /// Get script path
    pub fn get_path(&self) -> &String {
        return &self.path;
    }

    /// Get printed script
    pub fn get_string(&self) -> String {
        let mut script: String = String::new();
        for line in &self.commands {
            script.push_str(&line);
        }
        return script;
    }
}