mirror of
https://github.com/nushell/nushell.git
synced 2025-04-24 05:08:29 +02:00
Move to rawkey
This commit is contained in:
parent
a6b79748d9
commit
501482cc31
@ -1,11 +1,12 @@
|
|||||||
#![feature(option_flattening)]
|
#![feature(option_flattening)]
|
||||||
|
|
||||||
use crossterm::{cursor, 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;
|
||||||
@ -80,71 +81,36 @@ fn paint_textview(lines: &Vec<String>, starting_row: usize) -> (u16, u16) {
|
|||||||
|
|
||||||
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();
|
||||||
|
|
||||||
if let Ok(_raw) = RawScreen::into_raw_mode() {
|
if let Ok(_raw) = RawScreen::into_raw_mode() {
|
||||||
let terminal = terminal();
|
|
||||||
let input = crossterm::input();
|
|
||||||
let cursor = cursor();
|
let cursor = cursor();
|
||||||
|
|
||||||
let _ = cursor.hide();
|
let _ = cursor.hide();
|
||||||
|
|
||||||
let mut async_stdin = input.read_async();
|
|
||||||
let _ = terminal.clear(crossterm::ClearType::All);
|
|
||||||
|
|
||||||
let mut size = paint_textview(&lines, starting_row);
|
let mut size = paint_textview(&lines, starting_row);
|
||||||
loop {
|
loop {
|
||||||
if let Some(key_event) = async_stdin.next() {
|
if rawkey.is_pressed(rawkey::KeyCode::Escape) {
|
||||||
match key_event {
|
|
||||||
InputEvent::Keyboard(k) => match k {
|
|
||||||
KeyEvent::Esc => {
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
KeyEvent::Up => {
|
if rawkey.is_pressed(rawkey::KeyCode::UpArrow) {
|
||||||
if starting_row > 0 {
|
if starting_row > 0 {
|
||||||
starting_row -= 1;
|
starting_row -= 1;
|
||||||
size = paint_textview(&lines, starting_row);
|
size = paint_textview(&lines, starting_row);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
KeyEvent::Down => {
|
if rawkey.is_pressed(rawkey::KeyCode::DownArrow) {
|
||||||
if starting_row
|
if starting_row < (std::cmp::max(size.1 as usize, lines.len()) - size.1 as usize) {
|
||||||
< (std::cmp::max(size.1 as usize, lines.len()) - size.1 as usize)
|
|
||||||
{
|
|
||||||
starting_row += 1;
|
starting_row += 1;
|
||||||
size = paint_textview(&lines, starting_row);
|
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 => {
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
},
|
|
||||||
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
thread::sleep(Duration::from_millis(50));
|
thread::sleep(Duration::from_millis(50));
|
||||||
}
|
}
|
||||||
|
|
||||||
let _ = cursor.show();
|
let _ = cursor.show();
|
||||||
}
|
}
|
||||||
|
thread::sleep(Duration::from_millis(250));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn scroll_view(s: &str) {
|
fn scroll_view(s: &str) {
|
||||||
|
Loading…
Reference in New Issue
Block a user