blob: 15a818258d7eb8e782e41b668773bc225a81881d (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
use crate::cursor::*;
use std::io::Write;
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);
}
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() {}
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();
}
}
|