feat: does not exit on error and can now print coloured text
Brijesh Wawdhane brijesh@wawdhane.com
Sat, 26 Oct 2024 16:21:12 +0530
5 files changed,
37 insertions(+),
3 deletions(-)
A
src/colours.rs
@@ -0,0 +1,23 @@
+pub fn red(text: &str) -> String { + format!("\x1b[31m{}\x1b[0m", text) +} + +pub fn green(text: &str) -> String { + format!("\x1b[32m{}\x1b[0m", text) +} + +pub fn yellow(text: &str) -> String { + format!("\x1b[33m{}\x1b[0m", text) +} + +pub fn blue(text: &str) -> String { + format!("\x1b[34m{}\x1b[0m", text) +} + +pub fn magenta(text: &str) -> String { + format!("\x1b[35m{}\x1b[0m", text) +} + +pub fn cyan(text: &str) -> String { + format!("\x1b[36m{}\x1b[0m", text) +}
M
src/main.rs
→
src/main.rs
@@ -1,9 +1,11 @@
#![allow(unused)] +mod colours; mod error; mod executor; mod parser; mod shell; +mod tests; mod utils; fn main() -> Result<(), error::ShellError> {
M
src/parser.rs
→
src/parser.rs
@@ -1,11 +1,16 @@
use std::str::FromStr; +#[derive(Debug, PartialEq)] pub struct ParsedCommand { pub command: String, pub args: Vec<String>, } pub fn parse(input: &str) -> Result<ParsedCommand, &'static str> { + if input.trim().is_empty() { + return Err("Empty input should result in error"); + } + let mut command = String::new(); let mut args = Vec::new(); let mut current_arg = String::new();
M
src/shell.rs
→
src/shell.rs
@@ -1,3 +1,4 @@
+use crate::colours::red; use crate::{error::ShellError, executor}; use std::io::{self, Write};@@ -15,12 +16,12 @@
pub fn run(&mut self) -> Result<(), ShellError> { loop { print!("{}", self.prompt); - io::stdout().flush().map_err(|e| ShellError::Io(e))?; + io::stdout().flush().map_err(ShellError::from)?; let mut input = String::new(); io::stdin() .read_line(&mut input) - .map_err(|e| ShellError::Io(e))?; + .map_err(ShellError::from)?; let input = input.trim(); if input.is_empty() {@@ -31,7 +32,10 @@ println!("Exiting BSH...");
break; } - executor::execute(input)?; + match executor::execute(input) { + Ok(_) => {} + Err(err) => eprintln!("{}: {}", red("Error"), err), + } } Ok(()) }