From 3df17cc64b7a7a64f78f6a65389a6625acc259cc Mon Sep 17 00:00:00 2001 From: Alexandre Bury Date: Fri, 22 May 2015 00:01:23 -0700 Subject: [PATCH] Improved lorem example TODO: add margins around the textview? --- examples/lorem.rs | 8 +++++++- src/view/dialog.rs | 7 +++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/examples/lorem.rs b/examples/lorem.rs index 064e3a8..310c2ed 100644 --- a/examples/lorem.rs +++ b/examples/lorem.rs @@ -2,6 +2,7 @@ extern crate cursive; use cursive::Cursive; use cursive::view::TextView; +use cursive::view::Dialog; use std::fs::File; use std::io::Read; @@ -9,7 +10,7 @@ use std::io::Read; fn main() { let mut siv = Cursive::new(); - // Read some long text to showcase the layout + // Read some long text from a file. let mut file = File::open("assets/lorem.txt").unwrap(); let mut content = String::new(); file.read_to_string(&mut content).unwrap(); @@ -17,7 +18,12 @@ fn main() { // We can quit by pressing q siv.add_global_callback('q' as i32, |s,_| s.quit()); + // The text is too long to fit on a line, so the view will wrap lines, + // and will adapt to the terminal size. siv.add_layer(TextView::new(&content)); + // Show a popup on top of the view. + siv.add_layer(Dialog::new(TextView::new("Try resizing the terminal!\n(Press 'q' to quit when you're done)")) + .dismiss_button("Ok")); siv.run(); } diff --git a/src/view/dialog.rs b/src/view/dialog.rs index 9ce5f54..02b00ce 100644 --- a/src/view/dialog.rs +++ b/src/view/dialog.rs @@ -59,6 +59,13 @@ impl Dialog { self } + /// Shortcut method to add a button that will dismiss the dialog. + pub fn dismiss_button<'a>(self, label: &'a str) -> Self { + self.button(label, |s, _| s.screen_mut().pop_layer()) + } + + /// Sets the title of the dialog. + /// If not empty, it will be visible at the top. pub fn title(mut self, label: &str) -> Self { self.title = label.to_string(); self