From 341fed83d687c5285dd074d15356e68982822903 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sun, 7 Mar 2021 01:21:07 -0500 Subject: simplify, add some more methods --- src/async.rs | 6 ++---- src/blocking.rs | 4 ++-- src/lib.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 6 deletions(-) diff --git a/src/async.rs b/src/async.rs index 86f4bdb..5861443 100644 --- a/src/async.rs +++ b/src/async.rs @@ -39,16 +39,14 @@ impl Textmode { 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") - .await?; + self_.write_stdout(super::INIT).await?; Ok(self_) } // TODO: without async drop or async closures, i'm not sure how to do // better than this pub async fn cleanup(&mut self) -> std::io::Result<()> { - self.write_stdout(b"\x1b[?47l\x1b8\x1b[?25h").await + self.write_stdout(super::DEINIT).await } pub async fn refresh(&mut self) -> std::io::Result<()> { diff --git a/src/blocking.rs b/src/blocking.rs index c06e0a3..fc608ad 100644 --- a/src/blocking.rs +++ b/src/blocking.rs @@ -39,7 +39,7 @@ impl Textmode { 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")?; + self_.write_stdout(super::INIT)?; Ok(self_) } @@ -60,6 +60,6 @@ impl Textmode { impl Drop for Textmode { fn drop(&mut self) { - let _ = self.write_stdout(b"\x1b[?47l\x1b8\x1b[?25h"); + let _ = self.write_stdout(super::DEINIT); } } diff --git a/src/lib.rs b/src/lib.rs index 7f9aac4..486597d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,6 +7,9 @@ pub mod r#async; #[cfg(feature = "async")] pub use r#async::Textmode; +const INIT: &[u8] = b"\x1b7\x1b[?47h\x1b[2J\x1b[H\x1b[?25h"; +const DEINIT: &[u8] = b"\x1b[?47l\x1b8\x1b[?25h"; + mod private { pub trait TextmodeImpl { fn cur(&self) -> &vt100::Parser; @@ -74,6 +77,10 @@ pub trait TextmodeExt: private::TextmodeImpl { self.write(b"\x1b["); self.write_u8(30 + i); self.write(b"m"); + } else if i < 16 { + self.write(b"\x1b["); + self.write_u8(82 + i); + self.write(b"m"); } else { self.write(b"\x1b[38;5;"); self.write_u8(i); @@ -102,6 +109,10 @@ pub trait TextmodeExt: private::TextmodeImpl { self.write(b"\x1b["); self.write_u8(40 + i); self.write(b"m"); + } else if i < 16 { + self.write(b"\x1b["); + self.write_u8(92 + i); + self.write(b"m"); } else { self.write(b"\x1b[48;5;"); self.write_u8(i); @@ -119,4 +130,36 @@ pub trait TextmodeExt: private::TextmodeImpl { } } } + + fn set_bold(&mut self, bold: bool) { + if bold { + self.write(b"\x1b[1m"); + } else { + self.write(b"\x1b[22m"); + } + } + + fn set_italic(&mut self, italic: bool) { + if italic { + self.write(b"\x1b[3m"); + } else { + self.write(b"\x1b[23m"); + } + } + + fn set_underline(&mut self, underline: bool) { + if underline { + self.write(b"\x1b[4m"); + } else { + self.write(b"\x1b[24m"); + } + } + + fn set_inverse(&mut self, inverse: bool) { + if inverse { + self.write(b"\x1b[7m"); + } else { + self.write(b"\x1b[27m"); + } + } } -- cgit v1.2.3-54-g00ecf