From 27c6a9b71d62275bcdf2ff0659ee8938b013e1d8 Mon Sep 17 00:00:00 2001 From: Ahurac Date: Mon, 11 Dec 2023 19:39:04 +0100 Subject: [PATCH] =?UTF-8?q?variables=20:=20ajout=20du=20support=20des=20va?= =?UTF-8?q?riables,=20print=20du=20PS1=20dans=20ce=20fichier=20d=C3=A9sorm?= =?UTF-8?q?ais?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.rs | 14 ++++++++------ src/output.rs | 9 --------- src/variables.rs | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 15 deletions(-) delete mode 100644 src/output.rs create mode 100644 src/variables.rs 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"); + } + } +}