Merge pull request #211 from jonathandturner/more_textview

More textview
This commit is contained in:
Jonathan Turner 2019-07-25 15:00:23 +12:00 committed by GitHub
commit 0bca7f8b11
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 76 additions and 78 deletions

View File

@ -6,6 +6,7 @@ use nu::{
Value, Value,
}; };
use pretty_hex::*; use pretty_hex::*;
use std::{thread, time::Duration};
struct BinaryView; struct BinaryView;
@ -444,6 +445,9 @@ pub fn view_contents_interactive(
#[allow(unused)] #[allow(unused)]
let screen = RawScreen::disable_raw_mode(); let screen = RawScreen::disable_raw_mode();
println!("");
thread::sleep(Duration::from_millis(50));
Ok(()) Ok(())
} }

View File

@ -1,14 +1,16 @@
#![feature(option_flattening)] #![feature(option_flattening)]
use crossterm::{cursor, input, terminal, InputEvent, KeyEvent, RawScreen}; use crossterm::{cursor, terminal, RawScreen};
use indexmap::IndexMap; use indexmap::IndexMap;
use nu::{ use nu::{
serve_plugin, CallInfo, CommandConfig, Plugin, Primitive, ShellError, SourceMap, SpanSource, serve_plugin, CallInfo, CommandConfig, Plugin, Primitive, ShellError, SourceMap, SpanSource,
Spanned, Value, Spanned, Value,
}; };
use rawkey::RawKey;
use std::io::Write; use std::io::Write;
use std::path::Path; use std::path::Path;
use std::{thread, time::Duration};
struct TextView; struct TextView;
@ -35,99 +37,91 @@ 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>) { fn scroll_view_lines(lines: Vec<String>) {
let mut starting_row = 0; let mut starting_row = 0;
let rawkey = RawKey::new();
let terminal = terminal();
if let Ok(_raw) = RawScreen::into_raw_mode() { if let Ok(_raw) = RawScreen::into_raw_mode() {
let input = input();
let cursor = cursor(); let cursor = cursor();
let _ = cursor.hide(); let _ = cursor.hide();
let mut sync_stdin = input.read_sync(); let input = crossterm::input();
let _ = input.read_async();
let mut size = paint_textview(&lines, starting_row);
loop { loop {
let size = terminal.terminal_size(); if rawkey.is_pressed(rawkey::KeyCode::Escape) {
let _ = terminal.clear(crossterm::ClearType::All); break;
let _ = cursor.goto(0, 0); }
if rawkey.is_pressed(rawkey::KeyCode::UpArrow) {
let mut total_max_num_lines = 0; if starting_row > 0 {
for line in lines.iter().skip(starting_row).take(size.1 as usize) { starting_row -= 1;
//let pos = cursor.pos(); size = paint_textview(&lines, starting_row);
let stripped_line = strip_ansi_escapes::strip(&line.as_bytes()).unwrap(); }
let line_length = stripped_line.len(); }
if rawkey.is_pressed(rawkey::KeyCode::DownArrow) {
let max_num_lines = line_length as u16 / size.0 if starting_row < (std::cmp::max(size.1 as usize, lines.len()) - size.1 as usize) {
+ if (line_length as u16 % size.0) > 0 { starting_row += 1;
1 size = paint_textview(&lines, starting_row);
} 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); thread::sleep(Duration::from_millis(50));
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 {
match key_event {
InputEvent::Keyboard(k) => match k {
KeyEvent::Esc => {
break;
}
KeyEvent::Up => {
if starting_row > 0 {
starting_row -= 1;
}
}
KeyEvent::Down => {
if starting_row
< (std::cmp::max(size.1 as usize, lines.len()) - size.1 as usize)
{
starting_row += 1;
}
}
KeyEvent::PageUp => {
starting_row -= std::cmp::min(starting_row, size.1 as usize);
}
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;
}
}
KeyEvent::PageDown => {
if starting_row
< (std::cmp::max(size.1 as usize, lines.len()) - size.1 as usize)
{
starting_row += size.1 as usize;
}
}
_ => {}
},
_ => {}
}
}
} }
let _ = cursor.show(); let _ = cursor.show();
} }
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) { fn scroll_view(s: &str) {