diff --git a/examples/examples.rs b/examples/examples.rs index a0b115d..c633ff3 100644 --- a/examples/examples.rs +++ b/examples/examples.rs @@ -17,10 +17,26 @@ mod input; use crossterm::{Screen, Crossterm}; use std::{time, thread}; +use std::sync::mpsc; use crossterm::cursor::cursor; fn main() { + let nthreads = 5; + let (tx, rx) = mpsc::channel(); - thread::sleep(time::Duration::from_millis(2000)); + + for i in 0..nthreads { + let tx = tx.clone(); + thread::spawn(move || { + let response = Crossterm::new(&Screen::default()); + tx.send(response).unwrap(); + }); + } + + for _ in 0..nthreads { + let screen: Crossterm = rx.recv().unwrap(); + screen.terminal(); + + } } diff --git a/src/common/commands/unix_command.rs b/src/common/commands/unix_command.rs index 212399b..053e493 100644 --- a/src/common/commands/unix_command.rs +++ b/src/common/commands/unix_command.rs @@ -60,42 +60,4 @@ pub struct RawModeCommand { original_mode: Result, } -impl RawModeCommand -{ - pub fn new() -> Self { - RawModeCommand { - original_mode: terminal::get_terminal_mode(), - } - } -} -impl RawModeCommand { - /// Enables raw mode. - 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); - terminal::set_terminal_mode(&new_mode); - } else { - return Err(Error::new( - ErrorKind::Other, - "Could not set console mode when enabling raw mode", - )); - } - Ok(()) - } - - /// Disables raw mode. - 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( - ErrorKind::Other, - "Could not set console mode when enabling raw mode", - )); - } - Ok(()) - } -} diff --git a/src/modules/write/stdout.rs b/src/modules/write/stdout.rs index af816fd..0d230e5 100644 --- a/src/modules/write/stdout.rs +++ b/src/modules/write/stdout.rs @@ -35,7 +35,7 @@ use std::sync::Arc; /// /// For unix and windows 10 `stdout()` will be used for handle when on windows systems with versions lower than 10 WinApi `HANDLE` will be used. pub struct Stdout { - screen_manager: Box, + screen_manager: Box, pub is_in_raw_mode:bool, } @@ -43,13 +43,13 @@ impl Stdout { /// Create new screen write instance whereon screen related actions can be performed. pub fn new(is_in_raw_mode: bool) -> Self { #[cfg(target_os = "windows")] - let screen_manager = functions::get_module::>( + let screen_manager = functions::get_module::>( Box::from(WinApiStdout::new()), Box::from(AnsiStdout::new()), ).unwrap(); #[cfg(not(target_os = "windows"))] - let screen_manager = Box::from(AnsiStdout::new()) as Box; + let screen_manager = Box::from(AnsiStdout::new()) as Box; Stdout { screen_manager , is_in_raw_mode} } diff --git a/src/modules/write/winapi_stdout.rs b/src/modules/write/winapi_stdout.rs index 40966f3..37d60ba 100644 --- a/src/modules/write/winapi_stdout.rs +++ b/src/modules/write/winapi_stdout.rs @@ -6,11 +6,11 @@ use winapi::um::winnt::HANDLE; use std::ptr::NonNull; use std::any::Any; use std::io::{self, Write}; -use std::sync::Arc; +use std::sync::{Mutex,Arc, }; /// This struct is a wrapper for WINAPI `HANDLE` pub struct WinApiStdout { - pub handle: HANDLE, + pub handle: Arc>, } impl IStdout for WinApiStdout { @@ -20,7 +20,7 @@ impl IStdout for WinApiStdout { } fn write(&self, buf: &[u8]) -> io::Result { - writing::write_char_buffer(&self.handle, buf) + writing::write_char_buffer(&self.handle.lock().unwrap(), buf) } fn flush(&self) -> io::Result<()> { @@ -38,19 +38,19 @@ impl IStdout for WinApiStdout { impl WinApiStdout { pub fn new() -> Self { - WinApiStdout { handle: handle::get_output_handle().unwrap() } + WinApiStdout { handle: Arc::new(Mutex::new(handle::get_output_handle().unwrap())) } } pub fn set(&mut self, handle: HANDLE) { - self.handle = handle; + self.handle = Arc::new(Mutex::new(handle)); } - pub fn get_handle(&self) -> &HANDLE + pub fn get_handle(&self) -> &Arc> { return &self.handle; } } unsafe impl Send for WinApiStdout {} - +unsafe impl Sync for WinApiStdout {}