Add insert/update to lists (#4873)

This commit is contained in:
JT
2022-03-19 10:12:54 +13:00
committed by GitHub
parent 983d115bc0
commit b293282e9b
5 changed files with 119 additions and 8 deletions

View File

@ -1,9 +1,9 @@
use nu_engine::{eval_block, CallExt};
use nu_protocol::ast::{Call, CellPath};
use nu_protocol::ast::{Call, CellPath, PathMember};
use nu_protocol::engine::{CaptureBlock, Command, EngineState, Stack};
use nu_protocol::{
Category, Example, FromValue, IntoPipelineData, PipelineData, ShellError, Signature, Span,
SyntaxShape, Value,
Category, Example, FromValue, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData,
ShellError, Signature, Span, SyntaxShape, Value,
};
#[derive(Clone)]
@ -121,6 +121,24 @@ fn insert(
ctrlc,
)
} else {
if let Some(PathMember::Int { val, .. }) = cell_path.members.get(0) {
let mut input = input.into_iter();
let mut pre_elems = vec![];
for _ in 0..*val {
if let Some(v) = input.next() {
pre_elems.push(v);
} else {
pre_elems.push(Value::Nothing { span })
}
}
return Ok(pre_elems
.into_iter()
.chain(vec![replacement])
.chain(input)
.into_pipeline_data(ctrlc));
}
input.map(
move |mut input| {
let replacement = replacement.clone();

View File

@ -1,9 +1,9 @@
use nu_engine::{eval_block, CallExt};
use nu_protocol::ast::{Call, CellPath};
use nu_protocol::ast::{Call, CellPath, PathMember};
use nu_protocol::engine::{CaptureBlock, Command, EngineState, Stack};
use nu_protocol::{
Category, Example, FromValue, IntoPipelineData, PipelineData, ShellError, Signature, Span,
SyntaxShape, Value,
Category, Example, FromValue, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData,
ShellError, Signature, Span, SyntaxShape, Value,
};
#[derive(Clone)]
@ -131,6 +131,27 @@ fn upsert(
ctrlc,
)
} else {
if let Some(PathMember::Int { val, span }) = cell_path.members.get(0) {
let mut input = input.into_iter();
let mut pre_elems = vec![];
for idx in 0..*val {
if let Some(v) = input.next() {
pre_elems.push(v);
} else {
return Err(ShellError::AccessBeyondEnd(idx - 1, *span));
}
}
// Skip over the replaced value
let _ = input.next();
return Ok(pre_elems
.into_iter()
.chain(vec![replacement])
.chain(input)
.into_pipeline_data(ctrlc));
}
input.map(
move |mut input| {
let replacement = replacement.clone();