2019-11-12 08:07:43 +01:00
|
|
|
use crate::commands::WholeStreamCommand;
|
2019-12-04 20:52:31 +01:00
|
|
|
use crate::data::TaggedListBuilder;
|
2019-11-12 08:07:43 +01:00
|
|
|
use crate::prelude::*;
|
2020-01-04 05:00:39 +01:00
|
|
|
use crate::utils::data_processing::{columns_sorted, t_sort};
|
2019-11-12 08:07:43 +01:00
|
|
|
use chrono::{DateTime, NaiveDate, Utc};
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 03:30:48 +01:00
|
|
|
use nu_errors::ShellError;
|
2019-12-04 20:52:31 +01:00
|
|
|
use nu_protocol::{
|
|
|
|
Primitive, ReturnSuccess, Signature, SyntaxShape, TaggedDictBuilder, UntaggedValue, Value,
|
|
|
|
};
|
2019-11-21 15:33:14 +01:00
|
|
|
use nu_source::Tagged;
|
2019-12-09 19:52:01 +01:00
|
|
|
use nu_value_ext::get_data_by_key;
|
2019-11-12 08:07:43 +01:00
|
|
|
|
|
|
|
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")
|
|
|
|
.named(
|
|
|
|
"group_by",
|
|
|
|
SyntaxShape::String,
|
|
|
|
"the name of the column to group by",
|
|
|
|
)
|
|
|
|
.named(
|
|
|
|
"split_by",
|
|
|
|
SyntaxShape::String,
|
|
|
|
"the name of the column within the grouped by table to split by",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
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! {
|
2019-11-21 15:33:14 +01:00
|
|
|
let values: Vec<Value> = input.values.collect().await;
|
2019-11-12 08:07:43 +01:00
|
|
|
|
2019-11-12 09:38:55 +01:00
|
|
|
let column_grouped_by_name = if let Some(grouped_by) = group_by {
|
|
|
|
Some(grouped_by.item().clone())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2019-11-12 08:07:43 +01:00
|
|
|
|
2019-11-12 09:38:55 +01:00
|
|
|
if show_columns {
|
2019-11-04 16:47:03 +01:00
|
|
|
for label in columns_sorted(column_grouped_by_name, &values[0], &name).into_iter() {
|
2019-12-04 20:52:31 +01:00
|
|
|
yield ReturnSuccess::value(UntaggedValue::string(label.item).into_value(label.tag));
|
2019-11-12 09:38:55 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
match t_sort(column_grouped_by_name, None, &values[0], name) {
|
|
|
|
Ok(sorted) => yield ReturnSuccess::value(sorted),
|
|
|
|
Err(err) => yield Err(err)
|
2019-11-12 08:07:43 +01:00
|
|
|
}
|
2019-11-12 09:38:55 +01:00
|
|
|
}
|
|
|
|
}))
|
2019-11-12 08:07:43 +01:00
|
|
|
}
|