Change positional argument formatting

Only show `:` if description available
Fixes #9076

Refactored into a maybe a bit more verbose function making conditional
output simpler vs the multiple format strings in deeper nesting.
This commit is contained in:
sholderbach 2024-08-05 13:56:08 +02:00
parent 91bc1d9cd6
commit e10e148925

View File

@ -3,8 +3,8 @@ use nu_protocol::{
ast::{Argument, Call, Expr, Expression, RecordItem}, ast::{Argument, Call, Expr, Expression, RecordItem},
debugger::WithoutDebug, debugger::WithoutDebug,
engine::{Command, EngineState, Stack, UNKNOWN_SPAN_ID}, engine::{Command, EngineState, Stack, UNKNOWN_SPAN_ID},
record, Category, Config, Example, IntoPipelineData, PipelineData, Signature, Span, SpanId, record, Category, Config, Example, IntoPipelineData, PipelineData, PositionalArg, Signature,
Spanned, SyntaxShape, Type, Value, Span, SpanId, Spanned, SyntaxShape, Type, Value,
}; };
use std::{collections::HashMap, fmt::Write}; use std::{collections::HashMap, fmt::Write};
use terminal_size::{Height, Width}; use terminal_size::{Height, Width};
@ -72,7 +72,6 @@ fn get_documentation(
help_style.update_from_config(engine_state, &nu_config); help_style.update_from_config(engine_state, &nu_config);
let help_section_name = &help_style.section_name; let help_section_name = &help_style.section_name;
let help_subcolor_one = &help_style.subcolor_one; let help_subcolor_one = &help_style.subcolor_one;
let help_subcolor_two = &help_style.subcolor_two;
let cmd_name = &sig.name; let cmd_name = &sig.name;
let mut long_desc = String::new(); let mut long_desc = String::new();
@ -143,69 +142,37 @@ fn get_documentation(
{ {
let _ = write!(long_desc, "\n{help_section_name}Parameters{RESET}:\n"); let _ = write!(long_desc, "\n{help_section_name}Parameters{RESET}:\n");
for positional in &sig.required_positional { for positional in &sig.required_positional {
let text = match &positional.shape { write_positional(
SyntaxShape::Keyword(kw, shape) => { &mut long_desc,
format!( positional,
" {help_subcolor_one}\"{}\" + {RESET}<{help_subcolor_two}{}{RESET}>: {}", PositionalKind::Required,
String::from_utf8_lossy(kw), &help_style,
document_shape(shape), &nu_config,
positional.desc engine_state,
) stack,
} );
_ => {
format!(
" {help_subcolor_one}{}{RESET} <{help_subcolor_two}{}{RESET}>: {}",
positional.name,
document_shape(&positional.shape),
positional.desc
)
}
};
let _ = writeln!(long_desc, "{text}");
} }
for positional in &sig.optional_positional { for positional in &sig.optional_positional {
let text = match &positional.shape { write_positional(
SyntaxShape::Keyword(kw, shape) => { &mut long_desc,
format!( positional,
" {help_subcolor_one}\"{}\" + {RESET}<{help_subcolor_two}{}{RESET}>: {} (optional)", PositionalKind::Optional,
String::from_utf8_lossy(kw), &help_style,
document_shape(shape), &nu_config,
positional.desc engine_state,
) stack,
} );
_ => {
let opt_suffix = if let Some(value) = &positional.default_value {
format!(
" (optional, default: {})",
nu_highlight_string(
&value.to_parsable_string(", ", &nu_config),
engine_state,
stack
)
)
} else {
(" (optional)").to_string()
};
format!(
" {help_subcolor_one}{}{RESET} <{help_subcolor_two}{}{RESET}>: {}{}",
positional.name,
document_shape(&positional.shape),
positional.desc,
opt_suffix,
)
}
};
let _ = writeln!(long_desc, "{text}");
} }
if let Some(rest_positional) = &sig.rest_positional { if let Some(rest_positional) = &sig.rest_positional {
let _ = writeln!( write_positional(
long_desc, &mut long_desc,
" ...{help_subcolor_one}{}{RESET} <{help_subcolor_two}{}{RESET}>: {}", rest_positional,
rest_positional.name, PositionalKind::Rest,
document_shape(&rest_positional.shape), &help_style,
rest_positional.desc &nu_config,
engine_state,
stack,
); );
} }
} }
@ -502,6 +469,69 @@ fn document_shape(shape: &SyntaxShape) -> &SyntaxShape {
} }
} }
#[derive(PartialEq)]
enum PositionalKind {
Required,
Optional,
Rest,
}
fn write_positional(
long_desc: &mut String,
positional: &PositionalArg,
arg_kind: PositionalKind,
help_style: &HelpStyle,
nu_config: &Config,
engine_state: &EngineState,
stack: &mut Stack,
) {
let help_subcolor_one = &help_style.subcolor_one;
let help_subcolor_two = &help_style.subcolor_two;
// Indentation
long_desc.push_str(" ");
if arg_kind == PositionalKind::Rest {
long_desc.push_str("...");
}
match &positional.shape {
SyntaxShape::Keyword(kw, shape) => {
let _ = write!(
long_desc,
"{help_subcolor_one}\"{}\" + {RESET}<{help_subcolor_two}{}{RESET}>",
String::from_utf8_lossy(kw),
document_shape(shape),
);
}
_ => {
let _ = write!(
long_desc,
"{help_subcolor_one}{}{RESET} <{help_subcolor_two}{}{RESET}>",
positional.name,
document_shape(&positional.shape),
);
}
};
if !positional.desc.is_empty() || arg_kind == PositionalKind::Optional {
let _ = write!(long_desc, ": {}", positional.desc);
}
if arg_kind == PositionalKind::Optional {
if let Some(value) = &positional.default_value {
let _ = write!(
long_desc,
" (optional, default: {})",
nu_highlight_string(
&value.to_parsable_string(", ", nu_config),
engine_state,
stack
)
);
} else {
long_desc.push_str(" (optional)");
};
}
long_desc.push('\n');
}
pub fn get_flags_section<F>( pub fn get_flags_section<F>(
signature: &Signature, signature: &Signature,
help_style: &HelpStyle, help_style: &HelpStyle,