From bd3c150d9276ffeee8cd9356ba91ad7f5b7fc180 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sat, 6 Mar 2021 18:56:18 -0500 Subject: rename a bit --- examples/async.rs | 2 +- examples/basic.rs | 4 ++-- src/async.rs | 2 +- src/blocking.rs | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 7 ++++-- src/sync.rs | 65 ------------------------------------------------------- 6 files changed, 74 insertions(+), 71 deletions(-) create mode 100644 src/blocking.rs delete mode 100644 src/sync.rs diff --git a/examples/async.rs b/examples/async.rs index e8e4282..7c8769e 100644 --- a/examples/async.rs +++ b/examples/async.rs @@ -1,4 +1,4 @@ -use textmode::Textmode as _; +use textmode::TextmodeExt as _; async fn run(tm: &mut textmode::r#async::Textmode) -> std::io::Result<()> { tm.move_to(5, 5); diff --git a/examples/basic.rs b/examples/basic.rs index 150c9b0..a46d449 100644 --- a/examples/basic.rs +++ b/examples/basic.rs @@ -1,7 +1,7 @@ -use textmode::Textmode as _; +use textmode::TextmodeExt as _; fn main() { - let mut tm = textmode::sync::Textmode::new().unwrap(); + let mut tm = textmode::blocking::Textmode::new().unwrap(); tm.move_to(5, 5); tm.write_str("foo"); diff --git a/src/async.rs b/src/async.rs index a384da8..86f4bdb 100644 --- a/src/async.rs +++ b/src/async.rs @@ -25,7 +25,7 @@ impl super::private::TextmodeImpl for Textmode { } } -impl super::Textmode for Textmode {} +impl super::TextmodeExt for Textmode {} impl Textmode { pub async fn new() -> std::io::Result { diff --git a/src/blocking.rs b/src/blocking.rs new file mode 100644 index 0000000..c06e0a3 --- /dev/null +++ b/src/blocking.rs @@ -0,0 +1,65 @@ +use std::io::Write as _; + +use super::private::TextmodeImpl as _; + +pub struct Textmode { + cur: vt100::Parser, + next: vt100::Parser, +} + +impl super::private::TextmodeImpl for Textmode { + fn cur(&self) -> &vt100::Parser { + &self.cur + } + + fn cur_mut(&mut self) -> &mut vt100::Parser { + &mut self.cur + } + + fn next(&self) -> &vt100::Parser { + &self.next + } + + fn next_mut(&mut self) -> &mut vt100::Parser { + &mut self.next + } +} + +impl super::TextmodeExt for Textmode {} + +impl Textmode { + pub fn new() -> std::io::Result { + let (rows, cols) = match terminal_size::terminal_size() { + Some((terminal_size::Width(w), terminal_size::Height(h))) => { + (h, w) + } + _ => (24, 80), + }; + let cur = vt100::Parser::new(rows, cols, 0); + let next = vt100::Parser::new(rows, cols, 0); + + let self_ = Self { cur, next }; + self_.write_stdout(b"\x1b7\x1b[?47h\x1b[2J\x1b[H\x1b[?25h")?; + Ok(self_) + } + + pub fn refresh(&mut self) -> std::io::Result<()> { + let diff = self.next().screen().contents_diff(self.cur().screen()); + self.write_stdout(&diff)?; + self.cur_mut().process(&diff); + Ok(()) + } + + fn write_stdout(&self, buf: &[u8]) -> std::io::Result<()> { + let mut stdout = std::io::stdout(); + stdout.write_all(buf)?; + stdout.flush()?; + Ok(()) + } +} + +impl Drop for Textmode { + fn drop(&mut self) { + let _ = self.write_stdout(b"\x1b[?47l\x1b8\x1b[?25h"); + } +} diff --git a/src/lib.rs b/src/lib.rs index fcee1e9..4b09880 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,8 +1,11 @@ pub mod color; +pub mod blocking; + #[cfg(feature = "async")] pub mod r#async; -pub mod sync; +#[cfg(feature = "async")] +pub use r#async::Textmode; mod private { pub trait TextmodeImpl { @@ -23,7 +26,7 @@ mod private { } } -pub trait Textmode: private::TextmodeImpl { +pub trait TextmodeExt: private::TextmodeImpl { fn cursor_position(&self) -> (u16, u16) { self.next().screen().cursor_position() } diff --git a/src/sync.rs b/src/sync.rs deleted file mode 100644 index 047036d..0000000 --- a/src/sync.rs +++ /dev/null @@ -1,65 +0,0 @@ -use std::io::Write as _; - -use super::private::TextmodeImpl as _; - -pub struct Textmode { - cur: vt100::Parser, - next: vt100::Parser, -} - -impl super::private::TextmodeImpl for Textmode { - fn cur(&self) -> &vt100::Parser { - &self.cur - } - - fn cur_mut(&mut self) -> &mut vt100::Parser { - &mut self.cur - } - - fn next(&self) -> &vt100::Parser { - &self.next - } - - fn next_mut(&mut self) -> &mut vt100::Parser { - &mut self.next - } -} - -impl super::Textmode for Textmode {} - -impl Textmode { - pub fn new() -> std::io::Result { - let (rows, cols) = match terminal_size::terminal_size() { - Some((terminal_size::Width(w), terminal_size::Height(h))) => { - (h, w) - } - _ => (24, 80), - }; - let cur = vt100::Parser::new(rows, cols, 0); - let next = vt100::Parser::new(rows, cols, 0); - - let self_ = Self { cur, next }; - self_.write_stdout(b"\x1b7\x1b[?47h\x1b[2J\x1b[H\x1b[?25h")?; - Ok(self_) - } - - pub fn refresh(&mut self) -> std::io::Result<()> { - let diff = self.next().screen().contents_diff(self.cur().screen()); - self.write_stdout(&diff)?; - self.cur_mut().process(&diff); - Ok(()) - } - - fn write_stdout(&self, buf: &[u8]) -> std::io::Result<()> { - let mut stdout = std::io::stdout(); - stdout.write_all(buf)?; - stdout.flush()?; - Ok(()) - } -} - -impl Drop for Textmode { - fn drop(&mut self) { - let _ = self.write_stdout(b"\x1b[?47l\x1b8\x1b[?25h"); - } -} -- cgit v1.2.3-54-g00ecf