variables : ajout du support des variables, print du PS1 dans ce fichier désormais

This commit is contained in:
Ahurac 2023-12-11 19:39:04 +01:00
parent 7f2755c3cb
commit 27c6a9b71d
3 changed files with 47 additions and 15 deletions

View file

@ -1,22 +1,24 @@
mod input;
mod parser;
mod job;
mod variables;
use std::io::Result; use std::io::Result;
use std::process::{ use std::process::{
ExitStatus, ExitStatus,
exit, exit,
}; };
use variables::Variables;
mod input;
mod output;
mod parser;
mod job;
fn main() { fn main() {
let mut buffer = input::Buffer::new(); let mut buffer = input::Buffer::new();
let mut bytes_read: usize = 1; let mut bytes_read: usize = 1;
let mut exit_status: Option<Result<ExitStatus>>; let mut exit_status: Option<Result<ExitStatus>>;
let mut exit_code: i32 = 0; let mut exit_code: i32 = 0;
let mut variables = Variables::new();
while bytes_read != 0 { while bytes_read != 0 {
output::print_ps1(); variables.print_ps1();
bytes_read = buffer.read_line(); bytes_read = buffer.read_line();
exit_status = buffer.try_spawn(); exit_status = buffer.try_spawn();

View file

@ -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");
}
}

39
src/variables.rs Normal file
View file

@ -0,0 +1,39 @@
use std::collections::HashMap;
use std::io;
use std::io::Write;
use std::env;
pub struct Variables {
variables: HashMap<String, String>,
}
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");
}
}
}