From 439700b87c21645b2038dd27805d8a93815e1e51 Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Sun, 25 Aug 2019 19:15:56 +1200 Subject: [PATCH] Fix a few issues with textview and the parser --- src/parser/parse/parser.rs | 1 + src/plugins/textview.rs | 196 +++++++++++-------------------------- 2 files changed, 58 insertions(+), 139 deletions(-) diff --git a/src/parser/parse/parser.rs b/src/parser/parse/parser.rs index e54cc0f591..2f060ca29b 100644 --- a/src/parser/parse/parser.rs +++ b/src/parser/parse/parser.rs @@ -556,6 +556,7 @@ fn is_bare_char(c: char) -> bool { '@' => true, '*' => true, '?' => true, + '=' => true, _ => false, } } diff --git a/src/plugins/textview.rs b/src/plugins/textview.rs index 7a80c8be32..92d95c83e1 100644 --- a/src/plugins/textview.rs +++ b/src/plugins/textview.rs @@ -1,6 +1,7 @@ #![feature(option_flattening)] use crossterm::{cursor, terminal, RawScreen}; +use crossterm::{InputEvent, KeyEvent}; use indexmap::IndexMap; use nu::{ serve_plugin, CallInfo, Plugin, Primitive, ShellError, Signature, SourceMap, SpanSource, @@ -125,24 +126,6 @@ fn scroll_view_lines_if_needed(draw_commands: Vec, use_color_buffer let mut starting_row = 0; if let Ok(_raw) = RawScreen::into_raw_mode() { - let cursor = cursor(); - let _ = cursor.hide(); - - let input = crossterm::input(); - - #[allow(unused)] - let mut sync_stdin: Option; - - #[cfg(feature = "rawkey")] - { - let _ = input.read_async(); - } - - #[cfg(not(feature = "rawkey"))] - { - sync_stdin = Some(input.read_sync()); - } - let terminal = terminal(); let mut size = terminal.terminal_size(); let height = size.1 as usize - 1; @@ -151,135 +134,70 @@ fn scroll_view_lines_if_needed(draw_commands: Vec, use_color_buffer // Only scroll if needed if max_bottom_line > height as usize { - #[cfg(feature = "rawkey")] - { - let rawkey = rawkey::RawKey::new(); - loop { - use std::{thread, time::Duration}; + let cursor = cursor(); + let _ = cursor.hide(); - if rawkey.is_pressed(rawkey::KeyCode::Escape) { - break; - } - if rawkey.is_pressed(rawkey::KeyCode::UpArrow) { - if starting_row > 0 { - starting_row -= 1; - max_bottom_line = - paint_textview(&draw_commands, starting_row, use_color_buffer); - } - } - if rawkey.is_pressed(rawkey::KeyCode::DownArrow) { - 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(height, starting_row); - max_bottom_line = - paint_textview(&draw_commands, starting_row, use_color_buffer); - } - if rawkey.is_pressed(rawkey::KeyCode::PageDown) - || rawkey.is_pressed(rawkey::KeyCode::Space) - { - if starting_row < (max_bottom_line - height) { - starting_row += height; + let input = crossterm::input(); + let mut sync_stdin = input.read_sync(); - if starting_row > (max_bottom_line - height) { - starting_row = max_bottom_line - height; + loop { + if let Some(ev) = sync_stdin.next() { + match ev { + InputEvent::Keyboard(k) => match k { + KeyEvent::Esc => { + break; } - } - max_bottom_line = - paint_textview(&draw_commands, starting_row, use_color_buffer); - } - thread::sleep(Duration::from_millis(50)); - let new_size = terminal.terminal_size(); - if size != new_size { - size = new_size; - let _ = terminal.clear(crossterm::ClearType::All); - max_bottom_line = - paint_textview(&draw_commands, starting_row, use_color_buffer); + KeyEvent::Up => { + if starting_row > 0 { + starting_row -= 1; + max_bottom_line = paint_textview( + &draw_commands, + starting_row, + use_color_buffer, + ); + } + } + KeyEvent::Down => { + if starting_row < (max_bottom_line - height) { + starting_row += 1; + } + max_bottom_line = + paint_textview(&draw_commands, starting_row, use_color_buffer); + } + KeyEvent::PageUp => { + starting_row -= std::cmp::min(height, starting_row); + max_bottom_line = + paint_textview(&draw_commands, starting_row, use_color_buffer); + } + KeyEvent::PageDown | KeyEvent::Char(' ') => { + if starting_row < (max_bottom_line - height) { + starting_row += height; + + if starting_row > (max_bottom_line - height) { + starting_row = max_bottom_line - height; + } + } + max_bottom_line = + paint_textview(&draw_commands, starting_row, use_color_buffer); + } + _ => {} + }, + _ => {} } } - let _ = cursor.show(); - #[allow(unused)] - let screen = RawScreen::disable_raw_mode(); - } - #[cfg(not(feature = "rawkey"))] - { - use crossterm::{InputEvent, KeyEvent}; - - loop { - if let Some(ref mut sync_stdin) = sync_stdin { - if let Some(ev) = sync_stdin.next() { - match ev { - InputEvent::Keyboard(k) => match k { - KeyEvent::Esc => { - break; - } - KeyEvent::Up => { - if starting_row > 0 { - starting_row -= 1; - max_bottom_line = paint_textview( - &draw_commands, - starting_row, - use_color_buffer, - ); - } - } - KeyEvent::Down => { - if starting_row < (max_bottom_line - height) { - starting_row += 1; - } - max_bottom_line = paint_textview( - &draw_commands, - starting_row, - use_color_buffer, - ); - } - KeyEvent::PageUp => { - starting_row -= std::cmp::min(height, starting_row); - max_bottom_line = paint_textview( - &draw_commands, - starting_row, - use_color_buffer, - ); - } - KeyEvent::PageDown | KeyEvent::Char(' ') => { - if starting_row < (max_bottom_line - height) { - starting_row += height; - - if starting_row > (max_bottom_line - height) { - starting_row = max_bottom_line - height; - } - } - max_bottom_line = paint_textview( - &draw_commands, - starting_row, - use_color_buffer, - ); - } - _ => {} - }, - _ => {} - } - } - } - - let new_size = terminal.terminal_size(); - if size != new_size { - size = new_size; - let _ = terminal.clear(crossterm::ClearType::All); - max_bottom_line = - paint_textview(&draw_commands, starting_row, use_color_buffer); - } + let new_size = terminal.terminal_size(); + if size != new_size { + size = new_size; + let _ = terminal.clear(crossterm::ClearType::All); + max_bottom_line = + paint_textview(&draw_commands, starting_row, use_color_buffer); } - let _ = cursor.show(); - - #[allow(unused)] - let screen = RawScreen::disable_raw_mode(); } + let _ = cursor.show(); + + #[allow(unused)] + let screen = RawScreen::disable_raw_mode(); } }