aboutsummaryrefslogtreecommitdiffstats
path: root/src/linebuf.rs
diff options
context:
space:
mode:
authorJonas Maier <>2026-03-05 08:21:24 +0100
committerJonas Maier <>2026-03-05 08:21:24 +0100
commitb2c463e8bbc8c4685d5979d2b8c5a0cb2706fba2 (patch)
treea67c11a0684b9b575c99002eae101b30355ca828 /src/linebuf.rs
parenta8f9ac71cfa31302b8577065c56fab2bc4b035d2 (diff)
downloadpish-b2c463e8bbc8c4685d5979d2b8c5a0cb2706fba2.tar.gz
restructure
Diffstat (limited to 'src/linebuf.rs')
-rw-r--r--src/linebuf.rs64
1 files changed, 64 insertions, 0 deletions
diff --git a/src/linebuf.rs b/src/linebuf.rs
new file mode 100644
index 0000000..91d0306
--- /dev/null
+++ b/src/linebuf.rs
@@ -0,0 +1,64 @@
+use std::io::Write;
+use crate::cursor::*;
+
+pub struct LineBuf {
+ pre: Vec<u8>,
+ post: Vec<u8>,
+}
+
+#[allow(unused)]
+impl LineBuf {
+ pub fn new() -> Self {
+ Self {
+ pre: Vec::new(),
+ post: Vec::new(),
+ }
+ }
+
+ pub fn del_left(&mut self) -> Option<u8> {
+ self.pre.pop()
+ }
+
+ pub fn del_right(&mut self) -> Option<u8> {
+ self.post.pop()
+ }
+
+ pub fn left(&mut self) -> bool {
+ if let Some(byte) = self.del_left() {
+ self.post.push(byte);
+ true
+ } else {
+ false
+ }
+ }
+
+ pub fn right(&mut self) -> bool {
+ if let Some(byte) = self.del_right() {
+ self.pre.push(byte);
+ true
+ } else {
+ false
+ }
+ }
+
+ pub fn add(&mut self, chr: u8) {
+ self.pre.push(chr);
+ }
+
+ /// returns the whole contents of the buffer, and empties it in the process
+ pub fn dump(&mut self) -> Vec<u8> {
+ while self.right() {}
+ let mut buf = Vec::new();
+ core::mem::swap(&mut self.pre, &mut buf);
+ buf
+ }
+
+ /// TODO: kinda ugly that this is here
+ pub fn display_post(&self) {
+ for &x in self.post.iter().rev() {
+ std::io::stdout().write_all(&[x]).unwrap();
+ }
+ move_cursor(Direction::Left, self.post.len());
+ std::io::stdout().flush().unwrap();
+ }
+}