Skip to content

Commit

Permalink
Add abortable flag so that prompts can be skipped
Browse files Browse the repository at this point in the history
  • Loading branch information
Trivernis committed Apr 8, 2023
1 parent 9cf4d22 commit e588990
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 11 deletions.
23 changes: 18 additions & 5 deletions src/confirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
}
}
}
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
25 changes: 19 additions & 6 deletions src/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
}
}
}

0 comments on commit e588990

Please sign in to comment.