diff options
author | Jesse Luehrs <doy@tozt.net> | 2021-11-13 15:23:43 -0500 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2021-11-13 15:23:43 -0500 |
commit | 4490ced4e32f47863539d1d346e1619ba581322b (patch) | |
tree | 7439fc99bf7136eb814a66c6e6ec77a45ec98c04 | |
parent | 45af291f81f33f18080532d4aacaa27074503d20 (diff) | |
download | textmode-4490ced4e32f47863539d1d346e1619ba581322b.tar.gz textmode-4490ced4e32f47863539d1d346e1619ba581322b.zip |
add move_relative
-rw-r--r-- | CHANGELOG.md | 1 | ||||
-rw-r--r-- | examples/basic.rs | 4 | ||||
-rw-r--r-- | src/lib.rs | 25 |
3 files changed, 28 insertions, 2 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 7a04a0d..a18d37f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### Added * `hard_refresh` to fully redraw the screen +* `move_relative` to move the cursor relative to its location ### Changed diff --git a/examples/basic.rs b/examples/basic.rs index 624292f..dce12cc 100644 --- a/examples/basic.rs +++ b/examples/basic.rs @@ -14,7 +14,7 @@ async fn run( tm.move_to(8, 8); tm.set_fgcolor(textmode::color::GREEN); tm.write_str("bar"); - tm.move_to(11, 11); + tm.move_relative(3, 0); tm.set_fgcolor(textmode::Color::Default); tm.write_str("baz"); input.read_key().await?; @@ -47,7 +47,7 @@ fn main() { tm.move_to(8, 8); tm.set_fgcolor(textmode::color::GREEN); tm.write_str("bar"); - tm.move_to(11, 11); + tm.move_relative(3, 0); tm.set_fgcolor(textmode::Color::Default); tm.write_str("baz"); input.read_key().unwrap(); @@ -122,6 +122,31 @@ pub trait Textmode: private::Output { self.write(b"H"); } + fn move_relative(&mut self, row_offset: i16, col_offset: i16) { + let abs_row_offset = row_offset.unsigned_abs(); + let abs_col_offset = col_offset.unsigned_abs(); + if row_offset > 0 { + self.write(b"\x1b["); + self.write_u16(abs_row_offset); + self.write(b"B") + } + if row_offset < 0 { + self.write(b"\x1b["); + self.write_u16(abs_row_offset); + self.write(b"A") + } + if col_offset > 0 { + self.write(b"\x1b["); + self.write_u16(abs_col_offset); + self.write(b"C") + } + if col_offset < 0 { + self.write(b"\x1b["); + self.write_u16(abs_col_offset); + self.write(b"D") + } + } + /// Clears the in-memory screen. fn clear(&mut self) { self.write(b"\x1b[2J"); |