2019-11-12 07:07:43 +00:00
|
|
|
use crate::commands::WholeStreamCommand;
|
2019-12-04 19:52:31 +00:00
|
|
|
use crate::data::TaggedListBuilder;
|
2019-11-12 07:07:43 +00:00
|
|
|
use crate::prelude::*;
|
|
|
|
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 02:30:48 +00:00
|
|
|
use nu_errors::ShellError;
|
2019-12-04 19:52:31 +00:00
|
|
|
use nu_protocol::{
|
|
|
|
Primitive, ReturnSuccess, Signature, SyntaxShape, TaggedDictBuilder, UntaggedValue, Value,
|
|
|
|
};
|
2019-11-21 14:33:14 +00:00
|
|
|
use nu_source::Tagged;
|
2019-12-09 18:52:01 +00:00
|
|
|
use nu_value_ext::get_data_by_key;
|
2019-11-12 07:07:43 +00: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 14:33:14 +00:00
|
|
|
let values: Vec<Value> = input.values.collect().await;
|
2019-11-12 07:07:43 +00:00
|
|
|
|
2019-11-12 08:38:55 +00:00
|
|
|
let column_grouped_by_name = if let Some(grouped_by) = group_by {
|
|
|
|
Some(grouped_by.item().clone())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2019-11-12 07:07:43 +00:00
|
|
|
|
2019-11-12 08:38:55 +00:00
|
|
|
if show_columns {
|
2019-11-04 15:47:03 +00:00
|
|
|
for label in columns_sorted(column_grouped_by_name, &values[0], &name).into_iter() {
|
2019-12-04 19:52:31 +00:00
|
|
|
yield ReturnSuccess::value(UntaggedValue::string(label.item).into_value(label.tag));
|
2019-11-12 08:38:55 +00: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 07:07:43 +00:00
|
|
|
}
|
2019-11-12 08:38:55 +00:00
|
|
|
}
|
|
|
|
}))
|
2019-11-12 07:07:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn columns_sorted(
|
|
|
|
_group_by_name: Option<String>,
|
2019-11-21 14:33:14 +00:00
|
|
|
value: &Value,
|
2019-11-12 07:07:43 +00:00
|
|
|
tag: impl Into<Tag>,
|
2019-11-04 15:47:03 +00:00
|
|
|
) -> Vec<Tagged<String>> {
|
2019-11-12 07:07:43 +00:00
|
|
|
let origin_tag = tag.into();
|
|
|
|
|
|
|
|
match value {
|
2019-11-21 14:33:14 +00:00
|
|
|
Value {
|
|
|
|
value: UntaggedValue::Row(rows),
|
2019-11-12 07:07:43 +00:00
|
|
|
..
|
|
|
|
} => {
|
2019-11-21 14:33:14 +00:00
|
|
|
let mut keys: Vec<Value> = rows
|
|
|
|
.entries
|
|
|
|
.keys()
|
|
|
|
.map(|s| s.as_ref())
|
|
|
|
.map(|k: &str| {
|
|
|
|
let date = NaiveDate::parse_from_str(k, "%B %d-%Y");
|
|
|
|
|
|
|
|
let date = match date {
|
|
|
|
Ok(parsed) => UntaggedValue::Primitive(Primitive::Date(
|
|
|
|
DateTime::<Utc>::from_utc(parsed.and_hms(12, 34, 56), Utc),
|
|
|
|
)),
|
2019-12-04 19:52:31 +00:00
|
|
|
Err(_) => UntaggedValue::string(k),
|
2019-11-21 14:33:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
date.into_untagged_value()
|
|
|
|
})
|
|
|
|
.collect();
|
2019-11-12 07:07:43 +00:00
|
|
|
|
|
|
|
keys.sort();
|
|
|
|
|
2019-11-04 15:47:03 +00:00
|
|
|
let keys: Vec<String> = keys
|
2019-11-12 07:07:43 +00:00
|
|
|
.into_iter()
|
2019-11-04 15:47:03 +00:00
|
|
|
.map(|k| match k {
|
2019-11-21 14:33:14 +00:00
|
|
|
Value {
|
|
|
|
value: UntaggedValue::Primitive(Primitive::Date(d)),
|
2019-11-04 15:47:03 +00:00
|
|
|
..
|
|
|
|
} => format!("{}", d.format("%B %d-%Y")),
|
2019-12-31 07:36:08 +00:00
|
|
|
_ => k.as_string().unwrap(),
|
2019-11-12 07:07:43 +00:00
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
keys.into_iter().map(|k| k.tagged(&origin_tag)).collect()
|
|
|
|
}
|
2019-12-06 15:28:26 +00:00
|
|
|
_ => vec!["default".to_owned().tagged(&origin_tag)],
|
2019-11-12 07:07:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn t_sort(
|
|
|
|
group_by_name: Option<String>,
|
|
|
|
split_by_name: Option<String>,
|
2019-11-21 14:33:14 +00:00
|
|
|
value: &Value,
|
2019-11-12 07:07:43 +00:00
|
|
|
tag: impl Into<Tag>,
|
2019-11-21 14:33:14 +00:00
|
|
|
) -> Result<Value, ShellError> {
|
2019-11-12 07:07:43 +00:00
|
|
|
let origin_tag = tag.into();
|
|
|
|
|
|
|
|
match group_by_name {
|
|
|
|
Some(column_name) => {
|
2019-11-04 15:47:03 +00:00
|
|
|
let sorted_labels: Vec<Tagged<String>> =
|
|
|
|
columns_sorted(Some(column_name), value, &origin_tag);
|
2019-11-12 07:07:43 +00:00
|
|
|
|
|
|
|
match split_by_name {
|
|
|
|
None => {
|
|
|
|
let mut dataset = TaggedDictBuilder::new(&origin_tag);
|
2019-11-21 14:33:14 +00:00
|
|
|
dataset.insert_value("default", value.clone());
|
|
|
|
let dataset = dataset.into_value();
|
2019-11-12 07:07:43 +00:00
|
|
|
|
2019-11-04 15:47:03 +00:00
|
|
|
let split_labels: Vec<Tagged<String>> = match &dataset {
|
2019-11-21 14:33:14 +00:00
|
|
|
Value {
|
|
|
|
value: UntaggedValue::Row(rows),
|
2019-11-12 07:07:43 +00:00
|
|
|
..
|
|
|
|
} => {
|
2019-11-04 15:47:03 +00:00
|
|
|
let mut keys: Vec<Tagged<String>> = rows
|
2019-11-12 07:07:43 +00:00
|
|
|
.entries
|
|
|
|
.keys()
|
2019-11-04 15:47:03 +00:00
|
|
|
.map(|k| k.clone().tagged_unknown())
|
2019-11-12 07:07:43 +00:00
|
|
|
.collect();
|
|
|
|
|
|
|
|
keys.sort();
|
|
|
|
|
2019-11-04 15:47:03 +00:00
|
|
|
keys
|
2019-11-12 07:07:43 +00:00
|
|
|
}
|
|
|
|
_ => vec![],
|
|
|
|
};
|
|
|
|
|
2019-11-21 14:33:14 +00:00
|
|
|
let results: Vec<Vec<Value>> = split_labels
|
2019-11-04 15:47:03 +00:00
|
|
|
.iter()
|
2019-11-12 07:07:43 +00:00
|
|
|
.map(|split| {
|
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 02:30:48 +00:00
|
|
|
let groups = get_data_by_key(&dataset, split.borrow_spanned());
|
2019-11-12 07:07:43 +00:00
|
|
|
|
|
|
|
sorted_labels
|
|
|
|
.clone()
|
|
|
|
.into_iter()
|
2019-11-04 15:47:03 +00:00
|
|
|
.map(|label| match &groups {
|
2019-11-21 14:33:14 +00:00
|
|
|
Some(Value {
|
|
|
|
value: UntaggedValue::Row(dict),
|
2019-11-04 15:47:03 +00:00
|
|
|
..
|
2019-12-31 07:36:08 +00:00
|
|
|
}) => dict.get_data_by_key(label.borrow_spanned()).unwrap(),
|
2019-11-21 14:33:14 +00:00
|
|
|
_ => UntaggedValue::Table(vec![]).into_value(&origin_tag),
|
2019-11-12 07:07:43 +00:00
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let mut outer = TaggedListBuilder::new(&origin_tag);
|
|
|
|
|
|
|
|
for i in results {
|
2019-11-21 14:33:14 +00:00
|
|
|
outer.push_value(UntaggedValue::Table(i).into_value(&origin_tag));
|
2019-11-12 07:07:43 +00:00
|
|
|
}
|
|
|
|
|
2019-12-06 15:28:26 +00:00
|
|
|
Ok(UntaggedValue::Table(outer.list).into_value(&origin_tag))
|
2019-11-12 07:07:43 +00:00
|
|
|
}
|
2019-12-06 15:28:26 +00:00
|
|
|
Some(_) => Ok(UntaggedValue::nothing().into_value(&origin_tag)),
|
2019-11-12 07:07:43 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-06 15:28:26 +00:00
|
|
|
None => Ok(UntaggedValue::nothing().into_value(&origin_tag)),
|
2019-11-12 07:07:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
use crate::commands::group_by::group;
|
|
|
|
use crate::commands::t_sort_by::{columns_sorted, t_sort};
|
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 02:30:48 +00:00
|
|
|
use crate::data::value;
|
2019-11-12 07:07:43 +00:00
|
|
|
use indexmap::IndexMap;
|
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 02:30:48 +00:00
|
|
|
use nu_protocol::{UntaggedValue, Value};
|
2019-11-21 14:33:14 +00:00
|
|
|
use nu_source::*;
|
2019-11-12 08:38:55 +00:00
|
|
|
|
2019-11-21 14:33:14 +00:00
|
|
|
fn string(input: impl Into<String>) -> Value {
|
2019-12-04 19:52:31 +00:00
|
|
|
UntaggedValue::string(input.into()).into_untagged_value()
|
2019-11-12 07:07:43 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 14:33:14 +00:00
|
|
|
fn row(entries: IndexMap<String, Value>) -> Value {
|
2019-12-04 19:52:31 +00:00
|
|
|
UntaggedValue::row(entries).into_untagged_value()
|
2019-11-12 07:07:43 +00:00
|
|
|
}
|
|
|
|
|
2019-12-31 07:36:08 +00:00
|
|
|
fn table(list: &[Value]) -> Value {
|
2019-12-04 19:52:31 +00:00
|
|
|
UntaggedValue::table(list).into_untagged_value()
|
2019-11-12 07:07:43 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 14:33:14 +00:00
|
|
|
fn nu_releases_grouped_by_date() -> Value {
|
2019-11-12 07:07:43 +00:00
|
|
|
let key = String::from("date").tagged_unknown();
|
|
|
|
group(&key, nu_releases_commiters(), Tag::unknown()).unwrap()
|
|
|
|
}
|
|
|
|
|
2019-11-21 14:33:14 +00:00
|
|
|
fn nu_releases_commiters() -> Vec<Value> {
|
2019-11-12 07:07:43 +00:00
|
|
|
vec![
|
|
|
|
row(
|
|
|
|
indexmap! {"name".into() => string("AR"), "country".into() => string("EC"), "date".into() => string("August 23-2019")},
|
|
|
|
),
|
|
|
|
row(
|
|
|
|
indexmap! {"name".into() => string("JT"), "country".into() => string("NZ"), "date".into() => string("August 23-2019")},
|
|
|
|
),
|
|
|
|
row(
|
|
|
|
indexmap! {"name".into() => string("YK"), "country".into() => string("US"), "date".into() => string("October 10-2019")},
|
|
|
|
),
|
|
|
|
row(
|
|
|
|
indexmap! {"name".into() => string("AR"), "country".into() => string("EC"), "date".into() => string("September 24-2019")},
|
|
|
|
),
|
|
|
|
row(
|
|
|
|
indexmap! {"name".into() => string("JT"), "country".into() => string("NZ"), "date".into() => string("October 10-2019")},
|
|
|
|
),
|
|
|
|
row(
|
|
|
|
indexmap! {"name".into() => string("YK"), "country".into() => string("US"), "date".into() => string("September 24-2019")},
|
|
|
|
),
|
|
|
|
row(
|
|
|
|
indexmap! {"name".into() => string("AR"), "country".into() => string("EC"), "date".into() => string("October 10-2019")},
|
|
|
|
),
|
|
|
|
row(
|
|
|
|
indexmap! {"name".into() => string("JT"), "country".into() => string("NZ"), "date".into() => string("September 24-2019")},
|
|
|
|
),
|
|
|
|
row(
|
|
|
|
indexmap! {"name".into() => string("YK"), "country".into() => string("US"), "date".into() => string("August 23-2019")},
|
|
|
|
),
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn show_columns_sorted_given_a_column_to_sort_by() {
|
|
|
|
let by_column = String::from("date");
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
columns_sorted(
|
|
|
|
Some(by_column),
|
|
|
|
&nu_releases_grouped_by_date(),
|
|
|
|
Tag::unknown()
|
|
|
|
),
|
|
|
|
vec![
|
2019-12-31 07:36:08 +00:00
|
|
|
"August 23-2019".to_string().tagged_unknown(),
|
|
|
|
"September 24-2019".to_string().tagged_unknown(),
|
|
|
|
"October 10-2019".to_string().tagged_unknown()
|
2019-11-12 07:07:43 +00:00
|
|
|
]
|
|
|
|
)
|
|
|
|
}
|
2019-11-12 08:38:55 +00:00
|
|
|
|
2019-11-12 07:07:43 +00:00
|
|
|
#[test]
|
|
|
|
fn sorts_the_tables() {
|
|
|
|
let group_by = String::from("date");
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
t_sort(
|
|
|
|
Some(group_by),
|
|
|
|
None,
|
|
|
|
&nu_releases_grouped_by_date(),
|
|
|
|
Tag::unknown()
|
|
|
|
)
|
|
|
|
.unwrap(),
|
2019-12-31 07:36:08 +00:00
|
|
|
table(&[table(&[
|
|
|
|
table(&[
|
2019-11-12 07:07:43 +00:00
|
|
|
row(
|
|
|
|
indexmap! {"name".into() => string("AR"), "country".into() => string("EC"), "date".into() => string("August 23-2019")}
|
|
|
|
),
|
|
|
|
row(
|
|
|
|
indexmap! {"name".into() => string("JT"), "country".into() => string("NZ"), "date".into() => string("August 23-2019")}
|
|
|
|
),
|
|
|
|
row(
|
|
|
|
indexmap! {"name".into() => string("YK"), "country".into() => string("US"), "date".into() => string("August 23-2019")}
|
|
|
|
)
|
|
|
|
]),
|
2019-12-31 07:36:08 +00:00
|
|
|
table(&[
|
2019-11-12 07:07:43 +00:00
|
|
|
row(
|
|
|
|
indexmap! {"name".into() => string("AR"), "country".into() => string("EC"), "date".into() => string("September 24-2019")}
|
|
|
|
),
|
|
|
|
row(
|
|
|
|
indexmap! {"name".into() => string("YK"), "country".into() => string("US"), "date".into() => string("September 24-2019")}
|
|
|
|
),
|
|
|
|
row(
|
|
|
|
indexmap! {"name".into() => string("JT"), "country".into() => string("NZ"), "date".into() => string("September 24-2019")}
|
|
|
|
)
|
|
|
|
]),
|
2019-12-31 07:36:08 +00:00
|
|
|
table(&[
|
2019-11-12 07:07:43 +00:00
|
|
|
row(
|
|
|
|
indexmap! {"name".into() => string("YK"), "country".into() => string("US"), "date".into() => string("October 10-2019")}
|
|
|
|
),
|
|
|
|
row(
|
|
|
|
indexmap! {"name".into() => string("JT"), "country".into() => string("NZ"), "date".into() => string("October 10-2019")}
|
|
|
|
),
|
|
|
|
row(
|
|
|
|
indexmap! {"name".into() => string("AR"), "country".into() => string("EC"), "date".into() => string("October 10-2019")}
|
|
|
|
)
|
|
|
|
]),
|
|
|
|
]),])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|