From dc5efa534b80264f2e5a83ab60056c0834925aea Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Wed, 20 Nov 2019 06:06:36 -0500 Subject: handle wrapped cursors properly on output --- src/grid.rs | 48 ++++++++++++++++++++++++++++++++++++++++++++++-- tests/text.rs | 15 +++++++++++++++ 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/src/grid.rs b/src/grid.rs index 80d44ba..8076a6c 100644 --- a/src/grid.rs +++ b/src/grid.rs @@ -212,7 +212,29 @@ impl Grid { wrapping = row.wrapped(); } - crate::term::MoveFromTo::new(prev_pos, self.pos).write_buf(contents); + // writing a character to the last column of a row doesn't wrap the + // cursor immediately - it waits until the next character is actually + // drawn. it is only possible for the cursor to have this kind of + // position after drawing a character though, so if we end in this + // position, we need to redraw the character at the end of the row. + if prev_pos != self.pos && self.pos.col >= self.size.cols { + let mut pos = Pos { + row: self.pos.row, + col: self.size.cols - 2, + }; + if !self.visible_cell(pos).unwrap().is_wide() { + pos = Pos { + row: self.pos.row, + col: self.size.cols - 1, + }; + } + crate::term::MoveFromTo::new(prev_pos, pos).write_buf(contents); + let cell = self.visible_cell(pos).unwrap(); + contents.extend(cell.contents().as_bytes()); + } else { + crate::term::MoveFromTo::new(prev_pos, self.pos) + .write_buf(contents); + } prev_attrs } @@ -244,7 +266,29 @@ impl Grid { wrapping = row.wrapped(); } - crate::term::MoveFromTo::new(prev_pos, self.pos).write_buf(contents); + // writing a character to the last column of a row doesn't wrap the + // cursor immediately - it waits until the next character is actually + // drawn. it is only possible for the cursor to have this kind of + // position after drawing a character though, so if we end in this + // position, we need to redraw the character at the end of the row. + if prev_pos != self.pos && self.pos.col >= self.size.cols { + let mut pos = Pos { + row: self.pos.row, + col: self.size.cols - 2, + }; + if !self.visible_cell(pos).unwrap().is_wide() { + pos = Pos { + row: self.pos.row, + col: self.size.cols - 1, + }; + } + crate::term::MoveFromTo::new(prev_pos, pos).write_buf(contents); + let cell = self.visible_cell(pos).unwrap(); + contents.extend(cell.contents().as_bytes()); + } else { + crate::term::MoveFromTo::new(prev_pos, self.pos) + .write_buf(contents); + } prev_attrs } diff --git a/tests/text.rs b/tests/text.rs index cf90b89..a962741 100644 --- a/tests/text.rs +++ b/tests/text.rs @@ -221,3 +221,18 @@ fn wrap() { assert_eq!(parser.screen().cell(1, 2).unwrap().contents(), "a"); assert_eq!(parser.screen().cell(1, 3).unwrap().contents(), ""); } + +#[test] +fn wrap_weird() { + let mut parser = vt100::Parser::default(); + + let screen = parser.screen().clone(); + parser.process(b"foo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo"); + assert_eq!(parser.screen().contents_formatted(), &b"\x1b[?25h\x1b[m\x1b[H\x1b[Jfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo"[..]); + assert_eq!(parser.screen().contents_diff(&screen), &b"foo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo"[..]); + + let screen = parser.screen().clone(); + parser.process(b"\x1b[3;80H "); + assert_eq!(parser.screen().contents_formatted(), &b"\x1b[?25h\x1b[m\x1b[H\x1b[Jfoo\r\nfoo\r\nfoo\x1b[76C \r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\r\nfoo\x1b[3;80H "[..]); + assert_eq!(parser.screen().contents_diff(&screen), &b"\x1b[3;80H "[..]); +} -- cgit v1.2.3