-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
80 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
use nu_plugin::{EvaluatedCall, LabeledError}; | ||
use nu_protocol::Value; | ||
|
||
use crate::{prompt::UserPrompt, DialogPlugin}; | ||
|
||
impl DialogPlugin { | ||
pub(crate) fn password( | ||
&self, | ||
call: &EvaluatedCall, | ||
_input: &Value, | ||
) -> Result<Value, LabeledError> { | ||
let mut pw_input = dialoguer::Password::with_theme(&*self.theme); | ||
|
||
pw_input.allow_empty_password(call.has_flag("allow-empty")); | ||
|
||
if call.has_flag("confirm") { | ||
pw_input.with_confirmation("Repeat your input", "Error: The inputs don't match"); | ||
} | ||
|
||
let password = pw_input.ask(call.head)?; | ||
|
||
Ok(Value::String { | ||
val: password, | ||
span: call.head, | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,48 @@ | ||
use std::io; | ||
|
||
use dialoguer::Password; | ||
use nu_plugin::LabeledError; | ||
mod generic_select; | ||
pub use generic_select::GenericSelect; | ||
use nu_protocol::Span; | ||
|
||
pub trait UserPrompt { | ||
type Output; | ||
|
||
fn prompt(&self) -> Result<Self::Output, LabeledError>; | ||
fn ask(&self, span: Span) -> Result<Self::Output, LabeledError>; | ||
|
||
fn prompt_opt(&self) -> Result<Option<Self::Output>, LabeledError>; | ||
fn ask_opt(&self, span: Span) -> Result<Option<Self::Output>, LabeledError>; | ||
} | ||
|
||
impl<'a> UserPrompt for dialoguer::Confirm<'a> { | ||
type Output = bool; | ||
|
||
fn prompt(&self) -> Result<Self::Output, LabeledError> { | ||
self.interact().map_err(create_labeled_error) | ||
fn ask(&self, span: Span) -> Result<Self::Output, LabeledError> { | ||
self.interact().map_err(|e| create_labeled_error(e, span)) | ||
} | ||
|
||
fn prompt_opt(&self) -> Result<Option<Self::Output>, LabeledError> { | ||
self.interact_opt().map_err(create_labeled_error) | ||
fn ask_opt(&self, span: Span) -> Result<Option<Self::Output>, LabeledError> { | ||
self.interact_opt() | ||
.map_err(|e| create_labeled_error(e, span)) | ||
} | ||
} | ||
|
||
fn create_labeled_error(e: io::Error) -> LabeledError { | ||
impl<'a> UserPrompt for Password<'a> { | ||
type Output = String; | ||
|
||
fn ask(&self, span: Span) -> Result<Self::Output, LabeledError> { | ||
self.interact().map_err(|e| create_labeled_error(e, span)) | ||
} | ||
|
||
fn ask_opt(&self, span: Span) -> Result<Option<Self::Output>, LabeledError> { | ||
self.ask(span).map(Option::Some) | ||
} | ||
} | ||
|
||
fn create_labeled_error(e: io::Error, span: Span) -> LabeledError { | ||
LabeledError { | ||
label: "Failed to prompt user".into(), | ||
msg: e.to_string(), | ||
span: None, | ||
span: Some(span), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters