Resolve all errors unix. Now it is time to test
This commit is contained in:
parent
a0a56ffb92
commit
03bca7fe76
File diff suppressed because it is too large
Load Diff
10
Cargo.toml
10
Cargo.toml
@ -21,10 +21,10 @@ libc = "0.2"
|
||||
termios = "0.3.0"
|
||||
|
||||
|
||||
[lib]
|
||||
name = "crossterm"
|
||||
path = "src/lib.rs"
|
||||
|
||||
[[bin]]
|
||||
name = "a"
|
||||
path = "examples//bin.rs"
|
||||
|
||||
|
||||
|
||||
name = "crosstermexamples"
|
||||
path = "examples/bin.rs"
|
@ -13,45 +13,34 @@
|
||||
|
||||
// Import crossterm crate.
|
||||
extern crate crossterm;
|
||||
|
||||
// Add the usings for the crossterms modules to play with crossterm
|
||||
use self::crossterm::crossterm_style::{paint, Color };
|
||||
use self::crossterm::crossterm_cursor;
|
||||
//
|
||||
//// Add the usings for the crossterms modules to play with crossterm
|
||||
//use self::crossterm::crossterm_style::{paint, Color };
|
||||
use self::crossterm::crossterm_cursor::cursor;
|
||||
use self::crossterm::crossterm_terminal;
|
||||
//
|
||||
//// Import the example modules.
|
||||
//pub mod color;
|
||||
//pub mod cursor;
|
||||
//pub mod terminal;
|
||||
use std::io::{self, Error, ErrorKind, Write, stdout, stdin, BufRead};
|
||||
|
||||
// Import the example modules.
|
||||
pub mod color;
|
||||
pub mod cursor;
|
||||
pub mod terminal;
|
||||
use std::io::{Error, ErrorKind, Write};
|
||||
use std::io;
|
||||
use std::{time, thread};
|
||||
|
||||
use self::crossterm_terminal::screen::AlternateScreen;
|
||||
//use std::{time, thread};
|
||||
//
|
||||
use crossterm::crossterm_terminal::screen::{AlternateScreen, ToMainScreen, ToAlternateScreen};
|
||||
use crossterm::crossterm_terminal::IntoRawMode;
|
||||
|
||||
use crossterm::Context;
|
||||
|
||||
|
||||
use std::{time, thread};
|
||||
|
||||
fn main() {
|
||||
let mut context = Context::new();
|
||||
//
|
||||
let mut screen = io::stdout().into_raw_mode(&mut context).unwrap();
|
||||
{
|
||||
// let mut screen = io::stdout();
|
||||
crossterm_cursor::cursor().goto(10, 10);
|
||||
let mut screen = stdout();
|
||||
write!(screen, "{}", ToAlternateScreen);
|
||||
write!(screen, "Welcome to the alternate screen.\n\nPlease wait patiently until we arrive back at the main screen in a about three seconds.").unwrap();
|
||||
//screen.flush().unwrap();
|
||||
|
||||
let mut curs = crossterm::crossterm_cursor::cursor();
|
||||
curs.move_up(1);
|
||||
// print!("1");
|
||||
write!(screen, "{}", "1");
|
||||
curs.move_right(1);
|
||||
// print!("2");
|
||||
write!(screen, "{}", "2");
|
||||
curs.move_down(1);
|
||||
// print!("3");
|
||||
write!(screen, "{}", "3");
|
||||
curs.move_left(1);
|
||||
// write!()!("4");
|
||||
write!(screen, "{}", "4");
|
||||
}
|
||||
thread::sleep(time::Duration::from_secs(3));
|
||||
}
|
@ -123,7 +123,7 @@ pub fn print_font_with_attributes()
|
||||
#[cfg(unix)]#[cfg(unix)]
|
||||
pub fn print_supported_colors()
|
||||
{
|
||||
let count = crossterm::crossterm_style::get().get_available_color_count().unwrap();
|
||||
let count = crossterm::crossterm_style::color().get_available_color_count().unwrap();
|
||||
|
||||
for i in 0..count
|
||||
{
|
||||
|
@ -1,6 +0,0 @@
|
||||
pub struct Crossterm;
|
||||
|
||||
impl Crossterm
|
||||
{
|
||||
fn
|
||||
}
|
@ -1,9 +1,8 @@
|
||||
use std::io;
|
||||
use std::io::Write;
|
||||
|
||||
use Construct;
|
||||
use super::base_cursor::ITerminalCursor;
|
||||
use shared::functions;
|
||||
use super::ITerminalCursor;
|
||||
|
||||
use std::io::{ self, Write };
|
||||
|
||||
/// This struct is an ansi implementation for cursor related actions.
|
||||
pub struct AnsiCursor;
|
||||
|
@ -1,27 +0,0 @@
|
||||
//! This trait defines the actions that can be preformed with the termial cursor.
|
||||
//! This trait can be inplemented so that an concrete inplementation of the ITerminalCursor can forfill
|
||||
//! the wishes to work on an specific platform.
|
||||
//!
|
||||
//! ## For example:
|
||||
//!
|
||||
//! This trait is inplemented for winapi (Windows specific) and ansi (Unix specific),
|
||||
//! so that the cursor related actions can be preformed on both unix and windows systems.
|
||||
|
||||
pub trait ITerminalCursor {
|
||||
/// Goto some location (x,y) in the terminal.
|
||||
fn goto(&self, x: u16, y: u16);
|
||||
/// Get the location (x,y) of the current curor in the terminal
|
||||
fn pos(&self) -> (u16, u16);
|
||||
/// Move cursor n times up
|
||||
fn move_up(&self, count: u16);
|
||||
/// Move the cursor `n` times to the right.
|
||||
fn move_right(&self, count: u16);
|
||||
/// Move the cursor `n` times down.
|
||||
fn move_down(&self, count: u16);
|
||||
/// Move the cursor `n` times left.
|
||||
fn move_left(&self, count: u16);
|
||||
/// Save cursor position for recall later. Note that this position is stored program based not per instance of the cursor struct.
|
||||
fn save_position(&mut self);
|
||||
/// Return to saved cursor position
|
||||
fn reset_position(&self);
|
||||
}
|
@ -1,25 +1,19 @@
|
||||
//! With this module you can perform actions that are cursor related.
|
||||
//! Like changing and displaying the position of the cursor in terminal.
|
||||
//!
|
||||
use std::fmt::Display;
|
||||
use std::ops::Drop;
|
||||
|
||||
use super::*;
|
||||
use shared::functions;
|
||||
use {Construct, Context};
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
use shared::functions::get_module;
|
||||
|
||||
use super::base_cursor::ITerminalCursor;
|
||||
|
||||
use super::AnsiCursor;
|
||||
#[cfg(target_os = "windows")]
|
||||
use super::WinApiCursor;
|
||||
use std::fmt::Display;
|
||||
use std::ops::Drop;
|
||||
|
||||
/// Struct that stores an specific platform implementation for cursor related actions.
|
||||
pub struct TerminalCursor {
|
||||
terminal_cursor: Option<Box<ITerminalCursor>>,
|
||||
context: Context
|
||||
}
|
||||
|
||||
impl TerminalCursor
|
||||
{
|
||||
/// Create new cursor instance whereon cursor related actions can be performed.
|
||||
@ -30,7 +24,7 @@ impl TerminalCursor
|
||||
let cursor = get_module::<Box<ITerminalCursor>>(WinApiCursor::new(), AnsiCursor::new(), &mut context);
|
||||
|
||||
#[cfg(not(target_os = "windows"))]
|
||||
let cursor = Some(AnsiCursor::new());
|
||||
let cursor = Some(AnsiCursor::new() as Box<ITerminalCursor>);
|
||||
|
||||
TerminalCursor { terminal_cursor: cursor, context: context }
|
||||
}
|
||||
@ -260,6 +254,14 @@ impl TerminalCursor
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for TerminalCursor
|
||||
{
|
||||
fn drop(&mut self)
|
||||
{
|
||||
self.context.restore_changes();
|
||||
}
|
||||
}
|
||||
|
||||
/// Get an TerminalCursor implementation whereon cursor related actions can be performed.
|
||||
///
|
||||
/// Check `/examples/cursor` in the libary for more spesific examples.
|
||||
|
@ -1,15 +1,39 @@
|
||||
mod base_cursor;
|
||||
mod cursor;
|
||||
//! This trait defines the actions that can be preformed with the termial cursor.
|
||||
//! This trait can be inplemented so that an concrete inplementation of the ITerminalCursor can forfill
|
||||
//! the wishes to work on an specific platform.
|
||||
//!
|
||||
//! ## For example:
|
||||
//!
|
||||
//! This trait is inplemented for winapi (Windows specific) and ansi (Unix specific),
|
||||
//! so that the cursor related actions can be preformed on both unix and windows systems.
|
||||
//!
|
||||
|
||||
mod cursor;
|
||||
#[cfg(target_os = "windows")]
|
||||
mod winapi_cursor;
|
||||
mod ansi_cursor;
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
mod winapi_cursor;
|
||||
|
||||
use self::ansi_cursor::AnsiCursor;
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
use self::winapi_cursor::WinApiCursor;
|
||||
use self::ansi_cursor::AnsiCursor;
|
||||
|
||||
pub use self::cursor::{ cursor, TerminalCursor };
|
||||
|
||||
pub trait ITerminalCursor {
|
||||
/// Goto some location (x,y) in the terminal.
|
||||
fn goto(&self, x: u16, y: u16);
|
||||
/// Get the location (x,y) of the current curor in the terminal
|
||||
fn pos(&self) -> (u16, u16);
|
||||
/// Move cursor n times up
|
||||
fn move_up(&self, count: u16);
|
||||
/// Move the cursor `n` times to the right.
|
||||
fn move_right(&self, count: u16);
|
||||
/// Move the cursor `n` times down.
|
||||
fn move_down(&self, count: u16);
|
||||
/// Move the cursor `n` times left.
|
||||
fn move_left(&self, count: u16);
|
||||
/// Save cursor position for recall later. Note that this position is stored program based not per instance of the cursor struct.
|
||||
fn save_position(&mut self);
|
||||
/// Return to saved cursor position
|
||||
fn reset_position(&self);
|
||||
}
|
@ -1,8 +1,7 @@
|
||||
use Construct;
|
||||
use super::base_cursor::ITerminalCursor;
|
||||
use super::ITerminalCursor;
|
||||
use kernel::windows_kernel::{kernel, cursor};
|
||||
|
||||
|
||||
/// This struct is an windows implementation for cursor related actions.
|
||||
pub struct WinApiCursor;
|
||||
|
||||
|
@ -25,7 +25,8 @@ impl IContextCommand for NoncanonicalModeCommand
|
||||
{
|
||||
// println!("execute NoncanonicalModeCommand uxix");
|
||||
// Set noncanonical mode
|
||||
let orig = Termios::from_fd(FD_STDIN)?;
|
||||
if let Ok(orig) = Termios::from_fd(FD_STDIN)
|
||||
{
|
||||
let mut noncan = orig.clone();
|
||||
noncan.c_lflag &= !ICANON;
|
||||
noncan.c_lflag &= !ECHO;
|
||||
@ -35,13 +36,18 @@ impl IContextCommand for NoncanonicalModeCommand
|
||||
Ok(_) => return true,
|
||||
Err(_) => return false,
|
||||
};
|
||||
}else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
fn undo(&mut self) -> bool
|
||||
{
|
||||
|
||||
// println!("undo NoncanonicalModeCommand unix");
|
||||
// Disable noncanonical mode
|
||||
let orig = Termios::from_fd(FD_STDIN)?;
|
||||
if let Ok(orig) = Termios::from_fd(FD_STDIN)
|
||||
{
|
||||
let mut noncan = orig.clone();
|
||||
noncan.c_lflag &= ICANON;
|
||||
noncan.c_lflag &= ECHO;
|
||||
@ -52,6 +58,9 @@ impl IContextCommand for NoncanonicalModeCommand
|
||||
Ok(_) => return true,
|
||||
Err(_) => return false,
|
||||
};
|
||||
}else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -68,29 +77,40 @@ impl IContextCommand for EnableRawModeCommand
|
||||
// println!("new EnableRawModeCommand unix");
|
||||
let key = super::generate_key();
|
||||
let command = EnableRawModeCommand { original_mode: None, key: key };
|
||||
context.state.register_change(Box::from(command), key);
|
||||
context.register_change(Box::from(command), key);
|
||||
(Box::from(command),key)
|
||||
}
|
||||
|
||||
fn execute(&mut self) -> bool
|
||||
{
|
||||
// println!("execute EnableRawModeCommand unix");
|
||||
self.original_mode = terminal::get_terminal_mode()?;
|
||||
if let Ok(original_mode) = terminal::get_terminal_mode()
|
||||
{
|
||||
self.original_mode = Some(original_mode);
|
||||
let mut new_mode = self.original_mode.unwrap();
|
||||
new_mode.make_raw();
|
||||
terminal::set_terminal_mode(new_mode);
|
||||
terminal::make_raw(&mut new_mode);
|
||||
terminal::set_terminal_mode(&new_mode);
|
||||
true
|
||||
|
||||
}else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
fn undo(&mut self) -> bool
|
||||
{
|
||||
// println!("undo EnableRawModeCommand unix");
|
||||
let result = terminal::set_terminal_mode(self.original_mode).unwrap();
|
||||
if let Ok(original_mode) = terminal::get_terminal_mode()
|
||||
{
|
||||
let result = terminal::set_terminal_mode(&self.original_mode.unwrap());
|
||||
|
||||
match result
|
||||
{
|
||||
Ok(_) => true,
|
||||
Ok(()) => true,
|
||||
Err(_) => false
|
||||
}
|
||||
}else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,11 +1,8 @@
|
||||
use std::io;
|
||||
use std::io::Write;
|
||||
use std::string::String;
|
||||
|
||||
use Construct;
|
||||
use super::ITerminalColor;
|
||||
use super::super::{Color, ColorType};
|
||||
use super::base_color::ITerminalColor;
|
||||
|
||||
use std::io::{self, Write};
|
||||
|
||||
/// This struct is an ansi implementation for color related actions.
|
||||
#[derive(Debug)]
|
||||
|
@ -1,23 +0,0 @@
|
||||
///!
|
||||
///! This trait defines the actions that can be preformed with the termial color.
|
||||
///! This trait can be inplemented so that an concrete inplementation of the ITerminalColor can forfill
|
||||
///! the wishes to work on an specific platform.
|
||||
///!
|
||||
///! ## For example:
|
||||
///!
|
||||
///! This trait is inplemented for winapi (Windows specific) and ansi (Unix specific),
|
||||
///! so that the color related actions can be preformed on both unix and windows systems.
|
||||
///!
|
||||
|
||||
use super::super::{Color, ColorType};
|
||||
|
||||
pub trait ITerminalColor {
|
||||
/// Set the forground color to the given color.
|
||||
fn set_fg(&self, fg_color: Color);
|
||||
/// Set the background color to the given color.
|
||||
fn set_bg(&self, fg_color: Color);
|
||||
/// Reset the terminal color to default.
|
||||
fn reset(&self);
|
||||
/// Gets an value that represents an color from the given `Color` and `ColorType`.
|
||||
fn color_value(&self, color: Color, color_type: ColorType) -> String;
|
||||
}
|
@ -1,22 +1,12 @@
|
||||
//! With this module you can perform actions that are color related.
|
||||
//! Like styling the font, foreground color and background color.
|
||||
use super::*;
|
||||
use shared::functions;
|
||||
use { Construct, Context };
|
||||
use crossterm_style::{Color, ObjectStyle, StyledObject};
|
||||
|
||||
use std::ops::Drop;
|
||||
use std::fmt;
|
||||
use std::io;
|
||||
|
||||
use {Construct, Context };
|
||||
use crossterm_style::{ObjectStyle, StyledObject};
|
||||
use super::base_color::ITerminalColor;
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
use shared::functions::get_module;
|
||||
use super::super::Color;
|
||||
|
||||
use super::AnsiColor;
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
use super::WinApiColor;
|
||||
use std::{ fmt, io };
|
||||
|
||||
/// Struct that stores an specific platform implementation for color related actions.
|
||||
pub struct TerminalColor {
|
||||
@ -30,10 +20,10 @@ impl TerminalColor {
|
||||
let mut context = Context::new();
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
let color = get_module::<Box<ITerminalColor>>(WinApiColor::new(), AnsiColor::new(), &mut context);
|
||||
let color = functions::get_module::<Box<ITerminalColor>>(WinApiColor::new(), AnsiColor::new(), &mut context);
|
||||
|
||||
#[cfg(not(target_os = "windows"))]
|
||||
let color = Some(AnsiColor::new());
|
||||
let color = Some(AnsiColor::new() as Box<ITerminalColor>);
|
||||
|
||||
TerminalColor { terminal_color: color, context: context}
|
||||
}
|
||||
@ -125,6 +115,14 @@ impl TerminalColor {
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for TerminalColor
|
||||
{
|
||||
fn drop(&mut self)
|
||||
{
|
||||
self.context.restore_changes();
|
||||
}
|
||||
}
|
||||
|
||||
/// Get an TerminalColor implementation whereon color related actions can be performed.
|
||||
///
|
||||
/// # Example
|
||||
|
@ -1,12 +1,33 @@
|
||||
pub mod base_color;
|
||||
pub mod color;
|
||||
|
||||
mod ansi_color;
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
mod winapi_color;
|
||||
|
||||
use self::ansi_color::AnsiColor;
|
||||
mod ansi_color;
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
use self::winapi_color::WinApiColor;
|
||||
use self::ansi_color::AnsiColor;
|
||||
|
||||
///!
|
||||
///! This trait defines the actions that can be preformed with the termial color.
|
||||
///! This trait can be inplemented so that an concrete inplementation of the ITerminalColor can forfill
|
||||
///! the wishes to work on an specific platform.
|
||||
///!
|
||||
///! ## For example:
|
||||
///!
|
||||
///! This trait is inplemented for winapi (Windows specific) and ansi (Unix specific),
|
||||
///! so that the color related actions can be preformed on both unix and windows systems.
|
||||
///!
|
||||
|
||||
use super::{Color, ColorType};
|
||||
|
||||
pub trait ITerminalColor {
|
||||
/// Set the forground color to the given color.
|
||||
fn set_fg(&self, fg_color: Color);
|
||||
/// Set the background color to the given color.
|
||||
fn set_bg(&self, fg_color: Color);
|
||||
/// Reset the terminal color to default.
|
||||
fn reset(&self);
|
||||
/// Gets an value that represents an color from the given `Color` and `ColorType`.
|
||||
fn color_value(&self, color: Color, color_type: ColorType) -> String;
|
||||
}
|
@ -1,9 +1,8 @@
|
||||
use Construct;
|
||||
use super::ITerminalColor;
|
||||
use super::super::{ColorType, Color};
|
||||
use super::base_color::ITerminalColor;
|
||||
|
||||
use kernel::windows_kernel::kernel;
|
||||
use winapi::um::wincon;
|
||||
use kernel::windows_kernel::kernel;
|
||||
|
||||
/// This struct is an windows implementation for color related actions.
|
||||
#[derive(Debug)]
|
||||
|
@ -1,5 +1,5 @@
|
||||
use crossterm_style::{Color, StyledObject};
|
||||
use std::fmt::Display;
|
||||
use crossterm_style::{Color, StyledObject};
|
||||
|
||||
#[cfg(unix)]
|
||||
use super::super::Attribute;
|
||||
|
@ -1,5 +1,4 @@
|
||||
use std;
|
||||
use std::fmt;
|
||||
use std::{ fmt, self };
|
||||
use std::io::Write;
|
||||
|
||||
#[cfg(unix)]
|
||||
|
@ -1,11 +1,10 @@
|
||||
use Construct;
|
||||
use shared::functions;
|
||||
use super::{ClearType, ITerminal};
|
||||
|
||||
use std::io;
|
||||
use std::io::Write;
|
||||
|
||||
use Construct;
|
||||
use super::base_terminal::{ClearType, ITerminal};
|
||||
|
||||
use shared::functions::get_terminal_size;
|
||||
|
||||
/// This struct is an ansi implementation for terminal related actions.
|
||||
pub struct AnsiTerminal ;
|
||||
|
||||
@ -38,7 +37,7 @@ impl ITerminal for AnsiTerminal {
|
||||
}
|
||||
|
||||
fn terminal_size(&self) -> (u16, u16) {
|
||||
get_terminal_size()
|
||||
functions::get_terminal_size()
|
||||
}
|
||||
|
||||
fn scroll_up(&self, count: i16) {
|
||||
|
@ -1,21 +0,0 @@
|
||||
/// Enum that can be used for the kind of clearing that can be done in the terminal.
|
||||
pub enum ClearType {
|
||||
All,
|
||||
FromCursorDown,
|
||||
FromCursorUp,
|
||||
CurrentLine,
|
||||
UntilNewLine,
|
||||
}
|
||||
|
||||
pub trait ITerminal{
|
||||
/// Clear the current cursor by specifying the clear type
|
||||
fn clear(&self, clear_type: ClearType);
|
||||
/// Get the terminal size (x,y)
|
||||
fn terminal_size(&self) -> (u16, u16);
|
||||
/// Scroll `n` lines up in the current terminal.
|
||||
fn scroll_up(&self, count: i16);
|
||||
/// Scroll `n` lines down in the current terminal.
|
||||
fn scroll_down(&self, count: i16);
|
||||
/// Resize terminal to the given width and height.
|
||||
fn set_size(&self,width: i16, height: i16);
|
||||
}
|
@ -1,19 +1,37 @@
|
||||
mod raw_terminal;
|
||||
mod base_terminal;
|
||||
mod terminal;
|
||||
|
||||
pub mod screen;
|
||||
|
||||
mod ansi_terminal;
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
mod winapi_terminal;
|
||||
mod ansi_terminal;
|
||||
|
||||
use self::ansi_terminal::AnsiTerminal;
|
||||
pub mod screen;
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
use self::winapi_terminal::WinApiTerminal;
|
||||
use self::ansi_terminal::AnsiTerminal;
|
||||
|
||||
pub use self::base_terminal::ClearType;
|
||||
pub use self::terminal::{ Terminal, terminal};
|
||||
pub use self::raw_terminal::{RawTerminal, IntoRawMode};
|
||||
|
||||
/// Enum that can be used for the kind of clearing that can be done in the terminal.
|
||||
pub enum ClearType {
|
||||
All,
|
||||
FromCursorDown,
|
||||
FromCursorUp,
|
||||
CurrentLine,
|
||||
UntilNewLine,
|
||||
}
|
||||
|
||||
pub trait ITerminal{
|
||||
/// Clear the current cursor by specifying the clear type
|
||||
fn clear(&self, clear_type: ClearType);
|
||||
/// Get the terminal size (x,y)
|
||||
fn terminal_size(&self) -> (u16, u16);
|
||||
/// Scroll `n` lines up in the current terminal.
|
||||
fn scroll_up(&self, count: i16);
|
||||
/// Scroll `n` lines down in the current terminal.
|
||||
fn scroll_down(&self, count: i16);
|
||||
/// Resize terminal to the given width and height.
|
||||
fn set_size(&self,width: i16, height: i16);
|
||||
}
|
||||
|
@ -3,14 +3,12 @@ use crossterm_state::commands::unix_command::EnableRawModeCommand;
|
||||
#[cfg(windows)]
|
||||
use crossterm_state::commands::win_commands::EnableRawModeCommand;
|
||||
|
||||
use crossterm_state::commands::IContextCommand;
|
||||
|
||||
use shared::traits::Construct;
|
||||
use crossterm_state::{ Context };
|
||||
use crossterm_state::commands::ICommand;
|
||||
use { Construct, Context };
|
||||
use crossterm_state::commands::{ICommand, IContextCommand};
|
||||
|
||||
use std::io::{ self, Write};
|
||||
|
||||
|
||||
pub struct RawTerminal<'a, W: Write>
|
||||
{
|
||||
output: W,
|
||||
|
@ -1,14 +1,9 @@
|
||||
use std::io::{self, Write};
|
||||
use std::ops;
|
||||
use std::any::Any;
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
use shared::functions::get_module;
|
||||
use shared::functions;
|
||||
use { Context, Construct };
|
||||
use crossterm_state::commands::*;
|
||||
use shared::traits::Construct;
|
||||
use Context;
|
||||
|
||||
use std::fmt;
|
||||
use std::{ fmt, ops };
|
||||
use std::io::{self, Write};
|
||||
|
||||
/// let context = ScreenContext::new();
|
||||
/// ToMainScreen {}.execute(&mut context);
|
||||
@ -94,7 +89,7 @@ fn get_to_alternate_screen_command() -> Box<ICommand>
|
||||
let mut context = Context::new();
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
let command = get_module::<Box<ICommand>>(win_commands::ToAlternateScreenBufferCommand::new(), shared_commands::ToAlternateScreenBufferCommand::new(), &mut context).unwrap();
|
||||
let command = functions::get_module::<Box<ICommand>>(win_commands::ToAlternateScreenBufferCommand::new(), shared_commands::ToAlternateScreenBufferCommand::new(), &mut context).unwrap();
|
||||
|
||||
#[cfg(not(target_os = "windows"))]
|
||||
let command = shared_commands::ToAlternateScreenBufferCommand::new();
|
||||
|
@ -1,18 +1,11 @@
|
||||
//! With this module you can perform actions that are terminal related.
|
||||
//! Like clearing and scrolling in the terminal or getting the size of the terminal.
|
||||
use super::*;
|
||||
use shared::functions;
|
||||
use {Construct, Context};
|
||||
|
||||
use std::ops::Drop;
|
||||
|
||||
use {Construct, Context};
|
||||
use super::base_terminal::{ClearType, ITerminal};
|
||||
#[cfg(target_os = "windows")]
|
||||
use shared::functions::get_module;
|
||||
|
||||
use super::AnsiTerminal;
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
use super::WinApiTerminal;
|
||||
|
||||
/// Struct that stores an specific platform implementation for terminal related actions.
|
||||
pub struct Terminal {
|
||||
terminal: Option<Box<ITerminal>>,
|
||||
@ -25,10 +18,10 @@ impl Terminal {
|
||||
|
||||
let mut context = Context::new();
|
||||
#[cfg(target_os = "windows")]
|
||||
let terminal = get_module::<Box<ITerminal>>(WinApiTerminal::new(), AnsiTerminal::new(), &mut context);
|
||||
let terminal = functions::get_module::<Box<ITerminal>>(WinApiTerminal::new(), AnsiTerminal::new(), &mut context);
|
||||
|
||||
#[cfg(not(target_os = "windows"))]
|
||||
let terminal = Some(AnsiTerminal::new());
|
||||
let terminal = Some(AnsiTerminal::new() as Box<ITerminal>);
|
||||
|
||||
Terminal { terminal: terminal, context: context }
|
||||
}
|
||||
@ -149,6 +142,14 @@ impl Terminal {
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Terminal
|
||||
{
|
||||
fn drop(&mut self)
|
||||
{
|
||||
self.context.restore_changes();
|
||||
}
|
||||
}
|
||||
|
||||
/// Get an Terminal implementation whereon terminal related actions can be performed.
|
||||
///
|
||||
/// Check `/examples/terminal` in the libary for more spesific examples.
|
||||
|
@ -1,12 +1,11 @@
|
||||
use libc;
|
||||
use self::libc::{STDOUT_FILENO, TIOCGWINSZ, c_ushort, ioctl, c_int};
|
||||
pub use self::libc::{termios, cvt};
|
||||
use { libc, Context };
|
||||
use termios::Termios;
|
||||
pub use self::libc::{termios};
|
||||
use self::libc::{STDOUT_FILENO, TIOCGWINSZ, c_ushort, ioctl, c_int};
|
||||
use crossterm_state::commands::{ NoncanonicalModeCommand, IContextCommand} ;
|
||||
use Context;
|
||||
use std::io;
|
||||
use std::mem;
|
||||
|
||||
use std::{ io, mem };
|
||||
use std::io::Error;
|
||||
|
||||
/// A representation of the size of the current terminal
|
||||
#[repr(C)]
|
||||
@ -46,7 +45,7 @@ pub fn pos() -> (u16,u16)
|
||||
|
||||
let mut context = Context::new();
|
||||
{
|
||||
let command = NoncanonicalModeCommand::new(&mut context);
|
||||
let mut command = NoncanonicalModeCommand::new(&mut context);
|
||||
command.0.execute();
|
||||
|
||||
// This code is original written by term_cursor credits to them.
|
||||
@ -65,7 +64,7 @@ pub fn pos() -> (u16,u16)
|
||||
}
|
||||
|
||||
// Read rows and cols through a ad-hoc integer parsing function
|
||||
let read_num = || -> Result<(i32, char), Error> {
|
||||
let read_num = || -> (i32, char) {
|
||||
let mut num = 0;
|
||||
let mut c;
|
||||
|
||||
@ -81,11 +80,12 @@ pub fn pos() -> (u16,u16)
|
||||
}
|
||||
}
|
||||
|
||||
Ok((num, c))
|
||||
(num, c)
|
||||
};
|
||||
|
||||
// Read rows and expect `;`
|
||||
let (rows, c) = read_num();
|
||||
|
||||
if c != ';' {
|
||||
return (0, 0);
|
||||
}
|
||||
@ -94,7 +94,7 @@ pub fn pos() -> (u16,u16)
|
||||
let (cols, c) = read_num();
|
||||
|
||||
// Expect `R`
|
||||
let res = if c == 'R' { Ok((cols, rows)) } else { return Ok((0, 0)); };
|
||||
let res = if c == 'R' { (cols as u16, rows as u16) } else { return (0, 0) };
|
||||
|
||||
res
|
||||
}
|
||||
@ -105,7 +105,14 @@ pub fn set_terminal_mode(termios: &Termios) -> io::Result<()>
|
||||
extern "C" {
|
||||
pub fn tcsetattr(fd: c_int, opt: c_int, termptr: *const Termios) -> c_int;
|
||||
}
|
||||
unsafe { tcsetattr(0, 0, termios) }
|
||||
is_true(unsafe { tcsetattr(0, 0, termios) }).and(Ok(()))
|
||||
}
|
||||
|
||||
pub fn make_raw(termios: &mut Termios) {
|
||||
extern "C" {
|
||||
pub fn cfmakeraw(termptr: *mut Termios);
|
||||
}
|
||||
unsafe { cfmakeraw(termios) }
|
||||
}
|
||||
|
||||
pub fn get_terminal_mode() -> io::Result<Termios>
|
||||
@ -115,7 +122,17 @@ pub fn get_terminal_mode() -> io::Result<Termios>
|
||||
}
|
||||
unsafe {
|
||||
let mut termios = mem::zeroed();
|
||||
cvt(tcgetattr(0, &mut termios))?;
|
||||
is_true(tcgetattr(0, &mut termios))?;
|
||||
Ok(termios)
|
||||
}
|
||||
}
|
||||
|
||||
fn is_true(value: i32) -> Result<(), Error>
|
||||
{
|
||||
match value
|
||||
{
|
||||
-1 => Err(io::Error::last_os_error()),
|
||||
0 => Ok(()),
|
||||
_ => Err(io::Error::last_os_error()),
|
||||
}
|
||||
}
|
@ -1,10 +1,9 @@
|
||||
use crossterm_state::Context;
|
||||
use { Context, Contstruct };
|
||||
use crossterm_state::commands::IContextCommand;
|
||||
use shared::traits::Construct;
|
||||
|
||||
static mut HAS_BEEN_TRYED_TO_ENABLE: bool = false;
|
||||
static mut IS_ANSI_ON_WINDOWS_ENABLED: Option<bool> = None;
|
||||
static mut DOES_WINDOWS_SUPPORT_ANSI: Option<bool> = None;
|
||||
static mut HAS_BEEN_TRYED_TO_ENABLE: bool = false;
|
||||
|
||||
/// Try enable ANSI escape codes and return the result.
|
||||
pub fn try_enable_ansi_support(context: &mut Context) -> bool
|
||||
|
@ -1,5 +1,4 @@
|
||||
use super::kernel;
|
||||
use crossterm_cursor::cursor;
|
||||
use super::{ kernel, cursor };
|
||||
|
||||
/// This stores the cursor pos, at program level. So it can be recalled later.
|
||||
static mut SAVED_CURSOR_POS:(u16,u16) = (0,0);
|
||||
@ -15,7 +14,7 @@ pub fn reset_to_saved_position()
|
||||
/// Save current cursor position to recall later.
|
||||
pub fn save_cursor_pos()
|
||||
{
|
||||
let position = cursor().pos();
|
||||
let position = pos();
|
||||
|
||||
unsafe {
|
||||
SAVED_CURSOR_POS = (position.0, position.1);
|
||||
|
@ -3,17 +3,17 @@ use winapi::um::winbase::{STD_OUTPUT_HANDLE, STD_INPUT_HANDLE };
|
||||
use winapi::um::handleapi::INVALID_HANDLE_VALUE;
|
||||
use winapi::um::processenv::{GetStdHandle};
|
||||
use winapi::um::consoleapi::{SetConsoleMode,GetConsoleMode, };
|
||||
|
||||
use winapi::um::wincon;
|
||||
use winapi::shared::minwindef::{TRUE};
|
||||
use winapi::um::wincon::{ SetConsoleWindowInfo, SetConsoleCursorPosition, SetConsoleTextAttribute, SetConsoleScreenBufferSize, CreateConsoleScreenBuffer,SetConsoleActiveScreenBuffer,
|
||||
use winapi::um::wincon;
|
||||
use winapi::um::wincon::
|
||||
{
|
||||
SetConsoleWindowInfo, SetConsoleCursorPosition, SetConsoleTextAttribute, SetConsoleScreenBufferSize, CreateConsoleScreenBuffer,SetConsoleActiveScreenBuffer,
|
||||
GetLargestConsoleWindowSize, GetConsoleScreenBufferInfo,
|
||||
FillConsoleOutputCharacterA, FillConsoleOutputAttribute,
|
||||
CONSOLE_SCREEN_BUFFER_INFO, SMALL_RECT, COORD, CHAR_INFO, PSMALL_RECT
|
||||
};
|
||||
|
||||
use super::{Empty};
|
||||
|
||||
static mut CONSOLE_OUTPUT_HANDLE: Option<HANDLE> = None;
|
||||
static mut CONSOLE_INPUT_HANDLE: Option<HANDLE> = None;
|
||||
|
||||
@ -261,11 +261,6 @@ pub fn read_console_output(read_buffer: &HANDLE, copy_buffer: &mut [CHAR_INFO;16
|
||||
}
|
||||
}
|
||||
|
||||
pub fn write_console()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
pub fn write_console_output(write_buffer: &HANDLE, copy_buffer: &mut [CHAR_INFO;160], buffer_size: COORD, buffer_coord: COORD, source_buffer: PSMALL_RECT)
|
||||
{
|
||||
use self::wincon::WriteConsoleOutputA;
|
||||
|
@ -1,13 +1,12 @@
|
||||
extern crate winapi;
|
||||
|
||||
use winapi::um::wincon::{COORD, CONSOLE_SCREEN_BUFFER_INFO, SMALL_RECT};
|
||||
use shared::traits::Empty;
|
||||
|
||||
pub mod kernel;
|
||||
pub mod cursor;
|
||||
pub mod terminal;
|
||||
pub mod ansi_support;
|
||||
|
||||
use winapi;
|
||||
use shared::traits::Empty;
|
||||
use self::winapi::um::wincon::{COORD, CONSOLE_SCREEN_BUFFER_INFO, SMALL_RECT};
|
||||
|
||||
impl Empty for COORD {
|
||||
fn empty() -> COORD {
|
||||
COORD { X: 0, Y: 0 }
|
||||
|
11
src/lib.rs
11
src/lib.rs
@ -18,3 +18,14 @@ extern crate libc;
|
||||
extern crate termios;
|
||||
|
||||
extern crate rand;
|
||||
|
||||
|
||||
// private mod
|
||||
//
|
||||
// public mod
|
||||
//
|
||||
// own usings
|
||||
//
|
||||
// std usings
|
||||
//
|
||||
// extern crate
|
@ -1,7 +1,7 @@
|
||||
//! Some actions need to preformed platform independently.
|
||||
//!
|
||||
use Context;
|
||||
use shared::traits::Construct;
|
||||
use {Context, Construct};
|
||||
|
||||
#[cfg(windows)]
|
||||
use kernel::windows_kernel::terminal::terminal_size;
|
||||
#[cfg(unix)]
|
||||
|
Loading…
Reference in New Issue
Block a user