feature/variables #3
3 changed files with 47 additions and 15 deletions
14
src/main.rs
14
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<Result<ExitStatus>>;
|
||||
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();
|
||||
|
|
|
@ -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
39
src/variables.rs
Normal 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");
|
||||
}
|
||||
}
|
||||
}
|
Reference in a new issue