From 422a0f8496a1e93cd60d4b7e7447c3c27e84725b Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Thu, 22 Aug 2019 17:11:13 +1200 Subject: [PATCH 1/2] Disable rawkey on non-x11 linux --- src/plugins/textview.rs | 134 +++++++++++++++++++++++++++++++--------- 1 file changed, 106 insertions(+), 28 deletions(-) diff --git a/src/plugins/textview.rs b/src/plugins/textview.rs index 8c3f41b2e7..c3c313e4e8 100644 --- a/src/plugins/textview.rs +++ b/src/plugins/textview.rs @@ -125,6 +125,7 @@ fn paint_textview( fn scroll_view_lines_if_needed(draw_commands: Vec, use_color_buffer: bool) { let mut starting_row = 0; + let rawkey = RawKey::new(); if let Ok(_raw) = RawScreen::into_raw_mode() { @@ -132,7 +133,23 @@ fn scroll_view_lines_if_needed(draw_commands: Vec, use_color_buffer let _ = cursor.hide(); let input = crossterm::input(); - let _ = input.read_async(); + + let mut use_rawkey = false; + let mut sync_stdin = None; + + #[cfg(target_os = "linux")] + { + // if we're in Linux but not X11, we need to avoid using rawkey for now + if std::env::var("DISPLAY").is_err() { + use_rawkey = false; + } + } + + if use_rawkey { + let _ = input.read_async(); + } else { + sync_stdin = Some(input.read_sync()); + } let terminal = terminal(); let mut size = terminal.terminal_size(); @@ -143,42 +160,103 @@ fn scroll_view_lines_if_needed(draw_commands: Vec, use_color_buffer // Only scroll if needed if max_bottom_line > height 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; + if use_rawkey { + 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 - height) { + 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; + 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); } - 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 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) { - starting_row = max_bottom_line - 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); + } + thread::sleep(Duration::from_millis(50)); + } else { + use crossterm::{InputEvent, KeyEvent}; + + 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, + ); + } + _ => {} + }, + _ => {} + } } } - 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; From af51a65528d5393cc3cdd5f5cae44f79f30c5fb4 Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Thu, 22 Aug 2019 17:40:33 +1200 Subject: [PATCH 2/2] Try to fix warning --- src/plugins/textview.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/plugins/textview.rs b/src/plugins/textview.rs index c3c313e4e8..b5899ac19b 100644 --- a/src/plugins/textview.rs +++ b/src/plugins/textview.rs @@ -134,7 +134,7 @@ fn scroll_view_lines_if_needed(draw_commands: Vec, use_color_buffer let input = crossterm::input(); - let mut use_rawkey = false; + let use_rawkey; let mut sync_stdin = None; #[cfg(target_os = "linux")] @@ -142,9 +142,16 @@ fn scroll_view_lines_if_needed(draw_commands: Vec, use_color_buffer // if we're in Linux but not X11, we need to avoid using rawkey for now if std::env::var("DISPLAY").is_err() { use_rawkey = false; + } else { + use_rawkey = true; } } + #[cfg(not(target_os = "linux"))] + { + use_rawkey = true + } + if use_rawkey { let _ = input.read_async(); } else {