fix: pass metadata to more filter commands for ls_colors (#5009)

* fix(filters): pass metadata for select

* fix(filters): pass metadata for group, window

* fix(filters): pass metadata for each, every

* fix(filters): pass metadata for collect, compact, flatten

* fix(filters): pass metadata for get

* fix: get rid of necessary `.clone()``

* style: rename closure call to be consistent w/ others

* fix(filters): pass metadata for par-each, prepend

* fix(filters): pass metadata for range

* fix(filters): pass metadata for reject

* fix(filters): pass metadata for more commands

* style: run cargo fmt
This commit is contained in:
Richard Wang 2022-03-28 04:43:09 -07:00 committed by GitHub
parent e934062542
commit 1aef3a730a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 126 additions and 77 deletions

View File

@ -37,6 +37,7 @@ impl Command for Collect {
let block = engine_state.get_block(capture_block.block_id).clone();
let mut stack = stack.captures_to_stack(&capture_block.captures);
let metadata = input.metadata();
let input: Value = input.into_value(call.head);
if let Some(var) = block.signature.get_positional(0) {
@ -53,6 +54,7 @@ impl Command for Collect {
call.redirect_stdout,
call.redirect_stderr,
)
.map(|x| x.set_metadata(metadata))
}
fn examples(&self) -> Vec<Example> {

View File

@ -77,31 +77,34 @@ pub fn compact(
input: PipelineData,
) -> Result<nu_protocol::PipelineData, ShellError> {
let columns: Vec<String> = call.rest(engine_state, stack, 0)?;
input.filter(
move |item| {
match item {
// Nothing is filtered out
Value::Nothing { .. } => false,
Value::Record { .. } => {
for column in columns.iter() {
match item.get_data_by_key(column) {
None => return false,
Some(x) => {
if let Value::Nothing { .. } = x {
return false;
let metadata = input.metadata();
input
.filter(
move |item| {
match item {
// Nothing is filtered out
Value::Nothing { .. } => false,
Value::Record { .. } => {
for column in columns.iter() {
match item.get_data_by_key(column) {
None => return false,
Some(x) => {
if let Value::Nothing { .. } = x {
return false;
}
}
}
}
// No defined columns contained Nothing
true
}
// No defined columns contained Nothing
true
// Any non-Nothing, non-record should be kept
_ => true,
}
// Any non-Nothing, non-record should be kept
_ => true,
}
},
engine_state.ctrlc.clone(),
)
},
engine_state.ctrlc.clone(),
)
.map(|m| m.set_metadata(metadata))
}
#[cfg(test)]

View File

@ -113,6 +113,7 @@ impl Command for Each {
let numbered = call.has_flag("numbered");
let keep_empty = call.has_flag("keep-empty");
let metadata = input.metadata();
let ctrlc = engine_state.ctrlc.clone();
let outer_ctrlc = engine_state.ctrlc.clone();
let engine_state = engine_state.clone();
@ -243,6 +244,7 @@ impl Command for Each {
outer_ctrlc,
)
})
.map(|x| x.set_metadata(metadata))
}
}

View File

@ -69,6 +69,8 @@ impl Command for Every {
let skip = call.has_flag("skip");
let metadata = input.metadata();
Ok(input
.into_iter()
.enumerate()
@ -79,7 +81,8 @@ impl Command for Every {
None
}
})
.into_pipeline_data(engine_state.ctrlc.clone()))
.into_pipeline_data(engine_state.ctrlc.clone())
.set_metadata(metadata))
}
}

View File

@ -112,11 +112,14 @@ fn flatten(
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
let tag = call.head;
let columns: Vec<CellPath> = call.rest(engine_state, stack, 0)?;
let metadata = input.metadata();
input.flat_map(
move |item| flat_value(&columns, &item, tag),
engine_state.ctrlc.clone(),
)
input
.flat_map(
move |item| flat_value(&columns, &item, tag),
engine_state.ctrlc.clone(),
)
.map(|x| x.set_metadata(metadata))
}
enum TableInside<'a> {

View File

@ -46,6 +46,7 @@ impl Command for Get {
let rest: Vec<CellPath> = call.rest(engine_state, stack, 1)?;
let ignore_errors = call.has_flag("ignore-errors");
let ctrlc = engine_state.ctrlc.clone();
let metadata = input.metadata();
if rest.is_empty() {
let output = input
@ -81,6 +82,7 @@ impl Command for Get {
Ok(output.into_iter().into_pipeline_data(ctrlc))
}
.map(|x| x.set_metadata(metadata))
}
fn examples(&self) -> Vec<Example> {
vec![

View File

@ -73,6 +73,7 @@ impl Command for Group {
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
let group_size: Spanned<usize> = call.req(engine_state, stack, 0)?;
let ctrlc = engine_state.ctrlc.clone();
let metadata = input.metadata();
//FIXME: add in support for external redirection when engine-q supports it generally
@ -82,7 +83,9 @@ impl Command for Group {
span: call.head,
};
Ok(each_group_iterator.into_pipeline_data(ctrlc))
Ok(each_group_iterator
.into_pipeline_data(ctrlc)
.set_metadata(metadata))
}
}

View File

@ -77,11 +77,12 @@ impl Command for Headers {
input: PipelineData,
) -> Result<nu_protocol::PipelineData, ShellError> {
let config = stack.get_config()?;
let metadata = input.metadata();
let value = input.into_value(call.head);
let headers = extract_headers(&value, &config)?;
let new_headers = replace_headers(value, &headers)?;
Ok(new_headers.into_pipeline_data())
Ok(new_headers.into_pipeline_data().set_metadata(metadata))
}
}

View File

@ -61,6 +61,7 @@ impl Command for ParEach {
let capture_block: CaptureBlock = call.req(engine_state, stack, 0)?;
let numbered = call.has_flag("numbered");
let metadata = input.metadata();
let ctrlc = engine_state.ctrlc.clone();
let engine_state = engine_state.clone();
let block_id = capture_block.block_id;
@ -288,6 +289,7 @@ impl Command for ParEach {
)
}
}
.map(|res| res.set_metadata(metadata))
}
}

View File

@ -82,12 +82,14 @@ impl Command for Prepend {
) -> Result<PipelineData, ShellError> {
let val: Value = call.req(engine_state, stack, 0)?;
let vec: Vec<Value> = process_value(val);
let metadata = input.metadata();
Ok(vec
.into_iter()
.chain(input)
.into_iter()
.into_pipeline_data(engine_state.ctrlc.clone()))
.into_pipeline_data(engine_state.ctrlc.clone())
.set_metadata(metadata))
}
}

View File

@ -65,6 +65,7 @@ impl Command for Range {
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let metadata = input.metadata();
let rows: nu_protocol::Range = call.req(engine_state, stack, 0)?;
let rows_from = get_range_val(rows.from);
@ -112,6 +113,7 @@ impl Command for Range {
Ok(iter.into_pipeline_data(engine_state.ctrlc.clone()))
}
}
.map(|x| x.set_metadata(metadata))
}
}

View File

@ -72,6 +72,7 @@ fn reject(
if columns.is_empty() {
return Err(ShellError::CantFindColumn(span, span));
}
let metadata = input.metadata();
let mut keep_columns = vec![];
@ -160,6 +161,7 @@ fn reject(
}
x => Ok(x),
}
.map(|x| x.set_metadata(metadata))
}
fn get_input_cols(input: Vec<Value>) -> Vec<String> {

View File

@ -110,49 +110,52 @@ fn rename(
}
let columns: Vec<String> = call.rest(engine_state, stack, 0)?;
let metadata = input.metadata();
input.map(
move |item| match item {
Value::Record {
mut cols,
vals,
span,
} => {
match &specified_column {
Some(c) => {
// check if the specified column to be renamed exists
if !cols.contains(&c[0]) {
return Value::Error {
error: ShellError::UnsupportedInput(
"The specified column does not exist".to_string(),
specified_col_span.unwrap_or(span),
),
};
input
.map(
move |item| match item {
Value::Record {
mut cols,
vals,
span,
} => {
match &specified_column {
Some(c) => {
// check if the specified column to be renamed exists
if !cols.contains(&c[0]) {
return Value::Error {
error: ShellError::UnsupportedInput(
"The specified column does not exist".to_string(),
specified_col_span.unwrap_or(span),
),
};
}
for (idx, val) in cols.iter_mut().enumerate() {
if *val == c[0] {
cols[idx] = c[1].to_string();
break;
}
}
}
for (idx, val) in cols.iter_mut().enumerate() {
if *val == c[0] {
cols[idx] = c[1].to_string();
break;
None => {
for (idx, val) in columns.iter().enumerate() {
if idx >= cols.len() {
// skip extra new columns names if we already reached the final column
break;
}
cols[idx] = val.clone();
}
}
}
None => {
for (idx, val) in columns.iter().enumerate() {
if idx >= cols.len() {
// skip extra new columns names if we already reached the final column
break;
}
cols[idx] = val.clone();
}
}
Value::Record { cols, vals, span }
}
Value::Record { cols, vals, span }
}
x => x,
},
engine_state.ctrlc.clone(),
)
x => x,
},
engine_state.ctrlc.clone(),
)
.map(|x| x.set_metadata(metadata))
}
#[cfg(test)]

View File

@ -205,6 +205,7 @@ pub fn rotate(
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let metadata = input.metadata();
let col_given_names: Vec<String> = call.rest(engine_state, stack, 0)?;
let mut values = input.into_iter().collect::<Vec<_>>();
let mut old_column_names = vec![];
@ -288,7 +289,8 @@ pub fn rotate(
}],
span: call.head,
}
.into_pipeline_data());
.into_pipeline_data()
.set_metadata(metadata));
}
// holder for the new records
@ -344,7 +346,8 @@ pub fn rotate(
vals: final_values,
span: call.head,
}
.into_pipeline_data())
.into_pipeline_data()
.set_metadata(metadata))
}
#[cfg(test)]

View File

@ -90,6 +90,7 @@ fn select(
let input = if !rows.is_empty() {
rows.sort_unstable();
// let skip = call.has_flag("skip");
let metadata = input.metadata();
let pipeline_iter: PipelineIterator = input.into_iter();
NthIterator {
@ -99,6 +100,7 @@ fn select(
current: 0,
}
.into_pipeline_data(engine_state.ctrlc.clone())
.set_metadata(metadata)
} else {
input
};
@ -109,6 +111,7 @@ fn select(
vals: input_vals,
span,
},
metadata,
..,
) => {
let mut output = vec![];
@ -133,9 +136,10 @@ fn select(
Ok(output
.into_iter()
.into_pipeline_data(engine_state.ctrlc.clone()))
.into_pipeline_data(engine_state.ctrlc.clone())
.set_metadata(metadata))
}
PipelineData::ListStream(stream, ..) => Ok(stream
PipelineData::ListStream(stream, metadata, ..) => Ok(stream
.map(move |x| {
if !columns.is_empty() {
let mut cols = vec![];
@ -158,8 +162,9 @@ fn select(
x
}
})
.into_pipeline_data(engine_state.ctrlc.clone())),
PipelineData::Value(v, ..) => {
.into_pipeline_data(engine_state.ctrlc.clone())
.set_metadata(metadata)),
PipelineData::Value(v, metadata, ..) => {
if !columns.is_empty() {
let mut cols = vec![];
let mut vals = vec![];
@ -172,9 +177,11 @@ fn select(
vals.push(result);
}
Ok(Value::Record { cols, vals, span }.into_pipeline_data())
Ok(Value::Record { cols, vals, span }
.into_pipeline_data()
.set_metadata(metadata))
} else {
Ok(v.into_pipeline_data())
Ok(v.into_pipeline_data().set_metadata(metadata))
}
}
_ => Ok(PipelineData::new(span)),

View File

@ -133,6 +133,7 @@ pub fn transpose(
};
let ctrlc = engine_state.ctrlc.clone();
let metadata = input.metadata();
let input: Vec<_> = input.into_iter().collect();
let args = transpose_args;
@ -226,7 +227,8 @@ pub fn transpose(
span: name,
}
}))
.into_pipeline_data(ctrlc))
.into_pipeline_data(ctrlc)
.set_metadata(metadata))
}
#[cfg(test)]

View File

@ -129,6 +129,7 @@ impl Command for UpdateCells {
let orig_env_vars = stack.env_vars.clone();
let orig_env_hidden = stack.env_hidden.clone();
let metadata = input.metadata();
let ctrlc = engine_state.ctrlc.clone();
let block: Block = engine_state.get_block(block.block_id).clone();
@ -163,7 +164,8 @@ impl Command for UpdateCells {
redirect_stderr,
span,
}
.into_pipeline_data(ctrlc))
.into_pipeline_data(ctrlc)
.set_metadata(metadata))
}
}

View File

@ -144,6 +144,7 @@ impl Command for Window {
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
let group_size: Spanned<usize> = call.req(engine_state, stack, 0)?;
let ctrlc = engine_state.ctrlc.clone();
let metadata = input.metadata();
let stride: Option<usize> = call.get_flag(engine_state, stack, "stride")?;
let stride = stride.unwrap_or(1);
@ -158,7 +159,9 @@ impl Command for Window {
stride,
};
Ok(each_group_iterator.into_pipeline_data(ctrlc))
Ok(each_group_iterator
.into_pipeline_data(ctrlc)
.set_metadata(metadata))
}
}

View File

@ -87,6 +87,7 @@ impl Command for Zip {
let other: Value = call.req(engine_state, stack, 0)?;
let head = call.head;
let ctrlc = engine_state.ctrlc.clone();
let metadata = input.metadata();
Ok(input
.into_iter()
@ -95,7 +96,8 @@ impl Command for Zip {
vals: vec![x, y],
span: head,
})
.into_pipeline_data(ctrlc))
.into_pipeline_data(ctrlc)
.set_metadata(metadata))
}
}