mirror of
https://github.com/FliegendeWurst/cursive.git
synced 2024-11-23 17:35:00 +00:00
Replace some &str
-> S: Into<String>
Add back the "Ok" button to `examples/edit.rs`
This commit is contained in:
parent
ca6e16311f
commit
40f9a91a7a
@ -6,23 +6,33 @@ fn main() {
|
|||||||
let mut siv = Cursive::new();
|
let mut siv = Cursive::new();
|
||||||
|
|
||||||
// Create a dialog with an edit text and a button.
|
// 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()
|
siv.add_layer(Dialog::empty()
|
||||||
.title("Enter your name")
|
.title("Enter your name")
|
||||||
.padding((1, 1, 1, 0))
|
.padding((1, 1, 1, 0))
|
||||||
.content(EditView::new()
|
.content(EditView::new()
|
||||||
.min_length(20)
|
.min_length(20)
|
||||||
.on_submit(|s, name| {
|
.on_submit(show_popup)
|
||||||
if name.is_empty() {
|
.with_id("name"))
|
||||||
s.add_layer(Dialog::new(TextView::new("Please enter a name!"))
|
.button("Ok", |s| {
|
||||||
.dismiss_button("Ok"));
|
let name = s.find_id::<EditView>("name")
|
||||||
} else {
|
.unwrap()
|
||||||
let content = format!("Hello {}!", name);
|
.get_content();
|
||||||
s.pop_layer();
|
show_popup(s, &name);
|
||||||
s.add_layer(Dialog::new(TextView::new(&content))
|
}));
|
||||||
.button("Quit", |s| s.quit()));
|
|
||||||
}
|
|
||||||
}))
|
|
||||||
);
|
|
||||||
|
|
||||||
siv.run();
|
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()));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
@ -26,11 +26,11 @@ pub struct Button {
|
|||||||
|
|
||||||
impl Button {
|
impl Button {
|
||||||
/// Creates a new button with the given content and callback.
|
/// 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
|
where F: Fn(&mut Cursive) + 'static
|
||||||
{
|
{
|
||||||
Button {
|
Button {
|
||||||
label: label.to_string(),
|
label: label.into(),
|
||||||
callback: Callback::from_fn(cb),
|
callback: Callback::from_fn(cb),
|
||||||
enabled: true,
|
enabled: true,
|
||||||
}
|
}
|
||||||
|
@ -81,14 +81,14 @@ impl Dialog {
|
|||||||
/// Convenient method to create an infobox.
|
/// Convenient method to create an infobox.
|
||||||
///
|
///
|
||||||
/// It will contain the given text and a `Ok` dismiss button.
|
/// 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")
|
Self::new(TextView::new(text)).dismiss_button("Ok")
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Adds a button to the dialog with the given label and callback.
|
/// Adds a button to the dialog with the given label and callback.
|
||||||
///
|
///
|
||||||
/// Consumes and returns self for easy chaining.
|
/// 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
|
where F: Fn(&mut Cursive) + 'static
|
||||||
{
|
{
|
||||||
self.buttons.push(SizedView::new(Button::new(label, cb)));
|
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.
|
/// 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())
|
self.button(label, |s| s.screen_mut().pop_layer())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the title of the dialog.
|
/// Sets the title of the dialog.
|
||||||
/// If not empty, it will be visible at the top.
|
/// If not empty, it will be visible at the top.
|
||||||
pub fn title(mut self, label: &str) -> Self {
|
pub fn title<S: Into<String>>(mut self, label: S) -> Self {
|
||||||
self.title = label.to_string();
|
self.title = label.into();
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -167,8 +167,8 @@ impl EditView {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Get the current text.
|
/// Get the current text.
|
||||||
pub fn get_content(&self) -> &str {
|
pub fn get_content(&self) -> Rc<String> {
|
||||||
&&self.content
|
self.content.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the current content to the given value.
|
/// Sets the current content to the given value.
|
||||||
@ -231,7 +231,7 @@ impl View for EditView {
|
|||||||
if self.secret {
|
if self.secret {
|
||||||
printer.print_hline((0, 0), width, "*");
|
printer.print_hline((0, 0), width, "*");
|
||||||
} else {
|
} else {
|
||||||
printer.print((0, 0), self.get_content());
|
printer.print((0, 0), &self.content);
|
||||||
}
|
}
|
||||||
printer.print_hline((width, 0),
|
printer.print_hline((width, 0),
|
||||||
printer.size.x - width,
|
printer.size.x - width,
|
||||||
|
@ -30,20 +30,19 @@ pub struct TextView {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// If the last character is a newline, strip it.
|
// If the last character is a newline, strip it.
|
||||||
fn strip_last_newline(content: &str) -> &str {
|
fn strip_last_newline(content: &mut String) {
|
||||||
if !content.is_empty() && content.chars().last().unwrap() == '\n' {
|
if content.ends_with('\n') {
|
||||||
&content[..content.len() - 1]
|
content.pop().unwrap();
|
||||||
} else {
|
|
||||||
content
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TextView {
|
impl TextView {
|
||||||
/// Creates a new TextView with the given content.
|
/// Creates a new TextView with the given content.
|
||||||
pub fn new(content: &str) -> Self {
|
pub fn new<S: Into<String>>(content: S) -> Self {
|
||||||
let content = strip_last_newline(content);
|
let mut content = content.into();
|
||||||
|
strip_last_newline(&mut content);
|
||||||
TextView {
|
TextView {
|
||||||
content: content.to_string(),
|
content: content,
|
||||||
rows: Vec::new(),
|
rows: Vec::new(),
|
||||||
scrollable: true,
|
scrollable: true,
|
||||||
scrollbase: ScrollBase::new(),
|
scrollbase: ScrollBase::new(),
|
||||||
@ -99,9 +98,10 @@ impl TextView {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Replace the text in this view.
|
/// Replace the text in this view.
|
||||||
pub fn set_content(&mut self, content: &str) {
|
pub fn set_content<S: Into<String>>(&mut self, content: S) {
|
||||||
let content = strip_last_newline(content);
|
let mut content = content.into();
|
||||||
self.content = content.to_string();
|
strip_last_newline(&mut content);
|
||||||
|
self.content = content;
|
||||||
self.invalidate();
|
self.invalidate();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user