From 2cc32394b5211bfa4cecb6943275d35b1c054509 Mon Sep 17 00:00:00 2001 From: Timon Post Date: Sat, 3 Mar 2018 17:07:51 +0100 Subject: [PATCH] Some errors solved for unix --- src/crossterm_cursor/cursor.rs | 2 ++ src/crossterm_cursor/mod.rs | 4 +++ src/crossterm_style/color/color.rs | 2 ++ src/crossterm_style/color/mod.rs | 4 +++ src/crossterm_terminal/mod.rs | 4 +++ src/crossterm_terminal/screen/mod.rs | 45 ++++++++++------------------ src/crossterm_terminal/terminal.rs | 2 ++ src/kernel/unix_kernel/terminal.rs | 10 ++++--- 8 files changed, 39 insertions(+), 34 deletions(-) diff --git a/src/crossterm_cursor/cursor.rs b/src/crossterm_cursor/cursor.rs index df1cb04..598b222 100644 --- a/src/crossterm_cursor/cursor.rs +++ b/src/crossterm_cursor/cursor.rs @@ -7,7 +7,9 @@ use std::ops::Drop; use {Construct, Context}; use shared::functions::get_module; use super::base_cursor::ITerminalCursor; + use super::AnsiCursor; +#[cfg(target_os = "windows")] use super::WinApiCursor; /// Struct that stores an specific platform implementation for cursor related actions. diff --git a/src/crossterm_cursor/mod.rs b/src/crossterm_cursor/mod.rs index aee4a3b..608ba70 100644 --- a/src/crossterm_cursor/mod.rs +++ b/src/crossterm_cursor/mod.rs @@ -2,9 +2,13 @@ mod base_cursor; mod cursor; mod ansi_cursor; + +#[cfg(target_os = "windows")] mod winapi_cursor; use self::ansi_cursor::AnsiCursor; + +#[cfg(target_os = "windows")] use self::winapi_cursor::WinApiCursor; pub use self::cursor::{ cursor, TerminalCursor }; diff --git a/src/crossterm_style/color/color.rs b/src/crossterm_style/color/color.rs index c1ce7be..b6dc873 100644 --- a/src/crossterm_style/color/color.rs +++ b/src/crossterm_style/color/color.rs @@ -12,6 +12,8 @@ use shared::functions::get_module; use super::super::Color; use super::AnsiColor; + +#[cfg(target_os = "windows")] use super::WinApiColor; /// Struct that stores an specific platform implementation for color related actions. diff --git a/src/crossterm_style/color/mod.rs b/src/crossterm_style/color/mod.rs index 65acbc3..6829156 100644 --- a/src/crossterm_style/color/mod.rs +++ b/src/crossterm_style/color/mod.rs @@ -2,7 +2,11 @@ pub mod base_color; pub mod color; mod ansi_color; + +#[cfg(target_os = "windows")] mod winapi_color; use self::ansi_color::AnsiColor; + +#[cfg(target_os = "windows")] use self::winapi_color::WinApiColor; diff --git a/src/crossterm_terminal/mod.rs b/src/crossterm_terminal/mod.rs index ac942a8..a68461d 100644 --- a/src/crossterm_terminal/mod.rs +++ b/src/crossterm_terminal/mod.rs @@ -5,9 +5,13 @@ mod terminal; pub mod screen; mod ansi_terminal; + +#[cfg(target_os = "windows")] mod winapi_terminal; use self::ansi_terminal::AnsiTerminal; + +#[cfg(target_os = "windows")] use self::winapi_terminal::WinApiTerminal; pub use self::base_terminal::ClearType; diff --git a/src/crossterm_terminal/screen/mod.rs b/src/crossterm_terminal/screen/mod.rs index ff45c57..3bd3304 100644 --- a/src/crossterm_terminal/screen/mod.rs +++ b/src/crossterm_terminal/screen/mod.rs @@ -2,9 +2,10 @@ use std::io::{self, Write}; use std::ops; use std::any::Any; -use crossterm_state::commands::{shared_commands,win_commands,ICommand, CommandType}; -use crossterm_state::commands::IContextCommand; +use shared::functions::get_module; +use crossterm_state::commands::*; use shared::traits::Construct; +use Context; use std::fmt; @@ -45,12 +46,13 @@ impl fmt::Display for ToAlternateScreen pub struct AlternateScreen { /// The output target. output: W, + context: Context } impl AlternateScreen< W> { pub fn from(mut output: W) -> Self { write!(output, "{}", ToAlternateScreen); - AlternateScreen { output: output } + AlternateScreen { output: output, context: Context::new()} } } @@ -82,37 +84,20 @@ impl Drop for AlternateScreen { fn drop(&mut self) { + this write!(self, "{}", ToMainScreen).expect("switch to main screen"); } } fn get_to_alternate_screen_command() -> Box { -// let mut does_support = true; - let mut command: Option> = None; -// -// let succeeded = false; -// -// if cfg!(target_os = "windows") -// { -// #[cfg(windows)] -// use kernel::windows_kernel::ansi_support::try_enable_ansi_support; -// -// // Try to enable ansi on windows if not than use WINAPI. -// does_support = try_enable_ansi_support(); -// -// println!("does support: {}", does_support); -// if !does_support -// { - command = Some(win_commands::ToAlternateScreenBufferCommand::new()); - command.unwrap() -// } -// } -// -// if does_support -// { -// command = Some(shared_commands::ToAlternateScreenBufferCommand::new().0); -// } -// -// command.unwrap() + let mut context = Context::new(); + + #[cfg(target_os = "windows")] + let command = get_module::>(win_commands::ToAlternateScreenBufferCommand::new(), shared_commands::ToAlternateScreenBufferCommand::new(), &mut context).unwrap(); + + #[cfg(not(target_os = "windows"))] + let command = shared_commands::ToAlternateScreenBufferCommand::new(); + + command } \ No newline at end of file diff --git a/src/crossterm_terminal/terminal.rs b/src/crossterm_terminal/terminal.rs index e3d4227..a340952 100644 --- a/src/crossterm_terminal/terminal.rs +++ b/src/crossterm_terminal/terminal.rs @@ -8,6 +8,8 @@ use super::base_terminal::{ClearType, ITerminal}; use shared::functions::get_module; use super::AnsiTerminal; + +#[cfg(target_os = "windows")] use super::WinApiTerminal; /// Struct that stores an specific platform implementation for terminal related actions. diff --git a/src/kernel/unix_kernel/terminal.rs b/src/kernel/unix_kernel/terminal.rs index 6c7824b..30ee1d7 100644 --- a/src/kernel/unix_kernel/terminal.rs +++ b/src/kernel/unix_kernel/terminal.rs @@ -1,7 +1,9 @@ use libc; use self::libc::{STDOUT_FILENO, TIOCGWINSZ, c_ushort, ioctl}; pub use self::libc::termios as Termios; +use crossterm_state::commands::{NoncanonicaModeCommand, IContextCommand}; use std::io; +use std::mem; /// A representation of the size of the current terminal @@ -37,14 +39,14 @@ pub fn terminal_size() -> (u16,u16) { /// Get the current cursor position pub fn pos() -> (u16,u16) { - use std::io::{Write,Read}; use std::io::Error; + use std::io::{ Write,Read }; let command = NoncanonicalModeCommand::new(); command.execute(); // This code is original written by term_cursor credits to them. - let mut stdout = std::io::stdout(); + let mut stdout = io::stdout(); // Write command stdout.write(b"\x1B[6n")?; @@ -53,7 +55,7 @@ pub fn pos() -> (u16,u16) // Read back result let mut buf = [0u8; 2]; // Expect `ESC[` - std::io::stdin().read_exact(&mut buf)?; + io::stdin().read_exact(&mut buf)?; if buf[0] != 0x1B || buf[1] as char != '[' { return (0,0); } @@ -65,7 +67,7 @@ pub fn pos() -> (u16,u16) loop { let mut buf = [0u8; 1]; - std::io::stdin().read_exact(&mut buf)?; + io::stdin().read_exact(&mut buf)?; c = buf[0] as char; if let Some(d) = c.to_digit(10) { num = if num == 0 { 0 } else { num * 10 };