This commit is contained in:
TimonPost 2018-08-19 23:14:45 +02:00
parent 685bc5e961
commit f64a405236
40 changed files with 252 additions and 272 deletions

View File

@ -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.

View File

@ -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(())
}

View File

@ -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::<WinApiStdout>()
.downcast_mut::<WinApiOutput>()
{
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);

View File

@ -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>
stdout: Arc<TerminalOutput>
}
impl<'crossterm> Crossterm {

View File

@ -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<Stdout>) -> (u16, u16) {
#[cfg(unix)]
return pos(stdout);
#[cfg(windows)]
pub fn get_cursor_position(stdout: &Arc<TerminalOutput>) -> (u16, u16) {
return pos(stdout);
}
/// exit the current terminal.
pub fn exit_terminal() {
#[cfg(unix)]
exit();
#[cfg(windows)]
exit();
}

View File

@ -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};

View File

@ -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<AlternateScreen> {
pub fn to_alternate_screen(stdout: TerminalOutput) -> io::Result<AlternateScreen> {
#[cfg(target_os = "windows")]
let command = functions::get_module::<Box<commands::IAlternateScreenCommand + Send>>(
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)));
}

View File

@ -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;

View File

@ -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};

View File

@ -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<u8>,
pub stdout: Arc<Stdout>,
pub stdout: Arc<TerminalOutput>,
}
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<AlternateScreen> {
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<Stdout> for Screen
impl From<TerminalOutput> 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<Arc<Stdout>> for Screen
impl From<Arc<TerminalOutput>> for Screen
{
/// Create an screen with the given 'Arc<Stdout>'
fn from(stdout: Arc<Stdout>) -> Self {
fn from(stdout: Arc<TerminalOutput>) -> 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() };
}
}

View File

@ -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<Stdout>) -> (u16, u16) {
pub fn pos(stdout: &Arc<TerminalOutput>) -> (u16, u16) {
let mut crossterm = Crossterm::new(&Screen::default());
let input = crossterm.input();

View File

@ -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<Stdout>) -> Result<CONSOLE_SCREEN_BUFFER_INFO> {
pub fn get_csbi(screen_manager: &Arc<TerminalOutput>) -> Result<CONSOLE_SCREEN_BUFFER_INFO> {
let mut csbi = CONSOLE_SCREEN_BUFFER_INFO::empty();
let success;
@ -38,7 +38,7 @@ pub fn get_csbi(screen_manager: &Arc<Stdout>) -> Result<CONSOLE_SCREEN_BUFFER_IN
/// Get buffer info and handle of the current screen.
pub fn get_csbi_and_handle(
screen_manager: &Arc<Stdout>,
screen_manager: &Arc<TerminalOutput>,
) -> 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<CONSOLE_SCREEN_BUFFER_INFO>
}
/// Set the console screen buffer size
pub fn set_console_screen_buffer_size(size: COORD, screen_manager: &Arc<Stdout>) -> bool {
pub fn set_console_screen_buffer_size(size: COORD, screen_manager: &Arc<TerminalOutput>) -> bool {
let handle = handle::get_current_handle(screen_manager).unwrap();
unsafe {

View File

@ -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<Stdout>) {
pub fn reset_to_saved_position(stdout: &Arc<TerminalOutput>) {
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<Stdout>) {
let position = pos(screen_manager);
pub fn save_cursor_pos(stdout: &Arc<TerminalOutput>) {
let position = pos(stdout);
unsafe {
SAVED_CURSOR_POS = (position.0, position.1);
@ -35,8 +34,8 @@ pub fn save_cursor_pos(screen_manager: &Arc<Stdout>) {
}
/// get the current cursor position.
pub fn pos(screen_manager: &Arc<Stdout>) -> (u16, u16) {
let handle = handle::get_current_handle(screen_manager).unwrap();
pub fn pos(stdout: &Arc<TerminalOutput>) -> (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<Stdout>) -> (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<Stdout>) {
pub fn set_console_cursor_position(x: i16, y: i16, stdout: &Arc<TerminalOutput>) {
if x < 0 || x >= <i16>::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<Stdout>)
);
}
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<Stdout>)
}
/// change the cursor visibility.
pub fn cursor_visibility(visable: bool, screen_manager: &Arc<Stdout>) -> Result<()> {
let handle = handle::get_current_handle(screen_manager).unwrap();
pub fn cursor_visibility(visable: bool, stdout: &Arc<TerminalOutput>) -> io::Result<()> {
let handle = handle::get_current_handle(stdout).unwrap();
let cursor_info = CONSOLE_CURSOR_INFO {
dwSize: 100,

View File

@ -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<Stdout>) -> Result<HANDLE> {
pub fn get_current_handle(screen_manager: &Arc<TerminalOutput>) -> Result<HANDLE> {
let mut mutex = screen_manager;
let handle: Result<HANDLE>;
let winapi_screen_manager: &WinApiStdout = match screen_manager
let winapi_screen_manager: &WinApiOutput = match screen_manager
.as_any()
.downcast_ref::<WinApiStdout>()
.downcast_ref::<WinApiOutput>()
{
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'"))

View File

@ -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<Stdout>) -> bool {
let handle = handle::get_current_handle(screen_manager).unwrap();
pub fn set_console_text_attribute(value: u16, stdout: &Arc<TerminalOutput>) -> 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<Stdout>) -> b
}
/// Change console info.
pub fn set_console_info(absolute: bool, rect: &SMALL_RECT, screen_manager: &Arc<Stdout>) -> bool {
let handle = handle::get_current_handle(screen_manager).unwrap();
pub fn set_console_info(absolute: bool, rect: &SMALL_RECT, stdout: &Arc<TerminalOutput>) -> bool {
let handle = handle::get_current_handle(stdout).unwrap();
let absolute = match absolute {
true => 1,

View File

@ -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 {

View File

@ -11,12 +11,12 @@
//use super::kernel;
//use winapi::ctypes::c_void;
//
//pub fn read(buf: &mut [u8], screen_manager: &Rc<Mutex<ScreenManager>>) {
//pub fn read(buf: &mut [u8], stdout: &Rc<Mutex<ScreenManager>>) {
//// // Read more if the buffer is empty
//// let mut utf16: Vec<u16> = 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<Mutex<ScreenManager>>) -> ::std::io::Result<String>
//pub fn read_line(stdout: &Rc<Mutex<ScreenManager>>) -> ::std::io::Result<String>
//{
// 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);

View File

@ -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<Stdout>) -> (u16, u16) {
pub fn buffer_size(screen_manager: &Arc<TerminalOutput>) -> (u16, u16) {
let handle = handle::get_output_handle().unwrap();
if let Ok(csbi) = csbi::get_csbi_by_handle(&handle) {

View File

@ -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<Stdout>,
screen_manager: &Arc<TerminalOutput>,
) -> 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<Stdout>,
screen_manager: &Arc<TerminalOutput>,
) -> bool {
// Get the position of the current console window

View File

@ -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;

View File

@ -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<Stdout>) {
screen_manager.write_string(format!(csi!("{};{}H"), y + 1, x + 1));
fn goto(&self, x: u16, y: u16, stdout: &Arc<TerminalOutput>) {
stdout.write_string(format!(csi!("{};{}H"), y + 1, x + 1));
}
fn pos(&self, screen_manager: &Arc<Stdout>) -> (u16, u16) {
functions::get_cursor_position(screen_manager)
fn pos(&self, stdout: &Arc<TerminalOutput>) -> (u16, u16) {
functions::get_cursor_position(stdout)
}
fn move_up(&self, count: u16, screen_manager: &Arc<Stdout>) {
screen_manager.write_string(format!(csi!("{}A"), count));
fn move_up(&self, count: u16, stdout: &Arc<TerminalOutput>) {
stdout.write_string(format!(csi!("{}A"), count));
}
fn move_right(&self, count: u16, screen_manager: &Arc<Stdout>) {
fn move_right(&self, count: u16, screen_manager: &Arc<TerminalOutput>) {
screen_manager.write_string(format!(csi!("{}C"), count));
}
fn move_down(&self, count: u16, screen_manager: &Arc<Stdout>) {
fn move_down(&self, count: u16, screen_manager: &Arc<TerminalOutput>) {
screen_manager.write_string(format!(csi!("{}B"), count));
}
fn move_left(&self, count: u16, screen_manager: &Arc<Stdout>) {
fn move_left(&self, count: u16, screen_manager: &Arc<TerminalOutput>) {
screen_manager.write_string(format!(csi!("{}D"), count));
}
fn save_position(&self, screen_manager: &Arc<Stdout>) {
fn save_position(&self, screen_manager: &Arc<TerminalOutput>) {
screen_manager.write_str(csi!("s"));
}
fn reset_position(&self, screen_manager: &Arc<Stdout>) {
fn reset_position(&self, screen_manager: &Arc<TerminalOutput>) {
screen_manager.write_str(csi!("u"));
}
fn hide(&self, screen_manager: &Arc<Stdout>) {
fn hide(&self, screen_manager: &Arc<TerminalOutput>) {
screen_manager.write_str(csi!("?25l"));
}
fn show(&self, screen_manager: &Arc<Stdout>) {
fn show(&self, screen_manager: &Arc<TerminalOutput>) {
screen_manager.write_str(csi!("?25h"));
}
fn blink(&self, blink: bool, screen_manager: &Arc<Stdout>) {
fn blink(&self, blink: bool, screen_manager: &Arc<TerminalOutput>) {
if blink {
screen_manager.write_str(csi!("?12h"));
} else {

View File

@ -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<Stdout>,
screen: &'stdout Arc<TerminalOutput>,
terminal_cursor: Box<ITerminalCursor>,
}
impl<'stdout> TerminalCursor<'stdout> {
/// Create new cursor instance whereon cursor related actions can be performed.
pub fn new(screen: &'stdout Arc<Stdout>) -> TerminalCursor<'stdout> {
pub fn new(screen: &'stdout Arc<TerminalOutput>) -> TerminalCursor<'stdout> {
#[cfg(target_os = "windows")]
let cursor =
functions::get_module::<Box<ITerminalCursor>>(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)
}

View File

@ -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<Stdout>);
fn goto(&self, x: u16, y: u16, screen_manager: &Arc<TerminalOutput>);
/// Get the location (x,y) of the current cusror in the context
fn pos(&self, screen_manager: &Arc<Stdout>) -> (u16, u16);
fn pos(&self, screen_manager: &Arc<TerminalOutput>) -> (u16, u16);
/// Move cursor n times up
fn move_up(&self, count: u16, screen_manager: &Arc<Stdout>);
fn move_up(&self, count: u16, screen_manager: &Arc<TerminalOutput>);
/// Move the cursor `n` times to the right.
fn move_right(&self, count: u16, screen_manager: &Arc<Stdout>);
fn move_right(&self, count: u16, screen_manager: &Arc<TerminalOutput>);
/// Move the cursor `n` times down.
fn move_down(&self, count: u16, screen_manager: &Arc<Stdout>);
fn move_down(&self, count: u16, screen_manager: &Arc<TerminalOutput>);
/// Move the cursor `n` times left.
fn move_left(&self, count: u16, screen_manager: &Arc<Stdout>);
fn move_left(&self, count: u16, screen_manager: &Arc<TerminalOutput>);
/// 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<Stdout>);
fn save_position(&self, screen_manager: &Arc<TerminalOutput>);
/// Return to saved cursor position
fn reset_position(&self, screen_manager: &Arc<Stdout>);
fn reset_position(&self, screen_manager: &Arc<TerminalOutput>);
/// Hide the terminal cursor.
fn hide(&self, screen_manager: &Arc<Stdout>);
fn hide(&self, screen_manager: &Arc<TerminalOutput>);
/// Show the terminal cursor
fn show(&self, screen_manager: &Arc<Stdout>);
fn show(&self, screen_manager: &Arc<TerminalOutput>);
/// Enable or disable the blinking of the cursor.
fn blink(&self, blink: bool, screen_manager: &Arc<Stdout>);
fn blink(&self, blink: bool, screen_manager: &Arc<TerminalOutput>);
}

View File

@ -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<Stdout>) {
fn goto(&self, x: u16, y: u16, screen_manager: &Arc<TerminalOutput>) {
cursor::set_console_cursor_position(x as i16, y as i16, screen_manager);
}
fn pos(&self, screen_manager: &Arc<Stdout>) -> (u16, u16) {
fn pos(&self, screen_manager: &Arc<TerminalOutput>) -> (u16, u16) {
cursor::pos(screen_manager)
}
fn move_up(&self, count: u16, screen_manager: &Arc<Stdout>) {
fn move_up(&self, count: u16, screen_manager: &Arc<TerminalOutput>) {
let (xpos, ypos) = self.pos(screen_manager);
self.goto(xpos, ypos - count, screen_manager);
}
fn move_right(&self, count: u16, screen_manager: &Arc<Stdout>) {
fn move_right(&self, count: u16, screen_manager: &Arc<TerminalOutput>) {
let (xpos, ypos) = self.pos(screen_manager);
self.goto(xpos + count, ypos, screen_manager);
}
fn move_down(&self, count: u16, screen_manager: &Arc<Stdout>) {
fn move_down(&self, count: u16, screen_manager: &Arc<TerminalOutput>) {
let (xpos, ypos) = self.pos(screen_manager);
self.goto(xpos, ypos + count, screen_manager);
}
fn move_left(&self, count: u16, screen_manager: &Arc<Stdout>) {
fn move_left(&self, count: u16, screen_manager: &Arc<TerminalOutput>) {
let (xpos, ypos) = self.pos(screen_manager);
self.goto(xpos - count, ypos, screen_manager);
}
fn save_position(&self, screen_manager: &Arc<Stdout>) {
fn save_position(&self, screen_manager: &Arc<TerminalOutput>) {
cursor::save_cursor_pos(screen_manager);
}
fn reset_position(&self, screen_manager: &Arc<Stdout>) {
fn reset_position(&self, screen_manager: &Arc<TerminalOutput>) {
cursor::reset_to_saved_position(screen_manager);
}
fn hide(&self, screen_manager: &Arc<Stdout>) {
fn hide(&self, screen_manager: &Arc<TerminalOutput>) {
cursor::cursor_visibility(false, screen_manager);
}
fn show(&self, screen_manager: &Arc<Stdout>) {
fn show(&self, screen_manager: &Arc<TerminalOutput>) {
cursor::cursor_visibility(true, screen_manager);
}
fn blink(&self, blink: bool, screen_manager: &Arc<Stdout>) {}
fn blink(&self, blink: bool, screen_manager: &Arc<TerminalOutput>) {}
}

View File

@ -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<ITerminalInput>,
stdout: &'stdout Arc<Stdout>,
stdout: &'stdout Arc<TerminalOutput>,
}
impl<'stdout> TerminalInput<'stdout> {
/// Create new instance of TerminalInput whereon input related actions could be preformed.
pub fn new(stdout: &'stdout Arc<Stdout>) -> TerminalInput<'stdout> {
pub fn new(stdout: &'stdout Arc<TerminalOutput>) -> TerminalInput<'stdout> {
#[cfg(target_os = "windows")]
let input = Box::from(WindowsInput::new());

View File

@ -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<Stdout>) -> io::Result<String>;
fn read_line(&self, screen_manger: &Arc<TerminalOutput>) -> io::Result<String>;
/// Read one character from the user input
fn read_char(&self, screen_manger: &Arc<Stdout>) -> io::Result<char>;
fn read_char(&self, screen_manger: &Arc<TerminalOutput>) -> io::Result<char>;
/// Read the input asynchronously from the user.
fn read_async(&self, screen_manger: &Arc<Stdout>) -> AsyncReader;
fn read_async(&self, screen_manger: &Arc<TerminalOutput>) -> AsyncReader;
/// Read the input asynchronously until a certain character is hit.
fn read_until_async(&self, delimiter: u8, screen_manger: &Arc<Stdout>) -> AsyncReader;
fn read_until_async(&self, delimiter: u8, screen_manger: &Arc<TerminalOutput>) -> AsyncReader;
}
/// This is a wrapper for reading from the input asynchronously.

View File

@ -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<Stdout>) -> io::Result<String> {
fn read_line(&self, screen_manger: &Arc<TerminalOutput>) -> io::Result<String> {
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<Stdout>) -> io::Result<char> {
fn read_char(&self, screen_manger: &Arc<TerminalOutput>) -> io::Result<char> {
read_char()
}
fn read_async(&self, screen_manger: &Arc<Stdout>) -> AsyncReader {
fn read_async(&self, screen_manger: &Arc<TerminalOutput>) -> 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<Stdout>) -> AsyncReader {
fn read_until_async(&self, delimiter: u8, screen_manger: &Arc<TerminalOutput>) -> AsyncReader {
let (send, recv) = mpsc::channel();
thread::spawn(move || {

View File

@ -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<Stdout>) -> io::Result<String> {
fn read_line(&self, screen_manger: &Arc<TerminalOutput>) -> io::Result<String> {
let mut chars: Vec<char> = Vec::new();
loop {
@ -52,7 +50,7 @@ impl ITerminalInput for WindowsInput {
return Ok(chars.into_iter().collect());
}
fn read_char(&self, screen_manger: &Arc<Stdout>) -> io::Result<char> {
fn read_char(&self, screen_manger: &Arc<TerminalOutput>) -> io::Result<char> {
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<Stdout>) -> AsyncReader {
fn read_async(&self, screen_manger: &Arc<TerminalOutput>) -> 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<Stdout>) -> AsyncReader {
fn read_until_async(&self, delimiter: u8, screen_manger: &Arc<TerminalOutput>) -> AsyncReader {
let (tx, rx) = mpsc::channel();
let is_raw_screen = screen_manger.is_in_raw_mode;

View File

@ -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;

View File

@ -44,8 +44,8 @@ impl TerminalOutput {
pub fn new(is_in_raw_mode: bool) -> Self {
#[cfg(target_os = "windows")]
let stdout: Box<IStdout + Send + Sync> = functions::get_module::<Box<IStdout + Send + Sync>>(
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<IStdout + Send + Sync>>(
Box::from(WinApiStdout::new()),
Box::from(AnsiStdout::new()),
Box::from(WinApiOutput::new()),
Box::from(AnsiOutput::new()),
).unwrap();
#[cfg(not(target_os = "windows"))]

View File

@ -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<Stdout>) {
fn set_fg(&self, fg_color: Color, stdout: &Arc<TerminalOutput>) {
stdout.write_string(format!(
csi!("{}m"),
self.color_value(fg_color, ColorType::Foreground)
));
}
fn set_bg(&self, bg_color: Color, stdout: &Arc<Stdout>) {
fn set_bg(&self, bg_color: Color, stdout: &Arc<TerminalOutput>) {
stdout.write_string(format!(
csi!("{}m"),
self.color_value(bg_color, ColorType::Background)
));
}
fn reset(&self, stdout: &Arc<Stdout>) {
fn reset(&self, stdout: &Arc<TerminalOutput>) {
stdout.write_str(csi!("0m"));
}

View File

@ -25,12 +25,12 @@ use Screen;
/// ```
pub struct TerminalColor<'stdout> {
color: Box<ITerminalColor>,
stdout: &'stdout Arc<Stdout>,
stdout: &'stdout Arc<TerminalOutput>,
}
impl<'stdout> TerminalColor<'stdout> {
/// Create new instance whereon color related actions can be performed.
pub fn new(stdout: &'stdout Arc<Stdout>) -> TerminalColor<'stdout> {
pub fn new(stdout: &'stdout Arc<TerminalOutput>) -> TerminalColor<'stdout> {
#[cfg(target_os = "windows")]
let color = functions::get_module::<Box<ITerminalColor>>(
Box::from(WinApiColor::new()),

View File

@ -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<Stdout>);
fn set_fg(&self, fg_color: Color, stdout: &Arc<TerminalOutput>);
/// Set the background color to the given color.
fn set_bg(&self, fg_color: Color, stdout: &Arc<Stdout>);
fn set_bg(&self, fg_color: Color, stdout: &Arc<TerminalOutput>);
/// Reset the terminal color to default.
fn reset(&self, stdout: &Arc<Stdout>);
fn reset(&self, stdout: &Arc<TerminalOutput>);
/// Gets an value that represents an color from the given `Color` and `ColorType`.
fn color_value(&self, color: Color, color_type: ColorType) -> String;
}

View File

@ -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;

View File

@ -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<D: Display> {

View File

@ -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<Stdout>) {
fn set_fg(&self, fg_color: Color, stdout: &Arc<TerminalOutput>) {
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<Stdout>) {
fn set_bg(&self, bg_color: Color, stdout: &Arc<TerminalOutput>) {
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<Stdout>) {
fn reset(&self, stdout: &Arc<TerminalOutput>) {
kernel::set_console_text_attribute(self.original_color, stdout);
}

View File

@ -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<Stdout>) {
fn clear(&self, clear_type: ClearType, stdout: &Arc<TerminalOutput>) {
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<Stdout>) -> (u16, u16) {
fn terminal_size(&self, stdout: &Arc<TerminalOutput>) -> (u16, u16) {
functions::get_terminal_size()
}
fn scroll_up(&self, count: i16, screen_manager: &Arc<Stdout>) {
screen_manager.write_string(format!(csi!("{}S"), count));
fn scroll_up(&self, count: i16, stdout: &Arc<TerminalOutput>) {
stdout.write_string(format!(csi!("{}S"), count));
}
fn scroll_down(&self, count: i16, screen_manager: &Arc<Stdout>) {
screen_manager.write_string(format!(csi!("{}T"), count));
fn scroll_down(&self, count: i16, stdout: &Arc<TerminalOutput>) {
stdout.write_string(format!(csi!("{}T"), count));
}
fn set_size(&self, width: i16, height: i16, screen_manager: &Arc<Stdout>) {
screen_manager.write_string(format!(csi!("8;{};{}t"), width, height));
fn set_size(&self, width: i16, height: i16, stdout: &Arc<TerminalOutput>) {
stdout.write_string(format!(csi!("8;{};{}t"), width, height));
}
fn exit(&self,screen_manager: &Arc<Stdout>) {
fn exit(&self,stdout: &Arc<TerminalOutput>) {
// 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();
}

View File

@ -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<Stdout>);
fn clear(&self, clear_type: ClearType, stdout: &Arc<TerminalOutput>);
/// Get the terminal size (x,y)
fn terminal_size(&self, screen_manager: &Arc<Stdout>) -> (u16, u16);
fn terminal_size(&self, stdout: &Arc<TerminalOutput>) -> (u16, u16);
/// Scroll `n` lines up in the current terminal.
fn scroll_up(&self, count: i16, screen_manager: &Arc<Stdout>);
fn scroll_up(&self, count: i16, stdout: &Arc<TerminalOutput>);
/// Scroll `n` lines down in the current terminal.
fn scroll_down(&self, count: i16, screen_manager: &Arc<Stdout>);
fn scroll_down(&self, count: i16, stdout: &Arc<TerminalOutput>);
/// Resize terminal to the given width and height.
fn set_size(&self, width: i16, height: i16, screen_manager: &Arc<Stdout>);
fn set_size(&self, width: i16, height: i16, stdout: &Arc<TerminalOutput>);
/// Close the current terminal
fn exit(&self,screen_manager: &Arc<Stdout>);
fn exit(&self,stdout: &Arc<TerminalOutput>);
}

View File

@ -23,12 +23,12 @@ use std::io::Write;
/// ```
pub struct Terminal<'stdout> {
terminal: Box<ITerminal>,
screen: &'stdout Arc<Stdout>,
screen: &'stdout Arc<TerminalOutput>,
}
impl<'stdout> Terminal<'stdout> {
/// Create new terminal instance whereon terminal related actions can be performed.
pub fn new(screen: &'stdout Arc<Stdout>) -> Terminal<'stdout> {
pub fn new(screen: &'stdout Arc<TerminalOutput>) -> Terminal<'stdout> {
#[cfg(target_os = "windows")]
let terminal = functions::get_module::<Box<ITerminal>>(
Box::new(WinApiTerminal::new()),

View File

@ -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<Stdout>) {
let csbi = csbi::get_csbi(screen_manager).unwrap();
let pos = TerminalCursor::new(screen_manager).pos();
fn clear(&self, clear_type: ClearType, stdout: &Arc<TerminalOutput>) {
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<Stdout>) -> (u16, u16) {
fn terminal_size(&self, stdout: &Arc<TerminalOutput>) -> (u16, u16) {
terminal::terminal_size()
}
fn scroll_up(&self, count: i16, screen_manager: &Arc<Stdout>) {
let csbi = csbi::get_csbi(&screen_manager).unwrap();
fn scroll_up(&self, count: i16, stdout: &Arc<TerminalOutput>) {
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<Stdout>) {
let csbi = csbi::get_csbi(&screen_manager).unwrap();
fn scroll_down(&self, count: i16, stdout: &Arc<TerminalOutput>) {
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<Stdout>) {
fn set_size(&self, width: i16, height: i16, stdout: &Arc<TerminalOutput>) {
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<Stdout>) {
fn exit(&self, stdout: &Arc<TerminalOutput>) {
// 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>,
stdout: &Arc<TerminalOutput>,
) {
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>,
stdout: &Arc<TerminalOutput>,
) {
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<Stdout>) {
pub fn clear_entire_screen(csbi: CONSOLE_SCREEN_BUFFER_INFO, stdout: &Arc<TerminalOutput>) {
// 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>,
stdout: &Arc<TerminalOutput>,
) {
// 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>,
stdout: &Arc<TerminalOutput>,
) {
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<Stdout>) {
fn clear(start_loaction: COORD, cells_to_write: u32, stdout: &Arc<TerminalOutput>) {
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<Stdout
&mut cells_written,
start_loaction,
cells_to_write,
screen_manager,
stdout,
);
if !success {
@ -295,7 +296,7 @@ fn clear(start_loaction: COORD, cells_to_write: u32, screen_manager: &Arc<Stdout
&mut cells_written,
start_loaction,
cells_to_write,
screen_manager,
stdout,
);
if !success {