diff options
| -rw-r--r-- | src/linebuf.rs | 12 | ||||
| -rw-r--r-- | src/main.rs | 10 |
2 files changed, 21 insertions, 1 deletions
diff --git a/src/linebuf.rs b/src/linebuf.rs index 91d0306..15a8182 100644 --- a/src/linebuf.rs +++ b/src/linebuf.rs @@ -1,5 +1,5 @@ -use std::io::Write; use crate::cursor::*; +use std::io::Write; pub struct LineBuf { pre: Vec<u8>, @@ -45,6 +45,16 @@ impl LineBuf { self.pre.push(chr); } + pub fn is_empty(&self) -> bool { + self.pre.is_empty() && self.post.is_empty() + } + + /// sets content all to the left + pub fn set_content(&mut self, buf: Vec<u8>) { + self.pre = buf; + self.post = Vec::new(); + } + /// returns the whole contents of the buffer, and empties it in the process pub fn dump(&mut self) -> Vec<u8> { while self.right() {} diff --git a/src/main.rs b/src/main.rs index 5854272..b0db2e4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -108,6 +108,7 @@ fn main() { let mut buffer = [0u8; 1]; let mut line = LineBuf::new(); + let mut history = Vec::new(); print!("{PROMPT}"); @@ -127,6 +128,7 @@ fn main() { let line = line.dump(); if !line.is_empty() { print!("\r\n"); + history.push(line.clone()); run_command(&raw, line); } } @@ -164,6 +166,14 @@ fn main() { } } + b'|' if line.is_empty() && !history.is_empty() => { + let mut cmd = history[history.len()-1].clone(); + cmd.extend_from_slice(b" | "); + io::stdout().write_all(&cmd).unwrap(); + io::stdout().flush().unwrap(); + line.set_content(cmd); + } + // Normal character x => { line.add(x); |
