nushell/src/commands/size.rs

55 lines
1.6 KiB
Rust
Raw Normal View History

2019-05-26 04:04:13 +02:00
use crate::errors::ShellError;
2019-08-01 03:58:42 +02:00
use crate::object::{TaggedDictBuilder, Value};
2019-05-26 04:04:13 +02:00
use crate::prelude::*;
pub fn size(args: CommandArgs) -> Result<OutputStream, ShellError> {
2019-07-16 21:10:25 +02:00
let input = args.input;
Ok(input
.values
.map(move |v| match v.item {
2019-08-01 03:58:42 +02:00
Value::Primitive(Primitive::String(ref s)) => ReturnSuccess::value(count(s, v.span())),
2019-07-16 21:10:25 +02:00
_ => Err(ShellError::maybe_labeled_error(
"Expected string values from pipeline",
"expects strings from pipeline",
2019-08-01 03:58:42 +02:00
Some(v.span()),
2019-07-16 21:10:25 +02:00
)),
})
.to_output_stream())
2019-05-26 04:04:13 +02:00
}
2019-08-01 03:58:42 +02:00
fn count(contents: &str, span: impl Into<Span>) -> Tagged<Value> {
2019-05-26 04:04:13 +02:00
let mut lines: i64 = 0;
let mut words: i64 = 0;
let mut chars: i64 = 0;
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-01 03:58:42 +02:00
let mut dict = TaggedDictBuilder::new(span);
2019-07-16 21:10:25 +02:00
//TODO: add back in name when we have it in the span
//dict.insert("name", Value::string(name));
2019-07-09 06:31:26 +02: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 04:04:13 +02:00
2019-08-01 03:58:42 +02:00
dict.into_tagged_value()
2019-05-26 04:04:13 +02:00
}