diff --git a/src/common/commands/mod.rs b/src/common/commands/mod.rs index a361777..cb5fb45 100644 --- a/src/common/commands/mod.rs +++ b/src/common/commands/mod.rs @@ -1,6 +1,6 @@ //! This module contains some commands that could be executed for specific task. -use super::super::write::Stdout; +use super::super::output::TerminalOutput; use std::io; use std::sync::Mutex; @@ -26,8 +26,8 @@ pub trait IEnableAnsiCommand { // This trait provides an interface for switching to alternate screen and back. pub trait IAlternateScreenCommand: Send { - fn enable(&self, screen_manager: &mut Stdout) -> io::Result<()>; - fn disable(&self, screen_manager: &Stdout) -> io::Result<()>; + fn enable(&self, screen_manager: &mut TerminalOutput) -> io::Result<()>; + fn disable(&self, screen_manager: &TerminalOutput) -> io::Result<()>; } // This trait provides an interface for switching to raw mode and back. diff --git a/src/common/commands/shared_commands.rs b/src/common/commands/shared_commands.rs index 27626af..000dc6f 100644 --- a/src/common/commands/shared_commands.rs +++ b/src/common/commands/shared_commands.rs @@ -1,6 +1,6 @@ //! This module contains the commands that can be used for both unix and windows 10 systems because they support ANSI escape codes -use super::{IAlternateScreenCommand, Stdout}; +use super::{IAlternateScreenCommand, TerminalOutput}; use std::sync::Mutex; use std::io::{ Result, stdout, Write}; @@ -17,13 +17,13 @@ impl ToAlternateScreenCommand { impl IAlternateScreenCommand for ToAlternateScreenCommand { /// enable alternate screen. - fn enable(&self, screen_manager: &mut Stdout) -> Result<()> { + fn enable(&self, screen_manager: &mut TerminalOutput) -> Result<()> { screen_manager.write_str(csi!("?1049h")); Ok(()) } /// disable alternate screen. - fn disable(&self, screen_manager: &Stdout) -> Result<()> { + fn disable(&self, screen_manager: &TerminalOutput) -> Result<()> { screen_manager.write_str(csi!("?1049l")); Ok(()) } diff --git a/src/common/commands/win_commands.rs b/src/common/commands/win_commands.rs index c570cf5..f0c79b6 100644 --- a/src/common/commands/win_commands.rs +++ b/src/common/commands/win_commands.rs @@ -1,9 +1,9 @@ //! This module contains the commands that can be used for windows systems. -use super::{IAlternateScreenCommand, IEnableAnsiCommand, IRawScreenCommand, Stdout}; +use super::{IAlternateScreenCommand, IEnableAnsiCommand, IRawScreenCommand, TerminalOutput}; use kernel::windows_kernel::{ansi_support, csbi, handle, kernel}; -use modules::write::IStdout; +use modules::output::IStdout; use std::mem; use winapi::shared::minwindef::DWORD; use winapi::um::wincon; @@ -148,8 +148,8 @@ impl ToAlternateScreenCommand { } impl IAlternateScreenCommand for ToAlternateScreenCommand { - fn enable(&self, screen_manager: &mut Stdout) -> Result<()> { - use super::super::super::modules::write::WinApiStdout; + fn enable(&self, screen_manager: &mut TerminalOutput) -> Result<()> { + use super::super::super::modules::output::WinApiOutput; let handle = handle::get_output_handle()?; @@ -159,9 +159,9 @@ impl IAlternateScreenCommand for ToAlternateScreenCommand { // Make the new screen buffer the active screen buffer. csbi::set_active_screen_buffer(new_handle)?; - let b: &mut WinApiStdout = match screen_manager + let b: &mut WinApiOutput = match screen_manager .as_any_mut() - .downcast_mut::() + .downcast_mut::() { Some(b) => b, None => return Err(Error::new(ErrorKind::Other, "Invalid cast exception")), @@ -172,7 +172,7 @@ impl IAlternateScreenCommand for ToAlternateScreenCommand { Ok(()) } - fn disable(&self, screen_manager: &Stdout) -> Result<()> { + fn disable(&self, screen_manager: &TerminalOutput) -> Result<()> { let handle = handle::get_output_handle()?; csbi::set_active_screen_buffer(handle); diff --git a/src/common/crossterm.rs b/src/common/crossterm.rs index 2c2845d..1eb62a7 100644 --- a/src/common/crossterm.rs +++ b/src/common/crossterm.rs @@ -4,9 +4,9 @@ use super::screen::{AlternateScreen, Screen}; use super::super::cursor; use super::super::input; -use super::super::write; +use super::super::output; use super::super::style; - use super::super::terminal; +use super::super::terminal; use std::fmt::Display; use std::io::Write; @@ -20,7 +20,7 @@ use common::commands::unix_command; #[cfg(windows)] use common::commands::win_commands; -use write::Stdout; +use output::TerminalOutput; /// This type could be used to access the `cursor, terminal, color, input, styling` module more easily. /// You need to pass a reference to the screen where on you want to perform the actions to the `Crossterm` type. @@ -50,7 +50,7 @@ use write::Stdout; /// } /// ``` pub struct Crossterm { - stdout: Arc + stdout: Arc } impl<'crossterm> Crossterm { diff --git a/src/common/functions.rs b/src/common/functions.rs index d4b15b0..e9be014 100644 --- a/src/common/functions.rs +++ b/src/common/functions.rs @@ -1,6 +1,6 @@ //! Some actions need to preformed platform independently since they can not be solved `ANSI escape codes`. -use super::Stdout; +use super::TerminalOutput; use std::sync::Arc; #[cfg(windows)] @@ -17,28 +17,16 @@ use kernel::unix_kernel::terminal::{exit, pos, terminal_size}; /// Get the terminal size based on the current platform. pub fn get_terminal_size() -> (u16, u16) { - #[cfg(unix)] - return terminal_size(); - - #[cfg(windows)] return terminal_size(); } /// Get the cursor position based on the current platform. -pub fn get_cursor_position(stdout: &Arc) -> (u16, u16) { - #[cfg(unix)] - return pos(stdout); - - #[cfg(windows)] +pub fn get_cursor_position(stdout: &Arc) -> (u16, u16) { return pos(stdout); } /// exit the current terminal. pub fn exit_terminal() { - #[cfg(unix)] - exit(); - - #[cfg(windows)] exit(); } diff --git a/src/common/mod.rs b/src/common/mod.rs index 80b4e92..be80a6f 100644 --- a/src/common/mod.rs +++ b/src/common/mod.rs @@ -10,5 +10,4 @@ pub mod traits; mod crossterm; pub use self::crossterm::Crossterm; -use super::modules::Stdout; -pub use screen::Screen; +use {Screen, TerminalOutput}; diff --git a/src/common/screen/alternate.rs b/src/common/screen/alternate.rs index 0dc77e0..18a664c 100644 --- a/src/common/screen/alternate.rs +++ b/src/common/screen/alternate.rs @@ -7,7 +7,7 @@ //! Vim uses the entirety of the screen to edit the file, then returning to bash leaves the original buffer unchanged. use super::commands::{self, IAlternateScreenCommand}; -use super::{functions, Screen, Stdout, RawScreen}; +use super::{functions, Screen, TerminalOutput, RawScreen}; use std::convert::From; use std::io::{self, Write}; @@ -35,7 +35,7 @@ impl AlternateScreen { /// The alternate buffer is exactly the dimensions of the window, without any scrollback region. /// For an example of this behavior, consider when vim is launched from bash. /// Vim uses the entirety of the screen to edit the file, then returning to bash leaves the original buffer unchanged. - pub fn to_alternate_screen(screen_manager: Stdout) -> io::Result { + pub fn to_alternate_screen(stdout: TerminalOutput) -> io::Result { #[cfg(target_os = "windows")] let command = functions::get_module::>( Box::from(commands::win_commands::ToAlternateScreenCommand::new()), @@ -45,7 +45,7 @@ impl AlternateScreen { #[cfg(not(target_os = "windows"))] let command = Box::from(commands::shared_commands::ToAlternateScreenCommand::new()); - let mut stdout = screen_manager; + let mut stdout = stdout; command.enable(&mut stdout)?; return Ok(AlternateScreen::new(command, Screen::from(stdout))); } diff --git a/src/common/screen/mod.rs b/src/common/screen/mod.rs index 21878c1..ea00ad6 100644 --- a/src/common/screen/mod.rs +++ b/src/common/screen/mod.rs @@ -4,7 +4,7 @@ mod alternate; mod raw; mod screen; -use super::{commands, functions, Stdout}; +use super::{commands, functions, TerminalOutput}; pub use self::alternate::AlternateScreen; pub use self::screen::Screen; diff --git a/src/common/screen/raw.rs b/src/common/screen/raw.rs index 13a1ce5..2a39650 100644 --- a/src/common/screen/raw.rs +++ b/src/common/screen/raw.rs @@ -15,7 +15,7 @@ //! With these modes you can easier design the terminal screen. use super::commands::*; -use super::{functions, Screen, Stdout}; +use super::{functions, Screen, TerminalOutput}; use std::io::{self, Write}; diff --git a/src/common/screen/screen.rs b/src/common/screen/screen.rs index 8f3dc8f..44d3685 100644 --- a/src/common/screen/screen.rs +++ b/src/common/screen/screen.rs @@ -7,7 +7,7 @@ use common::commands::win_commands; use common::commands::IAlternateScreenCommand; use super::{AlternateScreen,RawScreen}; -use super::super::super::modules::write::Stdout; +use TerminalOutput; use std::io::Write; use std::io::Result; @@ -52,7 +52,7 @@ use std::sync::Arc; pub struct Screen { buffer: Vec, - pub stdout: Arc, + pub stdout: Arc, } impl Screen @@ -64,7 +64,7 @@ impl Screen if raw_mode { RawScreen::into_raw_mode();; - return Screen { stdout: Arc::new(Stdout::new(true)), buffer: Vec::new() }; + return Screen { stdout: Arc::new(TerminalOutput::new(true)), buffer: Vec::new() }; } return Screen::default(); @@ -98,7 +98,7 @@ impl Screen /// For an example of this behavior, consider when vim is launched from bash. /// Vim uses the entirety of the screen to edit the file, then returning to bash leaves the original buffer unchanged. pub fn enable_alternate_modes(&self, raw_mode: bool) -> Result { - let mut stdout = Stdout::new(raw_mode); + let mut stdout = TerminalOutput::new(raw_mode); if raw_mode { @@ -110,18 +110,18 @@ impl Screen } } -impl From for Screen +impl From for Screen { /// Create an screen with the given `Stdout` - fn from(stdout: Stdout) -> Self { + fn from(stdout: TerminalOutput) -> Self { return Screen { stdout: Arc::new(stdout), buffer: Vec::new() }; } } -impl From> for Screen +impl From> for Screen { /// Create an screen with the given 'Arc' - fn from(stdout: Arc) -> Self { + fn from(stdout: Arc) -> Self { return Screen { stdout: stdout, buffer: Vec::new() }; } } @@ -130,7 +130,7 @@ impl Default for Screen { /// Create an new screen which will not be in raw mode or alternate mode. fn default() -> Self { - return Screen { stdout: Arc::new(Stdout::new(false)), buffer: Vec::new() }; + return Screen { stdout: Arc::new(TerminalOutput::new(false)), buffer: Vec::new() }; } } diff --git a/src/kernel/unix_kernel/terminal.rs b/src/kernel/unix_kernel/terminal.rs index fa60940..36ff2b2 100644 --- a/src/kernel/unix_kernel/terminal.rs +++ b/src/kernel/unix_kernel/terminal.rs @@ -2,7 +2,7 @@ use self::libc::{c_int, c_ushort, ioctl, STDOUT_FILENO, TIOCGWINSZ}; use common::commands::unix_command::{RawModeCommand, NoncanonicalModeCommand}; -use {libc, Stdout, Screen}; +use {libc, TerminalOutput, Screen}; pub use libc::termios; use std::sync::Arc; @@ -45,7 +45,7 @@ pub fn terminal_size() -> (u16, u16) { } /// Get the current cursor position. -pub fn pos(stdout: &Arc) -> (u16, u16) { +pub fn pos(stdout: &Arc) -> (u16, u16) { let mut crossterm = Crossterm::new(&Screen::default()); let input = crossterm.input(); diff --git a/src/kernel/windows_kernel/csbi.rs b/src/kernel/windows_kernel/csbi.rs index 72c3d1a..4b0a5a3 100644 --- a/src/kernel/windows_kernel/csbi.rs +++ b/src/kernel/windows_kernel/csbi.rs @@ -7,18 +7,18 @@ use winapi::um::wincon::{ CreateConsoleScreenBuffer, GetConsoleScreenBufferInfo, SetConsoleActiveScreenBuffer, SetConsoleScreenBufferSize, CONSOLE_SCREEN_BUFFER_INFO, CONSOLE_TEXTMODE_BUFFER, COORD, }; -use winapi::um::winnt::HANDLE; + use winapi::um::winnt::{FILE_SHARE_READ, FILE_SHARE_WRITE, GENERIC_READ, GENERIC_WRITE}; -use super::{handle, kernel, Empty, Stdout}; +use super::{handle, kernel, Empty, TerminalOutput, HANDLE}; -use std::sync::{Once, ONCE_INIT}; use std::io::{self, ErrorKind, Result}; +use std::sync::{Once, ONCE_INIT}; use std::mem::size_of; use std::sync::Arc; /// Create a new console screen buffer info struct. -pub fn get_csbi(screen_manager: &Arc) -> Result { +pub fn get_csbi(screen_manager: &Arc) -> Result { let mut csbi = CONSOLE_SCREEN_BUFFER_INFO::empty(); let success; @@ -38,7 +38,7 @@ pub fn get_csbi(screen_manager: &Arc) -> Result, + screen_manager: &Arc, ) -> Result<(CONSOLE_SCREEN_BUFFER_INFO, HANDLE)> { let handle = handle::get_current_handle(screen_manager)?; let csbi = get_csbi_by_handle(&handle)?; @@ -63,7 +63,7 @@ pub fn get_csbi_by_handle(handle: &HANDLE) -> Result } /// Set the console screen buffer size -pub fn set_console_screen_buffer_size(size: COORD, screen_manager: &Arc) -> bool { +pub fn set_console_screen_buffer_size(size: COORD, screen_manager: &Arc) -> bool { let handle = handle::get_current_handle(screen_manager).unwrap(); unsafe { diff --git a/src/kernel/windows_kernel/cursor.rs b/src/kernel/windows_kernel/cursor.rs index e31b8bc..e8e9b5e 100644 --- a/src/kernel/windows_kernel/cursor.rs +++ b/src/kernel/windows_kernel/cursor.rs @@ -5,29 +5,28 @@ use winapi::um::wincon::{ SetConsoleCursorInfo, SetConsoleCursorPosition, CONSOLE_CURSOR_INFO, COORD, }; -use super::super::super::modules::write::{Stdout, WinApiStdout}; -use super::{csbi, handle, kernel}; +use super::{csbi, handle, kernel, TerminalOutput, WinApiOutput}; -use std::io::{self, ErrorKind, Result}; +use std::io; use std::sync::Arc; /// This stores the cursor pos, at program level. So it can be recalled later. static mut SAVED_CURSOR_POS: (u16, u16) = (0, 0); /// Reset to saved cursor position -pub fn reset_to_saved_position(screen_manager: &Arc) { +pub fn reset_to_saved_position(stdout: &Arc) { unsafe { set_console_cursor_position( SAVED_CURSOR_POS.0 as i16, SAVED_CURSOR_POS.1 as i16, - screen_manager, + stdout, ); } } /// Save current cursor position to recall later. -pub fn save_cursor_pos(screen_manager: &Arc) { - let position = pos(screen_manager); +pub fn save_cursor_pos(stdout: &Arc) { + let position = pos(stdout); unsafe { SAVED_CURSOR_POS = (position.0, position.1); @@ -35,8 +34,8 @@ pub fn save_cursor_pos(screen_manager: &Arc) { } /// get the current cursor position. -pub fn pos(screen_manager: &Arc) -> (u16, u16) { - let handle = handle::get_current_handle(screen_manager).unwrap(); +pub fn pos(stdout: &Arc) -> (u16, u16) { + let handle = handle::get_current_handle(stdout).unwrap(); if let Ok(csbi) = csbi::get_csbi_by_handle(&handle) { ( @@ -49,7 +48,7 @@ pub fn pos(screen_manager: &Arc) -> (u16, u16) { } /// Set the cursor position to the given x and y. Note that this is 0 based. -pub fn set_console_cursor_position(x: i16, y: i16, screen_manager: &Arc) { +pub fn set_console_cursor_position(x: i16, y: i16, stdout: &Arc) { if x < 0 || x >= ::max_value() { panic!( "Argument Out of Range Exception when setting cursor position to X: {}", @@ -64,7 +63,7 @@ pub fn set_console_cursor_position(x: i16, y: i16, screen_manager: &Arc) ); } - let handle = handle::get_current_handle(screen_manager).unwrap(); + let handle = handle::get_current_handle(stdout).unwrap(); let position = COORD { X: x, Y: y }; @@ -78,8 +77,8 @@ pub fn set_console_cursor_position(x: i16, y: i16, screen_manager: &Arc) } /// change the cursor visibility. -pub fn cursor_visibility(visable: bool, screen_manager: &Arc) -> Result<()> { - let handle = handle::get_current_handle(screen_manager).unwrap(); +pub fn cursor_visibility(visable: bool, stdout: &Arc) -> io::Result<()> { + let handle = handle::get_current_handle(stdout).unwrap(); let cursor_info = CONSOLE_CURSOR_INFO { dwSize: 100, diff --git a/src/kernel/windows_kernel/handle.rs b/src/kernel/windows_kernel/handle.rs index 3f8e8fe..b9873f1 100644 --- a/src/kernel/windows_kernel/handle.rs +++ b/src/kernel/windows_kernel/handle.rs @@ -3,19 +3,19 @@ use winapi::um::handleapi::INVALID_HANDLE_VALUE; use winapi::um::processenv::GetStdHandle; use winapi::um::winbase::{STD_INPUT_HANDLE, STD_OUTPUT_HANDLE}; -use winapi::um::winnt::HANDLE; +use super::*; + use std::sync::Arc; -use super::super::super::modules::write::{Stdout, WinApiStdout}; /// Get the global stored handle whits provides access to the current screen. -pub fn get_current_handle(screen_manager: &Arc) -> Result { +pub fn get_current_handle(screen_manager: &Arc) -> Result { let mut mutex = screen_manager; let handle: Result; - let winapi_screen_manager: &WinApiStdout = match screen_manager + let winapi_screen_manager: &WinApiOutput = match screen_manager .as_any() - .downcast_ref::() + .downcast_ref::() { Some(win_api) => win_api, None => return Err(io::Error::new(io::ErrorKind::Other,"Could not convert to winapi screen write, this could happen when the user has an ANSI screen write and is calling the platform specific operations 'get_cursor_pos' or 'get_terminal_size'")) diff --git a/src/kernel/windows_kernel/kernel.rs b/src/kernel/windows_kernel/kernel.rs index 65c5195..f93f1e2 100644 --- a/src/kernel/windows_kernel/kernel.rs +++ b/src/kernel/windows_kernel/kernel.rs @@ -4,10 +4,8 @@ use winapi::um::consoleapi::{GetConsoleMode, SetConsoleMode}; use winapi::um::wincon::{ GetLargestConsoleWindowSize, SetConsoleTextAttribute, SetConsoleWindowInfo, COORD, SMALL_RECT, }; -use winapi::um::winnt::HANDLE; -use super::super::super::modules::Stdout; -use super::{handle, Empty}; +use super::*; use std::io::{ErrorKind, Result}; use std::sync::Arc; @@ -34,8 +32,8 @@ pub fn get_console_mode(handle: &HANDLE, current_mode: &mut u32) -> bool { } /// Change the console text attribute. -pub fn set_console_text_attribute(value: u16, screen_manager: &Arc) -> bool { - let handle = handle::get_current_handle(screen_manager).unwrap(); +pub fn set_console_text_attribute(value: u16, stdout: &Arc) -> bool { + let handle = handle::get_current_handle(stdout).unwrap(); unsafe { return is_true(SetConsoleTextAttribute(handle, value)); @@ -43,8 +41,8 @@ pub fn set_console_text_attribute(value: u16, screen_manager: &Arc) -> b } /// Change console info. -pub fn set_console_info(absolute: bool, rect: &SMALL_RECT, screen_manager: &Arc) -> bool { - let handle = handle::get_current_handle(screen_manager).unwrap(); +pub fn set_console_info(absolute: bool, rect: &SMALL_RECT, stdout: &Arc) -> bool { + let handle = handle::get_current_handle(stdout).unwrap(); let absolute = match absolute { true => 1, diff --git a/src/kernel/windows_kernel/mod.rs b/src/kernel/windows_kernel/mod.rs index 9bb91a4..d08c874 100644 --- a/src/kernel/windows_kernel/mod.rs +++ b/src/kernel/windows_kernel/mod.rs @@ -8,9 +8,14 @@ pub mod kernel; pub mod terminal; pub mod writing; -use super::super::modules::Stdout; -use common::traits::Empty; use winapi::um::wincon::{CONSOLE_SCREEN_BUFFER_INFO, COORD, SMALL_RECT}; +use winapi::um::winnt::HANDLE; + +use TerminalOutput; +use super::super::modules::output::WinApiOutput; + +use common::traits::Empty; + impl Empty for COORD { fn empty() -> COORD { diff --git a/src/kernel/windows_kernel/reading.rs b/src/kernel/windows_kernel/reading.rs index f4fc052..535f090 100644 --- a/src/kernel/windows_kernel/reading.rs +++ b/src/kernel/windows_kernel/reading.rs @@ -11,12 +11,12 @@ //use super::kernel; //use winapi::ctypes::c_void; // -//pub fn read(buf: &mut [u8], screen_manager: &Rc>) { +//pub fn read(buf: &mut [u8], stdout: &Rc>) { //// // Read more if the buffer is empty //// let mut utf16: Vec = Vec::new(); //// let mut num: DWORD = 0; //// -//// let handle = kernel::get_current_handle(&screen_manager); +//// let handle = kernel::get_current_handle(&stdout); //// //// unsafe { //// ReadConsoleW(handle, @@ -36,12 +36,12 @@ // //} // -//pub fn read_line(screen_manager: &Rc>) -> ::std::io::Result +//pub fn read_line(stdout: &Rc>) -> ::std::io::Result //{ // const BUFFER_LENGHT: u32 = 1024; // let mut buffer: &mut [CHAR_INFO; BUFFER_LENGHT as usize] = unsafe {::std::mem::zeroed() }; // -// let handle = kernel::get_current_handle(&screen_manager); +// let handle = kernel::get_current_handle(&stdout); // // let mut dw_mode: DWORD = 0; // let console_mode = kernel::get_console_mode(&handle, &mut dw_mode); diff --git a/src/kernel/windows_kernel/terminal.rs b/src/kernel/windows_kernel/terminal.rs index 5851507..ce6d3e6 100644 --- a/src/kernel/windows_kernel/terminal.rs +++ b/src/kernel/windows_kernel/terminal.rs @@ -1,7 +1,6 @@ //! This module contains terminal specific logic. -use super::{csbi, handle, Stdout}; - +use super::{csbi, handle, TerminalOutput}; use std::sync::Arc; /// Get the terminal size @@ -18,7 +17,7 @@ pub fn terminal_size() -> (u16, u16) { } } -pub fn buffer_size(screen_manager: &Arc) -> (u16, u16) { +pub fn buffer_size(screen_manager: &Arc) -> (u16, u16) { let handle = handle::get_output_handle().unwrap(); if let Ok(csbi) = csbi::get_csbi_by_handle(&handle) { diff --git a/src/kernel/windows_kernel/writing.rs b/src/kernel/windows_kernel/writing.rs index 75d46b1..e986fa7 100644 --- a/src/kernel/windows_kernel/writing.rs +++ b/src/kernel/windows_kernel/writing.rs @@ -7,9 +7,8 @@ use winapi::um::wincon::{ self, FillConsoleOutputAttribute, FillConsoleOutputCharacterA, WriteConsoleOutputA, CHAR_INFO, COORD, PSMALL_RECT, }; -use winapi::um::winnt::HANDLE; -use super::{csbi, handle, kernel, Stdout}; +use super::{csbi, handle, kernel, TerminalOutput, HANDLE}; use std::io::{self, ErrorKind, Result}; use std::str; @@ -20,7 +19,7 @@ pub fn fill_console_output_character( cells_written: &mut u32, start_location: COORD, cells_to_write: u32, - screen_manager: &Arc, + screen_manager: &Arc, ) -> bool { let handle = handle::get_current_handle(screen_manager).unwrap(); @@ -42,7 +41,7 @@ pub fn fill_console_output_attribute( cells_written: &mut u32, start_location: COORD, cells_to_write: u32, - screen_manager: &Arc, + screen_manager: &Arc, ) -> bool { // Get the position of the current console window diff --git a/src/lib.rs b/src/lib.rs index 477059e..05e386b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,14 +12,20 @@ mod modules; pub use common::screen; pub use modules::cursor; pub use modules::input; -pub use modules::write; +pub use modules::output; pub use modules::style; pub use modules::terminal; pub use common::screen::Screen; pub use common::Crossterm; -pub use write::{Stdout}; -use write::IStdout; +pub use output::TerminalOutput; +pub use self::cursor::*; +pub use self::input::*; +pub use self::output::*; +pub use self::style::*; + +use output::IStdout; +use common::functions; #[cfg(unix)] extern crate libc; diff --git a/src/modules/cursor/ansi_cursor.rs b/src/modules/cursor/ansi_cursor.rs index 3a87de0..5aa749b 100644 --- a/src/modules/cursor/ansi_cursor.rs +++ b/src/modules/cursor/ansi_cursor.rs @@ -2,9 +2,7 @@ //! This module is used for windows 10 terminals and unix terminals by default. //! Note that the cursor position is 0 based. This means that we start counting at 0 when setting the cursor position ect. -use super::{functions, ITerminalCursor, Stdout}; - -use std::sync::Arc; +use super::*; /// This struct is an ansi implementation for cursor related actions. @@ -17,47 +15,47 @@ impl AnsiCursor { } impl ITerminalCursor for AnsiCursor { - fn goto(&self, x: u16, y: u16, screen_manager: &Arc) { - screen_manager.write_string(format!(csi!("{};{}H"), y + 1, x + 1)); + fn goto(&self, x: u16, y: u16, stdout: &Arc) { + stdout.write_string(format!(csi!("{};{}H"), y + 1, x + 1)); } - fn pos(&self, screen_manager: &Arc) -> (u16, u16) { - functions::get_cursor_position(screen_manager) + fn pos(&self, stdout: &Arc) -> (u16, u16) { + functions::get_cursor_position(stdout) } - fn move_up(&self, count: u16, screen_manager: &Arc) { - screen_manager.write_string(format!(csi!("{}A"), count)); + fn move_up(&self, count: u16, stdout: &Arc) { + stdout.write_string(format!(csi!("{}A"), count)); } - fn move_right(&self, count: u16, screen_manager: &Arc) { + fn move_right(&self, count: u16, screen_manager: &Arc) { screen_manager.write_string(format!(csi!("{}C"), count)); } - fn move_down(&self, count: u16, screen_manager: &Arc) { + fn move_down(&self, count: u16, screen_manager: &Arc) { screen_manager.write_string(format!(csi!("{}B"), count)); } - fn move_left(&self, count: u16, screen_manager: &Arc) { + fn move_left(&self, count: u16, screen_manager: &Arc) { screen_manager.write_string(format!(csi!("{}D"), count)); } - fn save_position(&self, screen_manager: &Arc) { + fn save_position(&self, screen_manager: &Arc) { screen_manager.write_str(csi!("s")); } - fn reset_position(&self, screen_manager: &Arc) { + fn reset_position(&self, screen_manager: &Arc) { screen_manager.write_str(csi!("u")); } - fn hide(&self, screen_manager: &Arc) { + fn hide(&self, screen_manager: &Arc) { screen_manager.write_str(csi!("?25l")); } - fn show(&self, screen_manager: &Arc) { + fn show(&self, screen_manager: &Arc) { screen_manager.write_str(csi!("?25h")); } - fn blink(&self, blink: bool, screen_manager: &Arc) { + fn blink(&self, blink: bool, screen_manager: &Arc) { if blink { screen_manager.write_str(csi!("?12h")); } else { diff --git a/src/modules/cursor/cursor.rs b/src/modules/cursor/cursor.rs index 9b3a01b..457495b 100644 --- a/src/modules/cursor/cursor.rs +++ b/src/modules/cursor/cursor.rs @@ -4,12 +4,11 @@ //! Note that positions of the cursor are 0 -based witch means that the coordinates (cells) starts counting from 0 use super::*; +use Screen; use std::fmt::Display; use std::io::Write; -use std::sync::Arc; - /// Struct that stores an specific platform implementation for cursor related actions. /// /// Check `/examples/cursor` in the library for more specific examples. @@ -31,13 +30,13 @@ use std::sync::Arc; /// cursor.move_left(2); /// ``` pub struct TerminalCursor<'stdout> { - screen: &'stdout Arc, + screen: &'stdout Arc, terminal_cursor: Box, } impl<'stdout> TerminalCursor<'stdout> { /// Create new cursor instance whereon cursor related actions can be performed. - pub fn new(screen: &'stdout Arc) -> TerminalCursor<'stdout> { + pub fn new(screen: &'stdout Arc) -> TerminalCursor<'stdout> { #[cfg(target_os = "windows")] let cursor = functions::get_module::>(WinApiCursor::new(), AnsiCursor::new()) @@ -203,6 +202,6 @@ impl<'stdout> TerminalCursor<'stdout> { /// Get an TerminalCursor implementation whereon cursor related actions can be performed. /// Pass the reference to any screen you want this type to perform actions on. -pub fn cursor<'stdout>(screen_manager: &'stdout Screen) -> TerminalCursor<'stdout> { - TerminalCursor::new(&screen_manager.stdout) +pub fn cursor<'stdout>(stdout: &'stdout Screen) -> TerminalCursor<'stdout> { + TerminalCursor::new(&stdout.stdout) } diff --git a/src/modules/cursor/mod.rs b/src/modules/cursor/mod.rs index b166584..9fb526b 100644 --- a/src/modules/cursor/mod.rs +++ b/src/modules/cursor/mod.rs @@ -12,8 +12,9 @@ use self::ansi_cursor::AnsiCursor; use self::winapi_cursor::WinApiCursor; pub use self::cursor::{cursor, TerminalCursor}; -use super::{functions, Stdout, Screen}; +use super::functions; +use TerminalOutput; use std::sync::Arc; ///! This trait defines the actions that can be preformed with the terminal cursor. @@ -26,25 +27,25 @@ use std::sync::Arc; ///! so that cursor related actions can be preformed on both unix and windows systems. trait ITerminalCursor { /// Goto some location (x,y) in the context. - fn goto(&self, x: u16, y: u16, screen_manager: &Arc); + fn goto(&self, x: u16, y: u16, screen_manager: &Arc); /// Get the location (x,y) of the current cusror in the context - fn pos(&self, screen_manager: &Arc) -> (u16, u16); + fn pos(&self, screen_manager: &Arc) -> (u16, u16); /// Move cursor n times up - fn move_up(&self, count: u16, screen_manager: &Arc); + fn move_up(&self, count: u16, screen_manager: &Arc); /// Move the cursor `n` times to the right. - fn move_right(&self, count: u16, screen_manager: &Arc); + fn move_right(&self, count: u16, screen_manager: &Arc); /// Move the cursor `n` times down. - fn move_down(&self, count: u16, screen_manager: &Arc); + fn move_down(&self, count: u16, screen_manager: &Arc); /// Move the cursor `n` times left. - fn move_left(&self, count: u16, screen_manager: &Arc); + fn move_left(&self, count: u16, screen_manager: &Arc); /// Save cursor position so that its saved position can be recalled later. Note that this position is stored program based not per instance of the cursor struct. - fn save_position(&self, screen_manager: &Arc); + fn save_position(&self, screen_manager: &Arc); /// Return to saved cursor position - fn reset_position(&self, screen_manager: &Arc); + fn reset_position(&self, screen_manager: &Arc); /// Hide the terminal cursor. - fn hide(&self, screen_manager: &Arc); + fn hide(&self, screen_manager: &Arc); /// Show the terminal cursor - fn show(&self, screen_manager: &Arc); + fn show(&self, screen_manager: &Arc); /// Enable or disable the blinking of the cursor. - fn blink(&self, blink: bool, screen_manager: &Arc); + fn blink(&self, blink: bool, screen_manager: &Arc); } diff --git a/src/modules/cursor/winapi_cursor.rs b/src/modules/cursor/winapi_cursor.rs index 2a23e21..8dbcf80 100644 --- a/src/modules/cursor/winapi_cursor.rs +++ b/src/modules/cursor/winapi_cursor.rs @@ -2,13 +2,10 @@ //! This module is used for windows terminals that do not support ANSI escape codes. //! Note that the cursor position is 0 based. This means that we start counting at 0 when setting the cursor position ect. -use super::super::super::write::{Stdout, WinApiStdout}; -use super::ITerminalCursor; - +use super::super::super::output::WinApiOutput; use kernel::windows_kernel::{cursor, kernel}; -use std::sync::Arc; - +use super::*; /// This struct is an windows implementation for cursor related actions. pub struct WinApiCursor; @@ -20,48 +17,48 @@ impl WinApiCursor { } impl ITerminalCursor for WinApiCursor { - fn goto(&self, x: u16, y: u16, screen_manager: &Arc) { + fn goto(&self, x: u16, y: u16, screen_manager: &Arc) { cursor::set_console_cursor_position(x as i16, y as i16, screen_manager); } - fn pos(&self, screen_manager: &Arc) -> (u16, u16) { + fn pos(&self, screen_manager: &Arc) -> (u16, u16) { cursor::pos(screen_manager) } - fn move_up(&self, count: u16, screen_manager: &Arc) { + fn move_up(&self, count: u16, screen_manager: &Arc) { let (xpos, ypos) = self.pos(screen_manager); self.goto(xpos, ypos - count, screen_manager); } - fn move_right(&self, count: u16, screen_manager: &Arc) { + fn move_right(&self, count: u16, screen_manager: &Arc) { let (xpos, ypos) = self.pos(screen_manager); self.goto(xpos + count, ypos, screen_manager); } - fn move_down(&self, count: u16, screen_manager: &Arc) { + fn move_down(&self, count: u16, screen_manager: &Arc) { let (xpos, ypos) = self.pos(screen_manager); self.goto(xpos, ypos + count, screen_manager); } - fn move_left(&self, count: u16, screen_manager: &Arc) { + fn move_left(&self, count: u16, screen_manager: &Arc) { let (xpos, ypos) = self.pos(screen_manager); self.goto(xpos - count, ypos, screen_manager); } - fn save_position(&self, screen_manager: &Arc) { + fn save_position(&self, screen_manager: &Arc) { cursor::save_cursor_pos(screen_manager); } - fn reset_position(&self, screen_manager: &Arc) { + fn reset_position(&self, screen_manager: &Arc) { cursor::reset_to_saved_position(screen_manager); } - fn hide(&self, screen_manager: &Arc) { + fn hide(&self, screen_manager: &Arc) { cursor::cursor_visibility(false, screen_manager); } - fn show(&self, screen_manager: &Arc) { + fn show(&self, screen_manager: &Arc) { cursor::cursor_visibility(true, screen_manager); } - fn blink(&self, blink: bool, screen_manager: &Arc) {} + fn blink(&self, blink: bool, screen_manager: &Arc) {} } diff --git a/src/modules/input/input.rs b/src/modules/input/input.rs index 31dc3b2..9bf0281 100644 --- a/src/modules/input/input.rs +++ b/src/modules/input/input.rs @@ -1,9 +1,8 @@ //! With this module you can perform actions that are input related. //! Like reading a line, reading a character and reading asynchronously. -use std::io; -use std::sync::Arc; use super::*; +use Screen; /// Struct that stores an specific platform implementation for input related actions. /// @@ -22,12 +21,12 @@ use super::*; /// ``` pub struct TerminalInput<'stdout> { terminal_input: Box, - stdout: &'stdout Arc, + stdout: &'stdout Arc, } impl<'stdout> TerminalInput<'stdout> { /// Create new instance of TerminalInput whereon input related actions could be preformed. - pub fn new(stdout: &'stdout Arc) -> TerminalInput<'stdout> { + pub fn new(stdout: &'stdout Arc) -> TerminalInput<'stdout> { #[cfg(target_os = "windows")] let input = Box::from(WindowsInput::new()); diff --git a/src/modules/input/mod.rs b/src/modules/input/mod.rs index 76a5576..0c64f23 100644 --- a/src/modules/input/mod.rs +++ b/src/modules/input/mod.rs @@ -14,11 +14,11 @@ use self::unix_input::UnixInput; use self::windows_input::WindowsInput; pub use self::input::{input, TerminalInput}; -use super::Stdout; use std::io::{self, Read, Error, ErrorKind}; use std::sync::{mpsc, Arc}; -use Screen; + +use TerminalOutput; /// This trait defines the actions that can be preformed with the terminal color. /// This trait can be implemented so that an concrete implementation of the ITerminalColor can forfill @@ -30,13 +30,13 @@ use Screen; /// Unix is using the tty and windows is using libc C functions to read the input. trait ITerminalInput { /// Read one line from the user input - fn read_line(&self, screen_manger: &Arc) -> io::Result; + fn read_line(&self, screen_manger: &Arc) -> io::Result; /// Read one character from the user input - fn read_char(&self, screen_manger: &Arc) -> io::Result; + fn read_char(&self, screen_manger: &Arc) -> io::Result; /// Read the input asynchronously from the user. - fn read_async(&self, screen_manger: &Arc) -> AsyncReader; + fn read_async(&self, screen_manger: &Arc) -> AsyncReader; /// Read the input asynchronously until a certain character is hit. - fn read_until_async(&self, delimiter: u8, screen_manger: &Arc) -> AsyncReader; + fn read_until_async(&self, delimiter: u8, screen_manger: &Arc) -> AsyncReader; } /// This is a wrapper for reading from the input asynchronously. diff --git a/src/modules/input/unix_input.rs b/src/modules/input/unix_input.rs index 1f30eba..c1bb4ee 100644 --- a/src/modules/input/unix_input.rs +++ b/src/modules/input/unix_input.rs @@ -1,13 +1,11 @@ //! This is an UNIX specific implementation for input related action. -use std::char; -use std::io::{self, Read, Write}; -use std::sync::{mpsc, Arc}; -use std::thread; - -use super::{AsyncReader, ITerminalInput, Stdout}; +use super::*; use kernel::unix_kernel::terminal::{get_tty, read_char}; +use std::char; +use std::thread; + pub struct UnixInput; impl UnixInput { @@ -17,7 +15,7 @@ impl UnixInput { } impl ITerminalInput for UnixInput { - fn read_line(&self, screen_manger: &Arc) -> 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(); @@ -25,11 +23,11 @@ impl ITerminalInput for UnixInput { Ok(rv) } - fn read_char(&self, screen_manger: &Arc) -> io::Result { + fn read_char(&self, screen_manger: &Arc) -> io::Result { read_char() } - fn read_async(&self, screen_manger: &Arc) -> AsyncReader { + fn read_async(&self, screen_manger: &Arc) -> AsyncReader { let (send, recv) = mpsc::channel(); thread::spawn(move || { @@ -43,7 +41,7 @@ impl ITerminalInput for UnixInput { AsyncReader { recv: recv } } - fn read_until_async(&self, delimiter: u8, screen_manger: &Arc) -> AsyncReader { + fn read_until_async(&self, delimiter: u8, screen_manger: &Arc) -> AsyncReader { let (send, recv) = mpsc::channel(); thread::spawn(move || { diff --git a/src/modules/input/windows_input.rs b/src/modules/input/windows_input.rs index fa57814..1a57780 100644 --- a/src/modules/input/windows_input.rs +++ b/src/modules/input/windows_input.rs @@ -1,15 +1,13 @@ //! This is an WINDOWS specific implementation for input related action. -use std::char; -use std::io::{self, Write}; -use std::sync::{mpsc, Arc}; -use std::thread; - -use super::{AsyncReader, ITerminalInput, Stdout}; +use super::*; use winapi::um::winnt::INT; use winapi::um::winuser; +use std::char; +use std::thread; + pub struct WindowsInput; impl WindowsInput { @@ -19,7 +17,7 @@ impl WindowsInput { } impl ITerminalInput for WindowsInput { - fn read_line(&self, screen_manger: &Arc) -> io::Result { + fn read_line(&self, screen_manger: &Arc) -> io::Result { let mut chars: Vec = Vec::new(); loop { @@ -52,7 +50,7 @@ impl ITerminalInput for WindowsInput { return Ok(chars.into_iter().collect()); } - fn read_char(&self, screen_manger: &Arc) -> io::Result { + fn read_char(&self, screen_manger: &Arc) -> io::Result { let is_raw_screen = screen_manger.is_in_raw_mode; // _getwch is without echo and _getwche is with echo @@ -83,7 +81,7 @@ impl ITerminalInput for WindowsInput { } } - fn read_async(&self, screen_manger: &Arc) -> AsyncReader { + fn read_async(&self, screen_manger: &Arc) -> AsyncReader { let (tx, rx) = mpsc::channel(); let is_raw_screen = screen_manger.is_in_raw_mode; @@ -115,7 +113,7 @@ impl ITerminalInput for WindowsInput { AsyncReader { recv: rx } } - fn read_until_async(&self, delimiter: u8, screen_manger: &Arc) -> AsyncReader { + fn read_until_async(&self, delimiter: u8, screen_manger: &Arc) -> AsyncReader { let (tx, rx) = mpsc::channel(); let is_raw_screen = screen_manger.is_in_raw_mode; diff --git a/src/modules/mod.rs b/src/modules/mod.rs index 74ee5c6..1cd5b96 100644 --- a/src/modules/mod.rs +++ b/src/modules/mod.rs @@ -1,12 +1,9 @@ pub mod cursor; pub mod input; -pub mod write; +pub mod output; pub mod style; -//pub mod handle; - pub mod terminal; +pub mod terminal; use super::common::commands; use super::common::functions; use super::common::traits; -pub use self::write::{Stdout, IStdout}; -pub use super::common::Screen; \ No newline at end of file diff --git a/src/modules/output/output.rs b/src/modules/output/output.rs index 1a84932..98297e0 100644 --- a/src/modules/output/output.rs +++ b/src/modules/output/output.rs @@ -44,8 +44,8 @@ impl TerminalOutput { pub fn new(is_in_raw_mode: bool) -> Self { #[cfg(target_os = "windows")] let stdout: Box = functions::get_module::>( - Box::from(WinApiStdout::new()), - Box::from(AnsiStdout::new()), + Box::from(WinApiOutput::new()), + Box::from(AnsiOutput::new()), ).unwrap(); #[cfg(not(target_os = "windows"))] @@ -88,8 +88,8 @@ impl Default for TerminalOutput fn default() -> Self { #[cfg(target_os = "windows")] let stdout = functions::get_module::>( - Box::from(WinApiStdout::new()), - Box::from(AnsiStdout::new()), + Box::from(WinApiOutput::new()), + Box::from(AnsiOutput::new()), ).unwrap(); #[cfg(not(target_os = "windows"))] diff --git a/src/modules/style/ansi_color.rs b/src/modules/style/ansi_color.rs index 22dcb94..294bf30 100644 --- a/src/modules/style/ansi_color.rs +++ b/src/modules/style/ansi_color.rs @@ -1,7 +1,7 @@ //! This is an ANSI specific implementation for styling related action. //! This module is used for windows 10 terminals and unix terminals by default. -use std::sync::Arc; -use super::{Color, ColorType, ITerminalColor, Stdout}; + +use super::*; /// This struct is an ANSI escape code implementation for color related actions. pub struct AnsiColor; @@ -13,21 +13,21 @@ impl AnsiColor { } impl ITerminalColor for AnsiColor { - fn set_fg(&self, fg_color: Color, stdout: &Arc) { + fn set_fg(&self, fg_color: Color, stdout: &Arc) { stdout.write_string(format!( csi!("{}m"), self.color_value(fg_color, ColorType::Foreground) )); } - fn set_bg(&self, bg_color: Color, stdout: &Arc) { + fn set_bg(&self, bg_color: Color, stdout: &Arc) { stdout.write_string(format!( csi!("{}m"), self.color_value(bg_color, ColorType::Background) )); } - fn reset(&self, stdout: &Arc) { + fn reset(&self, stdout: &Arc) { stdout.write_str(csi!("0m")); } diff --git a/src/modules/style/color.rs b/src/modules/style/color.rs index 972d37e..4ed2b46 100644 --- a/src/modules/style/color.rs +++ b/src/modules/style/color.rs @@ -25,12 +25,12 @@ use Screen; /// ``` pub struct TerminalColor<'stdout> { color: Box, - stdout: &'stdout Arc, + stdout: &'stdout Arc, } impl<'stdout> TerminalColor<'stdout> { /// Create new instance whereon color related actions can be performed. - pub fn new(stdout: &'stdout Arc) -> TerminalColor<'stdout> { + pub fn new(stdout: &'stdout Arc) -> TerminalColor<'stdout> { #[cfg(target_os = "windows")] let color = functions::get_module::>( Box::from(WinApiColor::new()), diff --git a/src/modules/style/mod.rs b/src/modules/style/mod.rs index bc5086a..30b0cef 100644 --- a/src/modules/style/mod.rs +++ b/src/modules/style/mod.rs @@ -20,7 +20,8 @@ use std::fmt::Display; pub use self::color::{TerminalColor, color}; pub use self::objectstyle::ObjectStyle; pub use self::styledobject::StyledObject; -use super::{functions, Stdout}; +use super::functions; +use TerminalOutput; /// This trait defines the actions that can be preformed with the terminal color. /// This trait can be implemented so that an concrete implementation of the ITerminalColor can forfill @@ -32,11 +33,11 @@ use super::{functions, Stdout}; /// so that color related actions can be preformed on both unix and windows systems. trait ITerminalColor { /// Set the foreground color to the given color. - fn set_fg(&self, fg_color: Color, stdout: &Arc); + fn set_fg(&self, fg_color: Color, stdout: &Arc); /// Set the background color to the given color. - fn set_bg(&self, fg_color: Color, stdout: &Arc); + fn set_bg(&self, fg_color: Color, stdout: &Arc); /// Reset the terminal color to default. - fn reset(&self, stdout: &Arc); + fn reset(&self, stdout: &Arc); /// Gets an value that represents an color from the given `Color` and `ColorType`. fn color_value(&self, color: Color, color_type: ColorType) -> String; } diff --git a/src/modules/style/objectstyle.rs b/src/modules/style/objectstyle.rs index a329a82..0b676f3 100644 --- a/src/modules/style/objectstyle.rs +++ b/src/modules/style/objectstyle.rs @@ -1,6 +1,6 @@ //! This module contains the `object style` that can be applied to an `styled object`. -use super::{Color, Stdout, StyledObject}; +use super::{Color, TerminalOutput, StyledObject}; use std::fmt::Display; use std::sync::Arc; diff --git a/src/modules/style/styledobject.rs b/src/modules/style/styledobject.rs index aec3d39..f3d2716 100644 --- a/src/modules/style/styledobject.rs +++ b/src/modules/style/styledobject.rs @@ -1,6 +1,6 @@ //! This module contains the logic to style an object that contains some state witch can be styled. -use super::{Color, ObjectStyle, Stdout}; +use super::{Color, ObjectStyle, TerminalOutput}; use Screen; use std::fmt::{self, Display}; @@ -10,7 +10,7 @@ use std::io::Write; use super::Attribute; #[cfg(windows)] -use super::super::super::write::WinApiStdout; +use super::super::super::output::WinApiOutput; /// Struct that contains both the style and the content wits can be styled. pub struct StyledObject { diff --git a/src/modules/style/winapi_color.rs b/src/modules/style/winapi_color.rs index 8b4d936..0ea66da 100644 --- a/src/modules/style/winapi_color.rs +++ b/src/modules/style/winapi_color.rs @@ -3,13 +3,10 @@ //! //! Windows versions lower then windows 10 are not supporting ANSI codes. Those versions will use this implementation instead. -use super::super::super::write::WinApiStdout; -use super::{Color, ColorType, ITerminalColor, Stdout}; +use super::*; use kernel::windows_kernel::{csbi, kernel}; use winapi::um::wincon; -use std::sync::Arc; - /// This struct is an windows implementation for color related actions. pub struct WinApiColor { @@ -23,7 +20,7 @@ impl WinApiColor { } impl ITerminalColor for WinApiColor { - fn set_fg(&self, fg_color: Color, stdout: &Arc) { + fn set_fg(&self, fg_color: Color, stdout: &Arc) { let color_value = &self.color_value(fg_color, ColorType::Foreground); let csbi = csbi::get_csbi(stdout).unwrap(); @@ -44,7 +41,7 @@ impl ITerminalColor for WinApiColor { kernel::set_console_text_attribute(color, stdout); } - fn set_bg(&self, bg_color: Color, stdout: &Arc) { + fn set_bg(&self, bg_color: Color, stdout: &Arc) { let color_value = &self.color_value(bg_color, ColorType::Background); let (csbi, handle) = csbi::get_csbi_and_handle(stdout).unwrap(); @@ -65,7 +62,7 @@ impl ITerminalColor for WinApiColor { kernel::set_console_text_attribute(color, stdout); } - fn reset(&self, stdout: &Arc) { + fn reset(&self, stdout: &Arc) { kernel::set_console_text_attribute(self.original_color, stdout); } diff --git a/src/modules/terminal/ansi_terminal.rs b/src/modules/terminal/ansi_terminal.rs index 51db870..d173176 100644 --- a/src/modules/terminal/ansi_terminal.rs +++ b/src/modules/terminal/ansi_terminal.rs @@ -1,7 +1,7 @@ //! This is an `ANSI escape code` specific implementation for terminal related action. //! This module is used for windows 10 terminals and unix terminals by default. -use super::super::cursor::cursor; +use cursor::cursor; use super::*; /// This struct is an ansi escape code implementation for terminal related actions. @@ -14,45 +14,45 @@ impl AnsiTerminal { } impl ITerminal for AnsiTerminal { - fn clear(&self, clear_type: ClearType, screen_manager: &Arc) { + fn clear(&self, clear_type: ClearType, stdout: &Arc) { match clear_type { ClearType::All => { - screen_manager.write_str(csi!("2J")); + stdout.write_str(csi!("2J")); } ClearType::FromCursorDown => { - screen_manager.write_str(csi!("J")); + stdout.write_str(csi!("J")); } ClearType::FromCursorUp => { - screen_manager.write_str(csi!("1J")); + stdout.write_str(csi!("1J")); } ClearType::CurrentLine => { - screen_manager.write_str(csi!("2K")); + stdout.write_str(csi!("2K")); } ClearType::UntilNewLine => { - screen_manager.write_str(csi!("K")); + stdout.write_str(csi!("K")); } }; } - fn terminal_size(&self, screen_manager: &Arc) -> (u16, u16) { + fn terminal_size(&self, stdout: &Arc) -> (u16, u16) { functions::get_terminal_size() } - fn scroll_up(&self, count: i16, screen_manager: &Arc) { - screen_manager.write_string(format!(csi!("{}S"), count)); + fn scroll_up(&self, count: i16, stdout: &Arc) { + stdout.write_string(format!(csi!("{}S"), count)); } - fn scroll_down(&self, count: i16, screen_manager: &Arc) { - screen_manager.write_string(format!(csi!("{}T"), count)); + fn scroll_down(&self, count: i16, stdout: &Arc) { + stdout.write_string(format!(csi!("{}T"), count)); } - fn set_size(&self, width: i16, height: i16, screen_manager: &Arc) { - screen_manager.write_string(format!(csi!("8;{};{}t"), width, height)); + fn set_size(&self, width: i16, height: i16, stdout: &Arc) { + stdout.write_string(format!(csi!("8;{};{}t"), width, height)); } - fn exit(&self,screen_manager: &Arc) { + fn exit(&self,stdout: &Arc) { // drop the screen with the current stdout. This will make sure when in raw mode this will be disabled first. - let mut screen = Screen::from(screen_manager.clone()); + let mut screen = Screen::from(stdout.clone()); drop(screen); functions::exit_terminal(); } diff --git a/src/modules/terminal/mod.rs b/src/modules/terminal/mod.rs index c53a28e..f8d71a7 100644 --- a/src/modules/terminal/mod.rs +++ b/src/modules/terminal/mod.rs @@ -11,9 +11,10 @@ use self::ansi_terminal::AnsiTerminal; use self::winapi_terminal::WinApiTerminal; pub use self::terminal::{terminal, Terminal}; -use super::{functions, Stdout}; + use std::sync::Arc; -use Screen; +use super::functions; +use {Screen, TerminalOutput}; /// Enum that specifies a way of clearing. pub enum ClearType { @@ -34,15 +35,15 @@ pub enum ClearType { /// so that color related actions can be preformed on both unix and windows systems. trait ITerminal { /// Clear the current cursor by specifying the clear type - fn clear(&self, clear_type: ClearType, screen_manager: &Arc); + fn clear(&self, clear_type: ClearType, stdout: &Arc); /// Get the terminal size (x,y) - fn terminal_size(&self, screen_manager: &Arc) -> (u16, u16); + fn terminal_size(&self, stdout: &Arc) -> (u16, u16); /// Scroll `n` lines up in the current terminal. - fn scroll_up(&self, count: i16, screen_manager: &Arc); + fn scroll_up(&self, count: i16, stdout: &Arc); /// Scroll `n` lines down in the current terminal. - fn scroll_down(&self, count: i16, screen_manager: &Arc); + fn scroll_down(&self, count: i16, stdout: &Arc); /// Resize terminal to the given width and height. - fn set_size(&self, width: i16, height: i16, screen_manager: &Arc); + fn set_size(&self, width: i16, height: i16, stdout: &Arc); /// Close the current terminal - fn exit(&self,screen_manager: &Arc); + fn exit(&self,stdout: &Arc); } diff --git a/src/modules/terminal/terminal.rs b/src/modules/terminal/terminal.rs index 73154b1..09984cb 100644 --- a/src/modules/terminal/terminal.rs +++ b/src/modules/terminal/terminal.rs @@ -23,12 +23,12 @@ use std::io::Write; /// ``` pub struct Terminal<'stdout> { terminal: Box, - screen: &'stdout Arc, + screen: &'stdout Arc, } impl<'stdout> Terminal<'stdout> { /// Create new terminal instance whereon terminal related actions can be performed. - pub fn new(screen: &'stdout Arc) -> Terminal<'stdout> { + pub fn new(screen: &'stdout Arc) -> Terminal<'stdout> { #[cfg(target_os = "windows")] let terminal = functions::get_module::>( Box::new(WinApiTerminal::new()), diff --git a/src/modules/terminal/winapi_terminal.rs b/src/modules/terminal/winapi_terminal.rs index 714242c..4192f0a 100644 --- a/src/modules/terminal/winapi_terminal.rs +++ b/src/modules/terminal/winapi_terminal.rs @@ -3,8 +3,9 @@ //! //! Windows versions lower then windows 10 are not supporting ANSI codes. Those versions will use this implementation instead. -use super::super::super::cursor::TerminalCursor; +use cursor::TerminalCursor; use super::*; + use kernel::windows_kernel::{csbi, kernel, terminal, writing}; use winapi::um::wincon::{CONSOLE_SCREEN_BUFFER_INFO, COORD, SMALL_RECT}; @@ -18,27 +19,27 @@ impl WinApiTerminal { } impl ITerminal for WinApiTerminal { - fn clear(&self, clear_type: ClearType, screen_manager: &Arc) { - let csbi = csbi::get_csbi(screen_manager).unwrap(); - let pos = TerminalCursor::new(screen_manager).pos(); + fn clear(&self, clear_type: ClearType, stdout: &Arc) { + let csbi = csbi::get_csbi(stdout).unwrap(); + let pos = TerminalCursor::new(stdout).pos(); match clear_type { ClearType::All => { - clear_entire_screen(csbi, screen_manager); + clear_entire_screen(csbi, stdout); } - ClearType::FromCursorDown => clear_after_cursor(pos, csbi, screen_manager), - ClearType::FromCursorUp => clear_before_cursor(pos, csbi, screen_manager), - ClearType::CurrentLine => clear_current_line(pos, csbi, screen_manager), - ClearType::UntilNewLine => clear_until_line(pos, csbi, screen_manager), + ClearType::FromCursorDown => clear_after_cursor(pos, csbi, stdout), + ClearType::FromCursorUp => clear_before_cursor(pos, csbi, stdout), + ClearType::CurrentLine => clear_current_line(pos, csbi, stdout), + ClearType::UntilNewLine => clear_until_line(pos, csbi, stdout), }; } - fn terminal_size(&self, screen_manager: &Arc) -> (u16, u16) { + fn terminal_size(&self, stdout: &Arc) -> (u16, u16) { terminal::terminal_size() } - fn scroll_up(&self, count: i16, screen_manager: &Arc) { - let csbi = csbi::get_csbi(&screen_manager).unwrap(); + fn scroll_up(&self, count: i16, stdout: &Arc) { + let csbi = csbi::get_csbi(&stdout).unwrap(); // Set srctWindow to the current window size and location. let mut srct_window = csbi.srWindow; @@ -48,15 +49,15 @@ impl ITerminal for WinApiTerminal { srct_window.Top -= count; // move top down srct_window.Bottom = count; // move bottom down - let success = kernel::set_console_info(false, &mut srct_window, &screen_manager); + let success = kernel::set_console_info(false, &mut srct_window, &stdout); if success { panic!("Something went wrong when scrolling down"); } } } - fn scroll_down(&self, count: i16, screen_manager: &Arc) { - let csbi = csbi::get_csbi(&screen_manager).unwrap(); + fn scroll_down(&self, count: i16, stdout: &Arc) { + let csbi = csbi::get_csbi(&stdout).unwrap(); // Set srctWindow to the current window size and location. let mut srct_window = csbi.srWindow; @@ -68,7 +69,7 @@ impl ITerminal for WinApiTerminal { srct_window.Top += count; // move top down srct_window.Bottom += count; // move bottom down - let success = kernel::set_console_info(true, &mut srct_window, &screen_manager); + let success = kernel::set_console_info(true, &mut srct_window, &stdout); if success { panic!("Something went wrong when scrolling down"); } @@ -76,7 +77,7 @@ impl ITerminal for WinApiTerminal { } /// Set the current terminal size - fn set_size(&self, width: i16, height: i16, screen_manager: &Arc) { + fn set_size(&self, width: i16, height: i16, stdout: &Arc) { if width <= 0 { panic!("Cannot set the terminal width lower than 1"); } @@ -86,7 +87,7 @@ impl ITerminal for WinApiTerminal { } // Get the position of the current console window - let csbi = csbi::get_csbi(&screen_manager).unwrap(); + let csbi = csbi::get_csbi(&stdout).unwrap(); let mut success = false; // If the buffer is smaller than this new window size, resize the @@ -115,7 +116,7 @@ impl ITerminal for WinApiTerminal { } if resize_buffer { - success = csbi::set_console_screen_buffer_size(size, &screen_manager); + success = csbi::set_console_screen_buffer_size(size, &stdout); if !success { panic!("Something went wrong when setting screen buffer size."); @@ -127,12 +128,12 @@ impl ITerminal for WinApiTerminal { fsr_window.Bottom = fsr_window.Top + height; fsr_window.Right = fsr_window.Left + width; - let success = kernel::set_console_info(true, &fsr_window, &screen_manager); + let success = kernel::set_console_info(true, &fsr_window, &stdout); if success { // If we resized the buffer, un-resize it. if resize_buffer { - csbi::set_console_screen_buffer_size(csbi.dwSize, &screen_manager); + csbi::set_console_screen_buffer_size(csbi.dwSize, &stdout); } let bounds = kernel::get_largest_console_window_size(); @@ -152,9 +153,9 @@ impl ITerminal for WinApiTerminal { } } - fn exit(&self, screen_manager: &Arc) { + fn exit(&self, stdout: &Arc) { // drop the screen with the current stdout. This will make sure when in raw mode this will be disabled first. - let mut screen = Screen::from(screen_manager.clone()); + let mut screen = Screen::from(stdout.clone()); drop(screen); functions::exit_terminal(); } @@ -163,7 +164,7 @@ impl ITerminal for WinApiTerminal { pub fn clear_after_cursor( pos: (u16, u16), csbi: CONSOLE_SCREEN_BUFFER_INFO, - screen_manager: &Arc, + stdout: &Arc, ) { let (mut x, mut y) = pos; @@ -181,13 +182,13 @@ pub fn clear_after_cursor( // get sum cells before cursor let cells_to_write = csbi.dwSize.X as u32 * csbi.dwSize.Y as u32; - clear(start_location, cells_to_write, screen_manager); + clear(start_location, cells_to_write, stdout); } pub fn clear_before_cursor( pos: (u16, u16), csbi: CONSOLE_SCREEN_BUFFER_INFO, - screen_manager: &Arc, + stdout: &Arc, ) { let (xpos, ypos) = pos; @@ -204,10 +205,10 @@ pub fn clear_before_cursor( // get sum cells before cursor let cells_to_write = (csbi.dwSize.X as u32 * ypos as u32) + (xpos as u32 + 1); - clear(start_location, cells_to_write, screen_manager); + clear(start_location, cells_to_write, stdout); } -pub fn clear_entire_screen(csbi: CONSOLE_SCREEN_BUFFER_INFO, screen_manager: &Arc) { +pub fn clear_entire_screen(csbi: CONSOLE_SCREEN_BUFFER_INFO, stdout: &Arc) { // position x at start let x = 0; // position y at start @@ -222,16 +223,16 @@ pub fn clear_entire_screen(csbi: CONSOLE_SCREEN_BUFFER_INFO, screen_manager: &Ar let cells_to_write = csbi.dwSize.X as u32 * csbi.dwSize.Y as u32; - clear(start_location, cells_to_write, &screen_manager); + clear(start_location, cells_to_write, &stdout); // put the cursor back at (0, 0) - TerminalCursor::new(screen_manager).goto(0, 0); + TerminalCursor::new(stdout).goto(0, 0); } pub fn clear_current_line( pos: (u16, u16), csbi: CONSOLE_SCREEN_BUFFER_INFO, - screen_manager: &Arc, + stdout: &Arc, ) { // position x at start let x = 0; @@ -247,16 +248,16 @@ pub fn clear_current_line( let cells_to_write = csbi.dwSize.X as u32; - clear(start_location, cells_to_write, screen_manager); + clear(start_location, cells_to_write, stdout); // put the cursor back at 1 cell on current row - TerminalCursor::new(screen_manager).goto(0, y); + TerminalCursor::new(stdout).goto(0, y); } pub fn clear_until_line( pos: (u16, u16), csbi: CONSOLE_SCREEN_BUFFER_INFO, - screen_manager: &Arc, + stdout: &Arc, ) { let (x, y) = pos; @@ -268,13 +269,13 @@ pub fn clear_until_line( // get sum cells before cursor let cells_to_write = (csbi.dwSize.X - x as i16) as u32; - clear(start_location, cells_to_write, &screen_manager); + clear(start_location, cells_to_write, &stdout); // put the cursor back at original cursor position - TerminalCursor::new(screen_manager).goto(x, y); + TerminalCursor::new(stdout).goto(x, y); } -fn clear(start_loaction: COORD, cells_to_write: u32, screen_manager: &Arc) { +fn clear(start_loaction: COORD, cells_to_write: u32, stdout: &Arc) { let mut cells_written = 0; let mut success = false; @@ -282,7 +283,7 @@ fn clear(start_loaction: COORD, cells_to_write: u32, screen_manager: &Arc