aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2019-11-02 13:00:42 -0400
committerJesse Luehrs <doy@tozt.net>2019-11-02 13:00:42 -0400
commit33f866f5056549ea585d97f2d9f620aeb7abcbe6 (patch)
tree6414585af86f46d92f7f74355d7b7ebeb427b0ed
parente50e3c4fe311d03d90c7bfb43b7612317fe183c0 (diff)
downloadvt100-rust-33f866f5056549ea585d97f2d9f620aeb7abcbe6.tar.gz
vt100-rust-33f866f5056549ea585d97f2d9f620aeb7abcbe6.zip
improve alternate screen handling
-rw-r--r--src/screen.rs40
-rw-r--r--tests/mode.rs81
2 files changed, 107 insertions, 14 deletions
diff --git a/src/screen.rs b/src/screen.rs
index 7e6f07b..ae14bc1 100644
--- a/src/screen.rs
+++ b/src/screen.rs
@@ -49,7 +49,8 @@ impl Default for MouseProtocolEncoding {
struct State {
grid: crate::grid::Grid,
- alternate_grid: Option<crate::grid::Grid>,
+ alternate_grid: crate::grid::Grid,
+ use_alternate_grid: bool,
attrs: crate::attrs::Attrs,
@@ -67,7 +68,8 @@ impl State {
let size = crate::grid::Size { rows, cols };
Self {
grid: crate::grid::Grid::new(size),
- alternate_grid: None,
+ alternate_grid: crate::grid::Grid::new(size),
+ use_alternate_grid: false,
attrs: crate::attrs::Attrs::default(),
@@ -86,16 +88,16 @@ impl State {
}
fn grid(&self) -> &crate::grid::Grid {
- if let Some(grid) = &self.alternate_grid {
- grid
+ if self.use_alternate_grid {
+ &self.alternate_grid
} else {
&self.grid
}
}
fn grid_mut(&mut self) -> &mut crate::grid::Grid {
- if let Some(grid) = &mut self.alternate_grid {
- grid
+ if self.use_alternate_grid {
+ &mut self.alternate_grid
} else {
&mut self.grid
}
@@ -121,13 +123,11 @@ impl State {
}
fn enter_alternate_grid(&mut self) {
- if self.alternate_grid.is_none() {
- self.alternate_grid = Some(self.new_grid());
- }
+ self.use_alternate_grid = true;
}
fn exit_alternate_grid(&mut self) {
- self.alternate_grid = None;
+ self.use_alternate_grid = false;
}
fn set_output(&mut self, output: Output) {
@@ -292,7 +292,8 @@ impl State {
// ESC c
fn ris(&mut self) {
self.grid = self.new_grid();
- self.alternate_grid = None;
+ self.alternate_grid = self.new_grid();
+ self.use_alternate_grid = false;
self.attrs = crate::attrs::Attrs::default();
self.modes = enumset::EnumSet::default();
self.mouse_protocol_mode = MouseProtocolMode::default();
@@ -429,12 +430,17 @@ impl State {
6 => self.grid_mut().set_origin_mode(true),
9 => self.set_mouse_mode(MouseProtocolMode::Press),
25 => self.clear_mode(Mode::HideCursor),
+ 47 => self.enter_alternate_grid(),
1000 => self.set_mouse_mode(MouseProtocolMode::PressRelease),
1002 => self.set_mouse_mode(MouseProtocolMode::ButtonMotion),
1003 => self.set_mouse_mode(MouseProtocolMode::AnyMotion),
1005 => self.set_mouse_encoding(MouseProtocolEncoding::Utf8),
1006 => self.set_mouse_encoding(MouseProtocolEncoding::Sgr),
- 1049 => self.enter_alternate_grid(),
+ 1049 => {
+ self.decsc();
+ self.alternate_grid = self.new_grid();
+ self.enter_alternate_grid();
+ }
2004 => self.set_mode(Mode::BracketedPaste),
_ => {}
}
@@ -454,6 +460,9 @@ impl State {
6 => self.grid_mut().set_origin_mode(false),
9 => self.clear_mouse_mode(MouseProtocolMode::Press),
25 => self.set_mode(Mode::HideCursor),
+ 47 => {
+ self.exit_alternate_grid();
+ }
1000 => {
self.clear_mouse_mode(MouseProtocolMode::PressRelease)
}
@@ -465,7 +474,10 @@ impl State {
self.clear_mouse_encoding(MouseProtocolEncoding::Utf8)
}
1006 => self.clear_mouse_encoding(MouseProtocolEncoding::Sgr),
- 1049 => self.exit_alternate_grid(),
+ 1049 => {
+ self.exit_alternate_grid();
+ self.decrc();
+ }
2004 => self.clear_mode(Mode::BracketedPaste),
_ => {}
}
@@ -804,7 +816,7 @@ impl Screen {
}
pub fn alternate_buffer_active(&self) -> bool {
- self.state.alternate_grid.is_some()
+ self.state.use_alternate_grid
}
pub fn application_cursor(&self) -> bool {
diff --git a/tests/mode.rs b/tests/mode.rs
index fb97e43..d6807a9 100644
--- a/tests/mode.rs
+++ b/tests/mode.rs
@@ -278,21 +278,102 @@ fn modes() {
#[test]
fn alternate_buffer() {
let mut screen = vt100::Screen::new(24, 80);
+
+ // 47
+
+ screen.process(b"\x1bc");
+ assert_eq!(
+ screen.window_contents(0, 0, 23, 79),
+ "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"
+ );
+ assert_eq!(screen.cursor_position(), (0, 0));
+ assert!(!screen.alternate_buffer_active());
+
screen.process(b"\x1b[m\x1b[2J\x1b[H1\r\n2\r\n3\r\n4\r\n5\r\n6\r\n7\r\n8\r\n9\r\n10\r\n11\r\n12\r\n13\r\n14\r\n15\r\n16\r\n17\r\n18\r\n19\r\n20\r\n21\r\n22\r\n23\r\n24");
assert_eq!(screen.window_contents(0, 0, 23, 79), "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n22\n23\n24\n");
+ assert_eq!(screen.cursor_position(), (23, 2));
+ assert!(!screen.alternate_buffer_active());
+
+ screen.process(b"\x1b[?47h");
+ assert_eq!(
+ screen.window_contents(0, 0, 23, 79),
+ "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"
+ );
+ assert_eq!(screen.cursor_position(), (0, 0));
+ assert!(screen.alternate_buffer_active());
+
+ screen.process(b"foobar");
+ assert_eq!(
+ screen.window_contents(0, 0, 23, 79),
+ "foobar\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"
+ );
+ assert_eq!(screen.cursor_position(), (0, 6));
+ assert!(screen.alternate_buffer_active());
+
+ screen.process(b"\x1b[?47l");
+ assert_eq!(screen.window_contents(0, 0, 23, 79), "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n22\n23\n24\n");
+ assert_eq!(screen.cursor_position(), (23, 2));
assert!(!screen.alternate_buffer_active());
+
+ screen.process(b"\x1b[?47h");
+ assert_eq!(
+ screen.window_contents(0, 0, 23, 79),
+ "foobar\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"
+ );
+ assert_eq!(screen.cursor_position(), (0, 6));
+ assert!(screen.alternate_buffer_active());
+
+ screen.process(b"\x1b[?47l");
+ assert_eq!(screen.window_contents(0, 0, 23, 79), "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n22\n23\n24\n");
+ assert_eq!(screen.cursor_position(), (23, 2));
+ assert!(!screen.alternate_buffer_active());
+
+ // 1049
+
+ screen.process(b"\x1bc");
+ assert_eq!(
+ screen.window_contents(0, 0, 23, 79),
+ "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"
+ );
+ assert_eq!(screen.cursor_position(), (0, 0));
+ assert!(!screen.alternate_buffer_active());
+
+ screen.process(b"\x1b[m\x1b[2J\x1b[H1\r\n2\r\n3\r\n4\r\n5\r\n6\r\n7\r\n8\r\n9\r\n10\r\n11\r\n12\r\n13\r\n14\r\n15\r\n16\r\n17\r\n18\r\n19\r\n20\r\n21\r\n22\r\n23\r\n24");
+ assert_eq!(screen.window_contents(0, 0, 23, 79), "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n22\n23\n24\n");
+ assert_eq!(screen.cursor_position(), (23, 2));
+ assert!(!screen.alternate_buffer_active());
+
screen.process(b"\x1b[?1049h");
assert_eq!(
screen.window_contents(0, 0, 23, 79),
"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"
);
+ assert_eq!(screen.cursor_position(), (0, 0));
assert!(screen.alternate_buffer_active());
+
screen.process(b"foobar");
assert_eq!(
screen.window_contents(0, 0, 23, 79),
"foobar\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"
);
+ assert_eq!(screen.cursor_position(), (0, 6));
+ assert!(screen.alternate_buffer_active());
+
+ screen.process(b"\x1b[?1049l");
+ assert_eq!(screen.window_contents(0, 0, 23, 79), "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n22\n23\n24\n");
+ assert_eq!(screen.cursor_position(), (23, 2));
+ assert!(!screen.alternate_buffer_active());
+
+ screen.process(b"\x1b[?1049h");
+ assert_eq!(
+ screen.window_contents(0, 0, 23, 79),
+ "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"
+ );
+ assert_eq!(screen.cursor_position(), (0, 0));
+ assert!(screen.alternate_buffer_active());
+
screen.process(b"\x1b[?1049l");
assert_eq!(screen.window_contents(0, 0, 23, 79), "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n22\n23\n24\n");
+ assert_eq!(screen.cursor_position(), (23, 2));
assert!(!screen.alternate_buffer_active());
}