forked from gyscos/cursive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselect.rs
55 lines (46 loc) · 1.77 KB
/
select.rs
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
use cursive::align::HAlign;
use cursive::event::EventResult;
use cursive::traits::*;
use cursive::views::{Dialog, OnEventView, SelectView, TextView};
use cursive::Cursive;
// We'll use a SelectView here.
//
// A SelectView is a scrollable list of items, from which the user can select
// one.
fn main() {
let mut select = SelectView::new()
// Center the text horizontally
.h_align(HAlign::Center)
// Use keyboard to jump to the pressed letters
.autojump();
// Read the list of cities from separate file, and fill the view with it.
// (We include the file at compile-time to avoid runtime read errors.)
let content = include_str!("assets/cities.txt");
select.add_all_str(content.lines());
// Sets the callback for when "Enter" is pressed.
select.set_on_submit(show_next_window);
// Let's override the `j` and `k` keys for navigation
let select = OnEventView::new(select)
.on_pre_event_inner('k', |s, _| {
let cb = s.select_up(1);
Some(EventResult::Consumed(Some(cb)))
})
.on_pre_event_inner('j', |s, _| {
let cb = s.select_down(1);
Some(EventResult::Consumed(Some(cb)))
});
let mut siv = cursive::default();
// Let's add a ResizedView to keep the list at a reasonable size
// (it can scroll anyway).
siv.add_layer(
Dialog::around(select.scrollable().fixed_size((20, 10))).title("Where are you from?"),
);
siv.run();
}
// Let's put the callback in a separate function to keep it clean,
// but it's not required.
fn show_next_window(siv: &mut Cursive, city: &str) {
siv.pop_layer();
let text = format!("{city} is a great city!");
siv.add_layer(Dialog::around(TextView::new(text)).button("Quit", |s| s.quit()));
}