From 4f1dfb90ba2a859875c2cee9990e9c94fb789ef3 Mon Sep 17 00:00:00 2001 From: Jonas Maier <> Date: Sun, 3 May 2026 14:31:05 +0200 Subject: keybinds --- src/main.rs | 132 +++++++++++++++++------------------------------------------- 1 file changed, 37 insertions(+), 95 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index a21b313..5ec5a56 100644 --- a/src/main.rs +++ b/src/main.rs @@ -143,7 +143,11 @@ impl Session { } fn prompt(&self) -> String { - format!("[{}]# ", self.pretty_cwd()) + #[cfg(debug_assertions)] + let dev = "dev "; + #[cfg(not(debug_assertions))] + let dev = ""; + format!("{dev}[{}]# ", self.pretty_cwd()) } fn clear_prompt(&mut self) { @@ -238,6 +242,10 @@ impl Session { io::stdout().flush().unwrap(); } + fn del_left_word(&mut self) { + todo!() + } + fn del_right_word(&mut self) { let mut del = 0; while let Some(x) = self.line.get_right() @@ -331,6 +339,34 @@ impl Session { } } + fn try_submit_command(session: Arc>) { + let mut se = session.lock().unwrap(); + let line = se.line.into_bytes(); + + if !line.is_empty() { + let parsed = match parse::do_parse(&line) { + Ok(p) => p, + Err((crate::parse::ParseError::Eof, _)) => { + se.line.add(b'\n'); + print!("\r\n> "); + return; + } + Err(e) => { + println!("{e:?}\n{}", se.prompt()); + return; + } + }; + print!("\r\n"); + let entry = HistoryEntry::new(line.clone()); + history::persist(&entry); + se.history.push(entry); + se.history_visit = 0; + se.line.dump(); + drop(se); + run::run(session.clone(), parsed); + } + } + fn screen_clear(&mut self) { clear_screen(); self.reprint_prompt(); @@ -432,37 +468,6 @@ fn event_loop() { } match key { - // TODO: make simple characters also keybinds and do not specially handle tab or newline here. - ansi::KbInput::Key([b'\t']) => { - drop(se); - Session::complete(session.clone()); - } - ansi::KbInput::Key([b'\r' | b'\n']) => { - let line = se.line.into_bytes(); - - if !line.is_empty() { - let parsed = match parse::do_parse(&line) { - Ok(p) => p, - Err((crate::parse::ParseError::Eof, _)) => { - se.line.add(b'\n'); - print!("\r\n> "); - continue; - } - Err(e) => { - println!("{e:?}\n{}", se.prompt()); - continue; - } - }; - print!("\r\n"); - let entry = HistoryEntry::new(line.clone()); - history::persist(&entry); - se.history.push(entry); - se.history_visit = 0; - se.line.dump(); - drop(se); - run::run(session.clone(), parsed); - } - } ansi::KbInput::Key([x]) => se.type_byte(x), ansi::KbInput::Escape(escape) => { for terminfo_key in escape.keys.iter() { @@ -474,72 +479,9 @@ fn event_loop() { continue 'repl; } } - - if matches!(&escape.value[..], b"\r" | b"\n") { - let line = se.line.into_bytes(); - - if !line.is_empty() { - let parsed = match parse::do_parse(&line) { - Ok(p) => p, - Err((crate::parse::ParseError::Eof, _)) => { - se.line.add(b'\n'); - print!("\r\n> "); - continue; - } - Err(e) => { - println!("{e:?}\n{}", se.prompt()); - continue; - } - }; - print!("\r\n"); - let entry = HistoryEntry::new(line.clone()); - history::persist(&entry); - se.history.push(entry); - se.history_visit = 0; - se.line.dump(); - drop(se); - run::run(session.clone(), parsed); - } - } } ansi::KbInput::InvalidEscape(_) => continue, } - - /* - match key { - Kb::Arrow(dir) => match dir { - Direction::Up => se.history_up(), - Direction::Down => se.history_down(), - Direction::Left => { - if se.line.left() { - move_cursor(Direction::Left, 1); - io::stdout().lock().flush().unwrap(); - } - } - Direction::Right => { - if se.line.right() { - move_cursor(Direction::Right, 1); - io::stdout().lock().flush().unwrap(); - } - } - }, - Kb::CtrlArrow(dir) => match dir { - Direction::Left => se.move_left_word(), - Direction::Right => se.move_right_word(), - _ => { - println!(" Ctrl+{dir:?} not implemented"); - se.reprint_prompt(); - } - }, - Kb::DeleteLeft => { - } - Kb::DeleteRight => se.del_right(), - Kb::CtrlDeleteRight => se.del_right_word(), - Kb::Home => se.move_to_begin(), - Kb::End => se.move_to_end(), - Kb::Key(x) => se.type_byte(x), - } - */ } session.lock().unwrap().raw.disable(); -- cgit v1.2.3