forked from console-rs/console
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon_term.rs
More file actions
101 lines (89 loc) · 2.84 KB
/
common_term.rs
File metadata and controls
101 lines (89 loc) · 2.84 KB
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
use std::io;
use crate::term::Term;
pub fn move_cursor_down(out: &Term, n: usize) -> io::Result<()> {
if n > 0 {
out.write_str(&format!("\x1b[{}B", n))
} else {
Ok(())
}
}
pub fn move_cursor_up(out: &Term, n: usize) -> io::Result<()> {
if n > 0 {
out.write_str(&format!("\x1b[{}A", n))
} else {
Ok(())
}
}
pub fn move_cursor_left(out: &Term, n: usize) -> io::Result<()> {
if n > 0 {
out.write_str(&format!("\x1b[{}D", n))
} else {
Ok(())
}
}
pub fn move_cursor_right(out: &Term, n: usize) -> io::Result<()> {
if n > 0 {
out.write_str(&format!("\x1b[{}C", n))
} else {
Ok(())
}
}
#[inline]
pub fn move_cursor_to(out: &Term, x: usize, y: usize) -> io::Result<()> {
out.write_str(&format!("\x1B[{};{}H", y + 1, x + 1))
}
/// Return the current cursor's position as a tuple `(n, m)`,
/// where `n` is the row and `m` the column of the cursor (both 1-based).
// FIXME: allow a larger range of characters than u8.
// FIXME: clear the terminal after this operation.
pub fn get_cursor_position(mut out: &Term) -> io::Result<(u8, u8)> {
// Send the code ESC6n to the terminal: asking for the current cursor position.
out.write_str("\x1b[6n")?;
// We expect a response ESC[n;mR, where n and m are the row and column of the cursor.
let mut buf = [0u8; 6];
let num_read = io::Read::read(&mut out, &mut buf)?;
let (n, m) = match buf.as_slice() {
//[a, b, nbyte, c, mbyte, d] if [a, b, c, d] = sdf => (nbyte, mbyte),
// If we didn't read enough bytes, we certainly didn't get the response we wanted.
_ if num_read < buf.len() => return Err(std::io::Error::new(
io::ErrorKind::Other, format!("invalid terminal response: expected six bytes, only read {}", num_read)
)),
[a, b, n, c, m, d] => {
// The bytes a, b, c and d should be byte string \x1 [ ; R.
if &[*a, *b, *c, *d] != b"\x1b[;R" {
return Err(std::io::Error::new(io::ErrorKind::Other, "invalid terminal response: should be of the form ESC[n;mR"));
} else {
(n, m)
}
}
_ => unreachable!(),
};
Ok((*n, *m))
}
pub fn clear_chars(out: &Term, n: usize) -> io::Result<()> {
if n > 0 {
out.write_str(&format!("\x1b[{}D\x1b[0K", n))
} else {
Ok(())
}
}
#[inline]
pub fn clear_line(out: &Term) -> io::Result<()> {
out.write_str("\r\x1b[2K")
}
#[inline]
pub fn clear_screen(out: &Term) -> io::Result<()> {
out.write_str("\r\x1b[2J\r\x1b[H")
}
#[inline]
pub fn clear_to_end_of_screen(out: &Term) -> io::Result<()> {
out.write_str("\r\x1b[0J")
}
#[inline]
pub fn show_cursor(out: &Term) -> io::Result<()> {
out.write_str("\x1b[?25h")
}
#[inline]
pub fn hide_cursor(out: &Term) -> io::Result<()> {
out.write_str("\x1b[?25l")
}