aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2021-03-07 01:21:07 -0500
committerJesse Luehrs <doy@tozt.net>2021-03-07 01:21:07 -0500
commit341fed83d687c5285dd074d15356e68982822903 (patch)
tree623024f0978f61978ff0f4e82dce65f698d2ab6a
parent8927ec8dafaaca3a14b55bb680b4f7f92fa1ed8b (diff)
downloadtextmode-341fed83d687c5285dd074d15356e68982822903.tar.gz
textmode-341fed83d687c5285dd074d15356e68982822903.zip
simplify, add some more methods
-rw-r--r--src/async.rs6
-rw-r--r--src/blocking.rs4
-rw-r--r--src/lib.rs43
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");
+ }
+ }
}