diff --git a/src/lib.rs b/src/lib.rs index 4b36fef..8d992d5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -103,6 +103,9 @@ pub fn print_events() { println!(); exit(0); } + "help" => { + help(&mut buffer, &mut cursor_position); + } "" => { println!("\r"); } @@ -151,6 +154,17 @@ fn lines_from_file>(filename: T) -> impl Iterator buf.lines().map(|l| l.expect("Could not parse line")) } +pub fn help(buffer: &mut String, cursor_position: &mut usize) { + let is_tty = termion::is_tty(&File::open("/dev/stdin").unwrap()); + if is_tty { + disable_raw_mode().unwrap(); + } + println!("\nCommands\nHelp -- Displays this help message\nexit -- Exits rustsh"); + buffer.clear(); + *cursor_position = 0; + enable_raw_mode().unwrap(); +} + fn get_command(n: usize) -> String { match lines_from_file(var("HOME").unwrap().as_str().to_owned() + "/.rustsh/history.txt").nth(n) { @@ -217,6 +231,30 @@ pub fn lex(input: &str) -> Vec { result.push(Token::Whitespace(c)); it.next(); } + 'h' => { + let mut tmp = String::new(); + let mut do_push = false; + tmp.push(*it.peek().unwrap()); + it.next(); + let target = "elp".chars().collect::>(); + let mut pos: usize = 0; + while it.peek().is_some() && pos < 3 { + if target.get(pos).unwrap() != it.peek().unwrap() { + break; + } + if pos == 2 && *target.get(pos).unwrap() == 'p' { + do_push = true; + } + tmp.push(*it.peek().unwrap()); + it.next(); + pos += 1; + } + if do_push { + result.push(Token::Builtin(tmp)); + } else { + result.push(Token::Word(tmp)); + } + } 'e' => { let mut tmp = String::new(); let mut do_push = false; diff --git a/src/main.rs b/src/main.rs index 477a562..b9dc360 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,5 @@ use crossterm::terminal::{disable_raw_mode, enable_raw_mode}; -use rustsh::{execute_command, init, print_events}; +use rustsh::{execute_command, help, init, print_events}; use std::fs::File; use std::io::stdin; use std::process::exit; @@ -15,5 +15,8 @@ fn main() { } let mut buffer = String::new(); stdin().read_line(&mut buffer).unwrap(); - execute_command(&buffer); + match buffer.as_str() { + "help" => help(&mut buffer, &mut 0), + _ => print!("{}", execute_command(&buffer)), + } }