1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
use std::iter::Iterator;
use termion::event::{Event, Key};
use tui::layout::Rect;
use tui::widgets::Clear;
use crate::commands::{CommandKeybind, KeyCommand};
use crate::config::AppKeyMapping;
use crate::context::AppContext;
use crate::event::AppEvent;
use crate::ui::views::TuiView;
use crate::ui::widgets::TuiMenu;
use crate::ui::TuiBackend;
use crate::util::input;
use crate::util::to_string::ToString;
const BORDER_HEIGHT: usize = 1;
const BOTTOM_MARGIN: usize = 1;
pub struct TuiCommandMenu;
impl TuiCommandMenu {
pub fn new() -> Self {
Self {}
}
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();
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_height = if display_str_len + BORDER_HEIGHT > area.height as usize {
area.height
} else {
(display_str_len + BORDER_HEIGHT) as u16
};
let menu_rect = Rect {
x: 0,
y,
width: area.width,
height: menu_height,
};
frame.render_widget(Clear, menu_rect);
frame.render_widget(TuiMenu::new(&display_str), menu_rect);
}
});
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),
}
}
}
}
}
|