Add ask password prompt
parent
e588990df0
commit
0b057140ef
@ -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,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
@ -1,33 +1,48 @@
|
|||||||
use std::io;
|
use std::io;
|
||||||
|
|
||||||
|
use dialoguer::Password;
|
||||||
use nu_plugin::LabeledError;
|
use nu_plugin::LabeledError;
|
||||||
mod generic_select;
|
mod generic_select;
|
||||||
pub use generic_select::GenericSelect;
|
pub use generic_select::GenericSelect;
|
||||||
|
use nu_protocol::Span;
|
||||||
|
|
||||||
pub trait UserPrompt {
|
pub trait UserPrompt {
|
||||||
type Output;
|
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> {
|
impl<'a> UserPrompt for dialoguer::Confirm<'a> {
|
||||||
type Output = bool;
|
type Output = bool;
|
||||||
|
|
||||||
fn prompt(&self) -> Result<Self::Output, LabeledError> {
|
fn ask(&self, span: Span) -> Result<Self::Output, LabeledError> {
|
||||||
self.interact().map_err(create_labeled_error)
|
self.interact().map_err(|e| create_labeled_error(e, span))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn prompt_opt(&self) -> Result<Option<Self::Output>, LabeledError> {
|
fn ask_opt(&self, span: Span) -> Result<Option<Self::Output>, LabeledError> {
|
||||||
self.interact_opt().map_err(create_labeled_error)
|
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 {
|
LabeledError {
|
||||||
label: "Failed to prompt user".into(),
|
label: "Failed to prompt user".into(),
|
||||||
msg: e.to_string(),
|
msg: e.to_string(),
|
||||||
span: None,
|
span: Some(span),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue