cursive/examples/select.rs

52 lines
1.5 KiB
Rust
Raw Permalink Normal View History

extern crate cursive;
use cursive::Cursive;
use cursive::align::HAlign;
2017-08-24 00:05:31 +00:00
use cursive::event::EventResult;
use cursive::traits::*;
2017-08-24 00:05:31 +00:00
use cursive::views::{Dialog, OnEventView, SelectView, TextView};
fn main() {
let mut select = SelectView::new().h_align(HAlign::Center);
// 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);
2017-08-24 00:05:31 +00:00
// Let's override the `j` and `k` keys for navigation
let select = OnEventView::new(select)
.on_pre_event_inner('k', |s| {
s.select_up(1);
Some(EventResult::Consumed(None))
})
.on_pre_event_inner('j', |s| {
s.select_down(1);
Some(EventResult::Consumed(None))
});
let mut siv = Cursive::new();
2017-10-15 04:18:50 +00:00
// Let's add a BoxView to keep the list at a reasonable size
// (it can scroll anyway).
2017-08-24 00:05:31 +00:00
siv.add_layer(
Dialog::around(select.fixed_size((20, 10)))
.title("Where are you from?"),
);
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) {
siv.pop_layer();
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()),
);
}