2018-03-11 03:33:06 +11:00
|
|
|
//! 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-06-17 04:10:51 +10:00
|
|
|
use Context;
|
2018-03-06 08:06:43 +11:00
|
|
|
use shared::functions;
|
2018-06-23 05:04:30 +10:00
|
|
|
use super::{ClearType, ITerminal, Rc};
|
2018-01-18 09:06:45 +11:00
|
|
|
|
|
|
|
/// This struct is an ansi implementation for terminal related actions.
|
2018-06-23 05:04:30 +10:00
|
|
|
pub struct AnsiTerminal
|
|
|
|
{
|
|
|
|
context: Rc<Context>
|
|
|
|
}
|
2018-01-18 09:06:45 +11:00
|
|
|
|
2018-06-17 04:10:51 +10:00
|
|
|
impl AnsiTerminal {
|
2018-06-23 05:04:30 +10:00
|
|
|
pub fn new(context: Rc<Context>) -> Box<AnsiTerminal> {
|
|
|
|
Box::from(AnsiTerminal {context: context})
|
2018-01-18 09:06:45 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-04 03:17:28 +11:00
|
|
|
impl ITerminal for AnsiTerminal {
|
2018-06-23 05:04:30 +10:00
|
|
|
fn clear(&self, clear_type: ClearType)
|
|
|
|
{
|
|
|
|
let mut screen_manager = self.context.screen_manager.lock().unwrap();
|
2018-06-10 05:59:43 +10:00
|
|
|
{
|
|
|
|
match clear_type {
|
|
|
|
ClearType::All => {
|
2018-06-23 05:04:30 +10:00
|
|
|
screen_manager.write_ansi_str(csi!("2J"));
|
2018-06-10 05:59:43 +10:00
|
|
|
},
|
|
|
|
ClearType::FromCursorDown => {
|
2018-06-23 05:04:30 +10:00
|
|
|
screen_manager.write_ansi_str(csi!("J"));
|
2018-06-10 05:59:43 +10:00
|
|
|
},
|
|
|
|
ClearType::FromCursorUp => {
|
2018-06-23 05:04:30 +10:00
|
|
|
screen_manager.write_ansi_str(csi!("1J"));
|
2018-06-10 05:59:43 +10:00
|
|
|
},
|
|
|
|
ClearType::CurrentLine => {
|
2018-06-23 05:04:30 +10:00
|
|
|
screen_manager.write_ansi_str(csi!("2K"));
|
2018-06-10 05:59:43 +10:00
|
|
|
},
|
|
|
|
ClearType::UntilNewLine => {
|
2018-06-23 05:04:30 +10:00
|
|
|
screen_manager.write_ansi_str(csi!("K"));
|
2018-06-10 05:59:43 +10:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
2018-01-18 09:06:45 +11:00
|
|
|
}
|
|
|
|
|
2018-06-23 05:04:30 +10:00
|
|
|
fn terminal_size(&self) -> (u16, u16) {
|
2018-03-06 08:06:43 +11:00
|
|
|
functions::get_terminal_size()
|
2018-01-18 09:06:45 +11:00
|
|
|
}
|
|
|
|
|
2018-06-23 05:04:30 +10:00
|
|
|
fn scroll_up(&self, count: i16) {
|
|
|
|
let mut screen = self.context.screen_manager.lock().unwrap();
|
2018-06-10 05:59:43 +10:00
|
|
|
{
|
|
|
|
screen.write_ansi(format!(csi!("{}S"), count));
|
|
|
|
}
|
2018-01-18 09:06:45 +11:00
|
|
|
}
|
|
|
|
|
2018-06-23 05:04:30 +10:00
|
|
|
fn scroll_down(&self, count: i16) {
|
|
|
|
let mut screen = self.context.screen_manager.lock().unwrap();
|
2018-06-10 05:59:43 +10:00
|
|
|
{
|
|
|
|
screen.write_ansi(format!(csi!("{}T"), count));
|
|
|
|
}
|
2018-01-18 09:06:45 +11:00
|
|
|
}
|
2018-01-26 04:26:08 +11:00
|
|
|
|
2018-06-23 05:04:30 +10:00
|
|
|
fn set_size(&self, width: i16, height: i16) {
|
|
|
|
let mut screen = self.context.screen_manager.lock().unwrap();
|
2018-06-10 05:59:43 +10:00
|
|
|
{
|
|
|
|
screen.write_ansi(format!(csi!("8;{};{}t"), width, height));
|
|
|
|
}
|
2018-01-26 04:26:08 +11:00
|
|
|
}
|
2018-06-24 03:48:22 +10:00
|
|
|
|
|
|
|
fn set_title(&self, title: String)
|
|
|
|
{
|
|
|
|
let mut screen = self.context.screen_manager.lock().unwrap();
|
|
|
|
{
|
|
|
|
screen.write_ansi_str("\x1b]2;New terminal titleBEL");
|
|
|
|
}
|
|
|
|
}
|
2018-06-27 04:21:47 +10:00
|
|
|
|
|
|
|
fn exit(&self)
|
|
|
|
{
|
|
|
|
functions::exit_terminal();
|
|
|
|
}
|
2018-01-18 09:06:45 +11:00
|
|
|
}
|