From 37a49e40099013ec7d99c95d1c85ceab8c98d7c9 Mon Sep 17 00:00:00 2001 From: Vulpesx <64671209+Vulpesx@users.noreply.github.com> Date: Tue, 30 Apr 2024 21:56:14 +1000 Subject: [PATCH 1/3] small quick things (#50) * 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 --- examples/input.rs | 2 +- examples/spinner-prompts.rs | 2 +- src/input.rs | 30 +++++++++++++----------------- src/lib.rs | 1 - src/spinner.rs | 7 +++++-- 5 files changed, 20 insertions(+), 22 deletions(-) diff --git a/examples/input.rs b/examples/input.rs index fc2ffc4..f91b554 100644 --- a/examples/input.rs +++ b/examples/input.rs @@ -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", diff --git a/examples/spinner-prompts.rs b/examples/spinner-prompts.rs index ae02a9a..044c202 100644 --- a/examples/spinner-prompts.rs +++ b/examples/spinner-prompts.rs @@ -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"), diff --git a/src/input.rs b/src/input.rs index f5bf96e..5d53f07 100644 --- a/src/input.rs +++ b/src/input.rs @@ -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 @@ -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, @@ -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 } @@ -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(()) } diff --git a/src/lib.rs b/src/lib.rs index 7e635e0..0215cbc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; diff --git a/src/spinner.rs b/src/spinner.rs index 65224e6..ee309a2 100644 --- a/src/spinner.rs +++ b/src/spinner.rs @@ -59,8 +59,11 @@ impl<'spinner> SpinnerActionRunner<'spinner> { } /// set the spinner title - pub fn title(&self, title: String) -> Result<(), std::sync::mpsc::SendError> { - self.sender.send(SpinnerAction::Title(title)) + pub fn title>( + &self, + title: S, + ) -> Result<(), std::sync::mpsc::SendError> { + self.sender.send(SpinnerAction::Title(title.into())) } } From e976e2f30be3474402005c62f8d38b60a0273eaa Mon Sep 17 00:00:00 2001 From: Vulpesx Date: Sun, 5 May 2024 17:15:25 +1000 Subject: [PATCH 2/3] fix: input not showing cursor when done --- src/input.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/input.rs b/src/input.rs index 5d53f07..59c1182 100644 --- a/src/input.rs +++ b/src/input.rs @@ -164,6 +164,7 @@ impl<'a> Input<'a> { self.clear_err()?; self.validate()?; if self.err.is_none() { + self.term.show_cursor()?; return self.handle_submit(); } } From 27b67a806c0d6586c38094ae7ed1fe01c8c83392 Mon Sep 17 00:00:00 2001 From: Vulpesx Date: Fri, 10 May 2024 18:13:38 +1000 Subject: [PATCH 3/3] fix: clippy warnings --- src/spinner.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/spinner.rs b/src/spinner.rs index ee309a2..6a51469 100644 --- a/src/spinner.rs +++ b/src/spinner.rs @@ -110,13 +110,13 @@ impl<'a> Spinner<'a> { /// Set the style of the spinner pub fn style(mut self, style: &'a SpinnerStyle) -> Self { - self.style = &style; + self.style = style; self } /// Set the theme of the dialog pub fn theme(mut self, theme: &'a Theme) -> Self { - self.theme = &theme; + self.theme = theme; self }