gnuplot_wrapper/
script.rsuse std::{fs::File, io::Write};
use crate::command::Command;
#[derive(Debug)]
pub struct Script {
commands: Vec<String>,
path: String
}
impl Script {
pub fn new(path: &str) -> Self {
return Script {
commands: Vec::new(),
path: path.to_string()
};
}
pub fn add_raw_command(&mut self, command: &str) {
self.commands.push(command.to_owned() + "\n");
}
pub fn add_command(&mut self, command: impl Command) {
self.commands.push(command.to_raw() + "\n");
}
pub fn br(&mut self) {
self.commands.push("\n".to_owned());
}
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));
}
pub fn get_path(&self) -> &String {
return &self.path;
}
pub fn get_string(&self) -> String {
let mut script: String = String::new();
for line in &self.commands {
script.push_str(&line);
}
return script;
}
}