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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
use std::io::{self, Read, Write, IsTerminal};
use std::os::unix::io::AsRawFd;
use termios::*;
struct ScopedRawMode {
fd: i32,
settings: Termios,
}
impl Drop for ScopedRawMode {
fn drop(&mut self) {
self.disable();
}
}
impl ScopedRawMode {
fn on_fd(fd: i32) -> Self {
let mut termios = Termios::from_fd(fd).unwrap();
let settings = termios.clone();
cfmakeraw(&mut termios);
tcsetattr(fd, TCSANOW, &termios).unwrap();
Self { fd, settings }
}
fn disable(&self) {
tcsetattr(self.fd, TCSANOW, &self.settings).unwrap();
}
}
macro_rules! print {
($($x:tt)*) => {{
write!(io::stdout(), $($x)*).unwrap();
io::stdout().flush().unwrap();
}}
}
struct LineBuffer {
pre: Vec<u8>,
post: Vec<u8>,
}
#[allow(unused)]
impl LineBuffer {
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
}
pub fn display_post(&self) {
for &x in self.post.iter().rev() {
io::stdout().write_all(&[x]).unwrap();
}
move_cursor(Direction::Left, self.post.len());
io::stdout().flush().unwrap();
}
}
#[derive(Debug, Clone, Copy)]
pub enum Direction {
Up,
Down,
Left,
Right,
}
pub fn move_cursor(direction: Direction, n: usize) {
if n == 0 {
return;
}
let code = match direction {
Direction::Up => 'A', // CUU
Direction::Down => 'B', // CUD
Direction::Right => 'C', // CUF
Direction::Left => 'D', // CUB
};
print!("\x1b[{n}{code}");
}
fn main() {
let stdin = io::stdin();
let stdout = io::stdout();
if !stdin.is_terminal() {
println!("need to run in a tty");
return;
}
let fd = stdin.as_raw_fd();
let _scoped_raw = ScopedRawMode::on_fd(fd); // needs to be a var, gets dropped on scope exit,
// even if something panics
let mut stdin = stdin.lock();
let mut stdout = stdout.lock();
let mut buffer = [0u8; 1];
let mut line = LineBuffer::new();
loop {
let Ok(_) = stdin.read_exact(&mut buffer) else {
break;
};
match buffer[0] {
// EOF
4 => {
break;
}
// Enter
b'\r' => {
print!("\r\n");
stdout.write_all(&line.dump()).unwrap();
print!("\r\n");
}
// Backspace (127 on most systems)
127 => {
line.del_left();
print!("\x08 \x08");
}
// Escape sequence
27 => {
let mut seq = [0u8; 2];
stdin.read_exact(&mut seq).unwrap();
if seq[0] == b'[' {
match seq[1] {
b'A' => {
// up
},
b'B' => {
// down
},
b'C' => {
move_cursor(Direction::Right, 1);
line.right();
}
b'D' => {
move_cursor(Direction::Left, 1);
line.left();
}
_ => {}
}
}
}
// Normal character
x => {
line.add(x);
stdout.write_all(&[x]).unwrap();
line.display_post();
}
}
}
}
|