Brijesh's Git Server — bsh @ 420896e777207fee868cfcaa7801f1d4b44cda46

my own shell program

src/shell.rs (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
use crate::colours::red;
use crate::{error::ShellError, executor};
use std::io::{self, Write};

pub struct Shell {
    prompt: String,
}

impl Shell {
    pub fn new() -> Self {
        Self {
            prompt: "bsh> ".to_string(),
        }
    }

    pub fn run(&mut self) -> Result<(), ShellError> {
        loop {
            print!("{}", self.prompt);
            io::stdout().flush().map_err(ShellError::from)?;

            let mut input = String::new();
            io::stdin()
                .read_line(&mut input)
                .map_err(ShellError::from)?;
            let input = input.trim();

            if input.is_empty() {
                continue;
            }
            if input == "exit" {
                println!("Exiting BSH...");
                break;
            }

            match executor::execute(input) {
                Ok(_) => {}
                Err(err) => eprintln!("{}: {}", red("Error"), err),
            }
        }
        Ok(())
    }
}