* move commands, futures.rs, script.rs, utils * move over maybe_print_errors * add nu_command crate references to nu_cli * in commands.rs open up to pub mod from pub(crate) * nu-cli, nu-command, and nu tests are now passing * cargo fmt * clean up nu-cli/src/prelude.rs * code cleanup * for some reason lex.rs was not formatted, may be causing my error * remove mod completion from lib.rs which was not being used along with quickcheck macros * add in allow unused imports * comment out one failing external test; comment out one failing internal test * revert commenting out failing tests; something else might be going on; someone with a windows machine should check and see what is going on with these failing windows tests * Update Cargo.toml Extend the optional features to nu-command Co-authored-by: Jonathan Turner <jonathandturner@users.noreply.github.com>
120 lines
3.8 KiB
Rust
120 lines
3.8 KiB
Rust
use crate::prelude::*;
|
|
use futures::stream::StreamExt;
|
|
use indexmap::IndexMap;
|
|
use nu_engine::WholeStreamCommand;
|
|
use nu_errors::ShellError;
|
|
use nu_protocol::Dictionary;
|
|
use nu_protocol::{Primitive, ReturnSuccess, Signature, UntaggedValue, Value};
|
|
|
|
pub struct Headers;
|
|
|
|
#[async_trait]
|
|
impl WholeStreamCommand for Headers {
|
|
fn name(&self) -> &str {
|
|
"headers"
|
|
}
|
|
|
|
fn signature(&self) -> Signature {
|
|
Signature::build("headers")
|
|
}
|
|
|
|
fn usage(&self) -> &str {
|
|
"Use the first row of the table as column names"
|
|
}
|
|
|
|
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
|
|
headers(args).await
|
|
}
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
vec![
|
|
Example {
|
|
description: "Create headers for a raw string",
|
|
example: r#"echo "a b c|1 2 3" | split row "|" | split column " " | headers"#,
|
|
result: None,
|
|
},
|
|
Example {
|
|
description: "Don't panic on rows with different headers",
|
|
example: r#"echo "a b c|1 2 3|1 2 3 4" | split row "|" | split column " " | headers"#,
|
|
result: None,
|
|
},
|
|
]
|
|
}
|
|
}
|
|
|
|
pub async fn headers(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
|
let input = args.input;
|
|
let rows: Vec<Value> = input.collect().await;
|
|
|
|
if rows.is_empty() {
|
|
return Err(ShellError::untagged_runtime_error(
|
|
"Couldn't find headers, was the input a properly formatted, non-empty table?",
|
|
));
|
|
}
|
|
|
|
//the headers are the first row in the table
|
|
let headers: Vec<String> = match &rows[0].value {
|
|
UntaggedValue::Row(d) => {
|
|
Ok(d.entries
|
|
.iter()
|
|
.map(|(k, v)| {
|
|
match v.as_string() {
|
|
Ok(s) => s,
|
|
Err(_) => {
|
|
//If a cell that should contain a header name is empty, we name the column Column[index]
|
|
match d.entries.get_full(k) {
|
|
Some((index, _, _)) => format!("Column{}", index),
|
|
None => "unknownColumn".to_string(),
|
|
}
|
|
}
|
|
}
|
|
})
|
|
.collect())
|
|
}
|
|
_ => Err(ShellError::unexpected_eof(
|
|
"Could not get headers, is the table empty?",
|
|
rows[0].tag.span,
|
|
)),
|
|
}?;
|
|
|
|
Ok(
|
|
futures::stream::iter(rows.into_iter().skip(1).map(move |r| {
|
|
//Each row is a dictionary with the headers as keys
|
|
match &r.value {
|
|
UntaggedValue::Row(d) => {
|
|
let mut entries = IndexMap::new();
|
|
for (i, header) in headers.iter().enumerate() {
|
|
let value = match d.entries.get_index(i) {
|
|
Some((_, value)) => value.clone(),
|
|
None => UntaggedValue::Primitive(Primitive::Nothing).into(),
|
|
};
|
|
|
|
entries.insert(header.clone(), value);
|
|
}
|
|
Ok(ReturnSuccess::Value(
|
|
UntaggedValue::Row(Dictionary { entries }).into_value(r.tag.clone()),
|
|
))
|
|
}
|
|
_ => Err(ShellError::unexpected_eof(
|
|
"Couldn't iterate through rows, was the input a properly formatted table?",
|
|
r.tag.span,
|
|
)),
|
|
}
|
|
}))
|
|
.to_output_stream(),
|
|
)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::Headers;
|
|
use super::ShellError;
|
|
|
|
#[test]
|
|
fn examples_work_as_expected() -> Result<(), ShellError> {
|
|
use crate::examples::test as test_examples;
|
|
|
|
Ok(test_examples(Headers {})?)
|
|
}
|
|
}
|