nu-engine: better display for shape when showing help params (#5715)

This commit is contained in:
Herlon Aguiar 2022-06-05 15:13:04 +02:00 committed by GitHub
parent fda456e469
commit e4a22799d5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,7 +1,7 @@
use nu_protocol::{ use nu_protocol::{
ast::Call, ast::Call,
engine::{EngineState, Stack}, engine::{EngineState, Stack},
Example, IntoPipelineData, Signature, Span, Value, Example, IntoPipelineData, Signature, Span, SyntaxShape, Value,
}; };
use std::fmt::Write; use std::fmt::Write;
@ -89,17 +89,20 @@ fn get_documentation(
{ {
long_desc.push_str("\nParameters:\n"); long_desc.push_str("\nParameters:\n");
for positional in &sig.required_positional { for positional in &sig.required_positional {
let _ = writeln!( long_desc.push_str(&format!(
long_desc, " (optional) {} <{:?}>: {}\n",
" {} <{:?}>: {}", positional.name,
positional.name, positional.shape, positional.desc document_shape(positional.shape.clone()),
); positional.desc
));
} }
for positional in &sig.optional_positional { for positional in &sig.optional_positional {
let _ = writeln!( let _ = writeln!(
long_desc, long_desc,
" (optional) {} <{:?}>: {}", " (optional) {} <{:?}>: {}",
positional.name, positional.shape, positional.desc positional.name,
document_shape(positional.shape.clone()),
positional.desc
); );
} }
@ -107,7 +110,9 @@ fn get_documentation(
let _ = writeln!( let _ = writeln!(
long_desc, long_desc,
" ...{} <{:?}>: {}", " ...{} <{:?}>: {}",
rest_positional.name, rest_positional.shape, rest_positional.desc rest_positional.name,
document_shape(rest_positional.shape.clone()),
rest_positional.desc
); );
} }
} }
@ -161,6 +166,14 @@ fn get_documentation(
long_desc long_desc
} }
// document shape helps showing more useful information
pub fn document_shape(shape: SyntaxShape) -> SyntaxShape {
match shape {
SyntaxShape::Custom(inner_shape, _) => *inner_shape,
_ => shape,
}
}
pub fn get_flags_section(signature: &Signature) -> String { pub fn get_flags_section(signature: &Signature) -> String {
let mut long_desc = String::new(); let mut long_desc = String::new();
long_desc.push_str("\nFlags:\n"); long_desc.push_str("\nFlags:\n");