nushell/src/commands/size.rs

63 lines
1.7 KiB
Rust
Raw Normal View History

2019-05-26 04:04:13 +02:00
use crate::errors::ShellError;
use crate::object::dict::Dictionary;
use crate::object::Value;
use crate::prelude::*;
use std::fs::File;
use std::io::prelude::*;
pub fn size(args: CommandArgs) -> Result<OutputStream, ShellError> {
if args.positional.is_empty() {
2019-05-26 04:04:13 +02:00
return Err(ShellError::string("size requires at least one file"));
}
let cwd = args.env.lock().unwrap().cwd().to_path_buf();
let mut contents = String::new();
let mut list = VecDeque::new();
for name in args.positional {
2019-05-26 04:04:13 +02:00
let name = name.as_string()?;
let path = cwd.join(&name);
let mut file = File::open(path)?;
file.read_to_string(&mut contents)?;
list.push_back(count(&name, &contents));
2019-05-26 04:04:13 +02:00
contents.clear();
}
Ok(list.boxed())
}
fn count(name: &str, contents: &str) -> ReturnValue {
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;
}
}
}
let mut dict = Dictionary::default();
dict.add("name", Value::string(name.to_owned()));
dict.add("lines", Value::int(lines));
dict.add("words", Value::int(words));
dict.add("chars", Value::int(chars));
dict.add("max length", Value::int(bytes));
ReturnValue::Value(Value::Object(dict))
}