mirror of
https://github.com/clap-rs/clap
synced 2024-12-14 14:52:33 +00:00
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:
parent
8fd0a93bdc
commit
e10e2ad249
3 changed files with 8 additions and 6 deletions
|
@ -207,6 +207,8 @@ pub struct Styles {
|
||||||
pub header: anstyle::Style,
|
pub header: anstyle::Style,
|
||||||
/// Error heading
|
/// Error heading
|
||||||
pub error: anstyle::Style,
|
pub error: anstyle::Style,
|
||||||
|
/// Usage heading
|
||||||
|
pub usage: anstyle::Style,
|
||||||
/// Literal command-line syntax, e.g. `--help`
|
/// Literal command-line syntax, e.g. `--help`
|
||||||
pub literal: anstyle::Style,
|
pub literal: anstyle::Style,
|
||||||
/// Descriptions within command-line syntax, e.g. [`value_name`][crate::Arg::value_name]
|
/// Descriptions within command-line syntax, e.g. [`value_name`][crate::Arg::value_name]
|
||||||
|
@ -223,6 +225,7 @@ impl Styles {
|
||||||
Self {
|
Self {
|
||||||
header: anstyle::Style::new(),
|
header: anstyle::Style::new(),
|
||||||
error: anstyle::Style::new(),
|
error: anstyle::Style::new(),
|
||||||
|
usage: anstyle::Style::new(),
|
||||||
literal: anstyle::Style::new(),
|
literal: anstyle::Style::new(),
|
||||||
placeholder: anstyle::Style::new(),
|
placeholder: anstyle::Style::new(),
|
||||||
valid: anstyle::Style::new(),
|
valid: anstyle::Style::new(),
|
||||||
|
@ -239,6 +242,7 @@ impl Styles {
|
||||||
error: anstyle::Style::new()
|
error: anstyle::Style::new()
|
||||||
.fg_color(Some(anstyle::Color::Ansi(anstyle::AnsiColor::Red)))
|
.fg_color(Some(anstyle::Color::Ansi(anstyle::AnsiColor::Red)))
|
||||||
.bold(),
|
.bold(),
|
||||||
|
usage: anstyle::Style::new().bold().underline(),
|
||||||
literal: anstyle::Style::new().bold(),
|
literal: anstyle::Style::new().bold(),
|
||||||
placeholder: anstyle::Style::new(),
|
placeholder: anstyle::Style::new(),
|
||||||
valid: anstyle::Style::new()
|
valid: anstyle::Style::new()
|
||||||
|
|
|
@ -130,7 +130,6 @@ impl<'cmd, 'writer> HelpTemplate<'cmd, 'writer> {
|
||||||
pub(crate) fn write_templated_help(&mut self, template: &str) {
|
pub(crate) fn write_templated_help(&mut self, template: &str) {
|
||||||
debug!("HelpTemplate::write_templated_help");
|
debug!("HelpTemplate::write_templated_help");
|
||||||
use std::fmt::Write as _;
|
use std::fmt::Write as _;
|
||||||
let header = &self.styles.header;
|
|
||||||
|
|
||||||
let mut parts = template.split('{');
|
let mut parts = template.split('{');
|
||||||
if let Some(first) = parts.next() {
|
if let Some(first) = parts.next() {
|
||||||
|
@ -171,8 +170,8 @@ impl<'cmd, 'writer> HelpTemplate<'cmd, 'writer> {
|
||||||
let _ = write!(
|
let _ = write!(
|
||||||
self.writer,
|
self.writer,
|
||||||
"{}Usage:{}",
|
"{}Usage:{}",
|
||||||
header.render(),
|
self.styles.usage.render(),
|
||||||
header.render_reset()
|
self.styles.usage.render_reset()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
"usage" => {
|
"usage" => {
|
||||||
|
|
|
@ -41,13 +41,12 @@ impl<'cmd> Usage<'cmd> {
|
||||||
let usage = some!(self.create_usage_no_title(used));
|
let usage = some!(self.create_usage_no_title(used));
|
||||||
|
|
||||||
use std::fmt::Write as _;
|
use std::fmt::Write as _;
|
||||||
let header = &self.styles.header;
|
|
||||||
let mut styled = StyledStr::new();
|
let mut styled = StyledStr::new();
|
||||||
let _ = write!(
|
let _ = write!(
|
||||||
styled,
|
styled,
|
||||||
"{}Usage:{} ",
|
"{}Usage:{} ",
|
||||||
header.render(),
|
self.styles.usage.render(),
|
||||||
header.render_reset()
|
self.styles.usage.render_reset()
|
||||||
);
|
);
|
||||||
styled.push_styled(&usage);
|
styled.push_styled(&usage);
|
||||||
Some(styled)
|
Some(styled)
|
||||||
|
|
Loading…
Reference in a new issue