https://github.com/cablehead/nu_plugin_stateful_filter
Nushell plugin to support stateful filtering
https://github.com/cablehead/nu_plugin_stateful_filter
nushell nushell-plugin
Last synced: 7 months ago
JSON representation
Nushell plugin to support stateful filtering
- Host: GitHub
- URL: https://github.com/cablehead/nu_plugin_stateful_filter
- Owner: cablehead
- Created: 2024-07-28T21:15:00.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2024-08-27T17:43:46.000Z (10 months ago)
- Last Synced: 2024-12-08T20:43:22.347Z (7 months ago)
- Topics: nushell, nushell-plugin
- Language: Rust
- Homepage:
- Size: 25.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
Status: sketching
It's similar to the
[`generate`](https://www.nushell.sh/commands/docs/generate.html#generate-for-generators)
command, but instead of generating a pipeline with no input, it allows you to
process an input pipeline.It's also similar to the
[`reduce`](https://www.nushell.sh/commands/docs/reduce.html) command, but it
preserves the pipeline, allowing streaming. With `reduce`, you would accumulate
a list in memory.## Usage
```nushell
let messages = [
{ type: "data", value: 1 }
{ type: "data", value: 2 }
{ type: "data", value: 3 }
{ type: "threshold" }
{ type: "data", value: 4 }
{ type: "data", value: 5 }
]# output the last message seen before encountering a “threshold” message,
# then output all subsequent messages in real time$messages | each {|x| sleep 1sec; $x } | stateful filter {found: false} { |state, x|
if $state.found {
return { out: $x }
}if $x.type == "threshold" {
return { state: {found: true}, out: ($state | get last?) }
}{ state: {found: false, last: $x} }
}
```Outputs:
```
{ type: "data", value: 3 }
{ type: "data", value: 4 }
{ type: "data", value: 5 }
```## Usage
```
Run closure on each element of a listUsage:
> stateful filterFlags:
-h, --help - Display the help message for this commandParameters:
initial : The initial state to pass to the closure
closure :
The closure receives `|state, value|` and should return a record in the
shape: { out?: value, state?: new_state }. Both `out` and `state` are
optional. You can drop rows by omitting the `out` key, and the current
state is preserved if its key is omitted.Input/output types:
─#─┬────input────┬───output────
0 │ list-stream │ list-stream
───┴─────────────┴─────────────
```