Some touchups to size

This commit is contained in:
JT 2021-10-13 17:15:37 +13:00
parent ef58348ea2
commit 5c46138563

View file

@ -1,13 +1,10 @@
extern crate unicode_segmentation; extern crate unicode_segmentation;
use std::collections::HashMap;
// use indexmap::indexmap;
use unicode_segmentation::UnicodeSegmentation; use unicode_segmentation::UnicodeSegmentation;
use nu_protocol::ast::Call; use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EvaluationContext}; use nu_protocol::engine::{Command, EvaluationContext};
use nu_protocol::{ShellError, Signature, Span, Spanned, Type, Value}; use nu_protocol::{Example, ShellError, Signature, Span, Type, Value};
pub struct Size; pub struct Size;
@ -33,32 +30,72 @@ impl Command for Size {
size(context, call, input) size(context, call, input)
} }
// fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {
// vec![ vec![
// Example { Example {
// description: "Count the number of words in a string", description: "Count the number of words in a string",
// example: r#"echo "There are seven words in this sentence" | size"#, example: r#""There are seven words in this sentence" | size"#,
// result: Some(vec![Value::row(indexmap! { result: Some(Value::Record {
// "lines".to_string() => UntaggedValue::int(0).into(), cols: vec![
// "words".to_string() => UntaggedValue::int(7).into(), "lines".into(),
// "chars".to_string() => UntaggedValue::int(38).into(), "words".into(),
// "bytes".to_string() => UntaggedValue::int(38).into(), "chars".into(),
// }) "bytes".into(),
// .into()]), ],
// }, vals: vec![
// Example { Value::Int {
// description: "Counts Unicode characters correctly in a string", val: 0,
// example: r#"echo "Amélie Amelie" | size"#, span: Span::unknown(),
// result: Some(vec![UntaggedValue::row(indexmap! { },
// "lines".to_string() => UntaggedValue::int(0).into(), Value::Int {
// "words".to_string() => UntaggedValue::int(2).into(), val: 7,
// "chars".to_string() => UntaggedValue::int(13).into(), span: Span::unknown(),
// "bytes".to_string() => UntaggedValue::int(15).into(), },
// }) Value::Int {
// .into()]), val: 38,
// }, span: Span::unknown(),
// ] },
// } Value::Int {
val: 38,
span: Span::unknown(),
},
],
span: Span::unknown(),
}),
},
Example {
description: "Counts Unicode characters correctly in a string",
example: r#""Amélie Amelie" | size"#,
result: Some(Value::Record {
cols: vec![
"lines".into(),
"words".into(),
"chars".into(),
"bytes".into(),
],
vals: vec![
Value::Int {
val: 0,
span: Span::unknown(),
},
Value::Int {
val: 2,
span: Span::unknown(),
},
Value::Int {
val: 13,
span: Span::unknown(),
},
Value::Int {
val: 15,
span: Span::unknown(),
},
],
span: Span::unknown(),
}),
},
]
}
} }
fn size(_context: &EvaluationContext, call: &Call, input: Value) -> Result<Value, ShellError> { fn size(_context: &EvaluationContext, call: &Call, input: Value) -> Result<Value, ShellError> {
@ -100,24 +137,32 @@ fn count(contents: &str, span: Span) -> Value {
} }
} }
let mut item: HashMap<String, Value> = HashMap::new(); let mut cols = vec![];
item.insert("lines".to_string(), Value::Int { val: lines, span }); let mut vals = vec![];
item.insert("words".to_string(), Value::Int { val: words, span });
item.insert("chars".to_string(), Value::Int { val: chars, span });
item.insert("bytes".to_string(), Value::Int { val: bytes, span });
Value::from(Spanned { item, span }) cols.push("lines".into());
vals.push(Value::Int { val: lines, span });
cols.push("words".into());
vals.push(Value::Int { val: words, span });
cols.push("chars".into());
vals.push(Value::Int { val: chars, span });
cols.push("bytes".into());
vals.push(Value::Int { val: bytes, span });
Value::Record { cols, vals, span }
} }
// #[cfg(test)] #[cfg(test)]
// mod tests { mod test {
// use super::ShellError; use super::*;
// use super::Size;
// #[test] #[test]
// fn examples_work_as_expected() -> Result<(), ShellError> { fn test_examples() {
// use crate::examples::test as test_examples; use crate::test_examples;
// test_examples(Size {}) test_examples(Size {})
// } }
// } }