mirror of
https://github.com/nushell/nushell.git
synced 2025-08-19 09:22:27 +02:00
Yet more improvements to textview (and binaryview)
This commit is contained in:
@@ -378,10 +378,10 @@ pub fn view_contents_interactive(
|
||||
let cursor = cursor();
|
||||
|
||||
let buttons = vec![
|
||||
KeyCode::LShift,
|
||||
KeyCode::LControl,
|
||||
KeyCode::Alt,
|
||||
KeyCode::LeftControl,
|
||||
KeyCode::Tab,
|
||||
KeyCode::Back,
|
||||
KeyCode::BackSpace,
|
||||
KeyCode::UpArrow,
|
||||
KeyCode::DownArrow,
|
||||
KeyCode::LeftArrow,
|
||||
|
@@ -11,7 +11,6 @@ use rawkey::RawKey;
|
||||
use syntect::easy::HighlightLines;
|
||||
use syntect::highlighting::{Style, ThemeSet};
|
||||
use syntect::parsing::SyntaxSet;
|
||||
use syntect::util::as_24_bit_terminal_escaped;
|
||||
|
||||
use std::io::Write;
|
||||
use std::path::Path;
|
||||
@@ -110,19 +109,20 @@ fn paint_textview(
|
||||
print!("{}", s);
|
||||
}
|
||||
|
||||
let _ = cursor.goto(0, size.1);
|
||||
print!(
|
||||
"{}",
|
||||
ansi_term::Colour::Blue.paint("[ESC to quit, arrow keys to move]")
|
||||
);
|
||||
print!("{}", crossterm::Attribute::Reset);
|
||||
if (frame_buffer.len() / width) > height {
|
||||
let _ = cursor.goto(0, size.1);
|
||||
print!(
|
||||
"{}",
|
||||
ansi_term::Colour::Blue.paint("[ESC to quit, arrow keys to move]")
|
||||
);
|
||||
}
|
||||
|
||||
let _ = std::io::stdout().flush();
|
||||
|
||||
frame_buffer.len() / width
|
||||
}
|
||||
|
||||
fn scroll_view_lines(draw_commands: Vec<DrawCommand>, use_color_buffer: bool) {
|
||||
fn scroll_view_lines_if_needed(draw_commands: Vec<DrawCommand>, use_color_buffer: bool) {
|
||||
let mut starting_row = 0;
|
||||
let rawkey = RawKey::new();
|
||||
|
||||
@@ -135,34 +135,54 @@ fn scroll_view_lines(draw_commands: Vec<DrawCommand>, use_color_buffer: bool) {
|
||||
|
||||
let terminal = terminal();
|
||||
let mut size = terminal.terminal_size();
|
||||
let mut max_bottom_line = size.1 as usize;
|
||||
let mut max_bottom_line = paint_textview(&draw_commands, starting_row, use_color_buffer);
|
||||
|
||||
paint_textview(&draw_commands, starting_row, use_color_buffer);
|
||||
loop {
|
||||
if rawkey.is_pressed(rawkey::KeyCode::Escape) {
|
||||
break;
|
||||
}
|
||||
if rawkey.is_pressed(rawkey::KeyCode::UpArrow) {
|
||||
if starting_row > 0 {
|
||||
starting_row -= 1;
|
||||
// Only scroll if needed
|
||||
if max_bottom_line > size.1 as usize {
|
||||
loop {
|
||||
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 - size.1 as usize) {
|
||||
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 - size.1 as usize) {
|
||||
starting_row += 1;
|
||||
if rawkey.is_pressed(rawkey::KeyCode::PageUp) {
|
||||
starting_row -= std::cmp::min(size.1 as usize, starting_row);
|
||||
max_bottom_line =
|
||||
paint_textview(&draw_commands, starting_row, use_color_buffer);
|
||||
}
|
||||
max_bottom_line = paint_textview(&draw_commands, starting_row, use_color_buffer);
|
||||
}
|
||||
if rawkey.is_pressed(rawkey::KeyCode::PageDown) {
|
||||
if starting_row < (max_bottom_line - size.1 as usize) {
|
||||
starting_row += size.1 as usize;
|
||||
|
||||
thread::sleep(Duration::from_millis(50));
|
||||
if starting_row > (max_bottom_line - size.1 as usize) {
|
||||
starting_row = max_bottom_line - size.1 as usize;
|
||||
}
|
||||
}
|
||||
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);
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -185,7 +205,7 @@ fn scroll_view(s: &str) {
|
||||
v.push(DrawCommand::DrawString(Style::default(), line.to_string()));
|
||||
v.push(DrawCommand::NextLine);
|
||||
}
|
||||
scroll_view_lines(v, false);
|
||||
scroll_view_lines_if_needed(v, false);
|
||||
}
|
||||
|
||||
fn view_text_value(value: &Spanned<Value>, source_map: &SourceMap) {
|
||||
@@ -220,9 +240,6 @@ fn view_text_value(value: &Spanned<Value>, source_map: &SourceMap) {
|
||||
for line in s.lines() {
|
||||
let ranges: Vec<(Style, &str)> = h.highlight(line, &ps);
|
||||
|
||||
// let escaped =
|
||||
// as_24_bit_terminal_escaped(&ranges[..], false);
|
||||
// v.push(format!("{}", escaped));
|
||||
for range in ranges {
|
||||
v.push(DrawCommand::DrawString(
|
||||
range.0,
|
||||
@@ -232,7 +249,7 @@ fn view_text_value(value: &Spanned<Value>, source_map: &SourceMap) {
|
||||
|
||||
v.push(DrawCommand::NextLine);
|
||||
}
|
||||
scroll_view_lines(v, true);
|
||||
scroll_view_lines_if_needed(v, true);
|
||||
} else {
|
||||
scroll_view(s);
|
||||
}
|
||||
|
Reference in New Issue
Block a user