2019-08-19 05:16:39 +00:00
|
|
|
use crate::commands::WholeStreamCommand;
|
2019-09-05 16:23:42 +00:00
|
|
|
use crate::data::{TaggedDictBuilder, Value};
|
2019-09-11 14:36:50 +00:00
|
|
|
use crate::errors::ShellError;
|
2019-05-26 02:04:13 +00:00
|
|
|
use crate::prelude::*;
|
|
|
|
|
2019-08-19 05:16:39 +00:00
|
|
|
pub struct Size;
|
|
|
|
|
|
|
|
impl WholeStreamCommand for Size {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"size"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
|
|
|
Signature::build("size")
|
|
|
|
}
|
2019-08-29 22:52:32 +00:00
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Gather word count statistics on the text."
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
args: CommandArgs,
|
|
|
|
registry: &CommandRegistry,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
|
|
|
size(args, registry)
|
|
|
|
}
|
2019-08-19 05:16:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn size(args: CommandArgs, _registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
|
2019-07-16 19:10:25 +00:00
|
|
|
let input = args.input;
|
2019-09-14 16:30:24 +00:00
|
|
|
let tag = args.call_info.name_tag;
|
2019-07-16 19:10:25 +00:00
|
|
|
Ok(input
|
|
|
|
.values
|
|
|
|
.map(move |v| match v.item {
|
2019-08-05 08:54:29 +00:00
|
|
|
Value::Primitive(Primitive::String(ref s)) => ReturnSuccess::value(count(s, v.tag())),
|
|
|
|
_ => Err(ShellError::labeled_error_with_secondary(
|
|
|
|
"Expected a string from pipeline",
|
|
|
|
"requires string input",
|
2019-09-14 16:30:24 +00:00
|
|
|
tag,
|
2019-08-05 08:54:29 +00:00
|
|
|
"value originates from here",
|
2019-09-14 16:30:24 +00:00
|
|
|
v.tag(),
|
2019-07-16 19:10:25 +00:00
|
|
|
)),
|
|
|
|
})
|
|
|
|
.to_output_stream())
|
2019-05-26 02:04:13 +00:00
|
|
|
}
|
|
|
|
|
2019-08-05 08:54:29 +00:00
|
|
|
fn count(contents: &str, tag: impl Into<Tag>) -> Tagged<Value> {
|
2019-05-26 02:04:13 +00:00
|
|
|
let mut lines: i64 = 0;
|
|
|
|
let mut words: i64 = 0;
|
|
|
|
let mut chars: i64 = 0;
|
2019-05-26 22:38:26 +00:00
|
|
|
let bytes = contents.len() as i64;
|
2019-05-26 02:04:13 +00: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 08:54:29 +00:00
|
|
|
let mut dict = TaggedDictBuilder::new(tag);
|
2019-09-14 16:30:24 +00:00
|
|
|
//TODO: add back in name when we have it in the tag
|
2019-07-16 19:10:25 +00:00
|
|
|
//dict.insert("name", Value::string(name));
|
2019-07-09 04:31:26 +00:00
|
|
|
dict.insert("lines", Value::int(lines));
|
|
|
|
dict.insert("words", Value::int(words));
|
|
|
|
dict.insert("chars", Value::int(chars));
|
|
|
|
dict.insert("max length", Value::int(bytes));
|
2019-05-26 02:04:13 +00:00
|
|
|
|
2019-08-01 01:58:42 +00:00
|
|
|
dict.into_tagged_value()
|
2019-05-26 02:04:13 +00:00
|
|
|
}
|