diff options
author | Jesse Luehrs <doy@tozt.net> | 2019-11-06 02:06:49 -0500 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2019-11-06 02:06:49 -0500 |
commit | 7e7f4a10930498c2d576c7103054408a0915370e (patch) | |
tree | bfbc996afb8ffdb2e8a050a7d9a8897c96d52c39 /src/grid.rs | |
parent | cd03c4f836baeae66e4a55407266f35f6ebf43a1 (diff) | |
download | vt100-rust-7e7f4a10930498c2d576c7103054408a0915370e.tar.gz vt100-rust-7e7f4a10930498c2d576c7103054408a0915370e.zip |
contents_formatted and contents_diff should also restore the cursor
since the cursor is a visible part of the terminal
Diffstat (limited to 'src/grid.rs')
-rw-r--r-- | src/grid.rs | 31 |
1 files changed, 29 insertions, 2 deletions
diff --git a/src/grid.rs b/src/grid.rs index fb0f7d7..218247e 100644 --- a/src/grid.rs +++ b/src/grid.rs @@ -139,9 +139,13 @@ impl Grid { pub fn contents_formatted(&self) -> Vec<u8> { let mut contents = vec![]; let mut prev_attrs = crate::attrs::Attrs::default(); + let mut final_col = 0; for row in self.rows() { - let (mut new_contents, new_attrs) = + let (mut new_contents, new_attrs, new_col) = row.contents_formatted(0, self.size.cols, prev_attrs); + if !new_contents.is_empty() { + final_col = new_col; + } contents.append(&mut new_contents); if !row.wrapped() { contents.extend(b"\r\n"); @@ -149,26 +153,49 @@ impl Grid { prev_attrs = new_attrs; } + let mut final_row = self.size.rows; while contents.ends_with(b"\r\n") { contents.truncate(contents.len() - 2); + final_row -= 1; } + + if final_row != self.pos.row || final_col != self.pos.col { + contents.extend( + format!("\x1b[{};{}H", self.pos.row + 1, self.pos.col + 1) + .as_bytes(), + ); + } + contents } pub fn contents_diff(&self, prev: &Self) -> Vec<u8> { let mut contents = b"\x1b[m".to_vec(); let mut prev_attrs = crate::attrs::Attrs::default(); + let mut final_row = prev.pos.row; + let mut final_col = prev.pos.col; for (idx, (row, prev_row)) in self.rows().zip(prev.rows()).enumerate() { - let (mut new_contents, new_attrs) = row.contents_diff( + let (mut new_contents, new_attrs, new_col) = row.contents_diff( idx.try_into().unwrap(), prev_row, prev_attrs, ); + if !new_contents.is_empty() { + final_row = idx.try_into().unwrap(); + final_col = new_col; + } contents.append(&mut new_contents); prev_attrs = new_attrs; } + if self.pos.row != final_row || self.pos.col != final_col { + contents.extend( + format!("\x1b[{};{}H", self.pos.row + 1, self.pos.col + 1) + .as_bytes(), + ); + } + contents } |