Fix indentation

main
Ashelyn Dawn 2 years ago
parent 6acc77e52e
commit aa53c75eed

@ -1,7 +1,7 @@
use wasm_bindgen::prelude::*;
use web_sys::KeyboardEvent;
use web_sys::Element;
use web_sys::KeyboardEvent;
use crate::State;
@ -10,85 +10,92 @@ pub enum Key {
Space,
Backspace,
Return,
Ctrl(Box<Key>)
Ctrl(Box<Key>),
}
pub fn parse_key_event(event : &KeyboardEvent, ignore_ctrl : bool) -> Option<Key> {
pub fn parse_key_event(event: &KeyboardEvent, ignore_ctrl: bool) -> Option<Key> {
if event.ctrl_key() && !ignore_ctrl {
let base = parse_key_event(event, true)?;
let base = parse_key_event(event, true)?;
return Some(Key::Ctrl(Box::new(base)))
return Some(Key::Ctrl(Box::new(base)));
}
let key = event.key();
// TODO: ^C
match key.as_str() {
"Space" => Some(Key::Space),
"Backspace" => Some(Key::Backspace),
"Enter" => Some(Key::Return),
_ => {
if key.len() != 1 {
None
} else {
event.prevent_default();
Some(Key::Letter(key.chars().next().unwrap()))
}
"Space" => Some(Key::Space),
"Backspace" => Some(Key::Backspace),
"Enter" => Some(Key::Return),
_ => {
if key.len() != 1 {
None
} else {
event.prevent_default();
Some(Key::Letter(key.chars().next().unwrap()))
}
}
}
}
pub fn handle_key_event(state : &mut State, key : Key) -> Option<String> {
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());
state.output.push_back(format!("{} {}", state.prompt, input));
if state.output.len() > state.max_rows {
state.output.pop_front();
}
Key::Return => {
let input = std::mem::replace(&mut state.input, String::new());
state
.output
.push_back(format!("{} {}", state.prompt, input));
if state.output.len() > state.max_rows {
state.output.pop_front();
}
return Some(input);
},
Key::Backspace => {
if state.input.len() > 0 {
state.input.remove(state.input.len() - 1);
}
},
Key::Space => state.input += " ",
Key::Letter(letter) => {
let mut tmp = [0; 4];
state.input += letter.encode_utf8(&mut tmp)
return Some(input);
}
Key::Backspace => {
if state.input.len() > 0 {
state.input.remove(state.input.len() - 1);
}
Key::Ctrl(letter) =>
match letter.as_ref() {
Key::Letter(letter) => {
if *letter == 'c' {
let mut input = std::mem::replace(&mut state.input, String::new());
input += "^C";
state.output.push_back(format!("{}", input));
if state.output.len() > state.max_rows {
state.output.pop_front();
}
}
}
_ => ()
}
Key::Space => state.input += " ",
Key::Letter(letter) => {
let mut tmp = [0; 4];
state.input += letter.encode_utf8(&mut tmp)
}
Key::Ctrl(letter) => match letter.as_ref() {
Key::Letter(letter) => {
if *letter == 'c' {
let mut input = std::mem::replace(&mut state.input, String::new());
input += "^C";
state.output.push_back(format!("{}", input));
if state.output.len() > state.max_rows {
state.output.pop_front();
}
}
}
_ => (),
},
};
return None
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>') }")]
#[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>') }"
)]
extern "C" {
fn format_html(text : String) -> String;
fn format_html(text: String) -> String;
}
pub fn print_state(target : &mut Element, state : &State) {
let mut output = state.output.iter().map(|s| (*s).clone()).collect::<Vec<String>>().join("\n");
pub fn print_state(target: &mut Element, state: &State) {
let mut output = state
.output
.iter()
.map(|s| (*s).clone())
.collect::<Vec<String>>()
.join("\n");
if state.output.len() > 0 {
output += "\n";
output += "\n";
}
if state.prompt.len() > 0 {
@ -106,7 +113,7 @@ pub fn print_state(target : &mut Element, state : &State) {
target.set_inner_html(formatted.as_str());
}
pub fn build_prompt(state : &State) -> String {
pub fn build_prompt(state: &State) -> String {
let mut prompt = String::new();
let cwd = state.cwd.replace("/home/ashe", "~");
@ -123,5 +130,5 @@ pub fn build_prompt(state : &State) -> String {
prompt += "\x1b[2;37m";
prompt += "$";
return prompt
}
return prompt;
}

@ -5,8 +5,8 @@ use js_sys::Promise;
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = console)]
pub fn log(s: &str);
#[wasm_bindgen(js_namespace = console)]
pub fn log(s: &str);
}
#[macro_export]
@ -18,7 +18,7 @@ macro_rules! console_log {
#[wasm_bindgen(inline_js = "export function _wait(ms) { return new Promise(res => setTimeout(res, ms)) }")]
extern "C" {
fn _wait(ms: i32) -> Promise;
fn _wait(ms: i32) -> Promise;
}

@ -21,115 +21,115 @@ use web_sys::Element;
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
pub enum Directory {
Home,
Projects
Home,
Projects
}
pub struct State {
fs_root: fs::DirEntry,
cwd: String,
input : String,
output : VecDeque<String>,
prompt: String,
max_rows: usize,
fs_root: fs::DirEntry,
cwd: String,
input : String,
output : VecDeque<String>,
prompt: String,
max_rows: usize,
}
impl State {
pub fn new() -> Self {
State {
fs_root: fs::setup_fs(),
cwd: "/home/ashe".to_string(),
input : String::new(),
output : VecDeque::new(),
prompt: String::new(),
max_rows: 24,
}
pub fn new() -> Self {
State {
fs_root: fs::setup_fs(),
cwd: "/home/ashe".to_string(),
input : String::new(),
output : VecDeque::new(),
prompt: String::new(),
max_rows: 24,
}
}
pub fn print(&mut self, line : String) {
self.output.push_back(line);
pub fn print(&mut self, line : String) {
self.output.push_back(line);
if self.output.len() > self.max_rows {
self.output.pop_front();
}
if self.output.len() > self.max_rows {
self.output.pop_front();
}
}
}
fn main() {
panic::set_hook(Box::new(console_error_panic_hook::hook));
panic::set_hook(Box::new(console_error_panic_hook::hook));
let window = web_sys::window().expect("should have a window in this context");
let document = window.document().expect("window should have a document");
let window = web_sys::window().expect("should have a window in this context");
let document = window.document().expect("window should have a document");
spawn_local(async {
init(document).await;
});
spawn_local(async {
init(document).await;
});
}
async fn init(document : Document) {
let mut state = State::new();
let mut state = State::new();
let mut render_target = document
.get_element_by_id("target")
.expect("should have #target on the page");
let mut render_target = document
.get_element_by_id("target")
.expect("should have #target on the page");
print_welcome(&mut render_target, &mut state).await;
print_welcome(&mut render_target, &mut state).await;
let closure = Closure::wrap(Box::new(move |event: web_sys::KeyboardEvent| {
let key = io::parse_key_event(&event, false);
let closure = Closure::wrap(Box::new(move |event: web_sys::KeyboardEvent| {
let key = io::parse_key_event(&event, false);
if key.is_none() {
return
}
if key.is_none() {
return
}
let key = key.unwrap();
let key = key.unwrap();
let command = io::handle_key_event(&mut state, key);
let command = io::handle_key_event(&mut state, key);
if let Some(command) = command {
handle_command(command, &mut state);
}
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(_)>);
state.prompt = io::build_prompt(&state);
io::print_state(&mut render_target, &state);
}) as Box<dyn FnMut(_)>);
document.add_event_listener_with_callback("keydown", closure.as_ref().unchecked_ref()).unwrap();
document.add_event_listener_with_callback("keydown", closure.as_ref().unchecked_ref()).unwrap();
closure.forget();
closure.forget();
}
pub async fn print_welcome(render_target : &mut Element, state : &mut State) {
let intro = include_str!("../res/welcome.txt");
let mut lines = intro.split("\n");
let intro = include_str!("../res/welcome.txt");
let mut lines = intro.split("\n");
let first_line = lines.next().unwrap();
state.input = String::from_str(first_line).unwrap();
io::print_state(render_target, state);
let first_line = lines.next().unwrap();
state.input = String::from_str(first_line).unwrap();
io::print_state(render_target, state);
js::wait(3000i32).await.unwrap();
js::wait(3000i32).await.unwrap();
state.output.push_back(String::from_str(first_line).unwrap());
state.input = String::new();
state.output.push_back(String::from_str(first_line).unwrap());
state.input = String::new();
io::print_state(render_target, state);
js::wait(1000i32).await.unwrap();
io::print_state(render_target, state);
js::wait(1000i32).await.unwrap();
for line in lines {
js::wait(30i32).await.unwrap();
for line in lines {
js::wait(30i32).await.unwrap();
state.output.push_back(String::from_str(line).unwrap());
if state.output.len() > state.max_rows {
state.output.pop_front();
}
io::print_state(render_target, state);
state.output.push_back(String::from_str(line).unwrap());
if state.output.len() > state.max_rows {
state.output.pop_front();
}
io::print_state(render_target, state);
}
js::wait(100i32).await.unwrap();
js::wait(100i32).await.unwrap();
state.prompt = io::build_prompt(&state);
io::print_state(render_target, state);
state.prompt = io::build_prompt(&state);
io::print_state(render_target, state);
}
Loading…
Cancel
Save