From 86cdb8c21dec737a3f0a40311782de851c1203d1 Mon Sep 17 00:00:00 2001 From: Jonas Maier <> Date: Sat, 7 Mar 2026 18:47:22 +0100 Subject: Ctrl+Left and Ctrl+Right --- src/main.rs | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 87 insertions(+), 2 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index cd7b244..5e29a6e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -120,6 +120,12 @@ impl Session { self.line.clear(); } + fn reprint_prompt(&self) { + print!("{}", self.prompt()); + self.line.display_pre(); + self.line.display_post(b""); + } + fn display_historic_entry(&mut self) { self.clear_prompt(); let new = if self.history_visit == 0 { @@ -146,6 +152,47 @@ impl Session { } } + // move to next + fn move_left_word(&mut self) { + let mut i = 0; + + // find word + while let Some(b' ') = self.line.get_left() { + self.line.left(); + i += 1; + } + + // skip it + while let Some(x) = self.line.get_left() && !x.is_ascii_whitespace() { + self.line.left(); + i += 1; + } + + cursor::move_cursor(Direction::Left, i); + io::stdout().flush().unwrap(); + } + + fn move_right_word(&mut self) { + let mut i = 0; + + // find word + while let Some(b' ') = self.line.get_right() { + self.line.right(); + i += 1; + } + + // skip it + while let Some(x) = self.line.get_right() + && !x.is_ascii_whitespace() + { + self.line.right(); + i += 1; + } + + cursor::move_cursor(Direction::Right, i); + io::stdout().flush().unwrap(); + } + fn type_byte(&mut self, b: u8) { self.line.add(b); io::stdout().lock().write_all(&[b]).unwrap(); @@ -244,6 +291,9 @@ fn event_loop() { break; } + // apparently another backspace, don't do anything. + 8 => {} + // Ctrl+L 12 => { clear_screen(); @@ -355,16 +405,51 @@ fn event_loop() { if seq.len() > 2 && seq[2] == b'~' { se.del_right(); } else { - todo!("unhandled: {seq:?}"); + todo!("unhandled: {}", seq.escape_ascii()); } } + // HOME button + b'H' => { + se.move_to_begin(); + } + // END button b'F' => { se.move_to_end(); } - x => todo!("escape character {x}"), + // Ctrl Arrow + b'1' => { + if seq[1..].starts_with(b"1;5") { + if seq.len() == 4 { + todo!("idk what this is."); + } + match seq[4] { + b'A' => { + println!("Ctrl+Up"); + se.reprint_prompt(); + continue; + } + b'B' => { + println!("Ctrl+Down"); + se.reprint_prompt(); + continue; + } + b'C' => { + se.move_right_word(); + } + b'D' => { + se.move_left_word(); + } + _ => todo!("unhandled {}", seq.escape_ascii()), + } + continue; + } + todo!("unhandled {}", seq[1..].escape_ascii()) + } + + _ => todo!("escape characters {}", seq[1..].escape_ascii()), } } } -- cgit v1.2.3