summaryrefslogtreecommitdiffstats
path: root/src/ui
diff options
context:
space:
mode:
authorJeff Zhao <jeff.no.zhao@gmail.com>2021-09-30 20:17:53 -0400
committerJeff Zhao <jeff.no.zhao@gmail.com>2021-09-30 20:17:53 -0400
commitfdbedc755be268884e111c357c8156561cd10f3d (patch)
tree52d820c70af135c81d14bbdd9d506d469c2ac5dc /src/ui
parent6b78a15f5dd4d3cd5329e67a42758cb53885d793 (diff)
move logic out of tui_command_menu.rs into input.rs
Diffstat (limited to 'src/ui')
-rw-r--r--src/ui/views/tui_command_menu.rs122
1 files changed, 41 insertions, 81 deletions
diff --git a/src/ui/views/tui_command_menu.rs b/src/ui/views/tui_command_menu.rs
index 3b9a295..2954d5a 100644
--- a/src/ui/views/tui_command_menu.rs
+++ b/src/ui/views/tui_command_menu.rs
@@ -1,10 +1,9 @@
use std::iter::Iterator;
-use termion::event::{Event, Key};
+use tui::buffer::Buffer;
use tui::layout::Rect;
-use tui::widgets::Clear;
+use tui::widgets::{Clear, Widget};
-use crate::commands::{CommandKeybind, KeyCommand};
use crate::config::AppKeyMapping;
use crate::context::AppContext;
use crate::event::AppEvent;
@@ -17,91 +16,52 @@ use crate::util::to_string::ToString;
const BORDER_HEIGHT: usize = 1;
const BOTTOM_MARGIN: usize = 1;
-pub struct TuiCommandMenu;
+pub struct TuiCommandMenu<'a> {
+ context: &'a AppContext,
+ keymap: &'a AppKeyMapping,
+}
-impl TuiCommandMenu {
- pub fn new() -> Self {
- Self {}
+impl<'a> TuiCommandMenu<'a> {
+ pub fn new(context: &'a AppContext, keymap: &'a AppKeyMapping) -> Self {
+ Self { context, keymap }
}
+}
- pub fn get_input<'a>(
- &mut self,
- backend: &mut TuiBackend,
- context: &mut AppContext,
- map: &'a AppKeyMapping,
- ) -> Option<&'a KeyCommand> {
- let mut map = map;
- let terminal = backend.terminal_mut();
- context.flush_event();
-
- loop {
- let _ = terminal.draw(|frame| {
- let area = frame.size();
-
- {
- let view = TuiView::new(context);
- frame.render_widget(view, area);
- }
-
- {
- // draw menu
- let mut display_vec: Vec<String> = map
- .as_ref()
- .iter()
- .map(|(k, v)| format!(" {} {}", k.to_string(), v))
- .collect();
- display_vec.sort();
- let display_str: Vec<&str> = display_vec.iter().map(|v| v.as_str()).collect();
- let display_str_len = display_str.len();
+impl<'a> Widget for TuiCommandMenu<'a> {
+ fn render(self, area: Rect, buf: &mut Buffer) {
+ TuiView::new(self.context).render(area, buf);
- let y = if (area.height as usize)
- < display_str_len + BORDER_HEIGHT + BOTTOM_MARGIN
- {
- 0
- } else {
- area.height
- - (BORDER_HEIGHT + BOTTOM_MARGIN) as u16
- - display_str_len as u16
- };
+ // draw menu
+ let mut display_vec: Vec<String> = self
+ .keymap
+ .as_ref()
+ .iter()
+ .map(|(k, v)| format!(" {} {}", k.to_string(), v))
+ .collect();
+ display_vec.sort();
+ let display_str: Vec<&str> = display_vec.iter().map(|v| v.as_str()).collect();
+ let display_str_len = display_str.len();
- let menu_height = if display_str_len + BORDER_HEIGHT > area.height as usize {
- area.height
- } else {
- (display_str_len + BORDER_HEIGHT) as u16
- };
+ let y = if (area.height as usize) < display_str_len + BORDER_HEIGHT + BOTTOM_MARGIN {
+ 0
+ } else {
+ area.height - (BORDER_HEIGHT + BOTTOM_MARGIN) as u16 - display_str_len as u16
+ };
- let menu_rect = Rect {
- x: 0,
- y,
- width: area.width,
- height: menu_height,
- };
+ let menu_height = if display_str_len + BORDER_HEIGHT > area.height as usize {
+ area.height
+ } else {
+ (display_str_len + BORDER_HEIGHT) as u16
+ };
- frame.render_widget(Clear, menu_rect);
- frame.render_widget(TuiMenu::new(&display_str), menu_rect);
- }
- });
+ let menu_rect = Rect {
+ x: 0,
+ y,
+ width: area.width,
+ height: menu_height,
+ };
- if let Ok(event) = context.poll_event() {
- match event {
- AppEvent::Termion(event) => {
- match event {
- Event::Key(Key::Esc) => return None,
- event => match map.as_ref().get(&event) {
- Some(CommandKeybind::SimpleKeybind(s)) => {
- return Some(s);
- }
- Some(CommandKeybind::CompositeKeybind(m)) => {
- map = m;
- }
- None => return None,
- },
- }
- context.flush_event();
- }
- event => input::process_noninteractive(event, context),
- }
- }
- }
+ Clear.render(menu_rect, buf);
+ TuiMenu::new(&display_str).render(menu_rect, buf);
}
}