Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ pub fn print_events() {
println!();
exit(0);
}
"help" => {
help(&mut buffer, &mut cursor_position);
}
"" => {
println!("\r");
}
Expand Down Expand Up @@ -151,6 +154,17 @@ fn lines_from_file<T: AsRef<Path>>(filename: T) -> impl Iterator<Item = String>
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)
{
Expand Down Expand Up @@ -217,6 +231,30 @@ pub fn lex(input: &str) -> Vec<Token> {
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::<Vec<char>>();
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;
Expand Down
7 changes: 5 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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)),
}
}