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
use std::io::{self, Write};
use crate::input::{InputEvent, TerminalInput};
use crate::utils::{
sys::unix::{disable_raw_mode, enable_raw_mode, is_raw_mode_enabled},
Result,
};
use crate::{csi, write_cout};
pub(crate) fn get_cursor_position() -> Result<(u16, u16)> {
if is_raw_mode_enabled() {
pos_raw()
} else {
pos()
}
}
pub(crate) fn show_cursor(show_cursor: bool) -> Result<()> {
if show_cursor {
write_cout!(csi!("?25h"))?;
} else {
write_cout!(csi!("?25l"))?;
}
Ok(())
}
fn pos() -> Result<(u16, u16)> {
enable_raw_mode()?;
let pos = pos_raw();
disable_raw_mode()?;
pos
}
fn pos_raw() -> Result<(u16, u16)> {
let mut stdout = io::stdout();
stdout.write_all(b"\x1B[6n")?;
stdout.flush()?;
let mut reader = TerminalInput::new().read_sync();
loop {
if let Some(InputEvent::CursorPosition(x, y)) = reader.next() {
return Ok((x, y));
}
}
}