REFACTOR: make input list a tiny bit tighter (#9115)

related to #8963
cc/ @melMass 

# Description
just a little refactoring attempt for `input list` 😌 

i wanted to refactor even more, but `Select`, `MultiSelect` and
`FuzzySelect` do not share a common trait, i could not find a nice way
to reduce the big `if` block...

# User-Facing Changes
```
$nothing
```

# Tests + Formatting
- 🟢 `toolkit fmt`
- 🟢 `toolkit clippy`
-  `toolkit test`
-  `toolkit test stdlib`

# After Submitting
```
$nothing
```
This commit is contained in:
Antoine Stevan 2023-05-08 19:45:55 +02:00 committed by GitHub
parent a5d02a0737
commit fe9f732c5f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -127,6 +127,13 @@ impl Command for InputList {
}); });
} }
if call.has_flag("multi") && call.has_flag("fuzzy") {
return Err(ShellError::TypeMismatch {
err_message: "Fuzzy search is not supported for multi select".to_string(),
span: head,
});
}
// could potentially be used to map the use theme colors at some point // could potentially be used to map the use theme colors at some point
// let theme = dialoguer::theme::ColorfulTheme { // let theme = dialoguer::theme::ColorfulTheme {
// active_item_style: Style::new().fg(Color::Cyan).bold(), // active_item_style: Style::new().fg(Color::Cyan).bold(),
@ -134,26 +141,19 @@ impl Command for InputList {
// }; // };
let ans: InteractMode = if call.has_flag("multi") { let ans: InteractMode = if call.has_flag("multi") {
if call.has_flag("fuzzy") { let mut multi_select = MultiSelect::new(); //::with_theme(&theme);
return Err(ShellError::TypeMismatch {
err_message: "Fuzzy search is not supported for multi select".to_string(),
span: head,
});
} else {
let mut multi_select = MultiSelect::new(); //::with_theme(&theme);
InteractMode::Multi( InteractMode::Multi(
if !prompt.is_empty() { if !prompt.is_empty() {
multi_select.with_prompt(&prompt) multi_select.with_prompt(&prompt)
} else { } else {
&mut multi_select &mut multi_select
} }
.items(&options) .items(&options)
.report(false) .report(false)
.interact_on_opt(&Term::stderr()) .interact_on_opt(&Term::stderr())
.map_err(|err| ShellError::IOError(format!("{}: {}", INTERACT_ERROR, err)))?, .map_err(|err| ShellError::IOError(format!("{}: {}", INTERACT_ERROR, err)))?,
) )
}
} else if call.has_flag("fuzzy") { } else if call.has_flag("fuzzy") {
let mut fuzzy_select = FuzzySelect::new(); //::with_theme(&theme); let mut fuzzy_select = FuzzySelect::new(); //::with_theme(&theme);
@ -185,32 +185,26 @@ impl Command for InputList {
) )
}; };
match ans { Ok(match ans {
InteractMode::Multi(res) => Ok({ InteractMode::Multi(res) => match res {
match res { Some(opts) => Value::List {
Some(opts) => Value::List { vals: opts.iter().map(|s| options[*s].value.clone()).collect(),
vals: opts.iter().map(|s| options[*s].value.clone()).collect(), span: head,
span: head, },
}, None => Value::List {
None => Value::List { vals: vec![],
vals: vec![], span: head,
span: head, },
}, },
} InteractMode::Single(res) => match res {
} Some(opt) => options[opt].value.clone(),
.into_pipeline_data()), None => Value::String {
InteractMode::Single(res) => Ok({ val: "".to_string(),
match res { span: head,
Some(opt) => options[opt].value.clone(), },
},
None => Value::String {
val: "".to_string(),
span: head,
},
}
}
.into_pipeline_data()),
} }
.into_pipeline_data())
} }
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {