gnuplot_wrapper/
script.rsuse std::{fs::File, io::Write};
use crate::command::Command;
#[derive(Debug)]
pub struct Script {
commands: Vec<String>,
path: Option<String>
}
impl Script {
pub fn new() -> Self {
return Script {
commands: Vec::new(),
path: None
};
}
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(&mut self, path: &str) {
self.path = Some(path.to_owned());
let mut file: File = File::create_new(&path).expect(&format!("Error when creating file: {}", path));
for command in &self.commands {
file.write_fmt(format_args!("{}", command)).expect(&format!("Error when writing to file: {}", path));
}
file.flush().expect(&format!("Error when flushing file: {}", path));
}
pub fn get_path(&self) -> Option<String> {
return self.path.clone();
}
pub fn get_string(&self) -> String {
let mut script: String = String::new();
for line in &self.commands {
script.push_str(&line);
}
return script;
}
}