mirror of
https://github.com/nushell/nushell.git
synced 2024-12-22 15:13:01 +01:00
commit
94a6c754ac
@ -48,6 +48,7 @@ pub async fn cli() -> Result<(), Box<Error>> {
|
|||||||
("view", Arc::new(view::view)),
|
("view", Arc::new(view::view)),
|
||||||
("skip", Arc::new(skip::skip)),
|
("skip", Arc::new(skip::skip)),
|
||||||
("first", Arc::new(take::take)),
|
("first", Arc::new(take::take)),
|
||||||
|
("size", Arc::new(size::size)),
|
||||||
("from-json", Arc::new(from_json::from_json)),
|
("from-json", Arc::new(from_json::from_json)),
|
||||||
("open", Arc::new(open::open)),
|
("open", Arc::new(open::open)),
|
||||||
("column", Arc::new(select::select)),
|
("column", Arc::new(select::select)),
|
||||||
|
@ -8,6 +8,7 @@ crate mod open;
|
|||||||
crate mod ps;
|
crate mod ps;
|
||||||
crate mod reject;
|
crate mod reject;
|
||||||
crate mod select;
|
crate mod select;
|
||||||
|
crate mod size;
|
||||||
crate mod skip;
|
crate mod skip;
|
||||||
crate mod sort_by;
|
crate mod sort_by;
|
||||||
crate mod split;
|
crate mod split;
|
||||||
|
62
src/commands/size.rs
Normal file
62
src/commands/size.rs
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
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.args.is_empty() {
|
||||||
|
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.args {
|
||||||
|
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));
|
||||||
|
contents.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(list.boxed())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn count(name: &str, contents: &str) -> ReturnValue {
|
||||||
|
let mut lines: i64 = 0;
|
||||||
|
let mut words: i64 = 0;
|
||||||
|
let mut chars: i64 = 0;
|
||||||
|
let bytes = contents.len() as i64;
|
||||||
|
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))
|
||||||
|
}
|
@ -19,6 +19,7 @@ impl Completer for NuCompleter {
|
|||||||
pos: usize,
|
pos: usize,
|
||||||
context: &rustyline::Context,
|
context: &rustyline::Context,
|
||||||
) -> rustyline::Result<(usize, Vec<completion::Pair>)> {
|
) -> rustyline::Result<(usize, Vec<completion::Pair>)> {
|
||||||
|
|
||||||
let commands: Vec<String> = self.commands.keys().cloned().collect();
|
let commands: Vec<String> = self.commands.keys().cloned().collect();
|
||||||
|
|
||||||
let mut completions = self.file_completer.complete(line, pos, context)?.1;
|
let mut completions = self.file_completer.complete(line, pos, context)?.1;
|
||||||
|
Loading…
Reference in New Issue
Block a user