From 4a22c9a36c2895b3937daa23568b4f1cef599ed9 Mon Sep 17 00:00:00 2001 From: Jeff Zhao Date: Sat, 2 Oct 2021 08:21:43 -0400 Subject: add basic support for command history - move worker processing into input.rs - change readline to read_and_execute --- src/ui/views/tui_textfield.rs | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) (limited to 'src/ui') diff --git a/src/ui/views/tui_textfield.rs b/src/ui/views/tui_textfield.rs index c86b49f..348c0f4 100644 --- a/src/ui/views/tui_textfield.rs +++ b/src/ui/views/tui_textfield.rs @@ -86,6 +86,8 @@ impl<'a> TuiTextField<'a> { let terminal = backend.terminal_mut(); let _ = terminal.show_cursor(); + let mut curr_history_index = context.commandline_context_ref().history_ref().len(); + loop { terminal .draw(|frame| { @@ -186,8 +188,40 @@ impl<'a> TuiTextField<'a> { line_buffer.move_end(); completion_tracker.take(); } - Key::Up => {} - Key::Down => {} + Key::Up => { + curr_history_index = if curr_history_index > 0 { + curr_history_index - 1 + } else { + 0 + }; + line_buffer.move_home(); + line_buffer.kill_line(); + if let Some(s) = context + .commandline_context_ref() + .history_ref() + .get(curr_history_index) + { + line_buffer.insert_str(0, s); + } + } + Key::Down => { + curr_history_index = if curr_history_index + < context.commandline_context_ref().history_ref().len() + { + curr_history_index + 1 + } else { + curr_history_index + }; + line_buffer.move_home(); + line_buffer.kill_line(); + if let Some(s) = context + .commandline_context_ref() + .history_ref() + .get(curr_history_index) + { + line_buffer.insert_str(0, s); + } + } Key::Esc => { let _ = terminal.hide_cursor(); return None; -- cgit v1.2.3