Add confirm command

pull/1/head
trivernis 1 year ago
commit 215c235f4b
WARNING! Although there is a key with this ID in the database it does not verify this commit! This commit is SUSPICIOUS.
GPG Key ID: DFFFCC2C7A02DB45

1
.gitignore vendored

@ -0,0 +1 @@
/target

1632
Cargo.lock generated

File diff suppressed because it is too large Load Diff

@ -0,0 +1,16 @@
[package]
name = "nu_plugin_dialog"
version = "0.1.0"
edition = "2021"
[[bin]]
name = "nu_plugin_dialog"
[lib]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
dialoguer = "0.10.4"
nu-plugin = "0.78.0"
nu-protocol = "0.78.0"

@ -0,0 +1,32 @@
use nu_plugin::{EvaluatedCall, LabeledError};
use nu_protocol::Value;
use crate::DialogPlugin;
impl DialogPlugin {
pub(crate) fn confirm(
&self,
call: &EvaluatedCall,
_input: &Value,
) -> Result<Value, LabeledError> {
let prompt: String = call.req(0)?;
let default_val: Option<bool> = call.get_flag("default")?;
let mut confirm = dialoguer::Confirm::with_theme(&*self.theme);
confirm.with_prompt(prompt);
if let Some(val) = default_val {
confirm.default(val);
}
let result = confirm.interact().map_err(|e| LabeledError {
label: "Failed to prompt user".into(),
msg: e.to_string(),
span: Some(call.head),
})?;
Ok(Value::Bool {
val: result,
span: call.head,
})
}
}

@ -0,0 +1,65 @@
use dialoguer::theme::{ColorfulTheme, Theme};
use nu_plugin::{LabeledError, Plugin};
use nu_protocol::{PluginSignature, SyntaxShape};
mod confirm;
pub struct DialogPlugin {
pub(crate) theme: Box<dyn Theme>,
}
impl DialogPlugin {
pub fn new() -> Self {
Self {
theme: Box::new(ColorfulTheme::default()),
}
}
}
impl Plugin for DialogPlugin {
fn signature(&self) -> Vec<nu_protocol::PluginSignature> {
vec![
PluginSignature::build("ask").required(
"subcommand",
SyntaxShape::String,
"The ask subcommand to run",
),
PluginSignature::build("ask confirm")
.usage("Prompt the user with a confirmation prompt.")
.required(
"prompt",
SyntaxShape::String,
"The question to ask the user.",
)
.named(
"default",
SyntaxShape::Boolean,
"The default selection.",
None,
)
.category(nu_protocol::Category::Misc),
]
}
fn run(
&mut self,
name: &str,
call: &nu_plugin::EvaluatedCall,
input: &nu_protocol::Value,
) -> Result<nu_protocol::Value, nu_plugin::LabeledError> {
match name {
"ask confirm" => self.confirm(call, input),
"ask" =>
Err(LabeledError {
label: "Missing subcommand".into(),
msg: "the subcommand to the ask command is missing".into(),
span: Some(call.head),
}),
_ => Err(LabeledError {
label: "Plugin call with wrong name signature".into(),
msg: "the signature used to call the plugin does not match any name in the plugin signature vector".into(),
span: Some(call.head),
})
}
}
}

@ -0,0 +1,6 @@
use nu_plugin::{serve_plugin, MsgPackSerializer};
use nu_plugin_dialog::DialogPlugin;
fn main() {
serve_plugin(&mut DialogPlugin::new(), MsgPackSerializer)
}
Loading…
Cancel
Save