Skip to content

Commit

Permalink
refactor: style usage based on command style
Browse files Browse the repository at this point in the history
Clap 4.4 stabilized Command::get_styles(). This allows the make_usage()
helper to construct the styled usage string using the default literal
style instead of hardwiring bold.
  • Loading branch information
jpgrayson committed Sep 3, 2023
1 parent bccfd17 commit 85c77a6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/cmd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,19 +135,21 @@ pub(crate) enum Error {
NoAppliedPatches,
}

pub(crate) const STYLES: clap::builder::Styles = clap::builder::Styles::styled();

pub(crate) fn make_usage(command_name: &str, usages: &[&str]) -> clap::builder::StyledStr {
use std::fmt::Write as _;
let mut s = StyledStr::new();
let bold = anstyle::Style::new().bold();
let literal = STYLES.get_literal();
for (i, &usage) in usages.iter().enumerate() {
let indent = if i == 0 { "" } else { " " };
let end = if i + 1 == usages.len() { "" } else { "\n" };
let sep = if usage.is_empty() { "" } else { " " };
write!(
s,
"{indent}{}{command_name}{}{sep}{usage}{end}",
bold.render(),
bold.render_reset()
literal.render(),
literal.render_reset()
)
.unwrap();
}
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const CONFLICT_ERROR: i32 = 3;
/// cost of instantiating [`clap::Command`] instances for every StGit subcommand.
fn get_base_command(color_choice: Option<termcolor::ColorChoice>) -> clap::Command {
let mut command = clap::Command::new("stg")
.styles(cmd::STYLES)
.about("Maintain a stack of patches on top of a Git branch.")
.override_usage(cmd::make_usage(
"stg",
Expand Down

0 comments on commit 85c77a6

Please sign in to comment.