1extern crate rustty;
2
3use std::time::Duration;
4
5use rustty::{Terminal, Event};
6use rustty::ui::{Painter, Dialog, DialogResult, Alignable, HorizontalAlign, VerticalAlign};
7
8fn create_maindlg() -> Dialog {
9 let mut maindlg = Dialog::new(60, 10);
10 let s = "Hello! This is a showcase of the ui module!";
11 let x = maindlg.window().halign_line(s, HorizontalAlign::Middle, 1);
12 maindlg.window_mut().printline(x, 2, s);
13 maindlg.add_button("Foo", 'f', DialogResult::Custom(1));
14 maindlg.add_button("Bar", 'b', DialogResult::Custom(2));
15 maindlg.add_button("Quit", 'q', DialogResult::Ok);
16 maindlg.draw_buttons();
17 maindlg.window_mut().draw_box();
18 maindlg
19}
20
21fn main() {
22 let mut term = Terminal::new().unwrap();
23 let mut maindlg = create_maindlg();
24 maindlg.window_mut().align(&term, HorizontalAlign::Middle, VerticalAlign::Middle, 0);
25 'main: loop {
26 while let Some(Event::Key(ch)) = term.get_event(Duration::new(0, 0)).unwrap() {
27 match maindlg.result_for_key(ch) {
28 Some(DialogResult::Ok) => break 'main,
29 Some(DialogResult::Custom(i)) => {
30 let msg = if i == 1 {
31 "Foo!"
32 } else {
33 "Bar!"
34 };
35 let w = maindlg.window_mut();
36 let x = w.halign_line(msg, HorizontalAlign::Middle, 1);
37 let y = w.valign_line(msg, VerticalAlign::Middle, 1);
38 w.printline(x, y, msg);
39 }
40 _ => {}
41 }
42 }
43
44 maindlg.window().draw_into(&mut term);
45 term.swap_buffers().unwrap();
46 }
47}