aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs91
1 files changed, 31 insertions, 60 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 39c201b..fcee1e9 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,53 +1,47 @@
-use std::io::Write as _;
-
pub mod color;
-pub struct Textmode {
- cur: vt100::Parser,
- next: vt100::Parser,
-}
+#[cfg(feature = "async")]
+pub mod r#async;
+pub mod sync;
-impl Textmode {
- pub fn new() -> std::io::Result<Self> {
- 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);
+mod private {
+ pub trait TextmodeImpl {
+ fn cur(&self) -> &vt100::Parser;
+ fn cur_mut(&mut self) -> &mut vt100::Parser;
+ fn next(&self) -> &vt100::Parser;
+ fn next_mut(&mut self) -> &mut vt100::Parser;
- let self_ = Self { cur, next };
- self_.write_stdout(b"\x1b7\x1b[?47h\x1b[2J\x1b[H\x1b[?25h")?;
- Ok(self_)
- }
+ fn write_u16(&mut self, i: u16) {
+ // unwrap is fine because vt100::Parser::write can never fail
+ itoa::write(self.next_mut(), i).unwrap();
+ }
- pub fn cursor_position(&self) -> (u16, u16) {
- self.next.screen().cursor_position()
+ fn write_u8(&mut self, i: u8) {
+ // unwrap is fine because vt100::Parser::write can never fail
+ itoa::write(self.next_mut(), i).unwrap();
+ }
}
+}
- pub fn write(&mut self, buf: &[u8]) {
- self.next.process(buf);
+pub trait Textmode: private::TextmodeImpl {
+ fn cursor_position(&self) -> (u16, u16) {
+ self.next().screen().cursor_position()
}
- pub fn refresh(&mut self) -> std::io::Result<()> {
- let diff = self.next.screen().contents_diff(self.cur.screen());
- self.write_stdout(&diff)?;
- self.cur.process(&diff);
- Ok(())
+ fn write(&mut self, buf: &[u8]) {
+ self.next_mut().process(buf);
}
- pub fn set_size(&mut self, rows: u16, cols: u16) {
- self.cur.set_size(rows, cols);
- self.next.set_size(rows, cols);
+ fn set_size(&mut self, rows: u16, cols: u16) {
+ self.cur_mut().set_size(rows, cols);
+ self.next_mut().set_size(rows, cols);
}
- pub fn write_str(&mut self, text: &str) {
+ fn write_str(&mut self, text: &str) {
self.write(text.as_bytes());
}
- pub fn move_to(&mut self, row: u16, col: u16) {
+ fn move_to(&mut self, row: u16, col: u16) {
self.write(b"\x1b[");
self.write_u16(row);
self.write(b";");
@@ -55,11 +49,11 @@ impl Textmode {
self.write(b"H");
}
- pub fn clear(&mut self) {
+ fn clear(&mut self) {
self.write(b"\x1b[2J");
}
- pub fn set_fgcolor(&mut self, color: vt100::Color) {
+ fn set_fgcolor(&mut self, color: vt100::Color) {
match color {
vt100::Color::Default => {
self.write(b"\x1b[39m");
@@ -87,7 +81,7 @@ impl Textmode {
}
}
- pub fn set_bgcolor(&mut self, color: vt100::Color) {
+ fn set_bgcolor(&mut self, color: vt100::Color) {
match color {
vt100::Color::Default => {
self.write(b"\x1b[49m");
@@ -114,27 +108,4 @@ impl Textmode {
}
}
}
-
- fn write_u16(&mut self, i: u16) {
- // unwrap is fine because vt100::Parser::write can never fail
- itoa::write(&mut self.next, i).unwrap();
- }
-
- fn write_u8(&mut self, i: u8) {
- // unwrap is fine because vt100::Parser::write can never fail
- itoa::write(&mut self.next, i).unwrap();
- }
-
- 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");
- }
}