forked from extern/nushell
522a828687
* Refactor InputStream and affected commands. First, making `values` private and leaning on the `Stream` implementation makes consumes of `InputStream` less likely to have to change in the future, if we change what an `InputStream` is internally. Second, we're dropping `Option<InputStream>` as the input to pipelines, internals, and externals. Instead, `InputStream.is_empty` can be used to check for "emptiness". Empty streams are typically only ever used as the first input to a pipeline. * Add run_external internal command. We want to push external commands closer to internal commands, eventually eliminating the concept of "external" completely. This means we can consolidate a couple of things: - Variable evaluation (for example, `$it`, `$nu`, alias vars) - Behaviour of whole stream vs per-item external execution It should also make it easier for us to start introducing argument signatures for external commands, * Update run_external.rs * Update run_external.rs * Update run_external.rs * Update run_external.rs Co-authored-by: Jonathan Turner <jonathandturner@users.noreply.github.com>
92 lines
2.5 KiB
Rust
92 lines
2.5 KiB
Rust
use crate::commands::WholeStreamCommand;
|
|
use crate::data::TaggedListBuilder;
|
|
use crate::prelude::*;
|
|
use crate::utils::data_processing::{columns_sorted, t_sort};
|
|
use chrono::{DateTime, NaiveDate, Utc};
|
|
use nu_errors::ShellError;
|
|
use nu_protocol::{
|
|
Primitive, ReturnSuccess, Signature, SyntaxShape, TaggedDictBuilder, UntaggedValue, Value,
|
|
};
|
|
use nu_source::Tagged;
|
|
use nu_value_ext::get_data_by_key;
|
|
|
|
pub struct TSortBy;
|
|
|
|
#[derive(Deserialize)]
|
|
pub struct TSortByArgs {
|
|
#[serde(rename(deserialize = "show-columns"))]
|
|
show_columns: bool,
|
|
group_by: Option<Tagged<String>>,
|
|
#[allow(unused)]
|
|
split_by: Option<String>,
|
|
}
|
|
|
|
impl WholeStreamCommand for TSortBy {
|
|
fn name(&self) -> &str {
|
|
"t-sort-by"
|
|
}
|
|
|
|
fn signature(&self) -> Signature {
|
|
Signature::build("t-sort-by")
|
|
.switch(
|
|
"show-columns",
|
|
"Displays the column names sorted",
|
|
Some('c'),
|
|
)
|
|
.named(
|
|
"group_by",
|
|
SyntaxShape::String,
|
|
"the name of the column to group by",
|
|
Some('g'),
|
|
)
|
|
.named(
|
|
"split_by",
|
|
SyntaxShape::String,
|
|
"the name of the column within the grouped by table to split by",
|
|
Some('s'),
|
|
)
|
|
}
|
|
|
|
fn usage(&self) -> &str {
|
|
"Sort by the given columns."
|
|
}
|
|
|
|
fn run(
|
|
&self,
|
|
args: CommandArgs,
|
|
registry: &CommandRegistry,
|
|
) -> Result<OutputStream, ShellError> {
|
|
args.process(registry, t_sort_by)?.run()
|
|
}
|
|
}
|
|
|
|
fn t_sort_by(
|
|
TSortByArgs {
|
|
show_columns,
|
|
group_by,
|
|
..
|
|
}: TSortByArgs,
|
|
RunnableContext { input, name, .. }: RunnableContext,
|
|
) -> Result<OutputStream, ShellError> {
|
|
Ok(OutputStream::new(async_stream! {
|
|
let values: Vec<Value> = input.collect().await;
|
|
|
|
let column_grouped_by_name = if let Some(grouped_by) = group_by {
|
|
Some(grouped_by.item().clone())
|
|
} else {
|
|
None
|
|
};
|
|
|
|
if show_columns {
|
|
for label in columns_sorted(column_grouped_by_name, &values[0], &name).into_iter() {
|
|
yield ReturnSuccess::value(UntaggedValue::string(label.item).into_value(label.tag));
|
|
}
|
|
} else {
|
|
match t_sort(column_grouped_by_name, None, &values[0], name) {
|
|
Ok(sorted) => yield ReturnSuccess::value(sorted),
|
|
Err(err) => yield Err(err)
|
|
}
|
|
}
|
|
}))
|
|
}
|