resolved some errors on unix system. code refactor will come when every ting is working again

This commit is contained in:
= 2018-03-03 23:38:57 +01:00
parent 2cc32394b5
commit a0a56ffb92
7 changed files with 73 additions and 60 deletions

View File

@ -5,7 +5,10 @@ use std::fmt::Display;
use std::ops::Drop; use std::ops::Drop;
use {Construct, Context}; use {Construct, Context};
#[cfg(target_os = "windows")]
use shared::functions::get_module; use shared::functions::get_module;
use super::base_cursor::ITerminalCursor; use super::base_cursor::ITerminalCursor;
use super::AnsiCursor; use super::AnsiCursor;

View File

@ -1,23 +1,24 @@
use crossterm_state::{Context}; use crossterm_state::{Context};
use super::IContextCommand; use super::IContextCommand;
use kernel::unix_kernel::terminal::Termios;
use kernel::unix_kernel::terminal; use kernel::unix_kernel::terminal;
use termios::{Termios, tcsetattr, TCSAFLUSH, ICANON, ECHO, CREAD};
const FD_STDIN: ::std::os::unix::io::RawFd = 1;
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
pub struct NoncanonicaModeCommand pub struct NoncanonicalModeCommand
{ {
key: i16 key: i16
} }
impl IContextCommand for NoncanonicalModeCommand impl IContextCommand for NoncanonicalModeCommand
{ {
fn new(context: &mut Context) -> (Box<NoncanonicalModeCommand>) { fn new(context: &mut Context) -> (Box<NoncanonicalModeCommand>, i16) {
// println!("new new NoncanonicalModeCommand unix"); // println!("new new NoncanonicalModeCommand unix");
let key = super::generate_key(); let key = super::generate_key();
let command = NoncanonicaModeCommand{ key: key }; let command = NoncanonicalModeCommand { key: key };
context.register_change(command,key); context.register_change(Box::from(command), key);
(Box::from(NoncanonicalModeCommand {}), key) (Box::from(command),key)
} }
fn execute(&mut self) -> bool fn execute(&mut self) -> bool
@ -63,7 +64,7 @@ pub struct EnableRawModeCommand
impl IContextCommand for EnableRawModeCommand impl IContextCommand for EnableRawModeCommand
{ {
fn new(context: &Context) -> (Box<EnableRawModeCommand>, i16) { fn new(context: &mut Context) -> (Box<EnableRawModeCommand>, i16) {
// println!("new EnableRawModeCommand unix"); // println!("new EnableRawModeCommand unix");
let key = super::generate_key(); let key = super::generate_key();
let command = EnableRawModeCommand { original_mode: None, key: key }; let command = EnableRawModeCommand { original_mode: None, key: key };

View File

@ -8,6 +8,8 @@ use std::io;
use {Construct, Context }; use {Construct, Context };
use crossterm_style::{ObjectStyle, StyledObject}; use crossterm_style::{ObjectStyle, StyledObject};
use super::base_color::ITerminalColor; use super::base_color::ITerminalColor;
#[cfg(target_os = "windows")]
use shared::functions::get_module; use shared::functions::get_module;
use super::super::Color; use super::super::Color;

View File

@ -2,6 +2,7 @@ use std::io::{self, Write};
use std::ops; use std::ops;
use std::any::Any; use std::any::Any;
#[cfg(target_os = "windows")]
use shared::functions::get_module; use shared::functions::get_module;
use crossterm_state::commands::*; use crossterm_state::commands::*;
use shared::traits::Construct; use shared::traits::Construct;
@ -84,7 +85,6 @@ impl<W: Write> Drop for AlternateScreen<W>
{ {
fn drop(&mut self) fn drop(&mut self)
{ {
this
write!(self, "{}", ToMainScreen).expect("switch to main screen"); write!(self, "{}", ToMainScreen).expect("switch to main screen");
} }
} }

View File

@ -5,6 +5,7 @@ use std::ops::Drop;
use {Construct, Context}; use {Construct, Context};
use super::base_terminal::{ClearType, ITerminal}; use super::base_terminal::{ClearType, ITerminal};
#[cfg(target_os = "windows")]
use shared::functions::get_module; use shared::functions::get_module;
use super::AnsiTerminal; use super::AnsiTerminal;

View File

@ -1,7 +1,9 @@
use libc; use libc;
use self::libc::{STDOUT_FILENO, TIOCGWINSZ, c_ushort, ioctl}; use self::libc::{STDOUT_FILENO, TIOCGWINSZ, c_ushort, ioctl, c_int};
pub use self::libc::termios as Termios; pub use self::libc::{termios, cvt};
use crossterm_state::commands::{NoncanonicaModeCommand, IContextCommand}; use termios::Termios;
use crossterm_state::commands::{NoncanonicalModeCommand, IContextCommand};
use Context;
use std::io; use std::io;
use std::mem; use std::mem;
@ -30,7 +32,7 @@ pub fn terminal_size() -> (u16,u16) {
let r = unsafe { ioctl(STDOUT_FILENO, TIOCGWINSZ, &us) }; let r = unsafe { ioctl(STDOUT_FILENO, TIOCGWINSZ, &us) };
if r == 0 { if r == 0 {
// because crossterm works starts counting at 0 and unix terminal starts at cell 1 you have subtract one to get 0-based results. // because crossterm works starts counting at 0 and unix terminal starts at cell 1 you have subtract one to get 0-based results.
Some((us.cols -1, us.rows -1)) (us.cols -1, us.rows -1)
} else { } else {
(0,0) (0,0)
} }
@ -42,66 +44,68 @@ pub fn pos() -> (u16,u16)
use std::io::Error; use std::io::Error;
use std::io::{ Write,Read }; use std::io::{ Write,Read };
let command = NoncanonicalModeCommand::new(); let mut context = Context::new();
command.execute(); {
let command = NoncanonicalModeCommand::new(&mut context);
command.0.execute();
// This code is original written by term_cursor credits to them. // This code is original written by term_cursor credits to them.
let mut stdout = io::stdout(); let mut stdout = io::stdout();
// Write command // Write command
stdout.write(b"\x1B[6n")?; stdout.write(b"\x1B[6n");
stdout.flush()?; stdout.flush();
// Read back result // Read back result
let mut buf = [0u8; 2]; let mut buf = [0u8; 2];
// Expect `ESC[` // Expect `ESC[`
io::stdin().read_exact(&mut buf)?; io::stdin().read_exact(&mut buf);
if buf[0] != 0x1B || buf[1] as char != '[' { if buf[0] != 0x1B || buf[1] as char != '[' {
return (0,0); return (0, 0);
}
// Read rows and cols through a ad-hoc integer parsing function
let read_num = || -> Result<(i32, char), Error> {
let mut num = 0;
let mut c;
loop {
let mut buf = [0u8; 1];
io::stdin().read_exact(&mut buf)?;
c = buf[0] as char;
if let Some(d) = c.to_digit(10) {
num = if num == 0 { 0 } else { num * 10 };
num += d as i32;
} else {
break;
}
} }
Ok((num, c)) // Read rows and cols through a ad-hoc integer parsing function
}; let read_num = || -> Result<(i32, char), Error> {
let mut num = 0;
let mut c;
// Read rows and expect `;` loop {
let (rows, c) = read_num()?; let mut buf = [0u8; 1];
if c != ';' { io::stdin().read_exact(&mut buf);
return (0,0); c = buf[0] as char;
if let Some(d) = c.to_digit(10) {
num = if num == 0 { 0 } else { num * 10 };
num += d as i32;
} else {
break;
}
}
Ok((num, c))
};
// Read rows and expect `;`
let (rows, c) = read_num();
if c != ';' {
return (0, 0);
}
// Read cols
let (cols, c) = read_num();
// Expect `R`
let res = if c == 'R' { Ok((cols, rows)) } else { return Ok((0, 0)); };
res
} }
// Read cols
let (cols, c) = read_num()?;
// Expect `R`
let res = if c == 'R' { Ok((cols, rows)) } else { return (0,0); };
command.undo();
res
} }
pub fn set_terminal_mode(terminal: &Termios) -> io::Result<()> pub fn set_terminal_mode(termios: &Termios) -> io::Result<()>
{ {
extern "C" { extern "C" {
pub fn tcsetattr(fd: c_int, opt: c_int, termptr: *const Termios) -> c_int; pub fn tcsetattr(fd: c_int, opt: c_int, termptr: *const Termios) -> c_int;
} }
cvt(unsafe { tcsetattr(0, 0, termios) }).and(Ok(())) unsafe { tcsetattr(0, 0, termios) }
} }
pub fn get_terminal_mode() -> io::Result<Termios> pub fn get_terminal_mode() -> io::Result<Termios>

View File

@ -23,6 +23,7 @@ pub fn get_cursor_position() -> (u16,u16)
pos() pos()
} }
#[cfg(windows)]
/// Get the module specific implementation based on the current platform /// Get the module specific implementation based on the current platform
pub fn get_module<T>(winapi_impl: T, unix_impl: T, context: &mut Context) -> Option<T> pub fn get_module<T>(winapi_impl: T, unix_impl: T, context: &mut Context) -> Option<T>
{ {
@ -34,6 +35,7 @@ pub fn get_module<T>(winapi_impl: T, unix_impl: T, context: &mut Context) -> Opt
use kernel::windows_kernel::ansi_support::try_enable_ansi_support; use kernel::windows_kernel::ansi_support::try_enable_ansi_support;
// Try to enable ansi on windows if not than use WINAPI. // Try to enable ansi on windows if not than use WINAPI.
#[cfg(windows)]
does_support = try_enable_ansi_support(context); does_support = try_enable_ansi_support(context);
if !does_support if !does_support