Add search terms to Command and Signature (#4980)

* Add search terms to command

* Rename Signature desc to usage

To be named uniformly with extra_usage

* Throw in foldl search term for reduce

* Add missing usage to post

* Add search terms to signature

* Try to add capnp Signature serialization
This commit is contained in:
Jakub Žádník
2022-03-27 22:25:30 +03:00
committed by GitHub
parent 0c9dd6a29a
commit 2873e943b3
32 changed files with 269 additions and 100 deletions

View File

@ -17,7 +17,6 @@ impl Command for Do {
fn signature(&self) -> nu_protocol::Signature {
Signature::build("do")
.desc(self.usage())
.required("block", SyntaxShape::Any, "the block to run")
.switch(
"ignore-errors",

View File

@ -7,6 +7,8 @@ use nu_protocol::{
use nu_engine::{get_full_help, CallExt};
use std::borrow::Borrow;
#[derive(Clone)]
pub struct Help;
@ -96,14 +98,22 @@ fn help(
let mut vals = vec![];
let decl = engine_state.get_decl(decl_id);
let sig = decl.signature();
let sig = decl.signature().update_from_command(decl.borrow());
let key = sig.name;
let usage = sig.usage;
let search_terms = sig.search_terms;
let matches_term = if search_terms.is_empty() {
search_terms
.iter()
.any(|term| term.to_lowercase().contains(&search_string))
} else {
false
};
let key = sig.name.clone();
let c = sig.usage.clone();
let e = sig.extra_usage.clone();
if key.to_lowercase().contains(&search_string)
|| c.to_lowercase().contains(&search_string)
|| e.to_lowercase().contains(&search_string)
|| usage.to_lowercase().contains(&search_string)
|| matches_term
{
cols.push("name".into());
vals.push(Value::String {
@ -136,10 +146,20 @@ fn help(
});
cols.push("usage".into());
vals.push(Value::String { val: c, span: head });
vals.push(Value::String {
val: usage,
span: head,
});
cols.push("extra_usage".into());
vals.push(Value::String { val: e, span: head });
cols.push("search_terms".into());
vals.push(if search_terms.is_empty() {
Value::nothing(head)
} else {
Value::String {
val: search_terms.join(", "),
span: head,
}
});
found_cmds_vec.push(Value::Record {
cols,
@ -163,11 +183,11 @@ fn help(
let mut vals = vec![];
let decl = engine_state.get_decl(decl_id);
let sig = decl.signature();
let sig = decl.signature().update_from_command(decl.borrow());
let key = sig.name.clone();
let c = sig.usage.clone();
let e = sig.extra_usage.clone();
let key = sig.name;
let usage = sig.usage;
let search_terms = sig.search_terms;
cols.push("name".into());
vals.push(Value::String {
@ -200,10 +220,20 @@ fn help(
});
cols.push("usage".into());
vals.push(Value::String { val: c, span: head });
vals.push(Value::String {
val: usage,
span: head,
});
cols.push("extra_usage".into());
vals.push(Value::String { val: e, span: head });
cols.push("search_terms".into());
vals.push(if search_terms.is_empty() {
Value::nothing(head)
} else {
Value::String {
val: search_terms.join(", "),
span: head,
}
});
found_cmds_vec.push(Value::Record {
cols,

View File

@ -20,7 +20,6 @@ impl Command for ViewSource {
fn signature(&self) -> nu_protocol::Signature {
Signature::build("view-source")
.desc(self.usage())
.required("item", SyntaxShape::Any, "name or block to view")
.category(Category::Core)
}

View File

@ -36,6 +36,10 @@ impl Command for Reduce {
"Aggregate a list table to a single value using an accumulator block."
}
fn search_terms(&self) -> Vec<&str> {
vec!["map", "fold", "foldl"]
}
fn examples(&self) -> Vec<Example> {
vec![
Example {

View File

@ -27,7 +27,6 @@ impl Command for SubCommand {
fn signature(&self) -> Signature {
Signature::build("fetch")
.desc("Load from a URL into a cell, convert to table if possible (avoid by appending '--raw').")
.required(
"URL",
SyntaxShape::String,
@ -45,15 +44,33 @@ impl Command for SubCommand {
"the password when authenticating",
Some('p'),
)
.named("timeout", SyntaxShape::Int, "timeout period in seconds", Some('t'))
.named("headers",SyntaxShape::Any, "custom headers you want to add ", Some('H'))
.switch("raw", "fetch contents as text rather than a table", Some('r'))
.named(
"timeout",
SyntaxShape::Int,
"timeout period in seconds",
Some('t'),
)
.named(
"headers",
SyntaxShape::Any,
"custom headers you want to add ",
Some('H'),
)
.switch(
"raw",
"fetch contents as text rather than a table",
Some('r'),
)
.filter()
.category(Category::Network)
}
fn usage(&self) -> &str {
"Fetch the contents from a URL (HTTP GET operation)."
"Fetch the contents from a URL."
}
fn extra_usage(&self) -> &str {
"Performs HTTP GET operation."
}
fn run(

View File

@ -24,7 +24,6 @@ impl Command for SubCommand {
fn signature(&self) -> Signature {
Signature::build("post")
.desc("Post content to a URL and retrieve data as a table if possible.")
.required("path", SyntaxShape::String, "the URL to post to")
.required("body", SyntaxShape::Any, "the contents of the post body")
.named(
@ -70,9 +69,15 @@ impl Command for SubCommand {
.filter()
.category(Category::Network)
}
fn usage(&self) -> &str {
"Post a body to a URL (HTTP POST operation)."
"Post a body to a URL."
}
fn extra_usage(&self) -> &str {
"Performs HTTP POST operation."
}
fn run(
&self,
engine_state: &EngineState,

View File

@ -16,7 +16,6 @@ impl Command for Ps {
fn signature(&self) -> Signature {
Signature::build("ps")
.desc("View information about system processes.")
.switch(
"long",
"list all available columns for each entry",

View File

@ -14,10 +14,7 @@ impl Command for Sys {
}
fn signature(&self) -> Signature {
Signature::build("sys")
.desc("View information about the current system.")
.filter()
.category(Category::System)
Signature::build("sys").filter().category(Category::System)
}
fn usage(&self) -> &str {