This commit is contained in:
Timon_Post 2019-05-15 17:01:22 +02:00
parent 4a0d748baa
commit 472614be37
5 changed files with 17 additions and 47 deletions

View File

@ -7,29 +7,19 @@ 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_cout1!(format!(csi!("{};{}H"), y + 1, x + 1), &mut self.stdout.write().unwrap())?; write_cout!(format!(csi!("{};{}H"), y + 1, x + 1))?;
// write_cout!(format!(csi!("{};{}H"), y + 1, x + 1))?;
Ok(()) Ok(())
} }

View File

@ -6,36 +6,34 @@ use crossterm_utils::Result;
use crate::Colored; use crate::Colored;
use std::io; use std::io;
use std::io::Write;
use std::io::Stdout;
use std::io::BufWriter; use std::io::BufWriter;
use std::io::Stdout;
use std::io::Write;
use std::sync::RwLock; 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 { stdout: RwLock::new(BufWriter::new(io::stdout())) } AnsiColor
} }
} }
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_cout1!(&format!( write_cout!(&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_cout1!(&format!( write_cout!(&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

@ -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!(format!("{}", value))?;
Ok(0) Ok(0)
} }
} }

View File

@ -16,7 +16,7 @@ macro_rules! write_cout {
let result = stdout.write($string.as_bytes()); let result = stdout.write($string.as_bytes());
match result { size += 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)),
}; };
@ -24,25 +24,6 @@ macro_rules! write_cout {
match 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
}};
}

View File

@ -1,10 +1,11 @@
# Changes crossterm_input 0.9.4 # Changes crossterm 0.9.4
- Reset foreground and background color individually. [PR](https://github.com/TimonPost/crossterm/pull/138) - Reset foreground and background color individually. [PR](https://github.com/TimonPost/crossterm/pull/138)
- Backtap input support. [PR](https://github.com/TimonPost/crossterm/pull/129) - Backtap input support. [PR](https://github.com/TimonPost/crossterm/pull/129)
- Corrected white/grey and added dark grey. - Corrected white/grey and added dark grey.
- Fixed getting cursor position with raw screen enabled. [PR](https://github.com/TimonPost/crossterm/pull/134) - Fixed getting cursor position with raw screen enabled. [PR](https://github.com/TimonPost/crossterm/pull/134)
- Removed one redundant stdout lock
# Changes crossterm_input 0.9.3 # Changes crossterm 0.9.3
- Removed println from `SyncReader` - Removed println from `SyncReader`
## Changes crossterm 0.9.2 ## Changes crossterm 0.9.2