headers plugin

This commit is contained in:
Sam Hedin 2020-03-27 20:24:47 +01:00
parent 06f87cfbe8
commit 509a856679
9 changed files with 108 additions and 0 deletions

10
Cargo.lock generated
View File

@ -2500,6 +2500,16 @@ dependencies = [
"url",
]
[[package]]
name = "nu_plugin_headers"
version = "0.1.0"
dependencies = [
"nu-errors",
"nu-plugin",
"nu-protocol",
"nu-source",
]
[[package]]
name = "nu_plugin_inc"
version = "0.11.0"

View File

@ -0,0 +1,39 @@
use crate::commands::WholeStreamCommand;
use crate::context::CommandRegistry;
use crate::prelude::*;
use futures::stream::StreamExt;
use nu_errors::ShellError;
use nu_protocol::{ReturnSuccess, Signature, UntaggedValue, Value};
pub struct Headers;
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 headers"
}
fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
args.process(registry, count)?.run()
}
}
pub fn count(
CountArgs {}: CountArgs,
RunnableContext { input, name, .. }: RunnableContext,
) -> Result<OutputStream, ShellError> {
let stream = async_stream! {
let rows: Vec<Value> = input.values.collect().await;
yield ReturnSuccess::value(UntaggedValue::int(rows.len()).into_value(name))
};
Ok(stream.to_output_stream())
}

View File

View File

@ -0,0 +1,13 @@
[package]
name = "nu_plugin_headers"
version = "0.1.0"
authors = ["Sam Hedin <sam.hedin@gmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
nu-plugin = { path = "../nu-plugin", version = "0.11.0" }
nu-protocol = { path = "../nu-protocol", version = "0.11.0" }
nu-source = { path = "../nu-source", version = "0.11.0" }
nu-errors = { path = "../nu-errors", version = "0.11.0" }

View File

@ -0,0 +1,2 @@
out:
cargo install --path . --force

View File

@ -0,0 +1,18 @@
pub struct Headers;
impl Headers {
pub fn new() -> Headers {
Headers
}
}
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 headers"
}
}

View File

@ -0,0 +1,3 @@
mod nu;
mod headers;
pub use headers::Headers;

View File

@ -0,0 +1,7 @@
use nu_plugin::serve_plugin;
use nu_plugin_headers::Headers;
//https://github.com/nushell/nushell/issues/1486
fn main() {
serve_plugin(&mut Headers::new())
}

View File

@ -0,0 +1,16 @@
use crate::headers::Headers;
use nu_errors::ShellError;
use nu_plugin::Plugin;
use nu_protocol::{CallInfo, ReturnSuccess, ReturnValue, Signature, Value};
impl Plugin for Headers {
fn config(&mut self) -> Result<Signature, ShellError> {
Ok(Signature::build("headers")
.desc("Use the first row of the table as headers")
.filter())
}
fn begin_filter(&mut self, call_info: CallInfo) -> Result<Vec<ReturnValue>, ShellError> {
Ok(vec![])
}
}