Skip to content

Commit

Permalink
small quick things (#50)
Browse files Browse the repository at this point in the history
* feat: input suggestions are now a slice

chore: update examples

* refactor: SpinnerActions doesnt need to be exported, that was from an earlier version of run actions

* fix: typo

* feat: allow non static str

* feat: SpinnerActionRunner.title now accepts into<string>
  • Loading branch information
Vulpesx authored Apr 30, 2024
1 parent ed9fcf9 commit 37a49e4
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 22 deletions.
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

0 comments on commit 37a49e4

Please sign in to comment.