Add abortable flag so that prompts can be skipped

pull/1/head
trivernis 1 year ago
parent 9cf4d225d7
commit e588990df0
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

@ -18,11 +18,24 @@ impl DialogPlugin {
if let Some(val) = default_val {
confirm.default(val);
}
let result = confirm.prompt()?;
Ok(Value::Bool {
val: result,
span: call.head,
})
if call.has_flag("abortable") {
let result = confirm.prompt_opt()?;
if let Some(val) = result {
Ok(Value::Bool {
val,
span: call.head,
})
} else {
Ok(Value::Nothing { span: call.head })
}
} else {
let result = confirm.prompt()?;
Ok(Value::Bool {
val: result,
span: call.head,
})
}
}
}

@ -33,6 +33,7 @@ impl Plugin for DialogPlugin {
SyntaxShape::String,
"The question to ask the user.",
)
.switch("abortable", "If set users can abort the prompt.", None)
.named(
"default",
SyntaxShape::Boolean,
@ -47,6 +48,8 @@ impl Plugin for DialogPlugin {
SyntaxShape::List(Box::new(SyntaxShape::String)),
"The items out of which one can be selected.",
)
.switch("fuzzy", "To add a fuzzy search to the select.", None)
.switch("abortable", "If set users can abort the prompt.", None)
.named(
"prompt",
SyntaxShape::String,

@ -28,12 +28,25 @@ impl DialogPlugin {
select.default(def);
}
let selection = select.prompt()?;
let selected_item = options.remove(selection);
if call.has_flag("abortable") {
if let Some(selection) = select.prompt_opt()? {
let selected_item = options.remove(selection);
Ok(Value::String {
val: selected_item,
span: call.head,
})
Ok(Value::String {
val: selected_item,
span: call.head,
})
} else {
Ok(Value::Nothing { span: call.head })
}
} else {
let selection = select.prompt()?;
let selected_item = options.remove(selection);
Ok(Value::String {
val: selected_item,
span: call.head,
})
}
}
}

Loading…
Cancel
Save