From e588990df0558ee3d97e1d06b6d293b81d3d7306 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sat, 8 Apr 2023 16:27:52 +0200 Subject: [PATCH] Add abortable flag so that prompts can be skipped --- src/confirm.rs | 23 ++++++++++++++++++----- src/lib.rs | 3 +++ src/select.rs | 25 +++++++++++++++++++------ 3 files changed, 40 insertions(+), 11 deletions(-) diff --git a/src/confirm.rs b/src/confirm.rs index e2060a3..bad33f6 100644 --- a/src/confirm.rs +++ b/src/confirm.rs @@ -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, + }) + } } } diff --git a/src/lib.rs b/src/lib.rs index 5a16e92..72b12ec 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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, diff --git a/src/select.rs b/src/select.rs index f883d54..3b9ff54 100644 --- a/src/select.rs +++ b/src/select.rs @@ -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, + }) + } } }