cursive/examples/edit.rs

54 lines
1.6 KiB
Rust
Raw Normal View History

extern crate cursive;
use cursive::traits::*;
use cursive::views::{Dialog, EditView, TextView};
2018-06-11 06:29:10 +00:00
use cursive::Cursive;
fn main() {
let mut siv = Cursive::default();
2015-05-26 23:11:22 +00:00
// Create a dialog with an edit text and a button.
// The user can either hit the <Ok> button,
// or press Enter on the edit text.
2017-10-12 23:38:55 +00:00
siv.add_layer(
Dialog::new()
.title("Enter your name")
2018-01-16 02:55:27 +00:00
// Padding is (left, right, top, bottom)
2017-10-12 23:38:55 +00:00
.padding((1, 1, 1, 0))
.content(
EditView::new()
.on_submit(show_popup)
.with_id("name")
.fixed_width(20),
)
.button("Ok", |s| {
2018-01-16 02:55:27 +00:00
// This will run the given closure, *ONLY* if a view with the
// correct type and the given ID is found.
2017-12-30 22:03:42 +00:00
let name = s.call_on_id("name", |view: &mut EditView| {
2018-01-16 02:55:27 +00:00
// We can return content from the closure!
2017-12-30 22:03:42 +00:00
view.get_content()
}).unwrap();
2018-01-16 02:55:27 +00:00
// Run the next step
2017-10-12 23:38:55 +00:00
show_popup(s, &name);
}),
);
siv.run();
}
2018-01-16 02:55:27 +00:00
// This will replace the current layer with a new popup.
// If the name is empty, we'll show an error message instead.
fn show_popup(s: &mut Cursive, name: &str) {
if name.is_empty() {
s.add_layer(Dialog::info("Please enter a name!"));
} else {
let content = format!("Hello {}!", name);
s.pop_layer();
2017-10-12 23:38:55 +00:00
s.add_layer(
Dialog::around(TextView::new(content))
.button("Quit", |s| s.quit()),
);
}
}