-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathterminal.rs
More file actions
85 lines (72 loc) · 1.94 KB
/
Copy pathterminal.rs
File metadata and controls
85 lines (72 loc) · 1.94 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
use crate::Position;
use std::io::{ self, stdout, Write};
use termion::event::Key;
use termion::input::TermRead;
use termion::raw::{ IntoRawMode, RawTerminal};
use termion::color;
pub struct Size {
pub width: u16,
pub height: u16,
}
pub struct Terminal {
size : Size,
_stdout: RawTerminal<std::io::Stdout>,
}
impl Terminal {
pub fn default() -> Result<Self, std::io::Error>{
let size = termion::terminal_size()?;
Ok(Self {
size: Size {
width: size.0,
height: size.1.saturating_sub(2),
},
_stdout: stdout().into_raw_mode()?,
})
}
pub fn size(&self) -> &Size {
&self.size
}
pub fn clear_screen(){
print!("{}", termion::clear::All);
}
#[allow(clippy::cast_possible_truncation)]
pub fn cursor_position( position: &Position){
let Position{mut x, mut y} = position;
x = x.saturating_add(1);
y = y.saturating_add(1);
let x = x as u16;
let y = y as u16;
print!("{}", termion::cursor::Goto(x,y));
}
pub fn flush() -> Result<(), std::io::Error>{
io::stdout().flush()
}
pub fn read_key() -> Result<Key, std::io::Error>{
loop{
if let Some(key) = io::stdin().lock().keys().next(){
return key;
}
}
}
pub fn cursor_hide(){
print!("{}", termion::cursor::Hide);
}
pub fn cursor_show(){
print!("{}", termion::cursor::Show);
}
pub fn clear_current_line(){
print!("{}", termion::clear::CurrentLine);
}
pub fn set_bg_color(color: color::Rgb){
print!("{}", color::Bg(color));
}
pub fn reset_bg_color(){
print!("{}", color::Bg(color::Reset));
}
pub fn set_fg_color(color: color::Rgb){
print!("{}", color::Fg(color));
}
pub fn reset_fg_color(){
print!("{}", color::Fg(color::Reset));
}
}