hecto/src/terminal.rs

72 lines
1.6 KiB
Rust
Raw Normal View History

2021-03-08 09:50:15 -05:00
use crate::Position;
2021-03-05 16:36:39 -05:00
use std::io::{self, stdout, Write};
use termion::event::Key;
use termion::input::TermRead;
use termion::raw::{IntoRawMode, RawTerminal};
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,
},
_stdout: stdout().into_raw_mode()?,
})
}
pub fn size(&self) -> &Size {
&self.size
}
pub fn clear_screen() {
print!("{}", termion::clear::All);
}
2021-03-08 09:50:15 -05:00
#[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;
2021-03-05 16:36:39 -05:00
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);
}
}