diff --git a/.idea/workspace.xml b/.idea/workspace.xml index a416570..325ca54 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -3,21 +3,15 @@ - - - - - - - + + + + - - - - + - - + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + @@ -103,17 +46,80 @@ - - + + - - + + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -129,7 +135,6 @@ - conte terminal_cursor term ScreenManager @@ -150,7 +155,6 @@ get_to_alternate_screen_command &context cursor - context Mutex WriteConsoleOutputW CONSOLE_CURSOR_INFO @@ -159,6 +163,8 @@ fmt STD_OUTPUT_HANDLE data + context + WinApiScreenManager crossterm_cursor @@ -187,7 +193,6 @@ @@ -272,7 +278,7 @@ - + @@ -285,33 +291,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -334,7 +313,7 @@ - + @@ -392,7 +371,7 @@ - + @@ -471,19 +450,6 @@ - - - - - @@ -595,10 +561,10 @@ - - + @@ -607,15 +573,15 @@ - + - + - + @@ -623,25 +589,47 @@ + + + + + + + - - + + + + + + + + + + + + + + + + + - + + - @@ -652,7 +640,7 @@ - + @@ -674,45 +662,10 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + @@ -727,20 +680,6 @@ - - - - - - - - - - - - - - @@ -748,24 +687,6 @@ - - - - - - - - - - - - - - - - - - @@ -780,38 +701,8 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + @@ -826,13 +717,6 @@ - - - - - - - @@ -840,24 +724,10 @@ - - - - - - - - - - - - - - - - + + @@ -882,24 +752,7 @@ - - - - - - - - - - - - - - - - - - + @@ -928,20 +781,6 @@ - - - - - - - - - - - - - - @@ -953,26 +792,6 @@ - - - - - - - - - - - - - - - - - - - - @@ -990,13 +809,6 @@ - - - - - - - @@ -1046,6 +858,130 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/cursor/cursor.rs b/src/cursor/cursor.rs index 938edc4..8ee03ad 100644 --- a/src/cursor/cursor.rs +++ b/src/cursor/cursor.rs @@ -26,7 +26,7 @@ impl TerminalCursor #[cfg(not(target_os = "windows"))] let cursor = Some(AnsiCursor::new(context.clone()) as Box); - TerminalCursor { terminal_cursor: Some(WinApiCursor::new()), context} + TerminalCursor { terminal_cursor: cursor, context} } /// Goto some position (x,y) in the terminal. diff --git a/src/kernel/unix_kernel/terminal.rs b/src/kernel/unix_kernel/terminal.rs index a44a2cc..2dff600 100644 --- a/src/kernel/unix_kernel/terminal.rs +++ b/src/kernel/unix_kernel/terminal.rs @@ -41,13 +41,13 @@ pub fn terminal_size() -> (u16,u16) { } /// Get the current cursor position. -pub fn pos(terminal: &Context) -> (u16, u16) +pub fn pos(context: Rc) -> (u16, u16) { use std::io::{ Write,Read }; - let mut command_id = NoncanonicalModeCommand::new(&terminal.state_manager); + let mut command_id = NoncanonicalModeCommand::new(&context.state_manager); - CommandManager::execute(terminal, command_id); + CommandManager::execute(context.clone(), command_id); // This code is original written by term_cursor credits to them. use std::io; @@ -97,7 +97,7 @@ pub fn pos(terminal: &Context) -> (u16, u16) // Expect `R` let res = if c == 'R' { (cols as u16, rows as u16) } else { return (0, 0) }; - CommandManager::undo(terminal, command_id); + CommandManager::undo(context.clone(), command_id); res } diff --git a/src/manager/manager.rs b/src/manager/manager.rs index dafa94d..080e477 100644 --- a/src/manager/manager.rs +++ b/src/manager/manager.rs @@ -1,6 +1,6 @@ //! This module provides an interface for working with the sceen. With that I mean that you can get or wirte to the handle of the current screen. stdout. //! Because crossterm can work with alternate screen, we need a place that holds the handle to the current screen. And this module provides this place. -use super::{AnsiScreenManager, WinApiScreenManager, IScreenManager }; +use super::*; use super::super::shared::functions; use std::any::Any; @@ -24,11 +24,11 @@ impl ScreenManager let screen_manager = functions::get_module::>(Box::from(WinApiScreenManager::new()), Box::from(AnsiScreenManager::new())).unwrap(); #[cfg(not(target_os = "windows"))] - let screen_manager = Box::new(AnsiScreenManager::new()); + let screen_manager = Box::from(AnsiScreenManager::new()) as Box; ScreenManager { - screen_manager: Box::from(WinApiScreenManager::new()) + screen_manager: screen_manager } } diff --git a/src/shared/functions.rs b/src/shared/functions.rs index a9d015c..58514bd 100644 --- a/src/shared/functions.rs +++ b/src/shared/functions.rs @@ -21,10 +21,10 @@ pub fn get_terminal_size() -> (u16, u16) } /// Get the cursor position based on the current platform. -pub fn get_cursor_position(screen: Rc) -> (u16, u16) +pub fn get_cursor_position(context: Rc) -> (u16, u16) { #[cfg(unix)] - return pos(&screen); + return pos(context.clone()); #[cfg(windows)] return pos(); diff --git a/src/shared/screen.rs b/src/shared/screen.rs index f40f2a8..251688d 100644 --- a/src/shared/screen.rs +++ b/src/shared/screen.rs @@ -95,9 +95,9 @@ impl Drop for AlternateScreen // Get the alternate screen command to enable and disable alternate screen based on the current platform fn get_to_alternate_screen_command(context: Rc) -> u16 { -// #[cfg(target_os = "windows")] -// let command = functions::get_module::>(win_commands::ToAlternateScreenBufferCommand::new(), shared_commands::ToAlternateScreenBufferCommand::new(), context).unwrap(); + #[cfg(target_os = "windows")] + let command = functions::get_module::>(win_commands::ToAlternateScreenBufferCommand::new(), shared_commands::ToAlternateScreenBufferCommand::new(), context).unwrap(); -// #[cfg(not(target_os = "windows"))] - win_commands::ToAlternateScreenBufferCommand::new(context) + #[cfg(not(target_os = "windows"))] + shared_commands::ToAlternateScreenBufferCommand::new(context.clone()) } \ No newline at end of file diff --git a/src/state/commands/shared_commands.rs b/src/state/commands/shared_commands.rs index f838207..122f866 100644 --- a/src/state/commands/shared_commands.rs +++ b/src/state/commands/shared_commands.rs @@ -2,6 +2,8 @@ use Context; use super::{IStateCommand}; +use std::rc::Rc; + pub struct EmptyCommand; impl IStateCommand for EmptyCommand @@ -18,19 +20,27 @@ impl IStateCommand for EmptyCommand } /// This command is used for switching to alternate screen and back to main screen. -pub struct ToAlternateScreenBufferCommand<'a> +pub struct ToAlternateScreenBufferCommand { - context: &'a Context + context: Rc } -impl<'a> ToAlternateScreenBufferCommand<'a> +impl ToAlternateScreenBufferCommand { - pub fn new(context: & 'a Context) -> Box < ToAlternateScreenBufferCommand > { - Box::from(ToAlternateScreenBufferCommand {context: context}) + pub fn new(context: Rc) -> u16 { + + let mut state = context.state_manager.lock().unwrap(); + { + let key = state.get_changes_count(); + let command = ToAlternateScreenBufferCommand {context: context.clone()}; + + state.register_change(Box::from(command), key); + key + } } } -impl<'context> IStateCommand for ToAlternateScreenBufferCommand<'context> +impl IStateCommand for ToAlternateScreenBufferCommand { fn execute(&mut self) -> bool { diff --git a/src/state/commands/unix_command.rs b/src/state/commands/unix_command.rs index 32a3cd5..d9f1f96 100644 --- a/src/state/commands/unix_command.rs +++ b/src/state/commands/unix_command.rs @@ -19,14 +19,14 @@ pub struct NoncanonicalModeCommand impl NoncanonicalModeCommand { - pub fn new(state: &Mutex) -> u16 + pub fn new(state_manager: &Mutex) -> u16 { - let key = 1; - - let mut state_manager = state.lock().unwrap(); + let mut state = state_manager.lock().unwrap(); { - let command = Box::from(NoncanonicalModeCommand { key: key }); - state_manager.register_change(command, key); + let key = state.get_changes_count(); + let command = NoncanonicalModeCommand { key: key }; + + state.register_change(Box::from(command), key); key } } @@ -84,11 +84,12 @@ pub struct EnableRawModeCommand impl EnableRawModeCommand { pub fn new(state_manager: &Mutex) -> u16{ - let key = 2; let mut state = state_manager.lock().unwrap(); { + let key = state.get_changes_count(); let command = EnableRawModeCommand { original_mode: None, command_id: key }; + state.register_change(Box::from(command), key); key } diff --git a/src/state/commands/win_commands.rs b/src/state/commands/win_commands.rs index e95c523..cab2dcc 100644 --- a/src/state/commands/win_commands.rs +++ b/src/state/commands/win_commands.rs @@ -158,10 +158,9 @@ impl ToAlternateScreenBufferCommand { pub fn new(context: Rc) -> u16 { - let key = 2; - let mut state = context.state_manager.lock().unwrap(); { + let key = state.get_changes_count(); let command = ToAlternateScreenBufferCommand {context: context.clone()}; state.register_change(Box::from(command), key); diff --git a/src/style/styles/styledobject.rs b/src/style/styles/styledobject.rs index 33325c5..8d3cd9a 100644 --- a/src/style/styles/styledobject.rs +++ b/src/style/styles/styledobject.rs @@ -135,7 +135,7 @@ macro_rules! impl_fmt let mutex = self.context.screen_manager; { let mut screen = mutex.lock().unwrap(); - screen.write_ansi_str(format!(csi!("{}m"), *attr as i16)).expect("Flush failed"); + screen.write_ansi_str(format!(csi!("{}m"), *attr as i16)); } reset = true; } @@ -146,7 +146,7 @@ macro_rules! impl_fmt { let mut screen = mutex.lock().unwrap(); - screen.flush().expect("Flush failed"); + screen.flush(); } if reset