Added --index flag to input list (#11580)

# Description
This PR closes #11571 

Add `--index` flag to input list.

For example:

![image](https://github.com/nushell/nushell/assets/72006223/19efb011-1ff8-4916-b2bd-6f73e89cb186))
 
# Tests + Formatting
 Added new example for `--index` flag.
This commit is contained in:
Andrei Pirlea 2024-01-24 19:57:29 +02:00 committed by GitHub
parent 323207ca1d
commit 0aabe84460
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -49,6 +49,7 @@ impl Command for InputList {
Some('m'),
)
.switch("fuzzy", "Use a fuzzy select.", Some('f'))
.switch("index", "Returns list indexes.", Some('i'))
.allow_variants_without_examples(true)
.category(Category::Platform)
}
@ -76,6 +77,7 @@ impl Command for InputList {
let prompt: Option<String> = call.opt(engine_state, stack, 0)?;
let multi = call.has_flag(engine_state, stack, "multi")?;
let fuzzy = call.has_flag(engine_state, stack, "fuzzy")?;
let index = call.has_flag(engine_state, stack, "index")?;
let options: Vec<Options> = match input {
PipelineData::Value(Value::Range { .. }, ..)
@ -168,17 +170,40 @@ impl Command for InputList {
};
Ok(match ans {
InteractMode::Multi(res) => match res {
Some(opts) => Value::list(
opts.iter().map(|s| options[*s].value.clone()).collect(),
head,
),
None => Value::nothing(head),
},
InteractMode::Single(res) => match res {
Some(opt) => options[opt].value.clone(),
None => Value::nothing(head),
},
InteractMode::Multi(res) => {
if index {
match res {
Some(opts) => Value::list(
opts.into_iter()
.map(|s| Value::int(s as i64, head))
.collect(),
head,
),
None => Value::nothing(head),
}
} else {
match res {
Some(opts) => Value::list(
opts.iter().map(|s| options[*s].value.clone()).collect(),
head,
),
None => Value::nothing(head),
}
}
}
InteractMode::Single(res) => {
if index {
match res {
Some(opt) => Value::int(opt as i64, head),
None => Value::nothing(head),
}
} else {
match res {
Some(opt) => options[opt].value.clone(),
None => Value::nothing(head),
}
}
}
}
.into_pipeline_data())
}
@ -205,6 +230,11 @@ impl Command for InputList {
example: r#"1..10 | input list"#,
result: None,
},
Example {
description: "Return the index of a selected item",
example: r#"[Banana Kiwi Pear Peach Strawberry] | input list --index"#,
result: None,
},
]
}
}