mirror of
https://github.com/nushell/nushell.git
synced 2025-04-10 05:39:00 +02:00
Allow more types for input list
(#11195)
`input list` now allows all types by using `into_string`. Custom formatting logic for records was removed. Allow ranges as an input types. Also made the prompt check depend on option, so `input list ""` will have an empty prompt, while `input list` does not. Resolve #11181
This commit is contained in:
parent
15c7e1b725
commit
6ea5bdcf47
@ -1,6 +1,5 @@
|
|||||||
use dialoguer::{console::Term, Select};
|
use dialoguer::{console::Term, Select};
|
||||||
use dialoguer::{FuzzySelect, MultiSelect};
|
use dialoguer::{FuzzySelect, MultiSelect};
|
||||||
use nu_ansi_term::Color;
|
|
||||||
use nu_engine::CallExt;
|
use nu_engine::CallExt;
|
||||||
use nu_protocol::ast::Call;
|
use nu_protocol::ast::Call;
|
||||||
use nu_protocol::engine::{Command, EngineState, Stack};
|
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||||
@ -45,6 +44,7 @@ impl Command for InputList {
|
|||||||
Type::List(Box::new(Type::Any)),
|
Type::List(Box::new(Type::Any)),
|
||||||
),
|
),
|
||||||
(Type::List(Box::new(Type::Any)), Type::Any),
|
(Type::List(Box::new(Type::Any)), Type::Any),
|
||||||
|
(Type::Range, Type::Int),
|
||||||
])
|
])
|
||||||
.optional("prompt", SyntaxShape::String, "the prompt to display")
|
.optional("prompt", SyntaxShape::String, "the prompt to display")
|
||||||
.switch(
|
.switch(
|
||||||
@ -82,87 +82,21 @@ impl Command for InputList {
|
|||||||
let options: Vec<Options> = match input {
|
let options: Vec<Options> = match input {
|
||||||
PipelineData::Value(Value::Range { .. }, ..)
|
PipelineData::Value(Value::Range { .. }, ..)
|
||||||
| PipelineData::Value(Value::List { .. }, ..)
|
| PipelineData::Value(Value::List { .. }, ..)
|
||||||
| PipelineData::ListStream { .. }
|
| PipelineData::ListStream { .. } => input
|
||||||
| PipelineData::Value(Value::Record { .. }, ..) => {
|
.into_iter()
|
||||||
let mut lentable = Vec::<usize>::new();
|
.map(move |val| Options {
|
||||||
let rows = input.into_iter().collect::<Vec<_>>();
|
name: val.into_string(", ", engine_state.get_config()),
|
||||||
rows.iter().for_each(|row| {
|
value: val,
|
||||||
if let Ok(record) = row.as_record() {
|
})
|
||||||
let columns = record.len();
|
.collect(),
|
||||||
for (i, (col, val)) in record.iter().enumerate() {
|
|
||||||
if i == columns - 1 {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Ok(val) = val.as_string() {
|
|
||||||
let len = nu_utils::strip_ansi_likely(&val).len()
|
|
||||||
+ nu_utils::strip_ansi_likely(col).len();
|
|
||||||
if let Some(max_len) = lentable.get(i) {
|
|
||||||
lentable[i] = (*max_len).max(len);
|
|
||||||
} else {
|
|
||||||
lentable.push(len);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
rows.into_iter()
|
|
||||||
.map_while(move |x| {
|
|
||||||
if let Ok(val) = x.as_string() {
|
|
||||||
Some(Options {
|
|
||||||
name: val,
|
|
||||||
value: x,
|
|
||||||
})
|
|
||||||
} else if let Ok(record) = x.as_record() {
|
|
||||||
let mut options = Vec::new();
|
|
||||||
let columns = record.len();
|
|
||||||
for (i, (col, val)) in record.iter().enumerate() {
|
|
||||||
if let Ok(val) = val.as_string() {
|
|
||||||
let len = nu_utils::strip_ansi_likely(&val).len()
|
|
||||||
+ nu_utils::strip_ansi_likely(col).len();
|
|
||||||
options.push(format!(
|
|
||||||
" {}{}{}: {}{}",
|
|
||||||
Color::Cyan.prefix(),
|
|
||||||
col,
|
|
||||||
Color::Cyan.suffix(),
|
|
||||||
&val,
|
|
||||||
if i == columns - 1 {
|
|
||||||
String::from("")
|
|
||||||
} else {
|
|
||||||
format!(
|
|
||||||
"{} |",
|
|
||||||
" ".repeat(
|
|
||||||
lentable
|
|
||||||
.get(i)
|
|
||||||
.cloned()
|
|
||||||
.unwrap_or_default()
|
|
||||||
.saturating_sub(len)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Some(Options {
|
|
||||||
name: options.join(""),
|
|
||||||
value: x,
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.collect()
|
|
||||||
}
|
|
||||||
|
|
||||||
_ => {
|
_ => {
|
||||||
return Err(ShellError::TypeMismatch {
|
return Err(ShellError::TypeMismatch {
|
||||||
err_message: "expected a list or table".to_string(),
|
err_message: "expected a list, a table, or a range".to_string(),
|
||||||
span: head,
|
span: head,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let prompt = prompt.unwrap_or_default();
|
|
||||||
|
|
||||||
if options.is_empty() {
|
if options.is_empty() {
|
||||||
return Err(ShellError::TypeMismatch {
|
return Err(ShellError::TypeMismatch {
|
||||||
@ -188,7 +122,7 @@ impl Command for InputList {
|
|||||||
let multi_select = MultiSelect::new(); //::with_theme(&theme);
|
let multi_select = MultiSelect::new(); //::with_theme(&theme);
|
||||||
|
|
||||||
InteractMode::Multi(
|
InteractMode::Multi(
|
||||||
if !prompt.is_empty() {
|
if let Some(prompt) = prompt {
|
||||||
multi_select.with_prompt(&prompt)
|
multi_select.with_prompt(&prompt)
|
||||||
} else {
|
} else {
|
||||||
multi_select
|
multi_select
|
||||||
@ -204,7 +138,7 @@ impl Command for InputList {
|
|||||||
let fuzzy_select = FuzzySelect::new(); //::with_theme(&theme);
|
let fuzzy_select = FuzzySelect::new(); //::with_theme(&theme);
|
||||||
|
|
||||||
InteractMode::Single(
|
InteractMode::Single(
|
||||||
if !prompt.is_empty() {
|
if let Some(prompt) = prompt {
|
||||||
fuzzy_select.with_prompt(&prompt)
|
fuzzy_select.with_prompt(&prompt)
|
||||||
} else {
|
} else {
|
||||||
fuzzy_select
|
fuzzy_select
|
||||||
@ -220,7 +154,7 @@ impl Command for InputList {
|
|||||||
} else {
|
} else {
|
||||||
let select = Select::new(); //::with_theme(&theme);
|
let select = Select::new(); //::with_theme(&theme);
|
||||||
InteractMode::Single(
|
InteractMode::Single(
|
||||||
if !prompt.is_empty() {
|
if let Some(prompt) = prompt {
|
||||||
select.with_prompt(&prompt)
|
select.with_prompt(&prompt)
|
||||||
} else {
|
} else {
|
||||||
select
|
select
|
||||||
@ -268,6 +202,11 @@ impl Command for InputList {
|
|||||||
example: r#"ls | input list --fuzzy 'Select the target'"#,
|
example: r#"ls | input list --fuzzy 'Select the target'"#,
|
||||||
result: None,
|
result: None,
|
||||||
},
|
},
|
||||||
|
Example {
|
||||||
|
description: "Choose an item from a range",
|
||||||
|
example: r#"1..10 | input list"#,
|
||||||
|
result: None,
|
||||||
|
},
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user