Changed module names and some method names to make the calls more cleaner. Fixed get cursor position for unix. Default ansi codes if not supported winapi will be used.

This commit is contained in:
TimonPost 2018-05-20 14:08:14 +02:00
parent 4c14ad836b
commit df86426c93
9 changed files with 373 additions and 593 deletions

File diff suppressed because it is too large Load Diff

View File

@ -13,16 +13,7 @@
extern crate crossterm;
mod terminal;
use terminal::alternate_screen;
use crossterm::Context;
use std::io::{Write, stdout};
use crossterm::cursor;
fn main() {
alternate_screen::manually_switch_to_alternate_screen();
// cursor::cursor().goto(10,10).print("@");
}

View File

@ -1,70 +1,73 @@
extern crate crossterm;
// alternate screen is not working correctly currently
use crossterm::terminal::screen::{AlternateScreen, ToAlternateScreen, ToMainScreen};
use crossterm::cursor::cursor;
use crossterm::terminal::{self, ClearType};
use std::io::{Write, stdout};
use std::{time, thread};
fn print_wait_screen(screen: &mut Write)
{
terminal::terminal().clear(ClearType::All);
write!(screen,
"Welcome to the wait screen.\n\
Please wait a few seconds until we arrive back at the main screen.\n\
Seconds to Go: "
);
let mut counter = 5;
// get cursor instance
let mut cursor = cursor();
// loop until the counter hits 0
loop
{
// 1 second delay
thread::sleep(time::Duration::from_secs(1));
// decrement counter
counter -= 1;
// print the current counter at the line of `Seconds to Go: {counter}`
cursor.goto(15,2).print(counter);
if counter <= 0
{
break;
}
}
}
pub fn with_alternate_screen_instance()
{
// create scope. If this scope ends the screen will be switched back to mainscreen.
// becouse `AlternateScreen` switches back to main screen when switching back.
{
// create new alternate screen instance and switch to the alternate screen.
let mut screen = AlternateScreen::from(stdout());
// Print the wait screen.
print_wait_screen(&mut screen);
}
println!("Whe are back at the main screen");
}
pub fn manually_switch_to_alternate_screen()
{
// You can switch to alternate screen manually but if you forget to switch back your terminal may cause some undefined behavior.
let mut screen = stdout();
// switch to alternate screeen
write!(screen, "{}", ToAlternateScreen);
// load wait screen
print_wait_screen(&mut screen);
// switch back
write!(screen,"{}", ToMainScreen);
println!("Whe are back at the main screen");
}
//extern crate crossterm;
//
//use crossterm::terminal::screen::{AlternateScreen, ToAlternateScreen, ToMainScreen};
//use crossterm::cursor::cursor;
//use crossterm::terminal::{self, ClearType};
//
//use std::io::{Write, stdout};
//use std::{time, thread};
//
//fn print_wait_screen(screen: &mut Write)
//{
// terminal::terminal().clear(ClearType::All);
// write!(screen,
// "Welcome to the wait screen.\n\
// Please wait a few seconds until we arrive back at the main screen.\n\
// Seconds to Go: "
// );
//
// let mut counter = 5;
// // get cursor instance
// let mut cursor = cursor();
//
// // loop until the counter hits 0
// loop
// {
// // 1 second delay
// thread::sleep(time::Duration::from_secs(1));
// // decrement counter
// counter -= 1;
//
// // print the current counter at the line of `Seconds to Go: {counter}`
// cursor.goto(15,2).print(counter);
//
// if counter <= 0
// {
// break;
// }
// }
//}
//
//pub fn with_alternate_screen_instance()
//{
// // create scope. If this scope ends the screen will be switched back to mainscreen.
// // becouse `AlternateScreen` switches back to main screen when switching back.
// {
// // create new alternate screen instance and switch to the alternate screen.
// let mut screen = AlternateScreen::from(stdout());
//
// // Print the wait screen.
// print_wait_screen(&mut screen);
// }
//
// println!("Whe are back at the main screen");
//}
//
//pub fn manually_switch_to_alternate_screen()
//{
// // You can switch to alternate screen manually but if you forget to switch back your terminal may cause some undefined behavior.
//
// let mut screen = stdout();
//
// // switch to alternate screeen
// write!(screen, "{}", ToAlternateScreen);
// // load wait screen
// print_wait_screen(&mut screen);
// // switch back
// write!(screen,"{}", ToMainScreen);
// println!("Whe are back at the main screen");
//
//}

View File

@ -1,3 +1,5 @@
pub mod alternate_screen;
pub mod raw_mode;
mod alternate_screen;
mod raw_mode;
pub mod terminal;

View File

@ -0,0 +1 @@
// raw screen is not working correctly currently

View File

@ -11,13 +11,11 @@ pub struct ToAlternateScreenBufferCommand;
impl ICommand for ToAlternateScreenBufferCommand
{
fn new() -> Box<ToAlternateScreenBufferCommand> {
// println!("create new unix alternate screen");
Box::from(ToAlternateScreenBufferCommand { })
}
fn execute(&mut self) -> bool
{
// println!("execute alternate screen");
let mut some_writer = io::stdout();
match write!(some_writer, csi!("?1049h"))
{
@ -28,7 +26,6 @@ impl ICommand for ToAlternateScreenBufferCommand
fn undo(&mut self) -> bool
{
// println!("undo alternate screen");
let mut some_writer = io::stdout();
match write!(some_writer, csi!("?1049l"))
{

View File

@ -17,7 +17,6 @@ pub struct NoncanonicalModeCommand
impl IContextCommand for NoncanonicalModeCommand
{
fn new(context: &mut Context) -> (Box<NoncanonicalModeCommand>, i16) {
// println!("new new NoncanonicalModeCommand unix");
let key = super::generate_key();
let command = NoncanonicalModeCommand { key: key };
context.register_change(Box::from(command), key);
@ -26,7 +25,6 @@ impl IContextCommand for NoncanonicalModeCommand
fn execute(&mut self) -> bool
{
// println!("execute NoncanonicalModeCommand uxix");
// Set noncanonical mode
if let Ok(orig) = Termios::from_fd(FD_STDIN)
{
@ -46,8 +44,6 @@ impl IContextCommand for NoncanonicalModeCommand
fn undo(&mut self) -> bool
{
// println!("undo NoncanonicalModeCommand unix");
// Disable noncanonical mode
if let Ok(orig) = Termios::from_fd(FD_STDIN)
{
@ -78,7 +74,6 @@ pub struct EnableRawModeCommand
impl IContextCommand for EnableRawModeCommand
{
fn new(context: &mut Context) -> (Box<EnableRawModeCommand>, i16) {
// println!("new EnableRawModeCommand unix");
let key = super::generate_key();
let command = EnableRawModeCommand { original_mode: None, key: key };
context.register_change(Box::from(command), key);
@ -87,7 +82,6 @@ impl IContextCommand for EnableRawModeCommand
fn execute(&mut self) -> bool
{
// println!("execute EnableRawModeCommand unix");
if let Ok(original_mode) = terminal::get_terminal_mode()
{
self.original_mode = Some(original_mode);
@ -103,7 +97,6 @@ impl IContextCommand for EnableRawModeCommand
fn undo(&mut self) -> bool
{
// println!("undo EnableRawModeCommand unix");
if let Ok(original_mode) = terminal::get_terminal_mode()
{
let result = terminal::set_terminal_mode(&self.original_mode.unwrap());

View File

@ -20,7 +20,6 @@ pub struct EnableAnsiCommand
impl ICommand for EnableAnsiCommand
{
fn new() -> Box<EnableAnsiCommand> {
// println!("new EnableRawModeCommand winapi");
let key = super::generate_key();
let command = EnableAnsiCommand { mask: ENABLE_VIRTUAL_TERMINAL_PROCESSING };
Box::from(command)
@ -28,7 +27,6 @@ impl ICommand for EnableAnsiCommand
fn execute(&mut self) -> bool
{
// println!("exucute EnableAnsiCommand winapi");
// we need to check whether we tried to enable ansi before. If we have we can just return if that had succeeded.
if ansi_support::has_been_tried_to_enable_ansi() && ansi_support::ansi_enabled()
{
@ -57,7 +55,6 @@ impl ICommand for EnableAnsiCommand
fn undo(&mut self) -> bool
{
// println!("undo EnableAnsiCommand winapi");
if ansi_support::ansi_enabled()
{
let output_handle = kernel::get_output_handle();
@ -94,7 +91,6 @@ impl IContextCommand for EnableRawModeCommand
fn new(context: &mut Context) -> (Box<EnableRawModeCommand>, i16) {
use self::wincon::{ENABLE_LINE_INPUT,ENABLE_PROCESSED_INPUT, ENABLE_PROCESSED_OUTPUT, ENABLE_WRAP_AT_EOL_OUTPUT, ENABLE_ECHO_INPUT};
// println!("new EnableRawModeCommand winapi");
let key = super::generate_key();
let command = EnableRawModeCommand { mask: ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT | ENABLE_ECHO_INPUT, key: key };
context.register_change(Box::from(command), key);
@ -105,7 +101,6 @@ impl IContextCommand for EnableRawModeCommand
{
use self::wincon::{ENABLE_LINE_INPUT,ENABLE_PROCESSED_INPUT, ENABLE_ECHO_INPUT};
// println!("execute EnableRawModeCommand winapi");
let input_handle = kernel::get_input_handle();
let mut dw_mode: DWORD = 0;
@ -126,7 +121,6 @@ impl IContextCommand for EnableRawModeCommand
fn undo(&mut self) -> bool
{
// println!("undo EnableRawModeCommand winapi");
let output_handle = kernel::get_output_handle();
let mut dw_mode: DWORD = 0;
@ -160,7 +154,6 @@ impl ICommand for ToAlternateScreenBufferCommand
fn execute(&mut self) -> bool
{
// println!("executte ToAlternateScreenBufferCommand winapi");
let mut chi_buffer: [CHAR_INFO;160] = unsafe {mem::zeroed() };
let handle = kernel::get_output_handle();
@ -215,7 +208,6 @@ impl ICommand for ToAlternateScreenBufferCommand
fn undo(&mut self) -> bool
{
// println!("undo ToAlternateScreenBufferCommand winapi");
let handle = kernel::get_output_handle();
kernel::set_active_screen_buffer(handle);
true

View File

@ -11,8 +11,8 @@ mod terminal;
mod winapi_terminal;
mod ansi_terminal;
pub mod screen;
pub mod raw;
mod screen;
mod raw;
#[cfg(target_os = "windows")]
use self::winapi_terminal::WinApiTerminal;