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
|
use std::io::{self, Write};
use std::iter::Iterator;
use termion::clear;
use termion::cursor::Goto;
use termion::event::Key;
use termion::input::TermRead;
use termion::raw::IntoRawMode;
use termion::screen::AlternateScreen;
use tui::backend::{Backend, TermionBackend};
use tui::layout::{Constraint, Direction, Layout, Rect};
use tui::style::{Color, Style};
use tui::widgets::{Block, Borders, List, Paragraph, Text, Widget};
use tui::Terminal;
use unicode_width::UnicodeWidthStr;
use crate::ui::TuiBackend;
use crate::util::event::{Event, Events};
pub struct OptionMenu<'a> {
backend: &'a mut TuiBackend,
events: &'a Events
}
impl<'a> OptionMenu<'a> {
pub fn new(backend: &'a mut TuiBackend,
events: &'a Events) -> Self {
Self { backend, events }
}
pub fn get_option(&mut self, options: &[&str]) -> Option<Key> {
let events = self.events;
// initially, clear the line for textfield and move the cursor there as well
let f_size = {
let frame = self.backend.terminal.get_frame();
frame.size()
};
let txt_y = if f_size.height < options.len() as u16 {
0
} else {
f_size.height - options.len() as u16
};
let termion_terminal = self.backend.terminal.backend_mut();
write!(termion_terminal, "{}", Goto(1, txt_y));
for (i, option) in options.iter().enumerate() {
write!(termion_terminal, "{}{}{}",
option, Goto(1, txt_y + i as u16), clear::AfterCursor);
}
io::stdout().flush().ok();
loop {
eprintln!("menu loop");
let event = events.next();
if let Ok(event) = event {
match event {
Event::Input(input) => match input {
Key::Esc => return None,
key => return Some(key),
},
_ => {},
}
}
}
}
}
|