From a6b79748d99c54f5d8173641910e580945c95c48 Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Thu, 25 Jul 2019 13:07:33 +1200 Subject: [PATCH 1/3] Move to async keys for textview --- src/plugins/textview.rs | 97 ++++++++++++++++++++++++----------------- 1 file changed, 57 insertions(+), 40 deletions(-) diff --git a/src/plugins/textview.rs b/src/plugins/textview.rs index 79c792cba..180ce9c5f 100644 --- a/src/plugins/textview.rs +++ b/src/plugins/textview.rs @@ -1,6 +1,6 @@ #![feature(option_flattening)] -use crossterm::{cursor, input, terminal, InputEvent, KeyEvent, RawScreen}; +use crossterm::{cursor, terminal, InputEvent, KeyEvent, RawScreen}; use indexmap::IndexMap; use nu::{ serve_plugin, CallInfo, CommandConfig, Plugin, Primitive, ShellError, SourceMap, SpanSource, @@ -9,6 +9,7 @@ use nu::{ use std::io::Write; use std::path::Path; +use std::{thread, time::Duration}; struct TextView; @@ -35,55 +36,64 @@ impl Plugin for TextView { } } +fn paint_textview(lines: &Vec, starting_row: usize) -> (u16, u16) { + let terminal = terminal(); + let cursor = cursor(); + + let _ = terminal.clear(crossterm::ClearType::All); + + let size = terminal.terminal_size(); + let _ = cursor.goto(0, 0); + + let mut total_max_num_lines = 0; + for line in lines.iter().skip(starting_row).take(size.1 as usize) { + //let pos = cursor.pos(); + let stripped_line = strip_ansi_escapes::strip(&line.as_bytes()).unwrap(); + let line_length = stripped_line.len(); + + let max_num_lines = line_length as u16 / size.0 + + if (line_length as u16 % size.0) > 0 { + 1 + } else { + 0 + }; + total_max_num_lines += max_num_lines; + + if total_max_num_lines < size.1 { + print!("{}\r\n", line); + } else { + break; + } + } + + let _ = cursor.goto(0, size.1); + print!( + "{}", + ansi_term::Colour::Blue.paint("[ESC to quit, arrow keys to move]") + ); + print!("{}", crossterm::Attribute::Reset); + + let _ = std::io::stdout().flush(); + + size +} + fn scroll_view_lines(lines: Vec) { let mut starting_row = 0; - let terminal = terminal(); - if let Ok(_raw) = RawScreen::into_raw_mode() { - let input = input(); + let terminal = terminal(); + let input = crossterm::input(); let cursor = cursor(); let _ = cursor.hide(); - let mut sync_stdin = input.read_sync(); + let mut async_stdin = input.read_async(); + let _ = terminal.clear(crossterm::ClearType::All); + let mut size = paint_textview(&lines, starting_row); loop { - let size = terminal.terminal_size(); - let _ = terminal.clear(crossterm::ClearType::All); - let _ = cursor.goto(0, 0); - - let mut total_max_num_lines = 0; - for line in lines.iter().skip(starting_row).take(size.1 as usize) { - //let pos = cursor.pos(); - let stripped_line = strip_ansi_escapes::strip(&line.as_bytes()).unwrap(); - let line_length = stripped_line.len(); - - let max_num_lines = line_length as u16 / size.0 - + if (line_length as u16 % size.0) > 0 { - 1 - } else { - 0 - }; - total_max_num_lines += max_num_lines; - - if total_max_num_lines < size.1 { - print!("{}\r\n", line); - } else { - break; - } - } - - let _ = cursor.goto(0, size.1); - print!( - "{}", - ansi_term::Colour::Blue.paint("[ESC to quit, arrow keys to move]") - ); - let _ = std::io::stdout().flush(); - - let event = sync_stdin.next(); - - if let Some(key_event) = event { + if let Some(key_event) = async_stdin.next() { match key_event { InputEvent::Keyboard(k) => match k { KeyEvent::Esc => { @@ -92,6 +102,7 @@ fn scroll_view_lines(lines: Vec) { KeyEvent::Up => { if starting_row > 0 { starting_row -= 1; + size = paint_textview(&lines, starting_row); } } KeyEvent::Down => { @@ -99,16 +110,19 @@ fn scroll_view_lines(lines: Vec) { < (std::cmp::max(size.1 as usize, lines.len()) - size.1 as usize) { starting_row += 1; + size = paint_textview(&lines, starting_row); } } KeyEvent::PageUp => { starting_row -= std::cmp::min(starting_row, size.1 as usize); + size = paint_textview(&lines, starting_row); } KeyEvent::Char(c) if c == ' ' => { if starting_row < (std::cmp::max(size.1 as usize, lines.len()) - size.1 as usize) { starting_row += size.1 as usize; + size = paint_textview(&lines, starting_row); } } KeyEvent::PageDown => { @@ -116,6 +130,7 @@ fn scroll_view_lines(lines: Vec) { < (std::cmp::max(size.1 as usize, lines.len()) - size.1 as usize) { starting_row += size.1 as usize; + size = paint_textview(&lines, starting_row); } } _ => {} @@ -124,6 +139,8 @@ fn scroll_view_lines(lines: Vec) { _ => {} } } + + thread::sleep(Duration::from_millis(50)); } let _ = cursor.show(); From 501482cc319fb0ac0cfd70066fdf80ba93c04145 Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Thu, 25 Jul 2019 13:25:17 +1200 Subject: [PATCH 2/3] Move to rawkey --- src/plugins/textview.rs | 68 +++++++++++------------------------------ 1 file changed, 17 insertions(+), 51 deletions(-) diff --git a/src/plugins/textview.rs b/src/plugins/textview.rs index 180ce9c5f..f7ee3b2a3 100644 --- a/src/plugins/textview.rs +++ b/src/plugins/textview.rs @@ -1,11 +1,12 @@ #![feature(option_flattening)] -use crossterm::{cursor, terminal, InputEvent, KeyEvent, RawScreen}; +use crossterm::{cursor, terminal, RawScreen}; use indexmap::IndexMap; use nu::{ serve_plugin, CallInfo, CommandConfig, Plugin, Primitive, ShellError, SourceMap, SpanSource, Spanned, Value, }; +use rawkey::RawKey; use std::io::Write; use std::path::Path; @@ -80,63 +81,27 @@ fn paint_textview(lines: &Vec, starting_row: usize) -> (u16, u16) { fn scroll_view_lines(lines: Vec) { let mut starting_row = 0; + let rawkey = RawKey::new(); if let Ok(_raw) = RawScreen::into_raw_mode() { - let terminal = terminal(); - let input = crossterm::input(); let cursor = cursor(); - let _ = cursor.hide(); - let mut async_stdin = input.read_async(); - let _ = terminal.clear(crossterm::ClearType::All); - let mut size = paint_textview(&lines, starting_row); loop { - if let Some(key_event) = async_stdin.next() { - match key_event { - InputEvent::Keyboard(k) => match k { - KeyEvent::Esc => { - break; - } - KeyEvent::Up => { - if starting_row > 0 { - starting_row -= 1; - size = paint_textview(&lines, starting_row); - } - } - KeyEvent::Down => { - if starting_row - < (std::cmp::max(size.1 as usize, lines.len()) - size.1 as usize) - { - starting_row += 1; - size = paint_textview(&lines, starting_row); - } - } - KeyEvent::PageUp => { - starting_row -= std::cmp::min(starting_row, size.1 as usize); - size = paint_textview(&lines, starting_row); - } - KeyEvent::Char(c) if c == ' ' => { - if starting_row - < (std::cmp::max(size.1 as usize, lines.len()) - size.1 as usize) - { - starting_row += size.1 as usize; - size = paint_textview(&lines, starting_row); - } - } - KeyEvent::PageDown => { - if starting_row - < (std::cmp::max(size.1 as usize, lines.len()) - size.1 as usize) - { - starting_row += size.1 as usize; - size = paint_textview(&lines, starting_row); - } - } - _ => {} - }, - - _ => {} + if rawkey.is_pressed(rawkey::KeyCode::Escape) { + break; + } + if rawkey.is_pressed(rawkey::KeyCode::UpArrow) { + if starting_row > 0 { + starting_row -= 1; + size = paint_textview(&lines, starting_row); + } + } + if rawkey.is_pressed(rawkey::KeyCode::DownArrow) { + if starting_row < (std::cmp::max(size.1 as usize, lines.len()) - size.1 as usize) { + starting_row += 1; + size = paint_textview(&lines, starting_row); } } @@ -145,6 +110,7 @@ fn scroll_view_lines(lines: Vec) { let _ = cursor.show(); } + thread::sleep(Duration::from_millis(250)); } fn scroll_view(s: &str) { From f2873edf6c00f968c5b2ed77d436506e4576400b Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Thu, 25 Jul 2019 14:43:51 +1200 Subject: [PATCH 3/3] One more go at fixing scroll textarea in windows --- src/plugins/binaryview.rs | 4 ++++ src/plugins/textview.rs | 13 ++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/plugins/binaryview.rs b/src/plugins/binaryview.rs index b71a8a88a..1a78cc03b 100644 --- a/src/plugins/binaryview.rs +++ b/src/plugins/binaryview.rs @@ -6,6 +6,7 @@ use nu::{ Value, }; use pretty_hex::*; +use std::{thread, time::Duration}; struct BinaryView; @@ -444,6 +445,9 @@ pub fn view_contents_interactive( #[allow(unused)] let screen = RawScreen::disable_raw_mode(); + println!(""); + thread::sleep(Duration::from_millis(50)); + Ok(()) } diff --git a/src/plugins/textview.rs b/src/plugins/textview.rs index f7ee3b2a3..7b2810b2d 100644 --- a/src/plugins/textview.rs +++ b/src/plugins/textview.rs @@ -87,6 +87,9 @@ fn scroll_view_lines(lines: Vec) { let cursor = cursor(); let _ = cursor.hide(); + let input = crossterm::input(); + let _ = input.read_async(); + let mut size = paint_textview(&lines, starting_row); loop { if rawkey.is_pressed(rawkey::KeyCode::Escape) { @@ -110,7 +113,15 @@ fn scroll_view_lines(lines: Vec) { let _ = cursor.show(); } - thread::sleep(Duration::from_millis(250)); + + let cursor = cursor(); + let _ = cursor.show(); + + #[allow(unused)] + let screen = RawScreen::disable_raw_mode(); + + println!(""); + thread::sleep(Duration::from_millis(50)); } fn scroll_view(s: &str) {