From 3196a63d1d195c5ab92130f134af4008b8564bb4 Mon Sep 17 00:00:00 2001 From: Ashelyn Dawn Date: Sat, 11 Dec 2021 23:18:57 -0700 Subject: [PATCH] Very fake cd --- index.html | 2 +- src/commands.rs | 57 +++++++++++++++++++++++++++++++++++++++++++++++++ src/io.rs | 15 ++++++++----- src/main.rs | 23 ++++++++++++++++---- 4 files changed, 87 insertions(+), 10 deletions(-) create mode 100644 src/commands.rs diff --git a/index.html b/index.html index 9042a2c..fa24613 100644 --- a/index.html +++ b/index.html @@ -2,7 +2,7 @@ - ~ashe + ashe@tilde.club diff --git a/src/commands.rs b/src/commands.rs new file mode 100644 index 0000000..6afd510 --- /dev/null +++ b/src/commands.rs @@ -0,0 +1,57 @@ +use std::str::FromStr; + +use crate::{Directory, State}; + +pub fn handle_command(command_string : String, state : &mut State) { + let mut words = command_string.split(' '); + + let command = words.next().unwrap(); + let args = words.collect::>(); + + match command { + "clear" => { + state.output.clear(); + } + + "cd" => { + if args.len() == 0 { + state.current_working_directory = Directory::Home; + } else { + match state.current_working_directory { + Directory::Home => { + match args[0] { + "~" => (), + ".." => state.output.push_back(String::from_str("bash: cd: ..: Permission denied").unwrap()), + "projects" => state.current_working_directory = Directory::Projects, + _ => { + if args[0].chars().next().unwrap() == '/' { + state.output.push_back(format!("bash: cd: {}: Permission denied", args[0])); + } else { + state.output.push_back(format!("bash: cd: {}: No such file or directory", args[0])); + } + } + } + }, + Directory::Projects => { + match args[0] { + "~" => state.current_working_directory = Directory::Home, + ".." => state.current_working_directory = Directory::Home, + _ => { + if args[0].chars().next().unwrap() == '/' { + state.output.push_back(format!("bash: cd: {}: Permission denied", args[0])); + } else { + state.output.push_back(format!("bash: cd: {}: No such file or directory", args[0])); + } + } + } + + } + } + } + } + + _ => { + state.output.push_back(format!("bash: {}: command not found", command)); + } + } +} \ No newline at end of file diff --git a/src/io.rs b/src/io.rs index de27bd4..cab0013 100644 --- a/src/io.rs +++ b/src/io.rs @@ -37,7 +37,7 @@ pub fn parse_key_event(event : &KeyboardEvent, ignore_ctrl : bool) -> Option Option { match key { Key::Return => { let input = std::mem::replace(&mut state.input, String::new()); @@ -46,7 +46,7 @@ pub fn update_state(state : &mut State, key : Key) { state.output.pop_front(); } - // TODO - parse the command + return Some(input); }, Key::Backspace => { if state.input.len() > 0 { @@ -72,7 +72,9 @@ pub fn update_state(state : &mut State, key : Key) { } _ => () } - } + }; + + return None } #[wasm_bindgen(inline_js = "const ansi_up = new AnsiUp; export function format_html(text) { return ansi_up.ansi_to_html(text).replace(/(https:[^ ]*)/g, '$1') }")] @@ -103,7 +105,7 @@ pub fn print_state(target : &mut Element, state : &State) { target.set_inner_html(formatted.as_str()); } -pub fn build_prompt() -> String { +pub fn build_prompt(state : &State) -> String { let mut prompt = String::new(); prompt += "\x1b[0;36m"; @@ -114,7 +116,10 @@ pub fn build_prompt() -> String { prompt += "\x1b[2;37m"; prompt += ":"; prompt += "\x1b[2;32m"; - prompt += "~"; + prompt += match state.current_working_directory { + crate::Directory::Home => "~", + crate::Directory::Projects => "~/projects" + }; prompt += "\x1b[2;37m"; prompt += "$"; diff --git a/src/main.rs b/src/main.rs index 26cd885..d38a1e9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,10 +3,12 @@ extern crate console_error_panic_hook; #[macro_use] mod js; mod io; +mod commands; use std::collections::VecDeque; use std::panic; use std::str::FromStr; +use commands::handle_command; use wasm_bindgen::prelude::*; use wasm_bindgen::JsCast; use wasm_bindgen_futures::spawn_local; @@ -16,12 +18,18 @@ use web_sys::Element; #[global_allocator] static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; +pub enum Directory { + Home, + Projects +} + pub struct State { // cwd : &Directory input : String, output : VecDeque, prompt: String, - max_rows: usize + max_rows: usize, + current_working_directory : Directory } impl State { @@ -30,7 +38,8 @@ impl State { input : String::new(), output : VecDeque::new(), prompt: String::new(), - max_rows: 24 + max_rows: 24, + current_working_directory: Directory::Home } } } @@ -66,7 +75,13 @@ async fn init(document : Document) { let key = key.unwrap(); - io::update_state(&mut state, key); + let command = io::handle_key_event(&mut state, key); + + if let Some(command) = command { + handle_command(command, &mut state); + } + + state.prompt = io::build_prompt(&state); io::print_state(&mut render_target, &state); }) as Box); @@ -104,6 +119,6 @@ pub async fn print_welcome(render_target : &mut Element, state : &mut State) { js::wait(100i32).await.unwrap(); - state.prompt = io::build_prompt(); + state.prompt = io::build_prompt(&state); io::print_state(render_target, state); } \ No newline at end of file