diff --git a/Cargo.lock b/Cargo.lock index 9f66f70445..55d1fac25d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1867,6 +1867,7 @@ dependencies = [ "sysinfo 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", "tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "term 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)", + "term_size 1.0.0-beta1 (registry+https://github.com/rust-lang/crates.io-index)", "toml 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", "toml-query 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)", "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -3035,6 +3036,16 @@ dependencies = [ "winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "term_size" +version = "1.0.0-beta1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.58 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "termcolor" version = "1.0.5" @@ -3920,6 +3931,7 @@ dependencies = [ "checksum sysinfo 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c3e2cab189e59f72710e3dd5e1e0d5be0f6c5c999c326f2fdcdf3bf4483ec9fd" "checksum tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e24d9338a0a5be79593e2fa15a648add6138caa803e2d5bc782c371732ca9" "checksum term 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "edd106a334b7657c10b7c540a0106114feadeb4dc314513e97df481d5d966f42" +"checksum term_size 1.0.0-beta1 (registry+https://github.com/rust-lang/crates.io-index)" = "a8a17d8699e154863becdf18e4fd28bd0be27ca72856f54daf75c00f2566898f" "checksum termcolor 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "96d6098003bde162e4277c70665bd87c326f5a0c3f3fbfb285787fa482d54e6e" "checksum termion 1.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "6a8fb22f7cde82c8220e5aeacb3258ed7ce996142c77cba193f203515e26c330" "checksum termios 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "72b620c5ea021d75a735c943269bb07d30c9b77d6ac6b236bc8b5c496ef05625" diff --git a/Cargo.toml b/Cargo.toml index 84bc8ff719..58fe9ff733 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -80,6 +80,7 @@ uuid = {version = "0.7.4", features = [ "v4", "serde" ]} syntect = "3.2.0" strip-ansi-escapes = "0.1.0" heim = "0.0.5" +term_size = "1.0.0-beta1" [dev-dependencies] pretty_assertions = "0.6.1" diff --git a/src/plugins/binaryview.rs b/src/plugins/binaryview.rs index 0db5673254..7aba60b879 100644 --- a/src/plugins/binaryview.rs +++ b/src/plugins/binaryview.rs @@ -1,5 +1,5 @@ #![feature(option_flattening)] -use crossterm::{cursor, terminal, Attribute, RawScreen}; +use crossterm::{cursor, Attribute, RawScreen}; use indexmap::IndexMap; use nu::{ serve_plugin, CallInfo, CommandConfig, NamedType, Plugin, ShellError, SpanSource, Spanned, @@ -82,7 +82,7 @@ impl RenderContext { } } pub fn clear(&mut self) { - self.frame_buffer = vec![(0, 0, 0); self.width * self.height as usize]; + self.frame_buffer = vec![(0, 0, 0); self.width * self.height]; } fn render_to_screen_lores(&mut self) -> Result<(), Box> { @@ -134,6 +134,7 @@ impl RenderContext { let fb_len = self.frame_buffer.len(); let cursor = cursor(); + cursor.goto(0, 0)?; while pos < (fb_len - self.width) { @@ -190,18 +191,17 @@ impl RenderContext { } } pub fn update(&mut self) -> Result<(), Box> { - let terminal = terminal(); - let terminal_size = terminal.terminal_size(); + let (width, height) = term_size::dimensions().unwrap(); - if (self.width != terminal_size.0 as usize) || (self.height != terminal_size.1 as usize) { + if (self.width != width) || (self.height != height) { let cursor = cursor(); cursor.hide()?; - self.width = terminal_size.0 as usize + 1; + self.width = width + 1; self.height = if self.lores_mode { - terminal_size.1 as usize + height } else { - terminal_size.1 as usize * 2 + height * 2 }; } diff --git a/src/plugins/textview.rs b/src/plugins/textview.rs index e0f8f63fd1..26184737ba 100644 --- a/src/plugins/textview.rs +++ b/src/plugins/textview.rs @@ -50,17 +50,17 @@ fn paint_textview( starting_row: usize, use_color_buffer: bool, ) -> usize { - let terminal = terminal(); let cursor = cursor(); - let size = terminal.terminal_size(); + let (width, height) = term_size::dimensions().unwrap(); // render let mut pos = 0; - let width = size.0 as usize + 1; - let height = size.1 as usize; + // account for the off-by-one + let width = width + 1; let mut frame_buffer = vec![]; //(' ', 0, 0, 0); max_pos]; + // TODO: Cache the full buffer and only recreate it if the terminal size changes for command in draw_commands { match command { DrawCommand::DrawString(style, string) => { @@ -112,7 +112,7 @@ fn paint_textview( } if buffer_needs_scrolling { - let _ = cursor.goto(0, size.1); + let _ = cursor.goto(0, height as u16); print!( "{}", ansi_term::Colour::Blue.paint("[ESC to quit, arrow keys to move]") @@ -136,11 +136,11 @@ fn scroll_view_lines_if_needed(draw_commands: Vec, use_color_buffer let _ = input.read_async(); let terminal = terminal(); - let mut size = terminal.terminal_size(); + let (mut width, mut height) = term_size::dimensions().unwrap(); let mut max_bottom_line = paint_textview(&draw_commands, starting_row, use_color_buffer); // Only scroll if needed - if max_bottom_line > size.1 as usize { + if max_bottom_line > height { loop { if rawkey.is_pressed(rawkey::KeyCode::Escape) { break; @@ -153,23 +153,23 @@ fn scroll_view_lines_if_needed(draw_commands: Vec, use_color_buffer } } if rawkey.is_pressed(rawkey::KeyCode::DownArrow) { - if starting_row < (max_bottom_line - size.1 as usize) { + if starting_row < (max_bottom_line - height) { starting_row += 1; } max_bottom_line = paint_textview(&draw_commands, starting_row, use_color_buffer); } if rawkey.is_pressed(rawkey::KeyCode::PageUp) { - starting_row -= std::cmp::min(size.1 as usize, starting_row); + starting_row -= std::cmp::min(height, starting_row); max_bottom_line = paint_textview(&draw_commands, starting_row, use_color_buffer); } if rawkey.is_pressed(rawkey::KeyCode::PageDown) { - if starting_row < (max_bottom_line - size.1 as usize) { - starting_row += size.1 as usize; + if starting_row < (max_bottom_line - height) { + starting_row += height; - if starting_row > (max_bottom_line - size.1 as usize) { - starting_row = max_bottom_line - size.1 as usize; + if starting_row > (max_bottom_line - height) { + starting_row = max_bottom_line - height; } } max_bottom_line = @@ -178,9 +178,11 @@ fn scroll_view_lines_if_needed(draw_commands: Vec, use_color_buffer thread::sleep(Duration::from_millis(50)); - let new_size = terminal.terminal_size(); - if size != new_size { - size = new_size; + let new_size = term_size::dimensions().unwrap(); + if width != new_size.0 || height != new_size.1 { + width = new_size.0; + height = new_size.1; + let _ = terminal.clear(crossterm::ClearType::All); max_bottom_line = paint_textview(&draw_commands, starting_row, use_color_buffer);