aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.rs
diff options
context:
space:
mode:
authorJonas Maier <jonas@x77.dev>2026-03-06 12:51:07 +0100
committerJonas Maier <jonas@x77.dev>2026-03-06 12:51:07 +0100
commite568ccb94011146288b4bd63952be741b7500df5 (patch)
tree97d229e2e89a1347c648fd6100b0c87dd925b0f4 /src/main.rs
parentfb80e9c1cd4c2dcbb2d2ba1e2be8c7e19b9f0ce1 (diff)
downloadpish-e568ccb94011146288b4bd63952be741b7500df5.tar.gz
completion somewhat works
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs40
1 files changed, 26 insertions, 14 deletions
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);
}
}
}