aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/linebuf.rs10
-rw-r--r--src/main.rs8
2 files changed, 17 insertions, 1 deletions
diff --git a/src/linebuf.rs b/src/linebuf.rs
index 15a8182..1d28549 100644
--- a/src/linebuf.rs
+++ b/src/linebuf.rs
@@ -4,6 +4,7 @@ use std::io::Write;
pub struct LineBuf {
pre: Vec<u8>,
post: Vec<u8>,
+ dirty: bool,
}
#[allow(unused)]
@@ -12,14 +13,17 @@ impl LineBuf {
Self {
pre: Vec::new(),
post: Vec::new(),
+ dirty: false,
}
}
pub fn del_left(&mut self) -> Option<u8> {
+ self.dirty = true;
self.pre.pop()
}
pub fn del_right(&mut self) -> Option<u8> {
+ self.dirty = true;
self.post.pop()
}
@@ -42,6 +46,7 @@ impl LineBuf {
}
pub fn add(&mut self, chr: u8) {
+ self.dirty = true;
self.pre.push(chr);
}
@@ -49,6 +54,10 @@ impl LineBuf {
self.pre.is_empty() && self.post.is_empty()
}
+ pub fn is_dirty(&self) -> bool {
+ self.dirty
+ }
+
/// sets content all to the left
pub fn set_content(&mut self, buf: Vec<u8>) {
self.pre = buf;
@@ -60,6 +69,7 @@ impl LineBuf {
while self.right() {}
let mut buf = Vec::new();
core::mem::swap(&mut self.pre, &mut buf);
+ self.dirty = false;
buf
}
diff --git a/src/main.rs b/src/main.rs
index 9d8f191..4e3bb23 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -195,7 +195,13 @@ fn main() {
// Backspace (127 on most systems)
127 => {
- if line.del_left().is_some() {
+ if line.is_empty() && !line.is_dirty() && !history.is_empty() {
+ // take previous command for editing
+ let cmd = history[history.len() - 1].clone();
+ io::stdout().write_all(&cmd).unwrap();
+ io::stdout().flush().unwrap();
+ line.set_content(cmd);
+ } else if line.del_left().is_some() {
print!("\x08 \x08");
}
}