2019-08-19 07:16:39 +02:00
|
|
|
use crate::commands::WholeStreamCommand;
|
2019-05-26 04:04:13 +02:00
|
|
|
use crate::prelude::*;
|
2020-05-18 14:56:01 +02: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 03:30:48 +01:00
|
|
|
use nu_errors::ShellError;
|
2019-12-04 20:52:31 +01:00
|
|
|
use nu_protocol::{ReturnSuccess, Signature, TaggedDictBuilder, UntaggedValue, Value};
|
2019-05-26 04:04:13 +02:00
|
|
|
|
2019-08-19 07:16:39 +02:00
|
|
|
pub struct Size;
|
|
|
|
|
2020-05-29 10:22:52 +02:00
|
|
|
#[async_trait]
|
2019-08-19 07:16:39 +02:00
|
|
|
impl WholeStreamCommand for Size {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"size"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
|
|
|
Signature::build("size")
|
|
|
|
}
|
2019-08-30 00:52:32 +02:00
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Gather word count statistics on the text."
|
|
|
|
}
|
|
|
|
|
2020-05-29 10:22:52 +02:00
|
|
|
async fn run(
|
2019-08-30 00:52:32 +02:00
|
|
|
&self,
|
|
|
|
args: CommandArgs,
|
|
|
|
registry: &CommandRegistry,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
|
|
|
size(args, registry)
|
|
|
|
}
|
2020-05-12 17:54:29 +02:00
|
|
|
|
2020-05-18 14:56:01 +02:00
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
|
|
vec![Example {
|
2020-05-12 17:54:29 +02:00
|
|
|
description: "Count the number of words in a string",
|
|
|
|
example: r#"echo "There are seven words in this sentence" | size"#,
|
2020-05-18 14:56:01 +02:00
|
|
|
result: Some(vec![UntaggedValue::row(indexmap! {
|
|
|
|
"lines".to_string() => UntaggedValue::int(0).into(),
|
|
|
|
"words".to_string() => UntaggedValue::int(7).into(),
|
|
|
|
"chars".to_string() => UntaggedValue::int(38).into(),
|
2020-09-01 04:51:35 +02:00
|
|
|
"bytes".to_string() => UntaggedValue::int(38).into(),
|
2020-05-18 14:56:01 +02:00
|
|
|
})
|
|
|
|
.into()]),
|
2020-05-12 17:54:29 +02:00
|
|
|
}]
|
|
|
|
}
|
2019-08-19 07:16:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn size(args: CommandArgs, _registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
|
2019-07-16 21:10:25 +02:00
|
|
|
let input = args.input;
|
2019-09-14 18:30:24 +02:00
|
|
|
let tag = args.call_info.name_tag;
|
2019-11-21 15:33:14 +01:00
|
|
|
let name_span = tag.span;
|
|
|
|
|
2019-07-16 21:10:25 +02:00
|
|
|
Ok(input
|
2019-12-03 07:44:59 +01:00
|
|
|
.map(move |v| {
|
|
|
|
if let Ok(s) = v.as_string() {
|
|
|
|
ReturnSuccess::value(count(&s, &v.tag))
|
|
|
|
} else {
|
|
|
|
Err(ShellError::labeled_error_with_secondary(
|
|
|
|
"Expected a string from pipeline",
|
|
|
|
"requires string input",
|
|
|
|
name_span,
|
|
|
|
"value originates from here",
|
|
|
|
v.tag.span,
|
|
|
|
))
|
2019-11-21 15:33:14 +01:00
|
|
|
}
|
2019-07-16 21:10:25 +02:00
|
|
|
})
|
|
|
|
.to_output_stream())
|
2019-05-26 04:04:13 +02:00
|
|
|
}
|
|
|
|
|
2019-11-21 15:33:14 +01:00
|
|
|
fn count(contents: &str, tag: impl Into<Tag>) -> Value {
|
2019-05-26 04:04:13 +02:00
|
|
|
let mut lines: i64 = 0;
|
|
|
|
let mut words: i64 = 0;
|
|
|
|
let mut chars: i64 = 0;
|
2019-05-27 00:38:26 +02:00
|
|
|
let bytes = contents.len() as i64;
|
2019-05-26 04:04:13 +02:00
|
|
|
let mut end_of_word = true;
|
|
|
|
|
|
|
|
for c in contents.chars() {
|
|
|
|
chars += 1;
|
|
|
|
|
|
|
|
match c {
|
|
|
|
'\n' => {
|
|
|
|
lines += 1;
|
|
|
|
end_of_word = true;
|
|
|
|
}
|
|
|
|
' ' => end_of_word = true,
|
|
|
|
_ => {
|
|
|
|
if end_of_word {
|
|
|
|
words += 1;
|
|
|
|
}
|
|
|
|
end_of_word = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-05 10:54:29 +02:00
|
|
|
let mut dict = TaggedDictBuilder::new(tag);
|
2019-09-14 18:30:24 +02:00
|
|
|
//TODO: add back in name when we have it in the tag
|
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
|
|
|
//dict.insert("name", value::string(name));
|
2019-12-04 20:52:31 +01:00
|
|
|
dict.insert_untagged("lines", UntaggedValue::int(lines));
|
|
|
|
dict.insert_untagged("words", UntaggedValue::int(words));
|
|
|
|
dict.insert_untagged("chars", UntaggedValue::int(chars));
|
2020-09-01 04:51:35 +02:00
|
|
|
dict.insert_untagged("bytes", UntaggedValue::int(bytes));
|
2019-05-26 04:04:13 +02:00
|
|
|
|
2019-11-21 15:33:14 +01:00
|
|
|
dict.into_value()
|
2019-05-26 04:04:13 +02:00
|
|
|
}
|
2020-05-18 14:56:01 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::Size;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn examples_work_as_expected() {
|
|
|
|
use crate::examples::test as test_examples;
|
|
|
|
|
|
|
|
test_examples(Size {})
|
|
|
|
}
|
|
|
|
}
|