Very fake cd

main
Ashelyn Dawn 3 years ago
parent a4ecded355
commit 3196a63d1d

@ -2,7 +2,7 @@
<html>
<head>
<meta charset="utf-8" />
<title>~ashe</title>
<title>ashe@tilde.club</title>
<script src="https://unpkg.com/ansi_up@5.1.0/ansi_up.js"></script>
</head>
<body>

@ -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::<Vec<&str>>();
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));
}
}
}

@ -37,7 +37,7 @@ pub fn parse_key_event(event : &KeyboardEvent, ignore_ctrl : bool) -> Option<Key
}
}
pub fn update_state(state : &mut State, key : Key) {
pub fn handle_key_event(state : &mut State, key : Key) -> Option<String> {
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, '<a href=\"$1\" target=\"_blank\">$1</a>') }")]
@ -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 += "$";

@ -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<String>,
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<dyn FnMut(_)>);
@ -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);
}
Loading…
Cancel
Save