mirror of
https://github.com/nushell/nushell.git
synced 2024-11-22 16:33:37 +01:00
plugin show signature (#6126)
* plugin show signature * remove expect from macro * use fold to create string
This commit is contained in:
parent
b12a3dd0e5
commit
6b4e577032
@ -1,8 +1,11 @@
|
||||
mod declaration;
|
||||
pub use declaration::PluginDeclaration;
|
||||
use nu_engine::documentation::get_flags_section;
|
||||
|
||||
use crate::protocol::{CallInput, LabeledError, PluginCall, PluginData, PluginResponse};
|
||||
use crate::EncodingType;
|
||||
use std::env;
|
||||
use std::fmt::Write;
|
||||
use std::io::BufReader;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::process::{Child, Command as CommandSys, Stdio};
|
||||
@ -15,6 +18,8 @@ use super::EvaluatedCall;
|
||||
pub(crate) const OUTPUT_BUFFER_SIZE: usize = 8192;
|
||||
|
||||
pub trait PluginEncoder: Clone {
|
||||
fn name(&self) -> &str;
|
||||
|
||||
fn encode_call(
|
||||
&self,
|
||||
plugin_call: &PluginCall,
|
||||
@ -183,6 +188,11 @@ pub trait Plugin {
|
||||
// That should be encoded correctly and sent to StdOut for nushell to decode and
|
||||
// and present its result
|
||||
pub fn serve_plugin(plugin: &mut impl Plugin, encoder: impl PluginEncoder) {
|
||||
if env::args().any(|arg| (arg == "-h") || (arg == "--help")) {
|
||||
print_help(plugin, encoder);
|
||||
std::process::exit(0)
|
||||
}
|
||||
|
||||
let mut stdin_buf = BufReader::with_capacity(OUTPUT_BUFFER_SIZE, std::io::stdin());
|
||||
let plugin_call = encoder.decode_call(&mut stdin_buf);
|
||||
|
||||
@ -253,3 +263,69 @@ pub fn serve_plugin(plugin: &mut impl Plugin, encoder: impl PluginEncoder) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn print_help(plugin: &mut impl Plugin, encoder: impl PluginEncoder) {
|
||||
println!("Nushell Plugin");
|
||||
println!("Encoder: {}", encoder.name());
|
||||
|
||||
let mut help = String::new();
|
||||
|
||||
plugin.signature().iter().for_each(|signature| {
|
||||
let res = write!(help, "\nCommand: {}", signature.name)
|
||||
.and_then(|_| writeln!(help, "\nUsage:\n > {}", signature.usage))
|
||||
.and_then(|_| {
|
||||
if !signature.extra_usage.is_empty() {
|
||||
writeln!(help, "\nExtra usage:\n > {}", signature.extra_usage)
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
})
|
||||
.and_then(|_| {
|
||||
let flags = get_flags_section(signature);
|
||||
write!(help, "{}", flags)
|
||||
})
|
||||
.and_then(|_| writeln!(help, "\nParameters:"))
|
||||
.and_then(|_| {
|
||||
signature
|
||||
.required_positional
|
||||
.iter()
|
||||
.try_for_each(|positional| {
|
||||
writeln!(
|
||||
help,
|
||||
" {} <{:?}>: {}",
|
||||
positional.name, positional.shape, positional.desc
|
||||
)
|
||||
})
|
||||
})
|
||||
.and_then(|_| {
|
||||
signature
|
||||
.optional_positional
|
||||
.iter()
|
||||
.try_for_each(|positional| {
|
||||
writeln!(
|
||||
help,
|
||||
" (optional) {} <{:?}>: {}",
|
||||
positional.name, positional.shape, positional.desc
|
||||
)
|
||||
})
|
||||
})
|
||||
.and_then(|_| {
|
||||
if let Some(rest_positional) = &signature.rest_positional {
|
||||
writeln!(
|
||||
help,
|
||||
" ...{} <{:?}>: {}",
|
||||
rest_positional.name, rest_positional.shape, rest_positional.desc
|
||||
)
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
})
|
||||
.and_then(|_| writeln!(help, "======================"));
|
||||
|
||||
if res.is_err() {
|
||||
println!("{:?}", res)
|
||||
}
|
||||
});
|
||||
|
||||
println!("{}", help)
|
||||
}
|
||||
|
@ -13,6 +13,10 @@ use crate::{plugin::PluginEncoder, protocol::PluginResponse};
|
||||
pub struct CapnpSerializer;
|
||||
|
||||
impl PluginEncoder for CapnpSerializer {
|
||||
fn name(&self) -> &str {
|
||||
"Capnp Serializer"
|
||||
}
|
||||
|
||||
fn encode_call(
|
||||
&self,
|
||||
plugin_call: &crate::protocol::PluginCall,
|
||||
|
@ -6,6 +6,10 @@ use crate::{plugin::PluginEncoder, protocol::PluginResponse};
|
||||
pub struct JsonSerializer;
|
||||
|
||||
impl PluginEncoder for JsonSerializer {
|
||||
fn name(&self) -> &str {
|
||||
"Json Serializer"
|
||||
}
|
||||
|
||||
fn encode_call(
|
||||
&self,
|
||||
plugin_call: &crate::protocol::PluginCall,
|
||||
|
Loading…
Reference in New Issue
Block a user