diff --git a/src/main.rs b/src/main.rs index 455d236..f1433ee 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,22 +1,24 @@ +mod input; +mod parser; +mod job; +mod variables; + use std::io::Result; use std::process::{ ExitStatus, exit, }; - -mod input; -mod output; -mod parser; -mod job; +use variables::Variables; fn main() { let mut buffer = input::Buffer::new(); let mut bytes_read: usize = 1; let mut exit_status: Option>; let mut exit_code: i32 = 0; + let mut variables = Variables::new(); while bytes_read != 0 { - output::print_ps1(); + variables.print_ps1(); bytes_read = buffer.read_line(); exit_status = buffer.try_spawn(); diff --git a/src/output.rs b/src/output.rs deleted file mode 100644 index 3888585..0000000 --- a/src/output.rs +++ /dev/null @@ -1,9 +0,0 @@ -use std::io::stdout; -use std::io::Write; - -pub fn print_ps1() { - print!("$ "); - if stdout().flush().is_err() { - eprintln!("error: can't fully flush stdout or reached EOF"); - } -} diff --git a/src/variables.rs b/src/variables.rs new file mode 100644 index 0000000..2c2af3b --- /dev/null +++ b/src/variables.rs @@ -0,0 +1,39 @@ +use std::collections::HashMap; +use std::io; +use std::io::Write; +use std::env; + +pub struct Variables { + variables: HashMap, +} + +impl Variables { + pub fn new() -> Self { + Self { + variables: HashMap::new(), + } + } + + pub fn get(&self, name: &str) -> String { + let content = env::var(name); + + if content.is_ok() { + content.unwrap() + } else { + let content = self.variables.get(name); + + if content.is_some() { + content.unwrap().to_string() + } else { + "".to_string() + } + } + } + + pub fn print_ps1(&self) { + print!("{}", self.get("PS1")); + if io::stdout().flush().is_err() { + eprintln!("error: can't fully flush stdout or reached EOF"); + } + } +}