From e4ac219bf96fb4f9368e1915ac2cb0009925daee Mon Sep 17 00:00:00 2001 From: Yehowshua Immanuel Date: Fri, 27 Dec 2024 10:19:09 -0500 Subject: [PATCH] addressing Martin's corrections --- Cargo.lock | 1 + frontend/Cargo.toml | 1 + frontend/src/main.rs | 1 - frontend/src/term.rs | 63 +++++++++++++++++++++++--------------------- src-tauri/f.txt | 2 ++ 5 files changed, 37 insertions(+), 31 deletions(-) create mode 100644 src-tauri/f.txt diff --git a/Cargo.lock b/Cargo.lock index 82df3f0..e9471d9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1823,6 +1823,7 @@ version = "0.1.0" dependencies = [ "gloo-file", "shared", + "unicode-segmentation", "wasm-bindgen-test", "web-sys", "wellen", diff --git a/frontend/Cargo.toml b/frontend/Cargo.toml index f463636..be972aa 100644 --- a/frontend/Cargo.toml +++ b/frontend/Cargo.toml @@ -14,6 +14,7 @@ wasm-bindgen-test = "0.3.19" unexpected_cfgs = { level = "allow", check-cfg = ['cfg(FASTWAVE_PLATFORM)'] } [dependencies] +unicode-segmentation = "1.10" zoon.workspace = true wellen.workspace = true shared = { path = "../shared", features = ["frontend"] } diff --git a/frontend/src/main.rs b/frontend/src/main.rs index 8acf849..5327a5c 100644 --- a/frontend/src/main.rs +++ b/frontend/src/main.rs @@ -106,7 +106,6 @@ fn main() { platform::listen_term_update(|down_msg| { term::TERMINAL_STATE.set(down_msg); }).await; - zoon::println!("Printing on line 106"); }); } diff --git a/frontend/src/term.rs b/frontend/src/term.rs index e19bd44..2282e40 100644 --- a/frontend/src/term.rs +++ b/frontend/src/term.rs @@ -4,6 +4,7 @@ use chrono::format; use zoon::*; use zoon::{println, eprintln, *}; use shared::term::{TerminalDownMsg, TerminalScreen, TerminalUpMsg}; +use unicode_segmentation::UnicodeSegmentation; // use tokio::time::timeout; pub static TERM_OPEN: Lazy> = Lazy::new(|| {false.into()}); @@ -16,7 +17,6 @@ pub static TERMINAL_STATE: Lazy> = }); pub fn root() -> impl Element { - term_request(); let terminal = El::new() .s(Width::fill()) @@ -61,10 +61,6 @@ pub fn root() -> impl Element { root } -// TODO : fill this out -fn term_request() { -} - fn send_char( s : &str, has_control : bool, @@ -82,18 +78,22 @@ fn send_char( } -fn make_grid_with_newlines(term : &TerminalScreen) -> String { - let mut formatted = String::new(); - for (i, c) in term.content.chars().enumerate() { +fn make_grid_with_newlines(term: &TerminalScreen) -> String { + let mut formatted = String::with_capacity(term.content.len() + (term.content.len() / term.cols as usize)); + + term.content.chars().enumerate().for_each(|(i, c)| { formatted.push(c); - if ((i + 1) as u16) % term.cols == 0 { + if (i + 1) as u16 % term.cols == 0 { formatted.push('\n'); } - } + }); + formatted } -fn process_str(s: &str, has_ctrl : bool) -> Option { + +fn process_str(s: &str, has_ctrl: bool) -> Option { + println!("process_str: {s}"); match s { "Enter" => {return Some('\n');} "Escape" => {return Some('\x1B');} @@ -102,37 +102,40 @@ fn process_str(s: &str, has_ctrl : bool) -> Option { "ArrowDown" => {return Some('\x0E');} "ArrowLeft" => {return Some('\x02');} "ArrowRight" => {return Some('\x06');} + "Control" => {return None;} + "Shift" => {return None;} + "Meta" => {return None;} + "Alt" => {return None;} _ => {} } - // Check if the string has exactly one character - if s.chars().count() == 1 { - // Safe unwrap because we know the length is 1 - let c = s.chars().next().unwrap(); - let c = process_for_ctrl_char(c, has_ctrl); - return Some(c); + + let mut graphemes = s.graphemes(true); + let first = graphemes.next(); + + if let Some(g) = first { + if g.len() == 1 { + if let Some(c) = g.chars().next() { + let c = process_for_ctrl_char(c, has_ctrl); + return Some(c); + } + } } + None } +// Helper function to process control characters + fn is_lowercase_alpha(c: char) -> bool { char_is_between_inclusive(c, 'a', 'z') } -fn process_for_ctrl_char(c: char, has_ctrl : bool) -> char { - let mut final_ctrl_char = c; +fn process_for_ctrl_char(c: char, has_ctrl: bool) -> char { if has_ctrl { - if is_lowercase_alpha(c) { - let c_u8 = (c as u8); - let ctrl_char_u8 = c_u8 - 96; - final_ctrl_char = (ctrl_char_u8 as char); - } else if char_is_between_inclusive(c, '[', '_') { - let c_u8 = (c as u8); - let ctrl_char_u8 = c_u8 - 90; - final_ctrl_char = (ctrl_char_u8 as char); - } - + (c as u8 & 0x1F) as char + } else { + c } - return final_ctrl_char } fn char_is_between_inclusive(c : char, lo_char : char, hi_char : char) -> bool { diff --git a/src-tauri/f.txt b/src-tauri/f.txt new file mode 100644 index 0000000..e009d34 --- /dev/null +++ b/src-tauri/f.txt @@ -0,0 +1,2 @@ +Hello, I'm typing a file in vim!! +