minicrossterm/src/terminal/ansi_terminal.rs

73 lines
2.1 KiB
Rust
Raw Normal View History

//! This is an `ANSI escape code` specific implementation for terminal related action.
//! This module is used for windows 10 terminals and unix terminals by default.
2018-07-10 03:37:07 +10:00
use super::super::cursor::cursor;
2018-07-22 22:55:14 +10:00
use super::{ClearType, ITerminal, Rc};
2018-07-02 06:43:43 +10:00
use shared::functions;
use Context;
/// This struct is an ansi implementation for terminal related actions.
2018-07-02 06:43:43 +10:00
pub struct AnsiTerminal {
context: Rc<Context>,
}
impl AnsiTerminal {
pub fn new(context: Rc<Context>) -> Box<AnsiTerminal> {
2018-07-02 06:43:43 +10:00
Box::from(AnsiTerminal { context: context })
}
}
impl ITerminal for AnsiTerminal {
2018-07-02 06:43:43 +10:00
fn clear(&self, clear_type: ClearType) {
let mut screen_manager = self.context.screen_manager.lock().unwrap();
{
match clear_type {
ClearType::All => {
screen_manager.write_str(csi!("2J"));
2018-07-02 06:43:43 +10:00
}
ClearType::FromCursorDown => {
screen_manager.write_str(csi!("J"));
2018-07-02 06:43:43 +10:00
}
ClearType::FromCursorUp => {
screen_manager.write_str(csi!("1J"));
2018-07-02 06:43:43 +10:00
}
ClearType::CurrentLine => {
screen_manager.write_str(csi!("2K"));
2018-07-02 06:43:43 +10:00
}
ClearType::UntilNewLine => {
screen_manager.write_str(csi!("K"));
2018-07-02 06:43:43 +10:00
}
};
}
}
fn terminal_size(&self) -> (u16, u16) {
functions::get_terminal_size(&self.context.screen_manager)
}
fn scroll_up(&self, count: i16) {
let mut screen = self.context.screen_manager.lock().unwrap();
{
screen.write_string(format!(csi!("{}S"), count));
}
}
fn scroll_down(&self, count: i16) {
let mut screen = self.context.screen_manager.lock().unwrap();
{
screen.write_string(format!(csi!("{}T"), count));
}
}
fn set_size(&self, width: i16, height: i16) {
let mut screen = self.context.screen_manager.lock().unwrap();
{
screen.write_string(format!(csi!("8;{};{}t"), width, height));
}
}
2018-07-02 06:43:43 +10:00
fn exit(&self) {
2018-06-27 04:21:47 +10:00
functions::exit_terminal();
}
}