Prints welcome
parent
9632e8705e
commit
a4ecded355
@ -0,0 +1,15 @@
|
||||
ashe@tilde.club's password:
|
||||
Welcome to AsheOS 21.12.1 LTS (HRT/Estrix 6.25.21-wasm)
|
||||
|
||||
* Technical: https://tempest.dev
|
||||
* Personal: https://ashe.gay
|
||||
* Community: https://tilde.club
|
||||
|
||||
System information as of Sun Dec 12 01:20:23 UTC 2021
|
||||
|
||||
Usage of /: 83.0% of 128MB
|
||||
Memory usage: 12%
|
||||
Processes: 15
|
||||
Users logged in: 1
|
||||
|
||||
Last login: Sat Dec 11 06:52:52 2021 from 142.44.150.184
|
@ -0,0 +1,122 @@
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
use web_sys::KeyboardEvent;
|
||||
use web_sys::Element;
|
||||
|
||||
use crate::State;
|
||||
|
||||
pub enum Key {
|
||||
Letter(char),
|
||||
Space,
|
||||
Backspace,
|
||||
Return,
|
||||
Ctrl(Box<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)?;
|
||||
|
||||
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 {
|
||||
Some(Key::Letter(key.chars().next().unwrap()))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update_state(state : &mut State, key : Key) {
|
||||
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();
|
||||
}
|
||||
|
||||
// TODO - parse the command
|
||||
},
|
||||
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)
|
||||
}
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => ()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[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;
|
||||
}
|
||||
|
||||
|
||||
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";
|
||||
}
|
||||
|
||||
if state.prompt.len() > 0 {
|
||||
output += "\x1b[0;31m";
|
||||
output += state.prompt.as_str();
|
||||
output += " ";
|
||||
}
|
||||
|
||||
// TODO: Line overflow
|
||||
output += state.input.as_str();
|
||||
|
||||
output += "\x1b[0m";
|
||||
|
||||
let formatted = format_html(output);
|
||||
target.set_inner_html(formatted.as_str());
|
||||
}
|
||||
|
||||
pub fn build_prompt() -> String {
|
||||
let mut prompt = String::new();
|
||||
|
||||
prompt += "\x1b[0;36m";
|
||||
prompt += "ashe";
|
||||
prompt += "\x1b[2;33m";
|
||||
prompt += "@";
|
||||
prompt += "tilde.club";
|
||||
prompt += "\x1b[2;37m";
|
||||
prompt += ":";
|
||||
prompt += "\x1b[2;32m";
|
||||
prompt += "~";
|
||||
prompt += "\x1b[2;37m";
|
||||
prompt += "$";
|
||||
|
||||
return prompt
|
||||
}
|
Loading…
Reference in New Issue