From aa53c75eede51b81e68269d319b9c65a9b8b65cd Mon Sep 17 00:00:00 2001 From: Ashelyn Dawn Date: Sun, 1 May 2022 20:18:59 -0600 Subject: [PATCH] Fix indentation --- src/io.rs | 119 +++++++++++++++++++++++---------------------- src/js.rs | 6 +-- src/main.rs | 136 ++++++++++++++++++++++++++-------------------------- 3 files changed, 134 insertions(+), 127 deletions(-) diff --git a/src/io.rs b/src/io.rs index 988e91d..923b8aa 100644 --- a/src/io.rs +++ b/src/io.rs @@ -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) + Ctrl(Box), } -pub fn parse_key_event(event : &KeyboardEvent, ignore_ctrl : bool) -> Option { +pub fn parse_key_event(event: &KeyboardEvent, ignore_ctrl: bool) -> Option { 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 { +pub fn handle_key_event(state: &mut State, key: Key) -> Option { 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, '$1') }")] +#[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') }" +)] 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::>().join("\n"); +pub fn print_state(target: &mut Element, state: &State) { + let mut output = state + .output + .iter() + .map(|s| (*s).clone()) + .collect::>() + .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 -} \ No newline at end of file + return prompt; +} diff --git a/src/js.rs b/src/js.rs index 38cfb19..b89723e 100644 --- a/src/js.rs +++ b/src/js.rs @@ -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; } diff --git a/src/main.rs b/src/main.rs index 0d0fd56..8a299be 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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, - prompt: String, - max_rows: usize, + fs_root: fs::DirEntry, + cwd: String, + input : String, + output : VecDeque, + 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); + state.prompt = io::build_prompt(&state); + io::print_state(&mut render_target, &state); + }) as Box); - 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); } \ No newline at end of file