Skip to content

Commit

Permalink
Add metadata to some filters (nushell#11160)
Browse files Browse the repository at this point in the history
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx

you can also mention related issues, PRs or discussions!
-->

# Description
This PR preserves metadata when running some filters. As discussed on
discord that helps when running for example `ls | reject modified`
because it keeps colouring and links.
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.

Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->

# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->

# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library

> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->

# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
  • Loading branch information
MarikaChlebowska committed Nov 27, 2023
1 parent 54398f9 commit fa83458
Show file tree
Hide file tree
Showing 33 changed files with 203 additions and 233 deletions.
3 changes: 1 addition & 2 deletions crates/nu-command/src/filters/append.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,7 @@ only unwrap the outer list, and leave the variable's contents untouched."#
Ok(input
.into_iter()
.chain(other.into_pipeline_data())
.into_pipeline_data(engine_state.ctrlc.clone())
.set_metadata(metadata))
.into_pipeline_data_with_metadata(metadata, engine_state.ctrlc.clone()))
}
}

Expand Down
3 changes: 1 addition & 2 deletions crates/nu-command/src/filters/columns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,7 @@ fn getcol(
Ok(input_cols
.into_iter()
.map(move |x| Value::string(x, head))
.into_pipeline_data(ctrlc)
.set_metadata(metadata))
.into_pipeline_data_with_metadata(metadata, ctrlc))
}
PipelineData::ExternalStream { .. } => Err(ShellError::OnlySupportsThisInputType {
exp_input_type: "record or table".into(),
Expand Down
65 changes: 35 additions & 30 deletions crates/nu-command/src/filters/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,51 +78,56 @@ fn default(
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let metadata = input.metadata();
let value: Value = call.req(engine_state, stack, 0)?;
let column: Option<Spanned<String>> = call.opt(engine_state, stack, 1)?;

let ctrlc = engine_state.ctrlc.clone();

if let Some(column) = column {
input.map(
move |item| {
let span = item.span();
match item {
Value::Record {
val: mut record, ..
} => {
let mut found = false;
input
.map(
move |item| {
let span = item.span();
match item {
Value::Record {
val: mut record, ..
} => {
let mut found = false;

for (col, val) in record.iter_mut() {
if *col == column.item {
found = true;
if matches!(val, Value::Nothing { .. }) {
*val = value.clone();
for (col, val) in record.iter_mut() {
if *col == column.item {
found = true;
if matches!(val, Value::Nothing { .. }) {
*val = value.clone();
}
}
}
}

if !found {
record.push(column.item.clone(), value.clone());
}
if !found {
record.push(column.item.clone(), value.clone());
}

Value::record(record, span)
Value::record(record, span)
}
_ => item,
}
_ => item,
}
},
ctrlc,
)
},
ctrlc,
)
.map(|x| x.set_metadata(metadata))
} else if input.is_nothing() {
Ok(value.into_pipeline_data())
} else {
input.map(
move |item| match item {
Value::Nothing { .. } => value.clone(),
x => x,
},
ctrlc,
)
input
.map(
move |item| match item {
Value::Nothing { .. } => value.clone(),
x => x,
},
ctrlc,
)
.map(|x| x.set_metadata(metadata))
}
}

Expand Down
7 changes: 4 additions & 3 deletions crates/nu-command/src/filters/drop/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ fn drop_cols(
// `[{a: 1}, {b: 2}] | drop column`
// This will drop the column "a" instead of "b" even though column "b"
// is displayed farther to the right.
let metadata = input.metadata();
match input {
PipelineData::ListStream(mut stream, ..) => {
if let Some(mut first) = stream.next() {
Expand All @@ -106,7 +107,7 @@ fn drop_cols(
Err(e) => Value::error(e, head),
}
}))
.into_pipeline_data(engine_state.ctrlc.clone()))
.into_pipeline_data_with_metadata(metadata, engine_state.ctrlc.clone()))
} else {
Ok(PipelineData::Empty)
}
Expand All @@ -121,14 +122,14 @@ fn drop_cols(
drop_record_cols(val, head, &drop_cols)?
}
}
Ok(Value::list(vals, span).into_pipeline_data())
Ok(Value::list(vals, span).into_pipeline_data_with_metadata(metadata))
}
Value::Record {
val: mut record, ..
} => {
let len = record.len().saturating_sub(columns);
record.truncate(len);
Ok(Value::record(record, span).into_pipeline_data())
Ok(Value::record(record, span).into_pipeline_data_with_metadata(metadata))
}
// Propagate errors
Value::Error { error, .. } => Err(*error),
Expand Down
7 changes: 2 additions & 5 deletions crates/nu-command/src/filters/drop/drop_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ impl Command for Drop {

if rows_to_drop == 0 {
Ok(v.into_iter()
.into_pipeline_data(engine_state.ctrlc.clone())
.set_metadata(metadata))
.into_pipeline_data_with_metadata(metadata, engine_state.ctrlc.clone()))
} else {
let k = if vlen < rows_to_drop {
0
Expand All @@ -106,9 +105,7 @@ impl Command for Drop {
};

let iter = v.into_iter().take(k as usize);
Ok(iter
.into_pipeline_data(engine_state.ctrlc.clone())
.set_metadata(metadata))
Ok(iter.into_pipeline_data_with_metadata(metadata, engine_state.ctrlc.clone()))
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions crates/nu-command/src/filters/drop/nth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ impl Command for DropNth {
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let metadata = input.metadata();
let number_or_range = extract_int_or_range(engine_state, stack, call)?;
let mut lower_bound = None;
let rows = match number_or_range {
Expand Down Expand Up @@ -165,14 +166,14 @@ impl Command for DropNth {
.into_iter()
.take(lower_bound)
.collect::<Vec<_>>()
.into_pipeline_data(engine_state.ctrlc.clone()))
.into_pipeline_data_with_metadata(metadata, engine_state.ctrlc.clone()))
} else {
Ok(DropNthIterator {
input: input.into_iter(),
rows,
current: 0,
}
.into_pipeline_data(engine_state.ctrlc.clone()))
.into_pipeline_data_with_metadata(metadata, engine_state.ctrlc.clone()))
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions crates/nu-command/src/filters/every.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ impl Command for Every {
None
}
})
.into_pipeline_data(engine_state.ctrlc.clone())
.set_metadata(metadata))
.into_pipeline_data_with_metadata(metadata, engine_state.ctrlc.clone()))
}
}

Expand Down
8 changes: 3 additions & 5 deletions crates/nu-command/src/filters/find.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ fn find_with_rest_and_highlight(
},
ctrlc,
),
PipelineData::ListStream(stream, meta) => Ok(ListStream::from_stream(
PipelineData::ListStream(stream, metadata) => Ok(ListStream::from_stream(
stream
.map(move |mut x| {
let span = x.span();
Expand Down Expand Up @@ -421,8 +421,7 @@ fn find_with_rest_and_highlight(
}),
ctrlc.clone(),
)
.into_pipeline_data(ctrlc)
.set_metadata(meta)),
.into_pipeline_data_with_metadata(metadata, ctrlc)),
PipelineData::ExternalStream { stdout: None, .. } => Ok(PipelineData::empty()),
PipelineData::ExternalStream {
stdout: Some(stream),
Expand Down Expand Up @@ -583,8 +582,7 @@ fn split_string_if_multiline(input: PipelineData, head_span: Span) -> PipelineDa
.collect(),
span,
)
.into_pipeline_data()
.set_metadata(input.metadata())
.into_pipeline_data_with_metadata(input.metadata())
} else {
input
}
Expand Down
13 changes: 4 additions & 9 deletions crates/nu-command/src/filters/first.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,7 @@ fn first_helper(

// early exit for `first 0`
if rows_desired == 0 {
return Ok(Vec::<Value>::new()
.into_pipeline_data(ctrlc)
.set_metadata(metadata));
return Ok(Vec::<Value>::new().into_pipeline_data_with_metadata(metadata, ctrlc));
}

match input {
Expand All @@ -126,8 +124,7 @@ fn first_helper(
Ok(vals
.into_iter()
.take(rows_desired)
.into_pipeline_data(ctrlc)
.set_metadata(metadata))
.into_pipeline_data_with_metadata(metadata, ctrlc))
}
}
Value::Binary { val, .. } => {
Expand All @@ -152,8 +149,7 @@ fn first_helper(
Ok(val
.into_range_iter(ctrlc.clone())?
.take(rows_desired)
.into_pipeline_data(ctrlc)
.set_metadata(metadata))
.into_pipeline_data_with_metadata(metadata, ctrlc))
}
}
// Propagate errors by explicitly matching them before the final case.
Expand All @@ -176,8 +172,7 @@ fn first_helper(
} else {
Ok(ls
.take(rows_desired)
.into_pipeline_data(ctrlc)
.set_metadata(metadata))
.into_pipeline_data_with_metadata(metadata, ctrlc))
}
}
PipelineData::ExternalStream { span, .. } => Err(ShellError::OnlySupportsThisInputType {
Expand Down
4 changes: 1 addition & 3 deletions crates/nu-command/src/filters/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ impl Command for Group {
span: call.head,
};

Ok(each_group_iterator
.into_pipeline_data(ctrlc)
.set_metadata(metadata))
Ok(each_group_iterator.into_pipeline_data_with_metadata(metadata, ctrlc))
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/nu-command/src/filters/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl Command for Headers {
let (old_headers, new_headers) = extract_headers(&value, config)?;
let new_headers = replace_headers(value, &old_headers, &new_headers)?;

Ok(new_headers.into_pipeline_data().set_metadata(metadata))
Ok(new_headers.into_pipeline_data_with_metadata(metadata))
}
}

Expand Down
Loading

0 comments on commit fa83458

Please sign in to comment.