Add gif, update macro implementation and fix few errors

This commit is contained in:
Lutetium-Vanadium 2022-09-01 17:29:03 +05:30
parent 09c9a0b7fc
commit 2e5ef595b1
68 changed files with 860 additions and 305 deletions

View File

@ -58,7 +58,7 @@ directory.
## In-built prompts
There are 10 in-built prompts:
There are 11 in-built prompts:
- ### Input

BIN
assets/order-select.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 141 KiB

View File

@ -1,23 +1,21 @@
---
source: src/question/order_select/tests.rs
assertion_line: 117
expression: backend
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? message (Press <space> to take and place an│
? order select (Press <space> to take and place an│
 option) │
0. 1
1. 2
2. 3
3. 4
4. 5
5. 6
6. 7
7. 8
8. 9
 9. 0
 
 1. 0
2. 1
3. 2
4. 3
5. 4
6. 5
7. 6
8. 7
9. 8
10. 9
│ │
│ │
│ │
│ │

View File

@ -1,23 +1,21 @@
---
source: src/question/order_select/tests.rs
assertion_line: 117
expression: backend
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? message (Press <space> to take and place an│
? order select (Press <space> to take and place an│
 option) │
 0. 0 │
│ 1. 1 │
│ 2. 2 │
│ 3. 3 │
│ 4. 4 │
│ 5. 5 │
│ 6. 6 │
│ 7. 7 │
│ 8. 8 │
│ 9. 9 │
  │
 2. 0 │
│ 3. 2 │
│ 4. 3 │
│ 5. 4 │
│ 6. 5 │
│ 7. 6 │
│ 8. 7 │
│ 9. 8 │
│ 10. 9 │
│ │
│ │
│ │
│ │

View File

@ -1,23 +1,21 @@
---
source: src/question/order_select/tests.rs
assertion_line: 117
expression: backend
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? message (Press <space> to take and place an│
? order select (Press <space> to take and place an│
 option) │
 0. 0 │
│ 1. 1 │
│ 2. 2 │
│ 3. 3 │
│ 4. 4 │
│ 5. 5 │
│ 6. 6 │
│ 7. 7 │
│ 8. 8 │
│ 9. 9 │
  │
 2. 0 │
│ 3. 2 │
│ 4. 3 │
│ 5. 4 │
│ 6. 5 │
│ 7. 6 │
│ 8. 7 │
│ 9. 8 │
│ 10. 9 │
│ │
│ │
│ │
│ │

View File

@ -0,0 +1,26 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
✔ order select · 0, 2, 3, 4, 5, 6, 7, 8, 9, 1 │
  │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,26 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? order select (Press <space> to take and place an│
 option) │
 1. 0 │
│ 2. 1 │
│ 3. 2 │
│ 4. 3 │
│ 5. 4 │
│ 6. 5 │
│ 7. 6 │
│ 8. 7 │
│ 9. 8 │
│ 10. 9 │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,26 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? message (Press <space> to take and place an opti│
on) │
 1. 0 │
│ 2. 1 │
│ 3. 2 │
│ 4. 3 │
│ 5. 4 │
│ 6. 5 │
│ 7. 6 │
│ 8. 7 │
│ 9. 8 │
│ 10. 9 │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,26 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
✖ message · Skipped │
  │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,26 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? message (Press <space> to take and place an opti│
on) │
 1. 0 │
│ 2. 1 │
│ 3. 2 │
│ 4. 3 │
│ 5. 4 │
│ 6. 5 │
│ 7. 6 │
│ 8. 7 │
│ 9. 8 │
│ 10. 9 │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,26 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? order select (Press <space> to take and place an│
 option) │
 1. 0 │
│ 2. 1 │
│ 3. 2 │
│ 4. 3 │
│ 5. 4 │
│ 6. 5 │
│ 7. 6 │
│ 8. 7 │
│ 9. 8 │
│ 10. 9 │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -1,23 +1,21 @@
---
source: src/question/order_select/tests.rs
assertion_line: 117
expression: backend
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? message (Press <space> to take and place an│
? order select (Press <space> to take and place an│
 option) │
 0. 0 │
│ 1. 1 │
│ 2. 2 │
│ 3. 3 │
│ 4. 4 │
│ 5. 5 │
│ 6. 6 │
│ 7. 7 │
│ 8. 8 │
│ 9. 9 │
  │
 2. 0 │
│ 3. 2 │
│ 4. 3 │
│ 5. 4 │
│ 6. 5 │
│ 7. 6 │
│ 8. 7 │
│ 9. 8 │
│ 10. 9 │
│ │
│ │
│ │
│ │

View File

@ -1,23 +1,21 @@
---
source: src/question/order_select/tests.rs
assertion_line: 117
expression: backend
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? message (Press <space> to take and place an│
? order select (Press <space> to take and place an│
 option) │
 0. 0 │
│ 1. 1 │
│ 2. 2 │
│ 3. 3 │
│ 4. 4 │
│ 5. 5 │
│ 6. 6 │
│ 7. 7 │
│ 8. 8 │
│ 9. 9 │
  │
 2. 0 │
│ 3. 2 │
│ 4. 3 │
│ 5. 4 │
│ 6. 5 │
│ 7. 6 │
│ 8. 7 │
│ 9. 8 │
│ 10. 9 │
│ │
│ │
│ │
│ │

View File

@ -0,0 +1,26 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
✔ order select · 1: 1, 0: 0, 2: 2, 3: 3, 4: 4, 5: │
5, 6: 6, 7: 7, 8: 8, 9: 9 │
  │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,26 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? order select (Press <space> to take and place an│
 option) │
 1. 0 │
│ 2. 1 │
│ 3. 2 │
│ 4. 3 │
│ 5. 4 │
│ 6. 5 │
│ 7. 6 │
│ 8. 7 │
│ 9. 8 │
│ 10. 9 │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,26 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
✔ order select · 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 │
  │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,26 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? order select (Press <space> to take and place an│
 option) │
 1. 0 │
│ 2. 1 │
│ 3. 2 │
│ 4. 3 │
│ 5. 4 │
│ 6. 5 │
│ 7. 6 │
│ 8. 7 │
│ 9. 8 │
│ 10. 9 │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,26 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? order select (Press <space> to take and place an│
 option) │
│ 1. 1 │
 2. 0 │
│ 3. 2 │
│ 4. 3 │
│ 5. 4 │
│ 6. 5 │
│ 7. 6 │
│ 8. 7 │
│ 9. 8 │
│ 10. 9 │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,26 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? order select (Press <space> to take and place an│
 option) │
│ 1. 1 │
 2. 0 │
│ 3. 2 │
│ 4. 3 │
│ 5. 4 │
│ 6. 5 │
│ 7. 6 │
│ 8. 7 │
│ 9. 8 │
│ 10. 9 │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,26 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? order select (Press <space> to take and place an│
 option) │
│ 1. 1 │
 2. 0 │
│ 3. 2 │
│ 4. 3 │
│ 5. 4 │
│ 6. 5 │
│ 7. 6 │
│ 8. 7 │
│ 9. 8 │
│ 10. 9 │
✖ Error │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,26 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? order select (Press <space> to take and place an│
 option) │
 1. 1 │
│ 2. 0 │
│ 3. 2 │
│ 4. 3 │
│ 5. 4 │
│ 6. 5 │
│ 7. 6 │
│ 8. 7 │
│ 9. 8 │
│ 10. 9 │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,26 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? order select (Press <space> to take and place an│
 option) │
 1. 1 │
│ 2. 0 │
│ 3. 2 │
│ 4. 3 │
│ 5. 4 │
│ 6. 5 │
│ 7. 6 │
│ 8. 7 │
│ 9. 8 │
│ 10. 9 │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,26 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? order select (Press <space> to take and place an│
 option) │
│ 1. 0 │
 2. 1 │
│ 3. 2 │
│ 4. 3 │
│ 5. 4 │
│ 6. 5 │
│ 7. 6 │
│ 8. 7 │
│ 9. 8 │
│ 10. 9 │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,26 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? order select (Press <space> to take and place an│
 option) │
│ 1. 0 │
 2. 1 │
│ 3. 2 │
│ 4. 3 │
│ 5. 4 │
│ 6. 5 │
│ 7. 6 │
│ 8. 7 │
│ 9. 8 │
│ 10. 9 │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,26 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? order select (Press <space> to take and place an│
 option) │
 1. 0 │
│ 2. 1 │
│ 3. 2 │
│ 4. 3 │
│ 5. 4 │
│ 6. 5 │
│ 7. 6 │
│ 8. 7 │
│ 9. 8 │
│ 10. 9 │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -1,28 +0,0 @@
---
source: src/question/order_select/tests.rs
assertion_line: 124
expression: backend
---
┌──────────────────────────────────────────────────┐
? message (Press <space> to take and place an│
 option) │
 0. 0 │
│ 1. 1 │
│ 2. 2 │
│ 3. 3 │
│ 4. 4 │
│ 5. 5 │
│ 6. 6 │
│ 7. 7 │
│ 8. 8 │
│ 9. 9 │
│ 10. 10 │
│ 11. 11 │
│ 12. 12 │
│ 13. 13 │
(Move up and down to reveal more choices) │
  │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -1,28 +0,0 @@
---
source: src/question/order_select/tests.rs
assertion_line: 124
expression: backend
---
┌──────────────────────────────────────────────────┐
? message (Press <space> to take and place an│
 option) │
│ 18. 19 │
 19. 0 │
│ 0. 1 │
│ 1. 2 │
│ 2. 3 │
│ 3. 4 │
│ 4. 5 │
│ 5. 6 │
│ 6. 7 │
│ 7. 8 │
│ 8. 9 │
│ 9. 10 │
│ 10. 11 │
│ 11. 12 │
(Move up and down to reveal more choices) │
  │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -1,28 +0,0 @@
---
source: src/question/order_select/tests.rs
assertion_line: 124
expression: backend
---
┌──────────────────────────────────────────────────┐
? message (Press <space> to take and place an│
 option) │
│ 18. 18 │
│ 19. 19 │
 0. 0 │
│ 1. 1 │
│ 2. 2 │
│ 3. 3 │
│ 4. 4 │
│ 5. 5 │
│ 6. 6 │
│ 7. 7 │
│ 8. 8 │
│ 9. 9 │
│ 10. 10 │
│ 11. 11 │
(Move up and down to reveal more choices) │
  │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -1,28 +0,0 @@
---
source: src/question/order_select/tests.rs
assertion_line: 124
expression: backend
---
┌──────────────────────────────────────────────────┐
? message (Press <space> to take and place an│
 option) │
│ 18. 18 │
│ 19. 19 │
 0. 0 │
│ 1. 1 │
│ 2. 2 │
│ 3. 3 │
│ 4. 4 │
│ 5. 5 │
│ 6. 6 │
│ 7. 7 │
│ 8. 8 │
│ 9. 9 │
│ 10. 10 │
│ 11. 11 │
(Move up and down to reveal more choices) │
  │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -1,28 +0,0 @@
---
source: src/question/order_select/tests.rs
assertion_line: 124
expression: backend
---
┌──────────────────────────────────────────────────┐
? message (Press <space> to take and place an│
 option) │
 0. 0 │
│ 1. 1 │
│ 2. 2 │
│ 3. 3 │
│ 4. 4 │
│ 5. 5 │
│ 6. 6 │
│ 7. 7 │
│ 8. 8 │
│ 9. 9 │
│ 10. 10 │
│ 11. 11 │
│ 12. 12 │
│ 13. 13 │
(Move up and down to reveal more choices) │
  │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -3,11 +3,7 @@ use requestty::Question;
fn main() {
let order_select = Question::order_select("home_tasks")
.message("Please organize the tasks to be done at home")
.choices(vec![
"Make the bed",
"Clean the dishes",
"Mow the lawn",
])
.choices(vec!["Make the bed", "Clean the dishes", "Mow the lawn"])
.validate(|c, _| {
if c[0].text() == "Make the bed" {
Ok(())
@ -18,4 +14,4 @@ fn main() {
.build();
println!("{:#?}", requestty::prompt_one(order_select));
}
}

View File

@ -87,9 +87,9 @@ impl Choices {
let content;
syn::bracketed!(content in input);
Ok(Choices::Array(content.parse_terminated(parser)?))
content.parse_terminated(parser).map(Choices::Array)
} else {
Ok(Choices::Expr(input.parse()?))
input.parse().map(Choices::Expr)
}
}
@ -100,6 +100,10 @@ impl Choices {
pub(crate) fn parse_multi_select_choice(input: syn::parse::ParseStream) -> syn::Result<Self> {
Choices::parse_impl(input, parse_multi_select_choice)
}
pub(crate) fn parse_order_select_choice(input: syn::parse::ParseStream) -> syn::Result<Self> {
input.parse().map(Choices::Expr)
}
}
impl From<syn::ExprPath> for Choices {

View File

@ -32,6 +32,7 @@ pub(crate) enum QuestionKind {
RawSelect,
Expand,
MultiSelect,
OrderSelect,
Password,
Editor,
Custom,
@ -48,6 +49,7 @@ impl QuestionKind {
QuestionKind::RawSelect => "raw_select",
QuestionKind::Expand => "expand",
QuestionKind::MultiSelect => "multi_select",
QuestionKind::OrderSelect => "order_select",
QuestionKind::Password => "password",
QuestionKind::Editor => "editor",
QuestionKind::Custom => "custom",
@ -89,6 +91,13 @@ impl QuestionKind {
| BuilderMethods::CHOICES
| BuilderMethods::ON_ESC
}
QuestionKind::OrderSelect => {
BuilderMethods::TRANSFORM
| BuilderMethods::VAL_FIL
| BuilderMethods::LOOP_PAGE_SIZE
| BuilderMethods::CHOICES
| BuilderMethods::ON_ESC
}
QuestionKind::Password => {
BuilderMethods::TRANSFORM
| BuilderMethods::VAL_FIL
@ -128,6 +137,8 @@ impl Parse for QuestionKind {
QuestionKind::Expand
} else if ident == "MultiSelect" {
QuestionKind::MultiSelect
} else if ident == "OrderSelect" {
QuestionKind::OrderSelect
} else if ident == "Password" {
QuestionKind::Password
} else if ident == "Editor" {
@ -265,6 +276,7 @@ impl Parse for Question {
} else if ident == "choices" {
let parser = match kind {
QuestionKind::MultiSelect => Choices::parse_multi_select_choice,
QuestionKind::OrderSelect => Choices::parse_order_select_choice,
_ => Choices::parse_choice,
};

View File

@ -215,11 +215,7 @@ impl_from!(Vec<ListItem> => ListItems);
impl From<Vec<OrderSelectItem>> for Answer {
fn from(v: Vec<OrderSelectItem>) -> Self {
Answer::ListItems(
v.into_iter()
.map(|o| o.into())
.collect()
)
Answer::ListItems(v.into_iter().map(|o| o.into()).collect())
}
}
@ -249,7 +245,10 @@ impl<I: Into<String>> From<(usize, I)> for ListItem {
impl From<OrderSelectItem> for ListItem {
fn from(o: OrderSelectItem) -> Self {
Self { index: o.initial_index(), text: o.text().to_string() }
ListItem {
index: o.initial_index,
text: o.text.text,
}
}
}

View File

@ -44,7 +44,7 @@ use options::Options;
/// A `Question` that can be asked.
///
/// There are 11 variants.
/// There are 12 variants.
///
/// - [`input`](Question::input)
/// - [`password`](Question::password)
@ -438,9 +438,8 @@ impl Question<'static> {
///
/// The choices are [`String`]s and can be multiline.
///
/// // TODO : add a gif for OrderSelect
/// <img
/// src="https://raw.githubusercontent.com/lutetium-vanadium/requestty/master/assets/multi-select.gif"
/// src="https://raw.githubusercontent.com/lutetium-vanadium/requestty/master/assets/order-select.gif"
/// style="max-height: 20rem"
/// />
///

View File

@ -1,9 +1,7 @@
use ui::backend::Backend;
use ui::widgets::Text;
use crate::{
question::{Options},
};
use crate::question::Options;
use super::{OrderSelect, OrderSelectItem};
@ -11,9 +9,8 @@ use super::{OrderSelect, OrderSelectItem};
///
/// The choices are [`String`]s and can be multiline.
///
/// // TODO : add gif
/// <img
/// src="https://raw.githubusercontent.com/lutetium-vanadium/requestty/master/assets/multi-select.gif"
/// src="https://raw.githubusercontent.com/lutetium-vanadium/requestty/master/assets/order-select.gif"
/// style="max-height: 20rem"
/// />
///
@ -23,7 +20,7 @@ use super::{OrderSelect, OrderSelectItem};
///
/// ```
/// use requestty::Question;
///
///
/// let order_select = Question::order_select("home_tasks")
/// .message("Please organize the tasks to be done at home")
/// .choices(vec![
@ -178,17 +175,18 @@ impl<'a> OrderSelectBuilder<'a> {
{
let len = self.order_select.choices.choices.len();
self.order_select.choices.choices.extend(
choices
.into_iter()
.enumerate()
.map(|(i, c)|
OrderSelectItem {
initial_index:len + i,
text: Text::new(c.into())
}
),
);
self.order_select
.choices
.choices
.extend(
choices
.into_iter()
.enumerate()
.map(|(i, c)| OrderSelectItem {
initial_index: len + i,
text: Text::new(c.into()),
}),
);
self
}
@ -255,7 +253,8 @@ impl<'a> OrderSelectBuilder<'a> {
///
/// [`Question`]: crate::question::Question
pub fn build(mut self) -> crate::question::Question<'a> {
self.order_select.max_index_width = (self.order_select.choices.len() as f64 + 1.0).log10() as usize + 1;
self.order_select.max_index_width =
(self.order_select.choices.len() as f64 + 1.0).log10() as usize + 1;
crate::question::Question::new(
self.opts,

View File

@ -10,10 +10,11 @@ use ui::{
Prompt, Widget,
};
use crate::{Answer, Answers, ListItem};
use crate::{Answer, Answers};
use super::{
handler::{Filter, Transform, Validate}, choice::SelectList,
choice::SelectList,
handler::{Filter, Transform, Validate},
};
pub use builder::OrderSelectBuilder;
@ -38,7 +39,7 @@ pub(super) struct OrderSelect<'a> {
impl<'a> Default for OrderSelect<'a> {
fn default() -> Self {
Self {
Self {
choices: SelectList::new(|_| true),
// can't put
@ -51,7 +52,7 @@ impl<'a> Default for OrderSelect<'a> {
filter: Default::default(),
}
}
}
}
impl widgets::List for OrderSelect<'_> {
fn render_item<B: ui::backend::Backend>(
@ -120,9 +121,8 @@ impl<'c> OrderSelect<'c> {
answers: &'a Answers,
) -> OrderSelectPrompt<'a, 'c> {
OrderSelectPrompt {
prompt: widgets::Prompt::new(message).with_hint(
"Press <space> to take and place an option",
),
prompt: widgets::Prompt::new(message)
.with_hint("Press <space> to take and place an option"),
select: widgets::Select::new(self),
answers,
}
@ -188,9 +188,7 @@ impl Prompt for OrderSelectPrompt<'_, '_> {
fn finish(self) -> Self::Output {
let OrderSelect {
choices,
filter,
..
choices, filter, ..
} = self.select.into_inner();
let mut c = choices.choices;
@ -230,13 +228,13 @@ impl Widget for OrderSelectPrompt<'_, '_> {
fn handle_key(&mut self, key: ui::events::KeyEvent) -> bool {
let prev_at = self.select.get_at();
if let ui::events::KeyCode::Char(' ') = key.code {
self.select.list.moving = !self.select.list.moving;
} else if self.select.handle_key(key) {
if self.select.list.moving {
let new_at = self.select.get_at();
if prev_at < new_at {
self.select.list.choices.choices[prev_at..=new_at].rotate_left(1);
} else {
@ -246,13 +244,13 @@ impl Widget for OrderSelectPrompt<'_, '_> {
} else {
return false;
}
true
}
}
// =============================================================================
//
//
// =============================================================================
/// The representation of each choice in an [`OrderSelect`].
@ -260,8 +258,8 @@ impl Widget for OrderSelectPrompt<'_, '_> {
/// It is different from [`ListItem`](crate::answer::ListItem) due to an implementation detail.
#[derive(Debug, Clone, PartialEq)]
pub struct OrderSelectItem {
initial_index: usize,
text: Text<String>,
pub(crate) initial_index: usize,
pub(crate) text: Text<String>,
}
impl OrderSelectItem {

View File

@ -1,4 +1,8 @@
use ui::{backend::TestBackend, layout::Layout, events::{KeyCode, KeyEvent}};
use ui::{
backend::TestBackend,
events::{KeyCode, KeyEvent},
layout::Layout,
};
use crate::question::{Question, QuestionKind};
@ -93,7 +97,7 @@ macro_rules! test_order_select {
layout,
base_layout.with_offset(0, $height).with_line_offset(0)
);
ui::assert_backend_snapshot!(backend);
ui::assert_backend_snapshot!(backend);
assert!(order_select.handle_key(key))
}

View File

@ -3,7 +3,7 @@ source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? multi select (Press <space> to take and place an│
? order select (Press <space> to take and place an│
 option) │
 1. 0 │
│ 2. 1 │

View File

@ -3,7 +3,7 @@ source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? multi select (Press <space> to take and place an│
? order select (Press <space> to take and place an│
 option) │
│ 1. 1 │
 2. 0 │

View File

@ -3,7 +3,7 @@ source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? multi select (Press <space> to take and place an│
? order select (Press <space> to take and place an│
 option) │
│ 1. 1 │
 2. 0 │

View File

@ -3,7 +3,7 @@ source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
✔ multi select · 1: 1, 0: 0, 2: 2, 3: 3, 4: 4, 5: │
✔ order select · 1: 1, 0: 0, 2: 2, 3: 3, 4: 4, 5: │
5, 6: 6, 7: 7, 8: 8, 9: 9 │
  │
│ │

View File

@ -3,7 +3,7 @@ source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? multi select (Press <space> to take and place an│
? order select (Press <space> to take and place an│
 option) │
 1. 0 │
│ 2. 1 │

View File

@ -0,0 +1,26 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
✔ order select · 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 │
  │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -3,19 +3,19 @@ source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
 order select · 1, 0, 2, 3, 4, 5, 6, 7, 8, 9
 
? order select (Press <space> to take and place an
 option) │
1. 1
 2. 0
3. 2
4. 3
5. 4
6. 5
7. 6
8. 7
9. 8
10. 9
✖ Error
│ │
│ │
│ │

View File

@ -0,0 +1,26 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? order select (Press <space> to take and place an│
 option) │
 1. 1 │
│ 2. 0 │
│ 3. 2 │
│ 4. 3 │
│ 5. 4 │
│ 6. 5 │
│ 7. 6 │
│ 8. 7 │
│ 9. 8 │
│ 10. 9 │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,26 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? order select (Press <space> to take and place an│
 option) │
 1. 1 │
│ 2. 0 │
│ 3. 2 │
│ 4. 3 │
│ 5. 4 │
│ 6. 5 │
│ 7. 6 │
│ 8. 7 │
│ 9. 8 │
│ 10. 9 │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,26 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? order select (Press <space> to take and place an│
 option) │
│ 1. 0 │
 2. 1 │
│ 3. 2 │
│ 4. 3 │
│ 5. 4 │
│ 6. 5 │
│ 7. 6 │
│ 8. 7 │
│ 9. 8 │
│ 10. 9 │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -0,0 +1,26 @@
---
source: tests/helpers/mod.rs
expression: self.backend
---
┌──────────────────────────────────────────────────┐
? order select (Press <space> to take and place an│
 option) │
│ 1. 0 │
 2. 1 │
│ 3. 2 │
│ 4. 3 │
│ 5. 4 │
│ 6. 5 │
│ 7. 6 │
│ 8. 7 │
│ 9. 8 │
│ 10. 9 │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────┘

View File

@ -86,6 +86,22 @@ fn test_multi_select() {
t.compile_fail("prompt");
}
#[test]
#[ignore = "proc-macro test"]
fn test_order_select() {
let t = Runner::new("order_select");
t.pass("valid");
t.compile_fail("default");
t.compile_fail("separator");
t.compile_fail("validate_on_key");
t.compile_fail("auto_complete");
t.compile_fail("mask");
t.compile_fail("extension");
t.compile_fail("editor");
t.compile_fail("prompt");
}
#[test]
#[ignore = "proc-macro test"]
fn test_confirm() {

View File

@ -0,0 +1,5 @@
fn main() {
requestty::questions![OrderSelect {
auto_complete: todo!()
}];
}

View File

@ -0,0 +1,5 @@
error: option `auto_complete` does not exist for kind `order_select`
--> tests/macros/order_select/auto_complete.rs:3:9
|
3 | auto_complete: todo!()
| ^^^^^^^^^^^^^

View File

@ -0,0 +1,3 @@
fn main() {
requestty::questions![OrderSelect { default: todo!() }];
}

View File

@ -0,0 +1,5 @@
error: option `default` does not exist for kind `order_select`
--> tests/macros/order_select/default.rs:2:41
|
2 | requestty::questions![OrderSelect { default: todo!() }];
| ^^^^^^^

View File

@ -0,0 +1,3 @@
fn main() {
let q = requestty::questions![OrderSelect { editor: todo!() }];
}

View File

@ -0,0 +1,5 @@
error: option `editor` does not exist for kind `order_select`
--> tests/macros/order_select/editor.rs:2:49
|
2 | let q = requestty::questions![OrderSelect { editor: todo!() }];
| ^^^^^^

View File

@ -0,0 +1,3 @@
fn main() {
let q = requestty::questions![OrderSelect { extension: todo!() }];
}

View File

@ -0,0 +1,5 @@
error: option `extension` does not exist for kind `order_select`
--> tests/macros/order_select/extension.rs:2:49
|
2 | let q = requestty::questions![OrderSelect { extension: todo!() }];
| ^^^^^^^^^

View File

@ -0,0 +1,3 @@
fn main() {
let q = requestty::questions![OrderSelect { mask: todo!() }];
}

View File

@ -0,0 +1,5 @@
error: option `mask` does not exist for kind `order_select`
--> tests/macros/order_select/mask.rs:2:49
|
2 | let q = requestty::questions![OrderSelect { mask: todo!() }];
| ^^^^

View File

@ -0,0 +1,3 @@
fn main() {
let q = requestty::questions![OrderSelect { prompt: todo!() }];
}

View File

@ -0,0 +1,5 @@
error: option `prompt` does not exist for kind `order_select`
--> tests/macros/order_select/prompt.rs:2:49
|
2 | let q = requestty::questions![OrderSelect { prompt: todo!() }];
| ^^^^^^

View File

@ -0,0 +1,10 @@
fn main() {
requestty::questions! [
OrderSelect {
name: "name",
choices: [
sep "separator",
],
}
];
}

View File

@ -0,0 +1,5 @@
error: expected `,` or `;`
--> tests/macros/order_select/separator.rs:6:21
|
6 | sep "separator",
| ^^^^^^^^^^^

View File

@ -0,0 +1,14 @@
fn main() {
let choice = "choice";
requestty::questions![OrderSelect {
name: "name",
on_esc: requestty::OnEsc::Terminate,
transform: |_, _, _| Ok(()),
validate: |_, _| Ok(()),
filter: |t, _| t,
choices: ["choice", choice],
page_size: 10,
should_loop: true,
}];
}

View File

@ -0,0 +1,5 @@
fn main() {
requestty::questions![OrderSelect {
validate_on_key: todo!()
}];
}

View File

@ -0,0 +1,5 @@
error: option `validate_on_key` does not exist for kind `order_select`
--> tests/macros/order_select/validate_on_key.rs:3:9
|
3 | validate_on_key: todo!()
| ^^^^^^^^^^^^^^^

View File

@ -9,14 +9,14 @@ fn choices(len: usize) -> impl Iterator<Item = String> {
#[test]
fn test_validate() {
let order_select = requestty::Question::order_select("name")
.message("order select")
.validate(|c, _| {
if c[0].text() != "1" {
Err("Error".to_string())
} else {
if c[0].text() == "0" {
Ok(())
} else {
Err("Error".to_string())
}
})
.message("order select")
.choices(choices(10));
let size = (50, 20).into();
@ -33,10 +33,7 @@ fn test_validate() {
KeyCode::Enter.into(),
]);
let indexes_a = choices(10)
.enumerate()
.map(|(i, _)| i)
.collect::<Vec<_>>();
let indexes_a = choices(10).enumerate().map(|(i, _)| i).collect::<Vec<_>>();
let indexes_b = requestty::prompt_one_with(order_select, &mut backend, &mut events)
.unwrap()
@ -68,10 +65,7 @@ fn test_filter() {
KeyCode::Enter.into(),
]);
let mut indexes_a = choices(10)
.enumerate()
.map(|(i, _)| i)
.collect::<Vec<_>>();
let mut indexes_a = choices(10).enumerate().map(|(i, _)| i).collect::<Vec<_>>();
indexes_a.swap(0, 1);
indexes_a.rotate_left(1);
@ -108,13 +102,10 @@ fn test_transform() {
KeyCode::Char(' ').into(),
KeyCode::Down.into(),
KeyCode::Char(' ').into(),
KeyCode::Enter.into()
KeyCode::Enter.into(),
]);
let mut indexes_a = choices(10)
.enumerate()
.map(|(i, _)| i)
.collect::<Vec<_>>();
let mut indexes_a = choices(10).enumerate().map(|(i, _)| i).collect::<Vec<_>>();
indexes_a.swap(0, 1);
let indexes_b = requestty::prompt_one_with(order_select, &mut backend, &mut events)