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

Pass selected autocomplete option through the completer #193

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
22 changes: 13 additions & 9 deletions inquire/src/prompts/text/prompt.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::cmp::min;
use std::{cmp::min, borrow::Cow};

use crate::{
autocompletion::{NoAutoCompletion, Replacement},
Expand Down Expand Up @@ -131,26 +131,30 @@ impl<'a> TextPrompt<'a> {
}
}

fn get_current_answer(&self) -> &str {
fn get_current_answer(&mut self) -> Cow<'_, str> {
// If there is a highlighted suggestion, assume user wanted it as
// the answer.
if let Some(suggestion) = self.get_highlighted_suggestion() {
return suggestion;
let opt = self.get_highlighted_suggestion();
if let Some(opt) = opt {
if let Ok(Some(value)) = self.autocompleter.get_completion(self.input.content(), Some(opt.into())) {
return value.into();
}
}

// Empty input with default values override any validators.
if self.input.content().is_empty() {
if let Some(val) = self.default {
return val;
return val.into();
}
}

self.input.content()
self.input.content().into()
}

fn validate_current_answer(&self) -> InquireResult<Validation> {
fn validate_current_answer(&mut self) -> InquireResult<Validation> {
let answer = self.get_current_answer().into_owned();
for validator in &self.validators {
match validator.validate(self.get_current_answer()) {
match validator.validate(&answer) {
Ok(Validation::Valid) => {}
Ok(Validation::Invalid(msg)) => return Ok(Validation::Invalid(msg)),
Err(err) => return Err(InquireError::Custom(err)),
Expand Down Expand Up @@ -194,7 +198,7 @@ where
}
};

Ok(result)
Ok(result.map(|r|r.into()))
}

fn handle(&mut self, action: TextPromptAction) -> InquireResult<ActionResult> {
Expand Down
Loading