diff --git a/Cargo.toml b/Cargo.toml index 75f5d23..6d62f45 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,7 +34,7 @@ members = [ [dependencies] crossterm_screen = { optional = true, path = "./crossterm_screen" } crossterm_cursor = { optional = true, path = "./crossterm_cursor" } -crossterm_terminal = { optional = true, version = "0.2.2" } +crossterm_terminal = { optional = true, path = "./crossterm_terminal" } crossterm_style = { optional = true, path = "./crossterm_style" } crossterm_input = { optional = true, path = "./crossterm_input" } crossterm_utils = { optional = false, path = "./crossterm_utils" } diff --git a/crossterm_cursor/src/cursor/ansi_cursor.rs b/crossterm_cursor/src/cursor/ansi_cursor.rs index c7d7c4a..d44854e 100644 --- a/crossterm_cursor/src/cursor/ansi_cursor.rs +++ b/crossterm_cursor/src/cursor/ansi_cursor.rs @@ -7,19 +7,29 @@ use crate::sys::get_cursor_position; use std::io::Write; use crossterm_utils::Result; +use std::io::Stdout; +use std::io::stdout; +use std::io::StdoutLock; +use std::io::BufWriter; +use std::sync::RwLock; /// This struct is an ANSI implementation for cursor related actions. -pub struct AnsiCursor; +pub struct AnsiCursor { + stdout: RwLock> +} impl AnsiCursor { pub fn new() -> AnsiCursor { - AnsiCursor + AnsiCursor { + stdout: RwLock::new(BufWriter::new(stdout())) + } } } impl ITerminalCursor for AnsiCursor { fn goto(&self, x: u16, y: u16) -> Result<()> { - write_cout!(format!(csi!("{};{}H"), y + 1, x + 1))?; + write_cout1!(format!(csi!("{};{}H"), y + 1, x + 1), &mut self.stdout.write().unwrap())?; +// write_cout!(format!(csi!("{};{}H"), y + 1, x + 1))?; Ok(()) } diff --git a/crossterm_style/Cargo.toml b/crossterm_style/Cargo.toml index 284a982..86d3e97 100644 --- a/crossterm_style/Cargo.toml +++ b/crossterm_style/Cargo.toml @@ -16,4 +16,4 @@ winapi = { version = "0.3.7", features = ["wincon"] } crossterm_winapi = "0.1.2" [dependencies] -crossterm_utils = "0.2.1" \ No newline at end of file +crossterm_utils = {path = "../crossterm_utils"} \ No newline at end of file diff --git a/crossterm_style/src/ansi_color.rs b/crossterm_style/src/ansi_color.rs index 6fb4a3a..f4c30e1 100644 --- a/crossterm_style/src/ansi_color.rs +++ b/crossterm_style/src/ansi_color.rs @@ -5,31 +5,37 @@ use crate::{Color, ITerminalColor}; use crossterm_utils::Result; use crate::Colored; +use std::io; use std::io::Write; +use std::io::Stdout; +use std::io::BufWriter; +use std::sync::RwLock; /// This struct is an ANSI escape code implementation for color related actions. -pub struct AnsiColor; +pub struct AnsiColor { + stdout: RwLock> +} impl AnsiColor { pub fn new() -> AnsiColor { - AnsiColor {} + AnsiColor { stdout: RwLock::new(BufWriter::new(io::stdout())) } } } impl ITerminalColor for AnsiColor { fn set_fg(&self, fg_color: Color) -> Result<()> { - write_cout!(&format!( + write_cout1!(&format!( csi!("{}m"), - self.color_value(Colored::Fg(fg_color)) - ))?; + self.color_value(Colored::Fg(fg_color)), + ), &mut self.stdout.write().unwrap())?; Ok(()) } fn set_bg(&self, bg_color: Color) -> Result<()> { - write_cout!(&format!( + write_cout1!(&format!( csi!("{}m"), self.color_value(Colored::Bg(bg_color)) - ))?; + ), &mut self.stdout.write().unwrap())?; Ok(()) } diff --git a/crossterm_terminal/Cargo.toml b/crossterm_terminal/Cargo.toml index 87d6918..380f51b 100644 --- a/crossterm_terminal/Cargo.toml +++ b/crossterm_terminal/Cargo.toml @@ -18,5 +18,5 @@ crossterm_winapi = "0.1.2" libc = "0.2.51" [dependencies] -crossterm_utils = "0.2.1" -crossterm_cursor = "0.2.1" \ No newline at end of file +crossterm_utils = {path = "../crossterm_utils"} +crossterm_cursor = {path = "../crossterm_cursor"} \ No newline at end of file diff --git a/crossterm_terminal/src/terminal/terminal.rs b/crossterm_terminal/src/terminal/terminal.rs index 7faaec2..da82d58 100644 --- a/crossterm_terminal/src/terminal/terminal.rs +++ b/crossterm_terminal/src/terminal/terminal.rs @@ -126,7 +126,7 @@ impl Terminal { /// /// This will also flush the standard output. pub fn write(&self, value: D) -> Result { - write_cout!(value)?; +// write_cout!(value)?; Ok(0) } } diff --git a/crossterm_utils/src/macros.rs b/crossterm_utils/src/macros.rs index c2fbcf2..f26f011 100644 --- a/crossterm_utils/src/macros.rs +++ b/crossterm_utils/src/macros.rs @@ -4,22 +4,45 @@ macro_rules! csi { ($( $l:expr ),*) => { concat!("\x1B[", $( $l ),*) }; } +pub static mut flush_count: u32 = 0; + /// Write a string to standard output whereafter the screen will be flushed. #[macro_export] macro_rules! write_cout { ($string:expr) => {{ + let stdout = ::std::io::stdout(); + let mut stdout = stdout.lock(); let mut size = 0; - let result = write!(::std::io::stdout(), "{}", $string); + let result = stdout.write($string.as_bytes()); match result { Ok(size) => size, Err(e) => return Err(crossterm_utils::ErrorKind::IoError(e)), }; - match ::std::io::stdout().flush() { + match stdout.flush() { Ok(_) => Ok(size), Err(e) => Err(crossterm_utils::ErrorKind::IoError(e)), - } + }; + + result }}; } + +/// Write a string to standard output whereafter the screen will be flushed. +#[macro_export] +macro_rules! write_cout1 { + ($string:expr, $stdout:expr) => {{ + let mut size = 0; + + let result = write!($stdout, "{}", $string); + + match result { + Ok(size) => size, + Err(e) => return Err(crossterm_utils::ErrorKind::IoError(e)), + }; + + result + }}; +} \ No newline at end of file