2015-06-02 21:23:51 +00:00
|
|
|
use cursive::align::HAlign;
|
2017-08-24 00:05:31 +00:00
|
|
|
use cursive::event::EventResult;
|
2016-09-29 05:45:27 +00:00
|
|
|
use cursive::traits::*;
|
2017-08-24 00:05:31 +00:00
|
|
|
use cursive::views::{Dialog, OnEventView, SelectView, TextView};
|
2018-06-11 06:29:10 +00:00
|
|
|
use cursive::Cursive;
|
2015-05-31 04:05:34 +00:00
|
|
|
|
2018-01-16 02:55:27 +00:00
|
|
|
// We'll use a SelectView here.
|
|
|
|
//
|
|
|
|
// A SelectView is a scrollable list of items, from which the user can select
|
|
|
|
// one.
|
|
|
|
|
2015-05-31 04:05:34 +00:00
|
|
|
fn main() {
|
2018-09-27 23:01:37 +00:00
|
|
|
let mut select = SelectView::new()
|
|
|
|
// Center the text horizontally
|
|
|
|
.h_align(HAlign::Center)
|
|
|
|
// Use keyboard to jump to the pressed letters
|
|
|
|
.autojump();
|
2015-05-31 23:38:53 +00:00
|
|
|
|
|
|
|
// Read the list of cities from separate file, and fill the view with it.
|
2016-07-18 01:41:36 +00:00
|
|
|
// (We include the file at compile-time to avoid runtime read errors.)
|
2020-04-24 00:13:50 +00:00
|
|
|
let content = include_str!("../../assets/cities.txt");
|
2016-08-02 07:49:59 +00:00
|
|
|
select.add_all_str(content.lines());
|
2016-07-18 01:41:36 +00:00
|
|
|
|
|
|
|
// Sets the callback for when "Enter" is pressed.
|
2016-07-30 08:52:34 +00:00
|
|
|
select.set_on_submit(show_next_window);
|
2015-05-31 23:38:53 +00:00
|
|
|
|
2017-08-24 00:05:31 +00:00
|
|
|
// Let's override the `j` and `k` keys for navigation
|
|
|
|
let select = OnEventView::new(select)
|
2018-11-09 18:54:57 +00:00
|
|
|
.on_pre_event_inner('k', |s, _| {
|
2017-08-24 00:05:31 +00:00
|
|
|
s.select_up(1);
|
|
|
|
Some(EventResult::Consumed(None))
|
2018-11-09 18:54:57 +00:00
|
|
|
})
|
|
|
|
.on_pre_event_inner('j', |s, _| {
|
2017-08-24 00:05:31 +00:00
|
|
|
s.select_down(1);
|
|
|
|
Some(EventResult::Consumed(None))
|
|
|
|
});
|
|
|
|
|
2020-04-15 19:20:50 +00:00
|
|
|
let mut siv = cursive::default();
|
2015-05-31 04:05:34 +00:00
|
|
|
|
2020-01-06 19:51:50 +00:00
|
|
|
// Let's add a ResizedView to keep the list at a reasonable size
|
2017-10-15 04:18:50 +00:00
|
|
|
// (it can scroll anyway).
|
2017-08-24 00:05:31 +00:00
|
|
|
siv.add_layer(
|
2018-07-25 02:38:24 +00:00
|
|
|
Dialog::around(select.scrollable().fixed_size((20, 10)))
|
2017-08-24 00:05:31 +00:00
|
|
|
.title("Where are you from?"),
|
|
|
|
);
|
2015-05-31 04:05:34 +00:00
|
|
|
|
|
|
|
siv.run();
|
|
|
|
}
|
|
|
|
|
2017-10-15 04:18:50 +00:00
|
|
|
// Let's put the callback in a separate function to keep it clean,
|
|
|
|
// but it's not required.
|
2016-09-28 22:07:02 +00:00
|
|
|
fn show_next_window(siv: &mut Cursive, city: &str) {
|
2015-06-01 03:47:04 +00:00
|
|
|
siv.pop_layer();
|
2016-08-04 04:55:41 +00:00
|
|
|
let text = format!("{} is a great city!", city);
|
2017-08-24 00:05:31 +00:00
|
|
|
siv.add_layer(
|
|
|
|
Dialog::around(TextView::new(text)).button("Quit", |s| s.quit()),
|
|
|
|
);
|
2015-06-01 03:47:04 +00:00
|
|
|
}
|