Skip to content

Commit

Permalink
refactor(help): Split out separate usage style
Browse files Browse the repository at this point in the history
Because this doesn't have a full section after it, people might want to
style it differently than other headers.
  • Loading branch information
epage committed Apr 18, 2023
1 parent 8fd0a93 commit e10e2ad
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
4 changes: 4 additions & 0 deletions clap_builder/src/builder/styled_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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(),
Expand All @@ -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()
Expand Down
5 changes: 2 additions & 3 deletions clap_builder/src/output/help_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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" => {
Expand Down
5 changes: 2 additions & 3 deletions clap_builder/src/output/usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit e10e2ad

Please sign in to comment.