refactor(help): Split out separate usage style

Because this doesn't have a full section after it, people might want to
style it differently than other headers.
This commit is contained in:
Ed Page 2023-04-18 15:03:20 -05:00
parent 8fd0a93bdc
commit e10e2ad249
3 changed files with 8 additions and 6 deletions

View file

@ -207,6 +207,8 @@ pub struct Styles {
pub header: anstyle::Style,
/// Error heading
pub error: anstyle::Style,
/// Usage heading
pub usage: anstyle::Style,
/// Literal command-line syntax, e.g. `--help`
pub literal: anstyle::Style,
/// Descriptions within command-line syntax, e.g. [`value_name`][crate::Arg::value_name]
@ -223,6 +225,7 @@ impl Styles {
Self {
header: anstyle::Style::new(),
error: anstyle::Style::new(),
usage: anstyle::Style::new(),
literal: anstyle::Style::new(),
placeholder: anstyle::Style::new(),
valid: anstyle::Style::new(),
@ -239,6 +242,7 @@ impl Styles {
error: anstyle::Style::new()
.fg_color(Some(anstyle::Color::Ansi(anstyle::AnsiColor::Red)))
.bold(),
usage: anstyle::Style::new().bold().underline(),
literal: anstyle::Style::new().bold(),
placeholder: anstyle::Style::new(),
valid: anstyle::Style::new()

View file

@ -130,7 +130,6 @@ impl<'cmd, 'writer> HelpTemplate<'cmd, 'writer> {
pub(crate) fn write_templated_help(&mut self, template: &str) {
debug!("HelpTemplate::write_templated_help");
use std::fmt::Write as _;
let header = &self.styles.header;
let mut parts = template.split('{');
if let Some(first) = parts.next() {
@ -171,8 +170,8 @@ impl<'cmd, 'writer> HelpTemplate<'cmd, 'writer> {
let _ = write!(
self.writer,
"{}Usage:{}",
header.render(),
header.render_reset()
self.styles.usage.render(),
self.styles.usage.render_reset()
);
}
"usage" => {

View file

@ -41,13 +41,12 @@ impl<'cmd> Usage<'cmd> {
let usage = some!(self.create_usage_no_title(used));
use std::fmt::Write as _;
let header = &self.styles.header;
let mut styled = StyledStr::new();
let _ = write!(
styled,
"{}Usage:{} ",
header.render(),
header.render_reset()
self.styles.usage.render(),
self.styles.usage.render_reset()
);
styled.push_styled(&usage);
Some(styled)