mirror of
https://github.com/nushell/nushell.git
synced 2024-11-23 00:43:33 +01:00
Merge pull request #223 from jonathandturner/term_size
Switch way of doing terminal size calculation
This commit is contained in:
commit
4a3ec867a7
12
Cargo.lock
generated
12
Cargo.lock
generated
@ -1867,6 +1867,7 @@ dependencies = [
|
|||||||
"sysinfo 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
"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)",
|
"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 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 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)",
|
"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)",
|
"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)",
|
"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]]
|
[[package]]
|
||||||
name = "termcolor"
|
name = "termcolor"
|
||||||
version = "1.0.5"
|
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 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 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 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 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 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"
|
"checksum termios 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "72b620c5ea021d75a735c943269bb07d30c9b77d6ac6b236bc8b5c496ef05625"
|
||||||
|
@ -80,6 +80,7 @@ uuid = {version = "0.7.4", features = [ "v4", "serde" ]}
|
|||||||
syntect = "3.2.0"
|
syntect = "3.2.0"
|
||||||
strip-ansi-escapes = "0.1.0"
|
strip-ansi-escapes = "0.1.0"
|
||||||
heim = "0.0.5"
|
heim = "0.0.5"
|
||||||
|
term_size = "1.0.0-beta1"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
pretty_assertions = "0.6.1"
|
pretty_assertions = "0.6.1"
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
#![feature(option_flattening)]
|
#![feature(option_flattening)]
|
||||||
use crossterm::{cursor, terminal, Attribute, RawScreen};
|
use crossterm::{cursor, Attribute, RawScreen};
|
||||||
use indexmap::IndexMap;
|
use indexmap::IndexMap;
|
||||||
use nu::{
|
use nu::{
|
||||||
serve_plugin, CallInfo, CommandConfig, NamedType, Plugin, ShellError, SpanSource, Spanned,
|
serve_plugin, CallInfo, CommandConfig, NamedType, Plugin, ShellError, SpanSource, Spanned,
|
||||||
@ -82,7 +82,7 @@ impl RenderContext {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn clear(&mut self) {
|
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<dyn std::error::Error>> {
|
fn render_to_screen_lores(&mut self) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
@ -134,6 +134,7 @@ impl RenderContext {
|
|||||||
let fb_len = self.frame_buffer.len();
|
let fb_len = self.frame_buffer.len();
|
||||||
|
|
||||||
let cursor = cursor();
|
let cursor = cursor();
|
||||||
|
|
||||||
cursor.goto(0, 0)?;
|
cursor.goto(0, 0)?;
|
||||||
|
|
||||||
while pos < (fb_len - self.width) {
|
while pos < (fb_len - self.width) {
|
||||||
@ -190,18 +191,17 @@ impl RenderContext {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn update(&mut self) -> Result<(), Box<dyn std::error::Error>> {
|
pub fn update(&mut self) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
let terminal = terminal();
|
let (width, height) = term_size::dimensions().unwrap();
|
||||||
let terminal_size = terminal.terminal_size();
|
|
||||||
|
|
||||||
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();
|
let cursor = cursor();
|
||||||
cursor.hide()?;
|
cursor.hide()?;
|
||||||
|
|
||||||
self.width = terminal_size.0 as usize + 1;
|
self.width = width + 1;
|
||||||
self.height = if self.lores_mode {
|
self.height = if self.lores_mode {
|
||||||
terminal_size.1 as usize
|
height
|
||||||
} else {
|
} else {
|
||||||
terminal_size.1 as usize * 2
|
height * 2
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,17 +50,17 @@ fn paint_textview(
|
|||||||
starting_row: usize,
|
starting_row: usize,
|
||||||
use_color_buffer: bool,
|
use_color_buffer: bool,
|
||||||
) -> usize {
|
) -> usize {
|
||||||
let terminal = terminal();
|
|
||||||
let cursor = cursor();
|
let cursor = cursor();
|
||||||
|
|
||||||
let size = terminal.terminal_size();
|
let (width, height) = term_size::dimensions().unwrap();
|
||||||
|
|
||||||
// render
|
// render
|
||||||
let mut pos = 0;
|
let mut pos = 0;
|
||||||
let width = size.0 as usize + 1;
|
// account for the off-by-one
|
||||||
let height = size.1 as usize;
|
let width = width + 1;
|
||||||
let mut frame_buffer = vec![]; //(' ', 0, 0, 0); max_pos];
|
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 {
|
for command in draw_commands {
|
||||||
match command {
|
match command {
|
||||||
DrawCommand::DrawString(style, string) => {
|
DrawCommand::DrawString(style, string) => {
|
||||||
@ -112,7 +112,7 @@ fn paint_textview(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if buffer_needs_scrolling {
|
if buffer_needs_scrolling {
|
||||||
let _ = cursor.goto(0, size.1);
|
let _ = cursor.goto(0, height as u16);
|
||||||
print!(
|
print!(
|
||||||
"{}",
|
"{}",
|
||||||
ansi_term::Colour::Blue.paint("[ESC to quit, arrow keys to move]")
|
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<DrawCommand>, use_color_buffer
|
|||||||
let _ = input.read_async();
|
let _ = input.read_async();
|
||||||
|
|
||||||
let terminal = terminal();
|
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);
|
let mut max_bottom_line = paint_textview(&draw_commands, starting_row, use_color_buffer);
|
||||||
|
|
||||||
// Only scroll if needed
|
// Only scroll if needed
|
||||||
if max_bottom_line > size.1 as usize {
|
if max_bottom_line > height {
|
||||||
loop {
|
loop {
|
||||||
if rawkey.is_pressed(rawkey::KeyCode::Escape) {
|
if rawkey.is_pressed(rawkey::KeyCode::Escape) {
|
||||||
break;
|
break;
|
||||||
@ -153,23 +153,23 @@ fn scroll_view_lines_if_needed(draw_commands: Vec<DrawCommand>, use_color_buffer
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if rawkey.is_pressed(rawkey::KeyCode::DownArrow) {
|
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;
|
starting_row += 1;
|
||||||
}
|
}
|
||||||
max_bottom_line =
|
max_bottom_line =
|
||||||
paint_textview(&draw_commands, starting_row, use_color_buffer);
|
paint_textview(&draw_commands, starting_row, use_color_buffer);
|
||||||
}
|
}
|
||||||
if rawkey.is_pressed(rawkey::KeyCode::PageUp) {
|
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 =
|
max_bottom_line =
|
||||||
paint_textview(&draw_commands, starting_row, use_color_buffer);
|
paint_textview(&draw_commands, starting_row, use_color_buffer);
|
||||||
}
|
}
|
||||||
if rawkey.is_pressed(rawkey::KeyCode::PageDown) {
|
if rawkey.is_pressed(rawkey::KeyCode::PageDown) {
|
||||||
if starting_row < (max_bottom_line - size.1 as usize) {
|
if starting_row < (max_bottom_line - height) {
|
||||||
starting_row += size.1 as usize;
|
starting_row += height;
|
||||||
|
|
||||||
if starting_row > (max_bottom_line - size.1 as usize) {
|
if starting_row > (max_bottom_line - height) {
|
||||||
starting_row = max_bottom_line - size.1 as usize;
|
starting_row = max_bottom_line - height;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
max_bottom_line =
|
max_bottom_line =
|
||||||
@ -178,9 +178,11 @@ fn scroll_view_lines_if_needed(draw_commands: Vec<DrawCommand>, use_color_buffer
|
|||||||
|
|
||||||
thread::sleep(Duration::from_millis(50));
|
thread::sleep(Duration::from_millis(50));
|
||||||
|
|
||||||
let new_size = terminal.terminal_size();
|
let new_size = term_size::dimensions().unwrap();
|
||||||
if size != new_size {
|
if width != new_size.0 || height != new_size.1 {
|
||||||
size = new_size;
|
width = new_size.0;
|
||||||
|
height = new_size.1;
|
||||||
|
|
||||||
let _ = terminal.clear(crossterm::ClearType::All);
|
let _ = terminal.clear(crossterm::ClearType::All);
|
||||||
max_bottom_line =
|
max_bottom_line =
|
||||||
paint_textview(&draw_commands, starting_row, use_color_buffer);
|
paint_textview(&draw_commands, starting_row, use_color_buffer);
|
||||||
|
Loading…
Reference in New Issue
Block a user