Move to async keys for textview

This commit is contained in:
Jonathan Turner 2019-07-25 13:07:33 +12:00
parent f3fdda8d35
commit a6b79748d9

View File

@ -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<String>, 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<String>) {
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<String>) {
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<String>) {
< (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<String>) {
< (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<String>) {
_ => {}
}
}
thread::sleep(Duration::from_millis(50));
}
let _ = cursor.show();