diff options
| author | Jonas Maier <jonas@x77.dev> | 2026-03-06 20:44:43 +0100 |
|---|---|---|
| committer | Jonas Maier <jonas@x77.dev> | 2026-03-06 20:44:43 +0100 |
| commit | 379fb61ab27a3ab649c0971e9f762fedcfa126e6 (patch) | |
| tree | 84aee4927ab35ead43332309797cc5e8a12995c2 /src/main.rs | |
| parent | e01a65991b167d74f58f154a0002fa1740cfde66 (diff) | |
| download | pish-379fb61ab27a3ab649c0971e9f762fedcfa126e6.tar.gz | |
up & down arrows
Diffstat (limited to 'src/main.rs')
| -rw-r--r-- | src/main.rs | 38 |
1 files changed, 36 insertions, 2 deletions
diff --git a/src/main.rs b/src/main.rs index c66ec67..b95c51a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,16 +8,16 @@ use std::process::{Command, Stdio}; use std::thread::sleep; use std::time::Duration; +mod basedir; mod completion; mod cursor; +mod history; mod linebuf; mod panic; mod parse; mod raw; mod reload; mod run; -mod basedir; -mod history; use linebuf::LineBuf; use raw::*; @@ -61,6 +61,10 @@ pub struct Session { history: Vec<BString>, dispatch: CommandDispatch, prev_path: BString, + + /// n before end of history.len() + /// 0 == not checking history + history_visit: usize, } /// relative path -- in case it is a proper subpath the result starts with a slash `/` @@ -112,6 +116,32 @@ impl Session { self.line.clear(); } + fn display_historic_entry(&mut self) { + self.clear_prompt(); + let new = if self.history_visit == 0 { + Vec::new() + } else { + self.history[self.history.len() - self.history_visit].clone() + }; + io::stdout().write_all(&new).unwrap(); + io::stdout().flush().unwrap(); + self.line.set_content(new); + } + + fn history_up(&mut self) { + if self.history_visit < self.history.len() { + self.history_visit += 1; + self.display_historic_entry(); + } + } + + fn history_down(&mut self) { + if self.history_visit > 0 { + self.history_visit -= 1; + self.display_historic_entry(); + } + } + fn type_byte(&mut self, b: u8) { self.line.add(b); io::stdout().lock().write_all(&[b]).unwrap(); @@ -160,6 +190,7 @@ fn event_loop() { history: Vec::new(), dispatch: CommandDispatch::new(), prev_path: vec![b'.'], + history_visit: 0, }; print!("{}", se.prompt()); @@ -200,6 +231,7 @@ fn event_loop() { if !line.is_empty() { print!("\r\n"); se.history.push(line.clone()); + se.history_visit = 0; run::run(&mut se, line); } } @@ -269,9 +301,11 @@ fn event_loop() { match seq[1] { b'A' => { // up + se.history_up(); } b'B' => { // down + se.history_down(); } b'C' => { if se.line.right() { |
