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; extern crate crossterm;
mod terminal;
use terminal::alternate_screen;
use crossterm::Context;
use std::io::{Write, stdout};
use crossterm::cursor; use crossterm::cursor;
fn main() { 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}; //extern crate crossterm;
use std::{time, thread}; //
//use crossterm::terminal::screen::{AlternateScreen, ToAlternateScreen, ToMainScreen};
fn print_wait_screen(screen: &mut Write) //use crossterm::cursor::cursor;
{ //use crossterm::terminal::{self, ClearType};
terminal::terminal().clear(ClearType::All); //
write!(screen, //use std::io::{Write, stdout};
"Welcome to the wait screen.\n\ //use std::{time, thread};
Please wait a few seconds until we arrive back at the main screen.\n\ //
Seconds to Go: " //fn print_wait_screen(screen: &mut Write)
); //{
// terminal::terminal().clear(ClearType::All);
let mut counter = 5; // write!(screen,
// get cursor instance // "Welcome to the wait screen.\n\
let mut cursor = cursor(); // Please wait a few seconds until we arrive back at the main screen.\n\
// Seconds to Go: "
// loop until the counter hits 0 // );
loop //
{ // let mut counter = 5;
// 1 second delay // // get cursor instance
thread::sleep(time::Duration::from_secs(1)); // let mut cursor = cursor();
// decrement counter //
counter -= 1; // // loop until the counter hits 0
// loop
// print the current counter at the line of `Seconds to Go: {counter}` // {
cursor.goto(15,2).print(counter); // // 1 second delay
// thread::sleep(time::Duration::from_secs(1));
if counter <= 0 // // decrement counter
{ // counter -= 1;
break; //
} // // print the current counter at the line of `Seconds to Go: {counter}`
} // cursor.goto(15,2).print(counter);
} //
// if counter <= 0
pub fn with_alternate_screen_instance() // {
{ // break;
// 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()); //pub fn with_alternate_screen_instance()
//{
// Print the wait screen. // // create scope. If this scope ends the screen will be switched back to mainscreen.
print_wait_screen(&mut screen); // // becouse `AlternateScreen` switches back to main screen when switching back.
} // {
// // create new alternate screen instance and switch to the alternate screen.
println!("Whe are back at the main screen"); // let mut screen = AlternateScreen::from(stdout());
} //
// // Print the wait screen.
pub fn manually_switch_to_alternate_screen() // print_wait_screen(&mut screen);
{ // }
// You can switch to alternate screen manually but if you forget to switch back your terminal may cause some undefined behavior. //
// println!("Whe are back at the main screen");
let mut screen = stdout(); //}
//
// switch to alternate screeen //pub fn manually_switch_to_alternate_screen()
write!(screen, "{}", ToAlternateScreen); //{
// load wait screen // // You can switch to alternate screen manually but if you forget to switch back your terminal may cause some undefined behavior.
print_wait_screen(&mut screen); //
// switch back // let mut screen = stdout();
write!(screen,"{}", ToMainScreen); //
println!("Whe are back at the main screen"); // // 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; mod alternate_screen;
pub mod raw_mode; mod raw_mode;
pub mod terminal; 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 impl ICommand for ToAlternateScreenBufferCommand
{ {
fn new() -> Box<ToAlternateScreenBufferCommand> { fn new() -> Box<ToAlternateScreenBufferCommand> {
// println!("create new unix alternate screen");
Box::from(ToAlternateScreenBufferCommand { }) Box::from(ToAlternateScreenBufferCommand { })
} }
fn execute(&mut self) -> bool fn execute(&mut self) -> bool
{ {
// println!("execute alternate screen");
let mut some_writer = io::stdout(); let mut some_writer = io::stdout();
match write!(some_writer, csi!("?1049h")) match write!(some_writer, csi!("?1049h"))
{ {
@ -28,7 +26,6 @@ impl ICommand for ToAlternateScreenBufferCommand
fn undo(&mut self) -> bool fn undo(&mut self) -> bool
{ {
// println!("undo alternate screen");
let mut some_writer = io::stdout(); let mut some_writer = io::stdout();
match write!(some_writer, csi!("?1049l")) match write!(some_writer, csi!("?1049l"))
{ {

View File

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

View File

@ -20,7 +20,6 @@ pub struct EnableAnsiCommand
impl ICommand for EnableAnsiCommand impl ICommand for EnableAnsiCommand
{ {
fn new() -> Box<EnableAnsiCommand> { fn new() -> Box<EnableAnsiCommand> {
// println!("new EnableRawModeCommand winapi");
let key = super::generate_key(); let key = super::generate_key();
let command = EnableAnsiCommand { mask: ENABLE_VIRTUAL_TERMINAL_PROCESSING }; let command = EnableAnsiCommand { mask: ENABLE_VIRTUAL_TERMINAL_PROCESSING };
Box::from(command) Box::from(command)
@ -28,7 +27,6 @@ impl ICommand for EnableAnsiCommand
fn execute(&mut self) -> bool 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. // 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() 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 fn undo(&mut self) -> bool
{ {
// println!("undo EnableAnsiCommand winapi");
if ansi_support::ansi_enabled() if ansi_support::ansi_enabled()
{ {
let output_handle = kernel::get_output_handle(); let output_handle = kernel::get_output_handle();
@ -94,7 +91,6 @@ impl IContextCommand for EnableRawModeCommand
fn new(context: &mut Context) -> (Box<EnableRawModeCommand>, i16) { 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}; 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 key = super::generate_key();
let command = EnableRawModeCommand { mask: ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT | ENABLE_ECHO_INPUT, key: key }; let command = EnableRawModeCommand { mask: ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT | ENABLE_ECHO_INPUT, key: key };
context.register_change(Box::from(command), 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}; use self::wincon::{ENABLE_LINE_INPUT,ENABLE_PROCESSED_INPUT, ENABLE_ECHO_INPUT};
// println!("execute EnableRawModeCommand winapi");
let input_handle = kernel::get_input_handle(); let input_handle = kernel::get_input_handle();
let mut dw_mode: DWORD = 0; let mut dw_mode: DWORD = 0;
@ -126,7 +121,6 @@ impl IContextCommand for EnableRawModeCommand
fn undo(&mut self) -> bool fn undo(&mut self) -> bool
{ {
// println!("undo EnableRawModeCommand winapi");
let output_handle = kernel::get_output_handle(); let output_handle = kernel::get_output_handle();
let mut dw_mode: DWORD = 0; let mut dw_mode: DWORD = 0;
@ -160,7 +154,6 @@ impl ICommand for ToAlternateScreenBufferCommand
fn execute(&mut self) -> bool fn execute(&mut self) -> bool
{ {
// println!("executte ToAlternateScreenBufferCommand winapi");
let mut chi_buffer: [CHAR_INFO;160] = unsafe {mem::zeroed() }; let mut chi_buffer: [CHAR_INFO;160] = unsafe {mem::zeroed() };
let handle = kernel::get_output_handle(); let handle = kernel::get_output_handle();
@ -215,7 +208,6 @@ impl ICommand for ToAlternateScreenBufferCommand
fn undo(&mut self) -> bool fn undo(&mut self) -> bool
{ {
// println!("undo ToAlternateScreenBufferCommand winapi");
let handle = kernel::get_output_handle(); let handle = kernel::get_output_handle();
kernel::set_active_screen_buffer(handle); kernel::set_active_screen_buffer(handle);
true true

View File

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