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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
#![forbid(unsafe_code)]
use smol::prelude::*;
use smol::{ unblock, Unblock, Timer };
use smol::lock::{ OnceCell, RwLock };
use std::fmt::Display;
use std::os::fd::AsRawFd;
use std::process::ExitCode;
use std::time::Duration;
use termios::Termios;
type Error = std::io::Error;
type Result<T> = std::result::Result<T, Error>;
struct Ivy {
stdin: Unblock<std::io::Stdin>,
stdout: Unblock<std::io::Stdout>,
initial_termios: OnceCell<Termios>,
width: RwLock<u64>,
height: RwLock<u64>,
}
enum EscapeType {
CSI,
CSIPrivate,
}
impl EscapeType {
fn intro(&self) -> &'static str {
match self {
EscapeType::CSI => "\x1b[",
EscapeType::CSIPrivate => "\x1b[?",
}
}
}
fn main() -> ExitCode {
smol::block_on(async {
if let Err(e) = Ivy::new().await.run().await {
println!("{:?}", e);
ExitCode::FAILURE
} else {
ExitCode::SUCCESS
}
})
}
impl Ivy {
async fn new() -> Self {
Ivy {
stdin: Unblock::new(std::io::stdin()),
stdout: Unblock::new(std::io::stdout()),
initial_termios: OnceCell::new(),
width: RwLock::new(80),
height: RwLock::new(24),
}
}
async fn run(mut self) -> Result<()> {
let mut error = None;
error = error.or(self.init_termios().await.err());
error = error.or(self.init_full_screen().await.err());
error = error.or({
for _ in 1 .. *self.height.read().await {
self.stdout.write_all("\n~".as_bytes()).await?;
}
self.do_cursor_position(0, 0).await?;
self.stdout.flush().await?;
Ok(())
}.err());
Timer::after(Duration::from_millis(1000)).await;
error = error.or(self.zap_full_screen().await.err());
error = error.or(self.zap_termios().await.err());
if let Some(error) = error {
Err(error)
} else {
Ok(())
}
}
async fn init_termios(&mut self) -> Result<()> {
let _ = self.initial_termios.set(unblock(|| {
let fd = std::io::stdin().as_raw_fd();
Termios::from_fd(fd)
}).await?).await;
let mut termios = self.initial_termios.get().unwrap().clone();
termios.c_lflag &= !(termios::ECHO
| termios::ECHONL
| termios::os::linux::ECHOCTL
| termios::ICANON);
termios.c_lflag |= termios::ISIG;
termios.c_cc[termios::VMIN] = 1;
termios.c_cc[termios::VTIME] = 0;
unblock(move || {
let fd = std::io::stdin().as_raw_fd();
termios::tcsetattr(fd, termios::TCSANOW, &mut termios)
}).await?;
Ok(())
}
async fn zap_termios(&mut self) -> Result<()> {
let mut termios = self.initial_termios.get().unwrap().clone();
unblock(move || {
let fd = std::io::stdin().as_raw_fd();
termios::tcsetattr(fd, termios::TCSANOW, &mut termios)
}).await?;
Ok(())
}
async fn init_full_screen(&mut self) -> Result<()> {
self.do_start_alternate_screen().await?;
self.do_cursor_position(0, 0).await?;
let (width, height) = self.do_report_size().await?;
*self.width.write().await = width;
*self.height.write().await = height;
Ok(())
}
async fn zap_full_screen(&mut self) -> Result<()> {
self.do_end_alternate_screen().await?;
self.stdout.flush().await?;
Ok(())
}
async fn do_escape(&mut self, escape_type: EscapeType, code: &str,
parameters: &[impl Display])
-> Result<()>
{
self.stdout.write_all(escape_type.intro().as_bytes()).await?;
for (index, parameter) in parameters.iter().enumerate() {
self.stdout.write_all(if index < parameters.len() - 1 {
format!("{};", parameter)
} else {
format!("{}{}", parameter, code)
}.as_bytes()).await?;
}
Ok(())
}
async fn read_report(&mut self, escape_type: EscapeType)
-> Result<(char, Vec<u64>)>
{
let mut expected = escape_type.intro();
while expected.len() > 0 {
let mut buf = vec![0; 1];
self.stdin.read_exact(&mut buf).await?;
expected = &expected[1..];
}
let mut values = Vec::new();
let mut number: u64 = 0;
let mut in_number = false;
let mut after_semicolon = false;
let mut buf = vec![0; 1];
self.stdin.read_exact(&mut buf).await?;
loop {
if let Ok(string) = std::str::from_utf8(&buf)
&& let Some(c) = string.chars().next()
{
if c.is_ascii_digit() {
if in_number {
number = number * 10
+ <u32 as Into<u64>>::into(c.to_digit(10).unwrap());
} else {
number = c.to_digit(10).unwrap().into();
in_number = true;
after_semicolon = false;
}
} else if c == ';' {
if in_number {
values.push(number);
in_number = false;
after_semicolon = true;
} else {
break;
}
} else if c.is_ascii_alphabetic() {
if in_number {
values.push(number);
} else if after_semicolon {
break;
}
return Ok((c, values))
} else {
break;
}
} else {
break;
}
self.stdin.read_exact(&mut buf).await?;
}
Err(std::io::Error::other("Invalid report from terminal."))
}
// xterm
async fn do_start_alternate_screen(&mut self) -> Result<()> {
self.do_escape(EscapeType::CSIPrivate, "h", &[1049]).await
}
// xterm
async fn do_end_alternate_screen(&mut self) -> Result<()> {
self.do_escape(EscapeType::CSIPrivate, "l", &[1049]).await
}
// vt220? vt100?
async fn do_cursor_position(&mut self, x: u64, y: u64) -> Result<()> {
self.do_escape(EscapeType::CSI, "H", &[y + 1, x + 1]).await
}
// dtterm? xterm
async fn do_report_size(&mut self) -> Result<(u64, u64)> {
self.do_escape(EscapeType::CSI, "t", &[18]).await?;
self.stdout.flush().await?;
let (code, values) = self.read_report(EscapeType::CSI).await?;
if code == 't' && values.len() == 3 && values[0] == 8 {
Ok((values[2], values[1]))
} else {
Err(std::io::Error::other("Couldn't read terminal size."))
}
}
}
|