From 076993268da470bbc28129170a9e5c9dad1b80b8 Mon Sep 17 00:00:00 2001 From: Jonas Maier <> Date: Thu, 5 Mar 2026 16:47:13 +0100 Subject: delete button, and various fixes --- src/main.rs | 56 ++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 48 insertions(+), 8 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 19b98c2..81cc5d3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -96,6 +96,12 @@ impl Session { } } +fn read1() -> u8 { + let mut buf = [0]; + io::stdin().lock().read_exact(&mut buf).unwrap(); + buf[0] +} + fn event_loop() { let stdin = io::stdin(); let stdout = io::stdout(); @@ -121,6 +127,17 @@ fn event_loop() { }; match buf[0] { + // Ctrl+C + 3 => { + // clear line + cursor::move_cursor(Direction::Right, se.line.distance_from_right_end()); + for _ in 0..se.line.len() { + write!(io::stdout(), "\x08 \x08").unwrap(); + } + io::stdout().lock().flush().unwrap(); + se.line.clear(); + } + // EOF 4 => { break; @@ -129,7 +146,10 @@ fn event_loop() { // Ctrl+L 12 => { clear_screen(); - print!("{}", se.prompt()); + write!(io::stdout(), "{}", se.prompt()).unwrap(); + io::stdout().write_all(&se.line.into_bytes()).unwrap(); + cursor::move_cursor(Direction::Left, se.line.distance_from_right_end()); + io::stdout().lock().flush().unwrap(); } // Ctrl+R @@ -164,10 +184,17 @@ fn event_loop() { // Escape sequence 27 => { - let mut seq = [0u8; 2]; - stdin.lock().read_exact(&mut seq).unwrap(); + let mut seq = vec![read1()]; if seq[0] == b'[' { + // still more + while { + let last = seq[seq.len() - 1]; + last < 0x40 || last > 0x7E || seq.len() == 1 + } { + seq.push(read1()); + } + match seq[1] { b'A' => { // up @@ -176,12 +203,25 @@ fn event_loop() { // down } b'C' => { - move_cursor(Direction::Right, 1); - se.line.right(); + if se.line.right() { + move_cursor(Direction::Right, 1); + io::stdout().lock().flush().unwrap(); + } } b'D' => { - move_cursor(Direction::Left, 1); - se.line.left(); + if se.line.left() { + move_cursor(Direction::Left, 1); + io::stdout().lock().flush().unwrap(); + } + } + b'3' => { + if seq.len() > 2 && seq[2] == b'~' { + // delete + se.line.del_right(); + se.line.display_post(b" "); + } else { + todo!("unhandled: {seq:?}"); + } } x => todo!("escape character {x}"), } @@ -200,7 +240,7 @@ fn event_loop() { x => { se.line.add(x); stdout.lock().write_all(&[x]).unwrap(); - se.line.display_post(); + se.line.display_post(b""); } } } -- cgit v1.2.3