Fix a few issues with textview and the parser

This commit is contained in:
Jonathan Turner 2019-08-25 19:15:56 +12:00
parent 5a355683ad
commit 439700b87c
2 changed files with 58 additions and 139 deletions

View File

@ -556,6 +556,7 @@ fn is_bare_char(c: char) -> bool {
'@' => true, '@' => true,
'*' => true, '*' => true,
'?' => true, '?' => true,
'=' => true,
_ => false, _ => false,
} }
} }

View File

@ -1,6 +1,7 @@
#![feature(option_flattening)] #![feature(option_flattening)]
use crossterm::{cursor, terminal, RawScreen}; use crossterm::{cursor, terminal, RawScreen};
use crossterm::{InputEvent, KeyEvent};
use indexmap::IndexMap; use indexmap::IndexMap;
use nu::{ use nu::{
serve_plugin, CallInfo, Plugin, Primitive, ShellError, Signature, SourceMap, SpanSource, serve_plugin, CallInfo, Plugin, Primitive, ShellError, Signature, SourceMap, SpanSource,
@ -125,24 +126,6 @@ fn scroll_view_lines_if_needed(draw_commands: Vec<DrawCommand>, use_color_buffer
let mut starting_row = 0; let mut starting_row = 0;
if let Ok(_raw) = RawScreen::into_raw_mode() { 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<crossterm::SyncReader>;
#[cfg(feature = "rawkey")]
{
let _ = input.read_async();
}
#[cfg(not(feature = "rawkey"))]
{
sync_stdin = Some(input.read_sync());
}
let terminal = terminal(); let terminal = terminal();
let mut size = terminal.terminal_size(); let mut size = terminal.terminal_size();
let height = size.1 as usize - 1; let height = size.1 as usize - 1;
@ -151,135 +134,70 @@ fn scroll_view_lines_if_needed(draw_commands: Vec<DrawCommand>, use_color_buffer
// Only scroll if needed // Only scroll if needed
if max_bottom_line > height as usize { if max_bottom_line > height as usize {
#[cfg(feature = "rawkey")] let cursor = cursor();
{ let _ = cursor.hide();
let rawkey = rawkey::RawKey::new();
loop {
use std::{thread, time::Duration};
if rawkey.is_pressed(rawkey::KeyCode::Escape) { let input = crossterm::input();
break; let mut sync_stdin = input.read_sync();
}
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;
if starting_row > (max_bottom_line - height) { loop {
starting_row = max_bottom_line - height; if let Some(ev) = sync_stdin.next() {
match ev {
InputEvent::Keyboard(k) => match k {
KeyEvent::Esc => {
break;
} }
} KeyEvent::Up => {
max_bottom_line = if starting_row > 0 {
paint_textview(&draw_commands, starting_row, use_color_buffer); starting_row -= 1;
} max_bottom_line = paint_textview(
thread::sleep(Duration::from_millis(50)); &draw_commands,
let new_size = terminal.terminal_size(); starting_row,
if size != new_size { use_color_buffer,
size = new_size; );
let _ = terminal.clear(crossterm::ClearType::All); }
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 new_size = terminal.terminal_size();
let screen = RawScreen::disable_raw_mode(); if size != new_size {
} size = new_size;
#[cfg(not(feature = "rawkey"))] let _ = terminal.clear(crossterm::ClearType::All);
{ max_bottom_line =
use crossterm::{InputEvent, KeyEvent}; paint_textview(&draw_commands, starting_row, use_color_buffer);
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 _ = cursor.show();
#[allow(unused)]
let screen = RawScreen::disable_raw_mode();
} }
let _ = cursor.show();
#[allow(unused)]
let screen = RawScreen::disable_raw_mode();
} }
} }