From e568ccb94011146288b4bd63952be741b7500df5 Mon Sep 17 00:00:00 2001 From: Jonas Maier Date: Fri, 6 Mar 2026 12:51:07 +0100 Subject: completion somewhat works --- src/main.rs | 40 ++++++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 14 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index a5820e7..19e7743 100644 --- a/src/main.rs +++ b/src/main.rs @@ -96,6 +96,27 @@ impl Session { fn prompt(&self) -> String { format!("{} $ ", self.pretty_cwd()) } + + fn clear_prompt(&mut self) { + cursor::move_cursor(Direction::Right, self.line.distance_from_right_end()); + for _ in 0..self.line.len() { + write!(io::stdout(), "\x08 \x08").unwrap(); + } + io::stdout().lock().flush().unwrap(); + self.line.clear(); + } + + fn type_byte(&mut self, b: u8) { + self.line.add(b); + io::stdout().lock().write_all(&[b]).unwrap(); + self.line.display_post(b""); + } + + fn type_bytes(&mut self, bs: &[u8]) { + for b in bs.iter() { + self.type_byte(*b); + } + } } fn read1() -> u8 { @@ -131,13 +152,7 @@ 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(); + se.clear_prompt(); } // EOF @@ -172,9 +187,7 @@ fn event_loop() { if se.line.is_empty() && !se.line.is_dirty() && !se.history.is_empty() { // take previous command for editing let cmd = se.history[se.history.len() - 1].clone(); - io::stdout().write_all(&cmd).unwrap(); - io::stdout().flush().unwrap(); - se.line.set_content(cmd); + se.type_bytes(&cmd); } else if se.line.del_left().is_some() { print!("\x08 \x08"); } @@ -193,7 +206,8 @@ fn event_loop() { if suggestions.len() == 1 { // apply suggestion - todo!("apply suggestion"); + se.type_bytes(&suggestions[0].delta); + continue; } cursor::save(); @@ -273,9 +287,7 @@ fn event_loop() { // Normal character x => { - se.line.add(x); - stdout.lock().write_all(&[x]).unwrap(); - se.line.display_post(b""); + se.type_byte(x); } } } -- cgit v1.2.3