cursive/examples/edit.rs

29 lines
1.1 KiB
Rust
Raw Permalink Normal View History

extern crate cursive;
use cursive::{Cursive};
2015-07-28 13:57:52 +00:00
use cursive::view::{Dialog,EditView,TextView};
fn main() {
let mut siv = Cursive::new();
2015-05-26 23:11:22 +00:00
// Create a dialog with an edit text and a button.
2015-07-28 13:57:52 +00:00
siv.add_layer(Dialog::new(EditView::new().min_length(20).with_id("edit"))
.padding((1,1,1,0))
.title("Enter your name")
.button("Ok", |s| {
2015-05-26 23:11:22 +00:00
// When the button is clicked, read the text and print it in a new dialog.
2015-07-30 12:51:12 +00:00
let name = s.find_id::<EditView>("edit").unwrap().get_content().to_string();
if name.is_empty() {
s.add_layer(Dialog::new(TextView::new("Please enter a name!"))
.dismiss_button("Ok"));
} else {
let content = format!("Hello {}!", name);
s.pop_layer();
s.add_layer(Dialog::new(TextView::new(&content))
.button("Quit", |s| s.quit()));
}
}));
siv.run();
}