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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
use std::iter::Iterator;
use termion::event::Key;
use tui::buffer::Buffer;
use tui::layout::Rect;
use tui::style::{Color, Style};
use tui::widgets::{Block, Borders, Widget};
use unicode_width::UnicodeWidthStr;
use super::TuiView;
use crate::commands::{CommandKeybind, JoshutoCommand};
use crate::config::JoshutoCommandMapping;
use crate::context::JoshutoContext;
use crate::ui::TuiBackend;
use crate::util::event::Event;
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: &JoshutoContext,
m: &'a JoshutoCommandMapping,
) -> Option<&'a Box<dyn JoshutoCommand>> {
let mut map: &JoshutoCommandMapping = &m;
loop {
backend.terminal.draw(|mut frame| {
let f_size = frame.size();
{
let mut view = TuiView::new(&context);
view.render(&mut frame, f_size);
}
{
// draw menu
let mut display_vec: Vec<String> = map
.iter()
.map(|(k, v)| format!(" {:?} {}", k, 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 (f_size.height as usize)
< display_str_len + BORDER_HEIGHT + BOTTOM_MARGIN
{
0
} else {
f_size.height
- (BORDER_HEIGHT + BOTTOM_MARGIN) as u16
- display_str_len as u16
};
let menu_rect = Rect {
x: 0,
y: y,
width: f_size.width,
height: (display_str_len + BORDER_HEIGHT) as u16,
};
TuiMenu::new(&display_str).render(&mut frame, menu_rect);
}
});
if let Ok(event) = context.events.next() {
match event {
Event::Input(Key::Esc) => {
return None;
}
Event::Input(key) => match map.get(&key) {
Some(CommandKeybind::SimpleKeybind(s)) => {
return Some(s);
}
Some(CommandKeybind::CompositeKeybind(m)) => {
map = m;
}
None => return None,
},
_ => {}
}
}
}
}
}
pub struct TuiMenu<'a> {
options: &'a Vec<&'a str>,
}
impl<'a> TuiMenu<'a> {
pub fn new(options: &'a Vec<&str>) -> Self {
Self { options }
}
}
const LONG_SPACE: &str = " ";
impl<'a> Widget for TuiMenu<'a> {
fn draw(&mut self, area: Rect, buf: &mut Buffer) {
let text_iter = self.options.iter();
let mut block = Block::default().borders(Borders::TOP);
block.draw(area, buf);
let style = Style::default();
let area_x = area.x + 1;
let area_y = area.y + 1;
for (i, text) in text_iter.enumerate() {
let width = text.width();
buf.set_stringn(area_x, area_y + i as u16, text, width, style);
buf.set_stringn(
area_x + width as u16,
area_y + i as u16,
LONG_SPACE,
area.width as usize,
style,
);
}
}
}
|