Unix errors fixed and started with testing
This commit is contained in:
parent
23dc4f661e
commit
f31bb1a656
@ -107,8 +107,8 @@ pub fn print_font_with_attributes() {
|
||||
style("Normal text").paint(&screen);
|
||||
style("Bold text").bold().paint(&screen);
|
||||
style("Italic text").italic().paint(&screen);
|
||||
style("Slow blinking text").slow().paint(&screen);
|
||||
style("Rapid blinking text").rapid().paint(&screen);
|
||||
style("Slow blinking text").slow_blink().paint(&screen);
|
||||
style("Rapid blinking text").rapid_blink().paint(&screen);
|
||||
style("Hidden text").hidden().paint(&screen);
|
||||
style("Underlined text").underlined().paint(&screen);
|
||||
style("Reversed text").reverse().paint(&screen);
|
||||
|
@ -17,4 +17,10 @@ extern crate crossterm;
|
||||
|
||||
fn main() {
|
||||
// call some test module function
|
||||
|
||||
terminal::terminal::resize_terminal();
|
||||
|
||||
use crossterm::screen::RawScreen;
|
||||
// RawScreen::into_raw_mode();
|
||||
// RawScreen::disable_raw_modes();
|
||||
}
|
||||
|
@ -34,6 +34,7 @@ pub fn run()
|
||||
print_welcome_screen(&screen);
|
||||
|
||||
start_algorithm(&screen);
|
||||
drop(screen);
|
||||
}
|
||||
|
||||
fn start_algorithm(screen: &Screen)
|
||||
@ -66,14 +67,14 @@ fn print_welcome_screen(screen: &Screen)
|
||||
// clear the screen and print the welcome message.
|
||||
terminal.clear(ClearType::All);
|
||||
cursor.goto(0, 0);
|
||||
terminal.write(WELCOME_MESSAGE.join("\n"));
|
||||
terminal.write(WELCOME_MESSAGE.join("\n\r"));
|
||||
|
||||
cursor.hide();
|
||||
cursor.goto(0, 10);
|
||||
terminal.write(
|
||||
"The first depth search algorithm will start in: Seconds\n\
|
||||
Press `q` to abort the program"
|
||||
);
|
||||
terminal.write("The first depth search algorithm will start in: Seconds");
|
||||
|
||||
cursor.goto(0, 11);
|
||||
terminal.write("\nPress `q` to abort the program");
|
||||
|
||||
let mut stdin = input.read_async().bytes();
|
||||
|
||||
|
@ -94,16 +94,16 @@ fn main()
|
||||
// a thread that will log all logs in the queue.
|
||||
handle_incoming_logs(more_jobs_rx.clone(), queue.clone());
|
||||
|
||||
for handle in thread_handles
|
||||
{
|
||||
handle.join();
|
||||
}
|
||||
// for handle in thread_handles
|
||||
// {
|
||||
// handle.join();
|
||||
// }
|
||||
}
|
||||
|
||||
fn handle_incoming_logs(more_jobs_rx: SyncFlagRx, queue: WorkQueue<String>)
|
||||
{
|
||||
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.
|
||||
while more_jobs_rx.get().unwrap() {
|
||||
@ -114,7 +114,6 @@ fn handle_incoming_logs(more_jobs_rx: SyncFlagRx, queue: WorkQueue<String>)
|
||||
|
||||
// write the log
|
||||
screen.stdout.write_string(log);
|
||||
screen.stdout.flush();
|
||||
}
|
||||
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 || {
|
||||
|
||||
// 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));
|
||||
more_jobs.set(true);
|
||||
|
@ -18,8 +18,8 @@ impl NoncanonicalModeCommand {
|
||||
}
|
||||
}
|
||||
|
||||
impl IStateCommand for NoncanonicalModeCommand {
|
||||
fn execute(&mut self) -> Result<()> {
|
||||
impl NoncanonicalModeCommand {
|
||||
pub fn enable(&mut self) -> Result<()> {
|
||||
// Set noncanonical mode
|
||||
if let Ok(orig) = Termios::from_fd(FD_STDIN) {
|
||||
let mut noncan = orig.clone();
|
||||
@ -36,7 +36,7 @@ impl IStateCommand for NoncanonicalModeCommand {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn undo(&mut self) -> Result<()> {
|
||||
pub fn disable(&self) -> Result<()> {
|
||||
// Disable noncanonical mode
|
||||
if let Ok(orig) = Termios::from_fd(FD_STDIN) {
|
||||
let mut noncan = orig.clone();
|
||||
@ -71,7 +71,7 @@ impl RawModeCommand
|
||||
|
||||
impl RawModeCommand {
|
||||
/// Enables raw mode.
|
||||
fn enable(&mut self) -> Result<()> {
|
||||
pub fn enable(&mut self) -> Result<()> {
|
||||
if let Ok(original_mode) = self.original_mode {
|
||||
let mut new_mode = original_mode;
|
||||
terminal::make_raw(&mut new_mode);
|
||||
@ -86,8 +86,9 @@ impl RawModeCommand {
|
||||
}
|
||||
|
||||
/// Disables raw mode.
|
||||
fn disable(&self) -> Result<()> {
|
||||
pub fn disable(&self) -> Result<()> {
|
||||
if let Ok(ref original_mode) = self.original_mode {
|
||||
|
||||
let result = terminal::set_terminal_mode(&original_mode)?;
|
||||
} else {
|
||||
return Err(Error::new(
|
||||
|
@ -87,9 +87,9 @@ impl RawModeCommand
|
||||
}
|
||||
}
|
||||
|
||||
impl IRawScreenCommand for RawModeCommand {
|
||||
impl RawModeCommand {
|
||||
/// Enables raw mode.
|
||||
fn enable(&mut self) -> Result<()> {
|
||||
pub fn enable(&mut self) -> Result<()> {
|
||||
let input_handle = handle::get_input_handle()?;
|
||||
|
||||
let mut dw_mode: DWORD = 0;
|
||||
@ -113,7 +113,7 @@ impl IRawScreenCommand for RawModeCommand {
|
||||
}
|
||||
|
||||
/// Disables raw mode.
|
||||
fn disable(&self) -> Result<()> {
|
||||
pub fn disable(&self) -> Result<()> {
|
||||
let output_handle = handle::get_input_handle()?;
|
||||
|
||||
let mut dw_mode: DWORD = 0;
|
||||
|
@ -67,5 +67,6 @@ impl Drop for AlternateScreen
|
||||
{
|
||||
fn drop(&mut self) {
|
||||
self.to_main_screen();
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -18,7 +18,14 @@ use super::{functions, Screen, Stdout};
|
||||
use std::io::{self, Write};
|
||||
|
||||
/// 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 {
|
||||
pub fn into_raw_mode() -> io::Result<()>
|
||||
@ -28,6 +35,7 @@ impl RawScreen {
|
||||
#[cfg(target_os = "windows")]
|
||||
let mut command = win_commands::RawModeCommand::new();
|
||||
|
||||
// command::new();
|
||||
command.enable()?;
|
||||
|
||||
Ok(())
|
||||
@ -40,7 +48,9 @@ impl RawScreen {
|
||||
#[cfg(target_os = "windows")]
|
||||
let mut command = win_commands::RawModeCommand::new();
|
||||
|
||||
command.disable()?;
|
||||
let a = command.disable();
|
||||
|
||||
|
||||
return Ok(())
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +1,8 @@
|
||||
//! This module contains all `unix` specific terminal related logic.
|
||||
|
||||
use self::libc::{c_int, c_ushort, ioctl, STDOUT_FILENO, TIOCGWINSZ};
|
||||
use common::commands::unix_command::{EnableRawModeCommand, NoncanonicalModeCommand};
|
||||
use {libc, Stdout};
|
||||
use common::commands::unix_command::{RawModeCommand, NoncanonicalModeCommand};
|
||||
use {libc, Stdout, Screen};
|
||||
pub use libc::termios;
|
||||
|
||||
use std::sync::Arc;
|
||||
@ -47,14 +47,14 @@ pub fn terminal_size() -> (u16, u16) {
|
||||
/// Get the current cursor position.
|
||||
pub fn pos(stdout: &Arc<Stdout>) -> (u16, u16) {
|
||||
let mut crossterm = Crossterm::new();
|
||||
let input = crossterm.input(stdout);
|
||||
let input = crossterm.input(&Screen::default());
|
||||
|
||||
let delimiter = b'R';
|
||||
let mut stdin = input.read_until_async(delimiter);
|
||||
|
||||
// Where is the cursor?
|
||||
// Use `ESC [ 6 n`.
|
||||
crossterm.active_screen.write_str("\x1B[6n");
|
||||
stdout.write_str("\x1B[6n");
|
||||
|
||||
let mut buf: [u8; 1] = [0];
|
||||
let mut read_chars = Vec::new();
|
||||
@ -105,15 +105,26 @@ pub fn make_raw(termios: &mut Termios) {
|
||||
unsafe { cfmakeraw(termios) }
|
||||
}
|
||||
|
||||
static mut ORIGINAL_TERMINAL_MODE: Option<Termios> = None;
|
||||
|
||||
/// Get the current terminal mode.
|
||||
pub fn get_terminal_mode() -> io::Result<Termios> {
|
||||
extern "C" {
|
||||
pub fn tcgetattr(fd: c_int, termptr: *mut Termios) -> c_int;
|
||||
}
|
||||
unsafe {
|
||||
if let Some(original_mode) = ORIGINAL_TERMINAL_MODE
|
||||
{
|
||||
return Ok(original_mode.clone())
|
||||
}
|
||||
else {
|
||||
let mut termios = mem::zeroed();
|
||||
is_true(tcgetattr(0, &mut termios))?;
|
||||
Ok(termios)
|
||||
ORIGINAL_TERMINAL_MODE = Some(termios.clone());
|
||||
|
||||
return Ok(termios)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -17,7 +17,7 @@ impl 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();
|
||||
io::stdin().read_line(&mut rv)?;
|
||||
let len = rv.trim_right_matches(&['\r', '\n'][..]).len();
|
||||
|
@ -3,6 +3,7 @@
|
||||
|
||||
use super::*;
|
||||
use std::io;
|
||||
use Screen;
|
||||
|
||||
/// 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.
|
||||
pub fn color(stdout: &Arc<Stdout>) -> TerminalColor {
|
||||
TerminalColor::new(stdout)
|
||||
pub fn color(screen: &Screen) -> TerminalColor {
|
||||
TerminalColor::new(&screen.stdout)
|
||||
}
|
||||
|
@ -17,7 +17,7 @@ use std::str::FromStr;
|
||||
use std::sync::Arc;
|
||||
use std::fmt::Display;
|
||||
|
||||
pub use self::color::TerminalColor;
|
||||
pub use self::color::{TerminalColor, color};
|
||||
pub use self::objectstyle::ObjectStyle;
|
||||
pub use self::styledobject::StyledObject;
|
||||
use super::{functions, Stdout};
|
||||
|
@ -144,7 +144,7 @@ impl<D: Display> StyledObject<D> {
|
||||
|
||||
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;
|
||||
|
||||
if let Some(bg) = self.object_style.bg_color {
|
||||
@ -159,8 +159,7 @@ impl<D: Display> StyledObject<D> {
|
||||
|
||||
#[cfg(unix)]
|
||||
for attr in self.object_style.attrs.iter() {
|
||||
self.stdout
|
||||
.write_string(format!(csi!("{}m"), *attr as i16));
|
||||
screen.stdout.write_string(format!(csi!("{}m"), *attr as i16));
|
||||
reset = true;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user