Unix errors fixed and started with testing

This commit is contained in:
Timon 2018-08-12 20:41:08 +00:00
parent 23dc4f661e
commit f31bb1a656
13 changed files with 67 additions and 38 deletions

View File

@ -107,8 +107,8 @@ pub fn print_font_with_attributes() {
style("Normal text").paint(&screen); style("Normal text").paint(&screen);
style("Bold text").bold().paint(&screen); style("Bold text").bold().paint(&screen);
style("Italic text").italic().paint(&screen); style("Italic text").italic().paint(&screen);
style("Slow blinking text").slow().paint(&screen); style("Slow blinking text").slow_blink().paint(&screen);
style("Rapid blinking text").rapid().paint(&screen); style("Rapid blinking text").rapid_blink().paint(&screen);
style("Hidden text").hidden().paint(&screen); style("Hidden text").hidden().paint(&screen);
style("Underlined text").underlined().paint(&screen); style("Underlined text").underlined().paint(&screen);
style("Reversed text").reverse().paint(&screen); style("Reversed text").reverse().paint(&screen);

View File

@ -17,4 +17,10 @@ extern crate crossterm;
fn main() { fn main() {
// call some test module function // call some test module function
terminal::terminal::resize_terminal();
use crossterm::screen::RawScreen;
// RawScreen::into_raw_mode();
// RawScreen::disable_raw_modes();
} }

View File

@ -34,6 +34,7 @@ pub fn run()
print_welcome_screen(&screen); print_welcome_screen(&screen);
start_algorithm(&screen); start_algorithm(&screen);
drop(screen);
} }
fn start_algorithm(screen: &Screen) fn start_algorithm(screen: &Screen)
@ -66,14 +67,14 @@ fn print_welcome_screen(screen: &Screen)
// clear the screen and print the welcome message. // clear the screen and print the welcome message.
terminal.clear(ClearType::All); terminal.clear(ClearType::All);
cursor.goto(0, 0); cursor.goto(0, 0);
terminal.write(WELCOME_MESSAGE.join("\n")); terminal.write(WELCOME_MESSAGE.join("\n\r"));
cursor.hide(); cursor.hide();
cursor.goto(0, 10); cursor.goto(0, 10);
terminal.write( terminal.write("The first depth search algorithm will start in: Seconds");
"The first depth search algorithm will start in: Seconds\n\
Press `q` to abort the program" cursor.goto(0, 11);
); terminal.write("\nPress `q` to abort the program");
let mut stdin = input.read_async().bytes(); let mut stdin = input.read_async().bytes();

View File

@ -94,16 +94,16 @@ fn main()
// a thread that will log all logs in the queue. // a thread that will log all logs in the queue.
handle_incoming_logs(more_jobs_rx.clone(), queue.clone()); handle_incoming_logs(more_jobs_rx.clone(), queue.clone());
for handle in thread_handles // for handle in thread_handles
{ // {
handle.join(); // handle.join();
} // }
} }
fn handle_incoming_logs(more_jobs_rx: SyncFlagRx, queue: WorkQueue<String>) fn handle_incoming_logs(more_jobs_rx: SyncFlagRx, queue: WorkQueue<String>)
{ {
thread::spawn( move || { thread::spawn( move || {
let mut screen: Screen = Screen::new(); let mut screen: Screen = Screen::default();
// Loop while there's expected to be work, looking for work. // Loop while there's expected to be work, looking for work.
while more_jobs_rx.get().unwrap() { while more_jobs_rx.get().unwrap() {
@ -114,7 +114,6 @@ fn handle_incoming_logs(more_jobs_rx: SyncFlagRx, queue: WorkQueue<String>)
// write the log // write the log
screen.stdout.write_string(log); screen.stdout.write_string(log);
screen.stdout.flush();
} }
std::thread::yield_now(); std::thread::yield_now();
} }
@ -136,7 +135,7 @@ fn log_with_different_threads(more_jobs_tx: SyncFlagTx, queue: WorkQueue<String>
let thread = thread::spawn(move || { let thread = thread::spawn(move || {
// log 400 messages // log 400 messages
for log_entry_count in 1..10000 for log_entry_count in 1..400
{ {
thread_queue.add_work(format!("Log {} from thread {} ",log_entry_count, thread_num)); thread_queue.add_work(format!("Log {} from thread {} ",log_entry_count, thread_num));
more_jobs.set(true); more_jobs.set(true);

View File

@ -18,8 +18,8 @@ impl NoncanonicalModeCommand {
} }
} }
impl IStateCommand for NoncanonicalModeCommand { impl NoncanonicalModeCommand {
fn execute(&mut self) -> Result<()> { pub fn enable(&mut self) -> Result<()> {
// Set noncanonical mode // Set noncanonical mode
if let Ok(orig) = Termios::from_fd(FD_STDIN) { if let Ok(orig) = Termios::from_fd(FD_STDIN) {
let mut noncan = orig.clone(); let mut noncan = orig.clone();
@ -36,7 +36,7 @@ impl IStateCommand for NoncanonicalModeCommand {
Ok(()) Ok(())
} }
fn undo(&mut self) -> Result<()> { pub fn disable(&self) -> Result<()> {
// Disable noncanonical mode // Disable noncanonical mode
if let Ok(orig) = Termios::from_fd(FD_STDIN) { if let Ok(orig) = Termios::from_fd(FD_STDIN) {
let mut noncan = orig.clone(); let mut noncan = orig.clone();
@ -71,7 +71,7 @@ impl RawModeCommand
impl RawModeCommand { impl RawModeCommand {
/// Enables raw mode. /// Enables raw mode.
fn enable(&mut self) -> Result<()> { pub fn enable(&mut self) -> Result<()> {
if let Ok(original_mode) = self.original_mode { if let Ok(original_mode) = self.original_mode {
let mut new_mode = original_mode; let mut new_mode = original_mode;
terminal::make_raw(&mut new_mode); terminal::make_raw(&mut new_mode);
@ -86,8 +86,9 @@ impl RawModeCommand {
} }
/// Disables raw mode. /// Disables raw mode.
fn disable(&self) -> Result<()> { pub fn disable(&self) -> Result<()> {
if let Ok(ref original_mode) = self.original_mode { if let Ok(ref original_mode) = self.original_mode {
let result = terminal::set_terminal_mode(&original_mode)?; let result = terminal::set_terminal_mode(&original_mode)?;
} else { } else {
return Err(Error::new( return Err(Error::new(

View File

@ -87,9 +87,9 @@ impl RawModeCommand
} }
} }
impl IRawScreenCommand for RawModeCommand { impl RawModeCommand {
/// Enables raw mode. /// Enables raw mode.
fn enable(&mut self) -> Result<()> { pub fn enable(&mut self) -> Result<()> {
let input_handle = handle::get_input_handle()?; let input_handle = handle::get_input_handle()?;
let mut dw_mode: DWORD = 0; let mut dw_mode: DWORD = 0;
@ -113,7 +113,7 @@ impl IRawScreenCommand for RawModeCommand {
} }
/// Disables raw mode. /// Disables raw mode.
fn disable(&self) -> Result<()> { pub fn disable(&self) -> Result<()> {
let output_handle = handle::get_input_handle()?; let output_handle = handle::get_input_handle()?;
let mut dw_mode: DWORD = 0; let mut dw_mode: DWORD = 0;

View File

@ -67,5 +67,6 @@ impl Drop for AlternateScreen
{ {
fn drop(&mut self) { fn drop(&mut self) {
self.to_main_screen(); self.to_main_screen();
} }
} }

View File

@ -18,7 +18,14 @@ use super::{functions, Screen, Stdout};
use std::io::{self, Write}; use std::io::{self, Write};
/// A wrapper for the raw terminal state. Which can be used to write to. /// A wrapper for the raw terminal state. Which can be used to write to.
pub struct RawScreen; pub struct RawScreen
{
// #[cfg(not(target_os = "windows"))]
// command: unix_command::RawModeCommand,
// #[cfg(not(target_os = "windows"))]
// command: win_commands::RawModeCommand,
}
impl RawScreen { impl RawScreen {
pub fn into_raw_mode() -> io::Result<()> pub fn into_raw_mode() -> io::Result<()>
@ -28,6 +35,7 @@ impl RawScreen {
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
let mut command = win_commands::RawModeCommand::new(); let mut command = win_commands::RawModeCommand::new();
// command::new();
command.enable()?; command.enable()?;
Ok(()) Ok(())
@ -40,7 +48,9 @@ impl RawScreen {
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
let mut command = win_commands::RawModeCommand::new(); let mut command = win_commands::RawModeCommand::new();
command.disable()?; let a = command.disable();
return Ok(()) return Ok(())
} }
} }

View File

@ -1,8 +1,8 @@
//! This module contains all `unix` specific terminal related logic. //! This module contains all `unix` specific terminal related logic.
use self::libc::{c_int, c_ushort, ioctl, STDOUT_FILENO, TIOCGWINSZ}; use self::libc::{c_int, c_ushort, ioctl, STDOUT_FILENO, TIOCGWINSZ};
use common::commands::unix_command::{EnableRawModeCommand, NoncanonicalModeCommand}; use common::commands::unix_command::{RawModeCommand, NoncanonicalModeCommand};
use {libc, Stdout}; use {libc, Stdout, Screen};
pub use libc::termios; pub use libc::termios;
use std::sync::Arc; use std::sync::Arc;
@ -47,14 +47,14 @@ pub fn terminal_size() -> (u16, u16) {
/// Get the current cursor position. /// Get the current cursor position.
pub fn pos(stdout: &Arc<Stdout>) -> (u16, u16) { pub fn pos(stdout: &Arc<Stdout>) -> (u16, u16) {
let mut crossterm = Crossterm::new(); let mut crossterm = Crossterm::new();
let input = crossterm.input(stdout); let input = crossterm.input(&Screen::default());
let delimiter = b'R'; let delimiter = b'R';
let mut stdin = input.read_until_async(delimiter); let mut stdin = input.read_until_async(delimiter);
// Where is the cursor? // Where is the cursor?
// Use `ESC [ 6 n`. // Use `ESC [ 6 n`.
crossterm.active_screen.write_str("\x1B[6n"); stdout.write_str("\x1B[6n");
let mut buf: [u8; 1] = [0]; let mut buf: [u8; 1] = [0];
let mut read_chars = Vec::new(); let mut read_chars = Vec::new();
@ -105,15 +105,26 @@ pub fn make_raw(termios: &mut Termios) {
unsafe { cfmakeraw(termios) } unsafe { cfmakeraw(termios) }
} }
static mut ORIGINAL_TERMINAL_MODE: Option<Termios> = None;
/// Get the current terminal mode. /// Get the current terminal mode.
pub fn get_terminal_mode() -> io::Result<Termios> { pub fn get_terminal_mode() -> io::Result<Termios> {
extern "C" { extern "C" {
pub fn tcgetattr(fd: c_int, termptr: *mut Termios) -> c_int; pub fn tcgetattr(fd: c_int, termptr: *mut Termios) -> c_int;
} }
unsafe { unsafe {
let mut termios = mem::zeroed(); if let Some(original_mode) = ORIGINAL_TERMINAL_MODE
is_true(tcgetattr(0, &mut termios))?; {
Ok(termios) return Ok(original_mode.clone())
}
else {
let mut termios = mem::zeroed();
is_true(tcgetattr(0, &mut termios))?;
ORIGINAL_TERMINAL_MODE = Some(termios.clone());
return Ok(termios)
}
} }
} }

View File

@ -17,7 +17,7 @@ impl UnixInput {
} }
impl ITerminalInput for UnixInput { impl ITerminalInput for UnixInput {
fn read_line(&self, screen_manger: &Stdout) -> io::Result<String> { fn read_line(&self, screen_manger: &Arc<Stdout>) -> io::Result<String> {
let mut rv = String::new(); let mut rv = String::new();
io::stdin().read_line(&mut rv)?; io::stdin().read_line(&mut rv)?;
let len = rv.trim_right_matches(&['\r', '\n'][..]).len(); let len = rv.trim_right_matches(&['\r', '\n'][..]).len();

View File

@ -3,6 +3,7 @@
use super::*; use super::*;
use std::io; use std::io;
use Screen;
/// Struct that stores an specific platform implementation for color related actions. /// Struct that stores an specific platform implementation for color related actions.
/// ///
@ -116,6 +117,6 @@ impl<'terminal> TerminalColor {
} }
/// Get an Terminal Color implementation whereon color related actions can be performed. /// Get an Terminal Color implementation whereon color related actions can be performed.
pub fn color(stdout: &Arc<Stdout>) -> TerminalColor { pub fn color(screen: &Screen) -> TerminalColor {
TerminalColor::new(stdout) TerminalColor::new(&screen.stdout)
} }

View File

@ -17,7 +17,7 @@ use std::str::FromStr;
use std::sync::Arc; use std::sync::Arc;
use std::fmt::Display; use std::fmt::Display;
pub use self::color::TerminalColor; pub use self::color::{TerminalColor, color};
pub use self::objectstyle::ObjectStyle; pub use self::objectstyle::ObjectStyle;
pub use self::styledobject::StyledObject; pub use self::styledobject::StyledObject;
use super::{functions, Stdout}; use super::{functions, Stdout};

View File

@ -144,7 +144,7 @@ impl<D: Display> StyledObject<D> {
pub fn paint(&self, screen: &Screen) pub fn paint(&self, screen: &Screen)
{ {
let mut colored_terminal = super::super::super::style::color::color(&screen.stdout); let mut colored_terminal = super::super::super::style::color::color(&screen);
let mut reset = true; let mut reset = true;
if let Some(bg) = self.object_style.bg_color { if let Some(bg) = self.object_style.bg_color {
@ -159,8 +159,7 @@ impl<D: Display> StyledObject<D> {
#[cfg(unix)] #[cfg(unix)]
for attr in self.object_style.attrs.iter() { for attr in self.object_style.attrs.iter() {
self.stdout screen.stdout.write_string(format!(csi!("{}m"), *attr as i16));
.write_string(format!(csi!("{}m"), *attr as i16));
reset = true; reset = true;
} }