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/completion.rs | 11 +++++++++-- src/main.rs | 40 ++++++++++++++++++++++++++-------------- 2 files changed, 35 insertions(+), 16 deletions(-) diff --git a/src/completion.rs b/src/completion.rs index cb030a1..407a397 100644 --- a/src/completion.rs +++ b/src/completion.rs @@ -3,7 +3,7 @@ use std::fs; pub struct Suggestion { pub display: BString, - full: BString, + pub delta: BString, } fn _path_completion(mut prefix: BString) -> io::Result> { @@ -27,9 +27,16 @@ fn _path_completion(mut prefix: BString) -> io::Result> { let entry = entry?; let name = entry.file_name().as_bytes().to_vec(); if name.starts_with(&partial_entry) { + let mut delta = name[partial_entry.len()..].to_vec(); + + let is_dir = entry.metadata().map(|m| m.is_dir()).unwrap_or(false); + if is_dir { + delta.push(b'/'); + } + sugs.push(Suggestion { display: name, - full: entry.path().as_os_str().as_bytes().to_vec(), + delta, }); } } 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