Replace some &str -> S: Into<String>

Add back the "Ok" button to `examples/edit.rs`
This commit is contained in:
Alexandre Bury 2016-07-30 12:58:25 -07:00
parent ca6e16311f
commit 40f9a91a7a
5 changed files with 47 additions and 37 deletions

View File

@ -6,23 +6,33 @@ fn main() {
let mut siv = Cursive::new();
// 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.
siv.add_layer(Dialog::empty()
.title("Enter your name")
.padding((1, 1, 1, 0))
.content(EditView::new()
.min_length(20)
.on_submit(|s, name| {
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()));
}
}))
);
.on_submit(show_popup)
.with_id("name"))
.button("Ok", |s| {
let name = s.find_id::<EditView>("name")
.unwrap()
.get_content();
show_popup(s, &name);
}));
siv.run();
}
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();
s.add_layer(Dialog::new(TextView::new(content))
.button("Quit", |s| s.quit()));
}
}

View File

@ -26,11 +26,11 @@ pub struct Button {
impl Button {
/// Creates a new button with the given content and callback.
pub fn new<F>(label: &str, cb: F) -> Self
pub fn new<F, S: Into<String>>(label: S, cb: F) -> Self
where F: Fn(&mut Cursive) + 'static
{
Button {
label: label.to_string(),
label: label.into(),
callback: Callback::from_fn(cb),
enabled: true,
}

View File

@ -81,14 +81,14 @@ impl Dialog {
/// Convenient method to create an infobox.
///
/// It will contain the given text and a `Ok` dismiss button.
pub fn info(text: &str) -> Self {
pub fn info<S: Into<String>>(text: S) -> Self {
Self::new(TextView::new(text)).dismiss_button("Ok")
}
/// Adds a button to the dialog with the given label and callback.
///
/// Consumes and returns self for easy chaining.
pub fn button<F>(mut self, label: &str, cb: F) -> Self
pub fn button<F, S: Into<String>>(mut self, label: S, cb: F) -> Self
where F: Fn(&mut Cursive) + 'static
{
self.buttons.push(SizedView::new(Button::new(label, cb)));
@ -113,14 +113,14 @@ impl Dialog {
}
/// Shortcut method to add a button that will dismiss the dialog.
pub fn dismiss_button(self, label: &str) -> Self {
pub fn dismiss_button<S: Into<String>>(self, label: S) -> 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();
pub fn title<S: Into<String>>(mut self, label: S) -> Self {
self.title = label.into();
self
}

View File

@ -167,8 +167,8 @@ impl EditView {
}
/// Get the current text.
pub fn get_content(&self) -> &str {
&&self.content
pub fn get_content(&self) -> Rc<String> {
self.content.clone()
}
/// Sets the current content to the given value.
@ -231,7 +231,7 @@ impl View for EditView {
if self.secret {
printer.print_hline((0, 0), width, "*");
} else {
printer.print((0, 0), self.get_content());
printer.print((0, 0), &self.content);
}
printer.print_hline((width, 0),
printer.size.x - width,

View File

@ -30,20 +30,19 @@ pub struct TextView {
}
// If the last character is a newline, strip it.
fn strip_last_newline(content: &str) -> &str {
if !content.is_empty() && content.chars().last().unwrap() == '\n' {
&content[..content.len() - 1]
} else {
content
fn strip_last_newline(content: &mut String) {
if content.ends_with('\n') {
content.pop().unwrap();
}
}
impl TextView {
/// Creates a new TextView with the given content.
pub fn new(content: &str) -> Self {
let content = strip_last_newline(content);
pub fn new<S: Into<String>>(content: S) -> Self {
let mut content = content.into();
strip_last_newline(&mut content);
TextView {
content: content.to_string(),
content: content,
rows: Vec::new(),
scrollable: true,
scrollbase: ScrollBase::new(),
@ -99,9 +98,10 @@ impl TextView {
}
/// Replace the text in this view.
pub fn set_content(&mut self, content: &str) {
let content = strip_last_newline(content);
self.content = content.to_string();
pub fn set_content<S: Into<String>>(&mut self, content: S) {
let mut content = content.into();
strip_last_newline(&mut content);
self.content = content;
self.invalidate();
}