diff --git a/examples/color/mod.rs b/examples/color/mod.rs index 091958c..a50cae7 100644 --- a/examples/color/mod.rs +++ b/examples/color/mod.rs @@ -107,8 +107,8 @@ pub fn print_font_with_attributes() { style("Normal text").paint(&screen); style("Bold text").bold().paint(&screen); style("Italic text").italic().paint(&screen); - style("Slow blinking text").slow().paint(&screen); - style("Rapid blinking text").rapid().paint(&screen); + style("Slow blinking text").slow_blink().paint(&screen); + style("Rapid blinking text").rapid_blink().paint(&screen); style("Hidden text").hidden().paint(&screen); style("Underlined text").underlined().paint(&screen); style("Reversed text").reverse().paint(&screen); diff --git a/examples/examples.rs b/examples/examples.rs index a8f512d..61ebbd7 100644 --- a/examples/examples.rs +++ b/examples/examples.rs @@ -17,4 +17,10 @@ extern crate crossterm; fn main() { // call some test module function + + terminal::terminal::resize_terminal(); + + use crossterm::screen::RawScreen; +// RawScreen::into_raw_mode(); +// RawScreen::disable_raw_modes(); } diff --git a/examples/program_examples/first_depth_search/src/main.rs b/examples/program_examples/first_depth_search/src/main.rs index ed07566..7ef3a27 100644 --- a/examples/program_examples/first_depth_search/src/main.rs +++ b/examples/program_examples/first_depth_search/src/main.rs @@ -34,6 +34,7 @@ pub fn run() print_welcome_screen(&screen); start_algorithm(&screen); + drop(screen); } fn start_algorithm(screen: &Screen) @@ -66,14 +67,14 @@ fn print_welcome_screen(screen: &Screen) // clear the screen and print the welcome message. terminal.clear(ClearType::All); cursor.goto(0, 0); - terminal.write(WELCOME_MESSAGE.join("\n")); + terminal.write(WELCOME_MESSAGE.join("\n\r")); cursor.hide(); cursor.goto(0, 10); - terminal.write( - "The first depth search algorithm will start in: Seconds\n\ - Press `q` to abort the program" - ); + terminal.write("The first depth search algorithm will start in: Seconds"); + + cursor.goto(0, 11); + terminal.write("\nPress `q` to abort the program"); let mut stdin = input.read_async().bytes(); diff --git a/examples/program_examples/logging.rs b/examples/program_examples/logging.rs index 9d728a0..ea87673 100644 --- a/examples/program_examples/logging.rs +++ b/examples/program_examples/logging.rs @@ -94,16 +94,16 @@ fn main() // a thread that will log all logs in the queue. handle_incoming_logs(more_jobs_rx.clone(), queue.clone()); - for handle in thread_handles - { - handle.join(); - } +// for handle in thread_handles +// { +// handle.join(); +// } } fn handle_incoming_logs(more_jobs_rx: SyncFlagRx, queue: WorkQueue) { thread::spawn( move || { - let mut screen: Screen = Screen::new(); + let mut screen: Screen = Screen::default(); // Loop while there's expected to be work, looking for work. while more_jobs_rx.get().unwrap() { @@ -114,7 +114,6 @@ fn handle_incoming_logs(more_jobs_rx: SyncFlagRx, queue: WorkQueue) // write the log screen.stdout.write_string(log); - screen.stdout.flush(); } std::thread::yield_now(); } @@ -136,7 +135,7 @@ fn log_with_different_threads(more_jobs_tx: SyncFlagTx, queue: WorkQueue let thread = thread::spawn(move || { // log 400 messages - for log_entry_count in 1..10000 + for log_entry_count in 1..400 { thread_queue.add_work(format!("Log {} from thread {} ",log_entry_count, thread_num)); more_jobs.set(true); diff --git a/src/common/commands/unix_command.rs b/src/common/commands/unix_command.rs index df4ca69..212399b 100644 --- a/src/common/commands/unix_command.rs +++ b/src/common/commands/unix_command.rs @@ -18,8 +18,8 @@ impl NoncanonicalModeCommand { } } -impl IStateCommand for NoncanonicalModeCommand { - fn execute(&mut self) -> Result<()> { +impl NoncanonicalModeCommand { + pub fn enable(&mut self) -> Result<()> { // Set noncanonical mode if let Ok(orig) = Termios::from_fd(FD_STDIN) { let mut noncan = orig.clone(); @@ -36,7 +36,7 @@ impl IStateCommand for NoncanonicalModeCommand { Ok(()) } - fn undo(&mut self) -> Result<()> { + pub fn disable(&self) -> Result<()> { // Disable noncanonical mode if let Ok(orig) = Termios::from_fd(FD_STDIN) { let mut noncan = orig.clone(); @@ -71,7 +71,7 @@ impl RawModeCommand impl RawModeCommand { /// Enables raw mode. - fn enable(&mut self) -> Result<()> { + pub fn enable(&mut self) -> Result<()> { if let Ok(original_mode) = self.original_mode { let mut new_mode = original_mode; terminal::make_raw(&mut new_mode); @@ -86,8 +86,9 @@ impl RawModeCommand { } /// Disables raw mode. - fn disable(&self) -> Result<()> { + pub fn disable(&self) -> Result<()> { if let Ok(ref original_mode) = self.original_mode { + let result = terminal::set_terminal_mode(&original_mode)?; } else { return Err(Error::new( diff --git a/src/common/commands/win_commands.rs b/src/common/commands/win_commands.rs index 6d59392..c570cf5 100644 --- a/src/common/commands/win_commands.rs +++ b/src/common/commands/win_commands.rs @@ -87,9 +87,9 @@ impl RawModeCommand } } -impl IRawScreenCommand for RawModeCommand { +impl RawModeCommand { /// Enables raw mode. - fn enable(&mut self) -> Result<()> { + pub fn enable(&mut self) -> Result<()> { let input_handle = handle::get_input_handle()?; let mut dw_mode: DWORD = 0; @@ -113,7 +113,7 @@ impl IRawScreenCommand for RawModeCommand { } /// Disables raw mode. - fn disable(&self) -> Result<()> { + pub fn disable(&self) -> Result<()> { let output_handle = handle::get_input_handle()?; let mut dw_mode: DWORD = 0; diff --git a/src/common/screen/alternate.rs b/src/common/screen/alternate.rs index b6eadbe..8bad19e 100644 --- a/src/common/screen/alternate.rs +++ b/src/common/screen/alternate.rs @@ -67,5 +67,6 @@ impl Drop for AlternateScreen { fn drop(&mut self) { self.to_main_screen(); + } } diff --git a/src/common/screen/raw.rs b/src/common/screen/raw.rs index 023008a..5e68a86 100644 --- a/src/common/screen/raw.rs +++ b/src/common/screen/raw.rs @@ -18,7 +18,14 @@ use super::{functions, Screen, Stdout}; use std::io::{self, Write}; /// A wrapper for the raw terminal state. Which can be used to write to. -pub struct RawScreen; +pub struct RawScreen +{ +// #[cfg(not(target_os = "windows"))] +// command: unix_command::RawModeCommand, +// #[cfg(not(target_os = "windows"))] +// command: win_commands::RawModeCommand, + +} impl RawScreen { pub fn into_raw_mode() -> io::Result<()> @@ -28,6 +35,7 @@ impl RawScreen { #[cfg(target_os = "windows")] let mut command = win_commands::RawModeCommand::new(); +// command::new(); command.enable()?; Ok(()) @@ -40,7 +48,9 @@ impl RawScreen { #[cfg(target_os = "windows")] let mut command = win_commands::RawModeCommand::new(); - command.disable()?; + let a = command.disable(); + + return Ok(()) } } diff --git a/src/kernel/unix_kernel/terminal.rs b/src/kernel/unix_kernel/terminal.rs index e3e8b71..469d0ac 100644 --- a/src/kernel/unix_kernel/terminal.rs +++ b/src/kernel/unix_kernel/terminal.rs @@ -1,8 +1,8 @@ //! This module contains all `unix` specific terminal related logic. use self::libc::{c_int, c_ushort, ioctl, STDOUT_FILENO, TIOCGWINSZ}; -use common::commands::unix_command::{EnableRawModeCommand, NoncanonicalModeCommand}; -use {libc, Stdout}; +use common::commands::unix_command::{RawModeCommand, NoncanonicalModeCommand}; +use {libc, Stdout, Screen}; pub use libc::termios; use std::sync::Arc; @@ -47,14 +47,14 @@ pub fn terminal_size() -> (u16, u16) { /// Get the current cursor position. pub fn pos(stdout: &Arc) -> (u16, u16) { let mut crossterm = Crossterm::new(); - let input = crossterm.input(stdout); + let input = crossterm.input(&Screen::default()); let delimiter = b'R'; let mut stdin = input.read_until_async(delimiter); // Where is the cursor? // Use `ESC [ 6 n`. - crossterm.active_screen.write_str("\x1B[6n"); + stdout.write_str("\x1B[6n"); let mut buf: [u8; 1] = [0]; let mut read_chars = Vec::new(); @@ -105,15 +105,26 @@ pub fn make_raw(termios: &mut Termios) { unsafe { cfmakeraw(termios) } } +static mut ORIGINAL_TERMINAL_MODE: Option = None; + /// Get the current terminal mode. pub fn get_terminal_mode() -> io::Result { extern "C" { pub fn tcgetattr(fd: c_int, termptr: *mut Termios) -> c_int; } unsafe { - let mut termios = mem::zeroed(); - is_true(tcgetattr(0, &mut termios))?; - Ok(termios) + if let Some(original_mode) = ORIGINAL_TERMINAL_MODE + { + return Ok(original_mode.clone()) + } + else { + let mut termios = mem::zeroed(); + is_true(tcgetattr(0, &mut termios))?; + ORIGINAL_TERMINAL_MODE = Some(termios.clone()); + + return Ok(termios) + } + } } diff --git a/src/modules/input/unix_input.rs b/src/modules/input/unix_input.rs index 69ff906..1f30eba 100644 --- a/src/modules/input/unix_input.rs +++ b/src/modules/input/unix_input.rs @@ -17,7 +17,7 @@ impl UnixInput { } impl ITerminalInput for UnixInput { - fn read_line(&self, screen_manger: &Stdout) -> io::Result { + fn read_line(&self, screen_manger: &Arc) -> io::Result { let mut rv = String::new(); io::stdin().read_line(&mut rv)?; let len = rv.trim_right_matches(&['\r', '\n'][..]).len(); diff --git a/src/modules/style/color.rs b/src/modules/style/color.rs index ec9e891..d62ee3c 100644 --- a/src/modules/style/color.rs +++ b/src/modules/style/color.rs @@ -3,6 +3,7 @@ use super::*; use std::io; +use Screen; /// Struct that stores an specific platform implementation for color related actions. /// @@ -116,6 +117,6 @@ impl<'terminal> TerminalColor { } /// Get an Terminal Color implementation whereon color related actions can be performed. -pub fn color(stdout: &Arc) -> TerminalColor { - TerminalColor::new(stdout) +pub fn color(screen: &Screen) -> TerminalColor { + TerminalColor::new(&screen.stdout) } diff --git a/src/modules/style/mod.rs b/src/modules/style/mod.rs index a002922..9742b0d 100644 --- a/src/modules/style/mod.rs +++ b/src/modules/style/mod.rs @@ -17,7 +17,7 @@ use std::str::FromStr; use std::sync::Arc; use std::fmt::Display; -pub use self::color::TerminalColor; +pub use self::color::{TerminalColor, color}; pub use self::objectstyle::ObjectStyle; pub use self::styledobject::StyledObject; use super::{functions, Stdout}; diff --git a/src/modules/style/styledobject.rs b/src/modules/style/styledobject.rs index 6381128..5ef7658 100644 --- a/src/modules/style/styledobject.rs +++ b/src/modules/style/styledobject.rs @@ -144,7 +144,7 @@ impl StyledObject { pub fn paint(&self, screen: &Screen) { - let mut colored_terminal = super::super::super::style::color::color(&screen.stdout); + let mut colored_terminal = super::super::super::style::color::color(&screen); let mut reset = true; if let Some(bg) = self.object_style.bg_color { @@ -159,8 +159,7 @@ impl StyledObject { #[cfg(unix)] for attr in self.object_style.attrs.iter() { - self.stdout - .write_string(format!(csi!("{}m"), *attr as i16)); + screen.stdout.write_string(format!(csi!("{}m"), *attr as i16)); reset = true; }