This commit is contained in:
Timon_Post 2019-05-10 20:05:53 +02:00
parent 83b4a27ca3
commit 4a0d748baa
7 changed files with 57 additions and 18 deletions

View File

@ -34,7 +34,7 @@ members = [
[dependencies] [dependencies]
crossterm_screen = { optional = true, path = "./crossterm_screen" } crossterm_screen = { optional = true, path = "./crossterm_screen" }
crossterm_cursor = { optional = true, path = "./crossterm_cursor" } 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_style = { optional = true, path = "./crossterm_style" }
crossterm_input = { optional = true, path = "./crossterm_input" } crossterm_input = { optional = true, path = "./crossterm_input" }
crossterm_utils = { optional = false, path = "./crossterm_utils" } crossterm_utils = { optional = false, path = "./crossterm_utils" }

View File

@ -7,19 +7,29 @@ use crate::sys::get_cursor_position;
use std::io::Write; use std::io::Write;
use crossterm_utils::Result; 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. /// This struct is an ANSI implementation for cursor related actions.
pub struct AnsiCursor; pub struct AnsiCursor {
stdout: RwLock<BufWriter<Stdout>>
}
impl AnsiCursor { impl AnsiCursor {
pub fn new() -> AnsiCursor { pub fn new() -> AnsiCursor {
AnsiCursor AnsiCursor {
stdout: RwLock::new(BufWriter::new(stdout()))
}
} }
} }
impl ITerminalCursor for AnsiCursor { impl ITerminalCursor for AnsiCursor {
fn goto(&self, x: u16, y: u16) -> Result<()> { 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(()) Ok(())
} }

View File

@ -16,4 +16,4 @@ winapi = { version = "0.3.7", features = ["wincon"] }
crossterm_winapi = "0.1.2" crossterm_winapi = "0.1.2"
[dependencies] [dependencies]
crossterm_utils = "0.2.1" crossterm_utils = {path = "../crossterm_utils"}

View File

@ -5,31 +5,37 @@ use crate::{Color, ITerminalColor};
use crossterm_utils::Result; use crossterm_utils::Result;
use crate::Colored; use crate::Colored;
use std::io;
use std::io::Write; 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. /// This struct is an ANSI escape code implementation for color related actions.
pub struct AnsiColor; pub struct AnsiColor {
stdout: RwLock<BufWriter<Stdout>>
}
impl AnsiColor { impl AnsiColor {
pub fn new() -> AnsiColor { pub fn new() -> AnsiColor {
AnsiColor {} AnsiColor { stdout: RwLock::new(BufWriter::new(io::stdout())) }
} }
} }
impl ITerminalColor for AnsiColor { impl ITerminalColor for AnsiColor {
fn set_fg(&self, fg_color: Color) -> Result<()> { fn set_fg(&self, fg_color: Color) -> Result<()> {
write_cout!(&format!( write_cout1!(&format!(
csi!("{}m"), csi!("{}m"),
self.color_value(Colored::Fg(fg_color)) self.color_value(Colored::Fg(fg_color)),
))?; ), &mut self.stdout.write().unwrap())?;
Ok(()) Ok(())
} }
fn set_bg(&self, bg_color: Color) -> Result<()> { fn set_bg(&self, bg_color: Color) -> Result<()> {
write_cout!(&format!( write_cout1!(&format!(
csi!("{}m"), csi!("{}m"),
self.color_value(Colored::Bg(bg_color)) self.color_value(Colored::Bg(bg_color))
))?; ), &mut self.stdout.write().unwrap())?;
Ok(()) Ok(())
} }

View File

@ -18,5 +18,5 @@ crossterm_winapi = "0.1.2"
libc = "0.2.51" libc = "0.2.51"
[dependencies] [dependencies]
crossterm_utils = "0.2.1" crossterm_utils = {path = "../crossterm_utils"}
crossterm_cursor = "0.2.1" crossterm_cursor = {path = "../crossterm_cursor"}

View File

@ -126,7 +126,7 @@ impl Terminal {
/// ///
/// This will also flush the standard output. /// This will also flush the standard output.
pub fn write<D: fmt::Display>(&self, value: D) -> Result<usize> { pub fn write<D: fmt::Display>(&self, value: D) -> Result<usize> {
write_cout!(value)?; // write_cout!(value)?;
Ok(0) Ok(0)
} }
} }

View File

@ -4,22 +4,45 @@ macro_rules! csi {
($( $l:expr ),*) => { concat!("\x1B[", $( $l ),*) }; ($( $l:expr ),*) => { concat!("\x1B[", $( $l ),*) };
} }
pub static mut flush_count: u32 = 0;
/// Write a string to standard output whereafter the screen will be flushed. /// Write a string to standard output whereafter the screen will be flushed.
#[macro_export] #[macro_export]
macro_rules! write_cout { macro_rules! write_cout {
($string:expr) => {{ ($string:expr) => {{
let stdout = ::std::io::stdout();
let mut stdout = stdout.lock();
let mut size = 0; let mut size = 0;
let result = write!(::std::io::stdout(), "{}", $string); let result = stdout.write($string.as_bytes());
match result { match result {
Ok(size) => size, Ok(size) => size,
Err(e) => return Err(crossterm_utils::ErrorKind::IoError(e)), Err(e) => return Err(crossterm_utils::ErrorKind::IoError(e)),
}; };
match ::std::io::stdout().flush() { match stdout.flush() {
Ok(_) => Ok(size), Ok(_) => Ok(size),
Err(e) => Err(crossterm_utils::ErrorKind::IoError(e)), 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
}};
}