2019-12-31 05:05:02 +01:00
|
|
|
use crate::commands::WholeStreamCommand;
|
|
|
|
use crate::context::CommandRegistry;
|
|
|
|
use crate::prelude::*;
|
2020-06-21 02:22:06 +02:00
|
|
|
use indexmap::map::IndexMap;
|
2019-12-31 05:05:02 +01:00
|
|
|
use nu_errors::ShellError;
|
2020-06-21 02:22:06 +02:00
|
|
|
use nu_protocol::Signature;
|
2019-12-31 05:05:02 +01:00
|
|
|
|
|
|
|
pub struct Uniq;
|
|
|
|
|
2020-05-29 10:22:52 +02:00
|
|
|
#[async_trait]
|
2019-12-31 05:05:02 +01:00
|
|
|
impl WholeStreamCommand for Uniq {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"uniq"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
2020-06-21 02:22:06 +02:00
|
|
|
Signature::build("uniq").switch("count", "Count the unique rows", Some('c'))
|
2019-12-31 05:05:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Return the unique rows"
|
|
|
|
}
|
|
|
|
|
2020-05-29 10:22:52 +02:00
|
|
|
async fn run(
|
2019-12-31 05:05:02 +01:00
|
|
|
&self,
|
|
|
|
args: CommandArgs,
|
|
|
|
registry: &CommandRegistry,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
2020-06-04 10:42:23 +02:00
|
|
|
uniq(args, registry).await
|
2019-12-31 05:05:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-21 02:22:06 +02:00
|
|
|
async fn uniq(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
|
|
|
|
let args = args.evaluate_once(®istry).await?;
|
|
|
|
let should_show_count = args.has("count");
|
2020-06-04 10:42:23 +02:00
|
|
|
let input = args.input;
|
2020-06-21 02:22:06 +02:00
|
|
|
let uniq_values = {
|
|
|
|
let mut counter = IndexMap::<nu_protocol::Value, usize>::new();
|
|
|
|
for line in input.into_vec().await {
|
|
|
|
*counter.entry(line).or_insert(0) += 1;
|
|
|
|
}
|
|
|
|
counter
|
|
|
|
};
|
2019-12-31 05:05:02 +01:00
|
|
|
|
2020-06-04 10:42:23 +02:00
|
|
|
let mut values_vec_deque = VecDeque::new();
|
2019-12-31 05:05:02 +01:00
|
|
|
|
2020-06-21 02:22:06 +02:00
|
|
|
if should_show_count {
|
|
|
|
for item in uniq_values {
|
|
|
|
use nu_protocol::{UntaggedValue, Value};
|
|
|
|
let value = {
|
|
|
|
match item.0.value {
|
|
|
|
UntaggedValue::Row(mut row) => {
|
|
|
|
row.entries.insert(
|
|
|
|
"count".to_string(),
|
|
|
|
UntaggedValue::int(item.1).into_untagged_value(),
|
|
|
|
);
|
|
|
|
Value {
|
|
|
|
value: UntaggedValue::Row(row),
|
|
|
|
tag: item.0.tag,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
UntaggedValue::Primitive(p) => {
|
|
|
|
let mut map = IndexMap::<String, Value>::new();
|
|
|
|
map.insert(
|
|
|
|
"value".to_string(),
|
|
|
|
UntaggedValue::Primitive(p).into_untagged_value(),
|
|
|
|
);
|
|
|
|
map.insert(
|
|
|
|
"count".to_string(),
|
|
|
|
UntaggedValue::int(item.1).into_untagged_value(),
|
|
|
|
);
|
|
|
|
Value {
|
|
|
|
value: UntaggedValue::row(map),
|
|
|
|
tag: item.0.tag,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
UntaggedValue::Table(_) => {
|
|
|
|
return Err(ShellError::labeled_error(
|
|
|
|
"uniq -c cannot operate on tables.",
|
|
|
|
"source",
|
|
|
|
item.0.tag.span,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
UntaggedValue::Error(_) | UntaggedValue::Block(_) => item.0,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
values_vec_deque.push_back(value);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for item in uniq_values {
|
|
|
|
values_vec_deque.push_back(item.0);
|
|
|
|
}
|
2020-06-04 10:42:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(futures::stream::iter(values_vec_deque).to_output_stream())
|
2019-12-31 05:05:02 +01:00
|
|
|
}
|
2020-05-18 14:56:01 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::Uniq;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn examples_work_as_expected() {
|
|
|
|
use crate::examples::test as test_examples;
|
|
|
|
|
|
|
|
test_examples(Uniq {})
|
|
|
|
}
|
|
|
|
}
|