Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

small quick things #50

Merged
merged 5 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn main() {
.description("We'll use this to personalize your experience.")
.placeholder("Enter your name")
.prompt("Name: ")
.suggestions(vec![
.suggestions(&[
"Adam Grant",
"Danielle Steel",
"Eveline Widmer-Schlumpf",
Expand Down
2 changes: 1 addition & 1 deletion examples/spinner-prompts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fn main() {
.unwrap();
Input::new("input ")
.description("go on say something")
.suggestions(vec!["hello there"])
.suggestions(&["hello there"])
.validation(|s| match !s.contains('j') {
true => Ok(()),
false => Err("ew stinky 'j' not welcome here"),
Expand Down
30 changes: 13 additions & 17 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub struct Input<'a> {
/// A placeholder to display in the input
pub placeholder: String,
/// A list of suggestions to autocomplete from
pub suggestions: Vec<&'a str>,
pub suggestions: Option<&'a [&'a str]>,
/// Show the input inline
pub inline: bool,
/// Whether to mask the input
Expand Down Expand Up @@ -62,7 +62,7 @@ impl<'a> Input<'a> {
description: String::new(),
prompt: "> ".to_string(),
placeholder: String::new(),
suggestions: vec![],
suggestions: None,
input: String::new(),
inline: false,
password: false,
Expand Down Expand Up @@ -111,8 +111,8 @@ impl<'a> Input<'a> {
}

/// Sets the suggestions of the input
pub fn suggestions(mut self, suggestions: Vec<&'static str>) -> Self {
self.suggestions = suggestions;
pub fn suggestions(mut self, suggestions: &'a [&'a str]) -> Self {
self.suggestions = Some(suggestions);
self
}

Expand Down Expand Up @@ -394,19 +394,15 @@ impl<'a> Input<'a> {
self.suggestion = None;
return Ok(());
}
self.suggestion = self
.suggestions
.clone()
.into_iter()
.find(|s| s.to_lowercase().starts_with(&self.input.to_lowercase()))
.and_then(|s| {
let suggestion = s[self.input.len()..].to_string();
if !suggestion.is_empty() {
Some(suggestion)
} else {
None
}
});
if let Some(suggestions) = &self.suggestions {
self.suggestion = suggestions
.iter()
.find(|s| s.to_lowercase().starts_with(&self.input.to_lowercase()))
.and_then(|s| {
let suggestion = s[self.input.len()..].to_string();
(!suggestion.is_empty()).then_some(suggestion)
});
}
Ok(())
}

Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ pub use multiselect::MultiSelect;
pub use option::DemandOption;
pub use select::Select;
pub use spinner::Spinner;
pub use spinner::SpinnerAction;
pub use spinner::SpinnerStyle;
pub use theme::Theme;

Expand Down
7 changes: 5 additions & 2 deletions src/spinner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,11 @@ impl<'spinner> SpinnerActionRunner<'spinner> {
}

/// set the spinner title
pub fn title(&self, title: String) -> Result<(), std::sync::mpsc::SendError<SpinnerAction>> {
self.sender.send(SpinnerAction::Title(title))
pub fn title<S: Into<String>>(
&self,
title: S,
) -> Result<(), std::sync::mpsc::SendError<SpinnerAction>> {
self.sender.send(SpinnerAction::Title(title.into()))
}
}

Expand Down