mirror of
https://github.com/nushell/nushell.git
synced 2025-04-18 18:28:19 +02:00
Add ability to extract headers from first row
This commit is contained in:
parent
c05511f6c1
commit
ec0bd20f29
@ -1,9 +1,12 @@
|
|||||||
use crate::commands::WholeStreamCommand;
|
use crate::commands::WholeStreamCommand;
|
||||||
|
use indexmap::IndexMap;
|
||||||
use crate::context::CommandRegistry;
|
use crate::context::CommandRegistry;
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use futures::stream::StreamExt;
|
use futures::stream::StreamExt;
|
||||||
use nu_errors::ShellError;
|
use nu_errors::ShellError;
|
||||||
use nu_protocol::{ReturnSuccess, Signature, UntaggedValue, Value};
|
use nu_protocol::{ReturnSuccess, Signature, UntaggedValue, Value};
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io::Write;
|
||||||
|
|
||||||
pub struct Headers;
|
pub struct Headers;
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
@ -27,13 +30,37 @@ impl WholeStreamCommand for Headers {
|
|||||||
args.process(registry, count)?.run()
|
args.process(registry, count)?.run()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Rows is an array of dictionaries. Each dictionary maps header to content for that row.
|
||||||
|
//Take the first row and extract all content and save them as headers.
|
||||||
|
//Take the rest of the rows and replace the old column names with the new headers.
|
||||||
|
|
||||||
pub fn count(
|
pub fn count(
|
||||||
HeadersArgs {}: HeadersArgs,
|
HeadersArgs {}: HeadersArgs,
|
||||||
RunnableContext { input, name, .. }: RunnableContext,
|
RunnableContext { input, name, .. }: RunnableContext,
|
||||||
) -> Result<OutputStream, ShellError> {
|
) -> Result<OutputStream, ShellError> {
|
||||||
|
|
||||||
|
|
||||||
let stream = async_stream! {
|
let stream = async_stream! {
|
||||||
let rows: Vec<Value> = input.values.collect().await;
|
let rows: Vec<Value> = input.values.collect().await;
|
||||||
|
|
||||||
|
let mut entries = IndexMap::new();
|
||||||
|
let headers: Value = rows[0].clone();
|
||||||
|
match &headers.value {
|
||||||
|
UntaggedValue::Row(d) => {
|
||||||
|
entries = d.entries.clone();
|
||||||
|
()
|
||||||
|
}
|
||||||
|
_ => ()
|
||||||
|
}
|
||||||
|
let mut heads = vec![];
|
||||||
|
for (k, v) in entries.iter() {
|
||||||
|
heads.push(v.as_string().unwrap());
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut file = File::create("headout").unwrap();
|
||||||
|
write!(file, "args: {:#?}", heads);
|
||||||
|
|
||||||
yield ReturnSuccess::value(UntaggedValue::int(rows.len()).into_value(name))
|
yield ReturnSuccess::value(UntaggedValue::int(rows.len()).into_value(name))
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user