mirror of
https://github.com/clap-rs/clap
synced 2025-03-04 15:27:16 +00:00
fix(help: Be more precise on literal vs not
This commit is contained in:
parent
6dc8c9943c
commit
ad05d1624b
2 changed files with 46 additions and 12 deletions
|
@ -219,6 +219,24 @@ impl From<&'_ &'static str> for StyledStr {
|
|||
}
|
||||
}
|
||||
|
||||
impl PartialOrd for StyledStr {
|
||||
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
|
||||
Some(self.cmp(other))
|
||||
}
|
||||
}
|
||||
|
||||
impl Ord for StyledStr {
|
||||
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
|
||||
self.iter().map(cmp_key).cmp(other.iter().map(cmp_key))
|
||||
}
|
||||
}
|
||||
|
||||
fn cmp_key(c: (Option<Style>, &str)) -> (Option<usize>, &str) {
|
||||
let style = c.0.map(|s| s.as_usize());
|
||||
let content = c.1;
|
||||
(style, content)
|
||||
}
|
||||
|
||||
/// Color-unaware printing. Never uses coloring.
|
||||
impl std::fmt::Display for StyledStr {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
|
@ -240,3 +258,17 @@ pub(crate) enum Style {
|
|||
Error,
|
||||
Hint,
|
||||
}
|
||||
|
||||
impl Style {
|
||||
fn as_usize(&self) -> usize {
|
||||
match self {
|
||||
Self::Header => 0,
|
||||
Self::Literal => 1,
|
||||
Self::Placeholder => 2,
|
||||
Self::Good => 3,
|
||||
Self::Warning => 4,
|
||||
Self::Error => 5,
|
||||
Self::Hint => 6,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
// Std
|
||||
use std::borrow::Cow;
|
||||
use std::cmp;
|
||||
use std::fmt::Write as _;
|
||||
use std::usize;
|
||||
|
||||
// Internal
|
||||
|
@ -816,16 +815,18 @@ impl<'cmd, 'writer> Help<'cmd, 'writer> {
|
|||
.get_subcommands()
|
||||
.filter(|subcommand| should_show_subcommand(subcommand))
|
||||
{
|
||||
let mut sc_str = String::new();
|
||||
sc_str.push_str(subcommand.get_name());
|
||||
let mut styled = StyledStr::new();
|
||||
styled.literal(subcommand.get_name());
|
||||
if let Some(short) = subcommand.get_short_flag() {
|
||||
write!(sc_str, " -{}", short).unwrap();
|
||||
styled.none(" ");
|
||||
styled.literal(format!("-{}", short));
|
||||
}
|
||||
if let Some(long) = subcommand.get_long_flag() {
|
||||
write!(sc_str, " --{}", long).unwrap();
|
||||
styled.none(" ");
|
||||
styled.literal(format!("--{}", long));
|
||||
}
|
||||
longest = longest.max(display_width(&sc_str));
|
||||
ord_v.push((subcommand.get_display_order(), sc_str, subcommand));
|
||||
longest = longest.max(styled.display_width());
|
||||
ord_v.push((subcommand.get_display_order(), styled, subcommand));
|
||||
}
|
||||
ord_v.sort_by(|a, b| (a.0, &a.1).cmp(&(b.0, &b.1)));
|
||||
|
||||
|
@ -834,7 +835,7 @@ impl<'cmd, 'writer> Help<'cmd, 'writer> {
|
|||
let next_line_help = self.will_subcommands_wrap(cmd.get_subcommands(), longest);
|
||||
|
||||
let mut first = true;
|
||||
for (_, sc_str, sc) in &ord_v {
|
||||
for (_, sc_str, sc) in ord_v {
|
||||
if first {
|
||||
first = false;
|
||||
} else {
|
||||
|
@ -861,7 +862,7 @@ impl<'cmd, 'writer> Help<'cmd, 'writer> {
|
|||
|
||||
fn write_subcommand(
|
||||
&mut self,
|
||||
sc_str: &str,
|
||||
sc_str: StyledStr,
|
||||
cmd: &Command,
|
||||
next_line_help: bool,
|
||||
longest: usize,
|
||||
|
@ -921,11 +922,12 @@ impl<'cmd, 'writer> Help<'cmd, 'writer> {
|
|||
}
|
||||
|
||||
/// Writes subcommand to the wrapped stream.
|
||||
fn subcmd(&mut self, sc_str: &str, next_line_help: bool, longest: usize) {
|
||||
fn subcmd(&mut self, sc_str: StyledStr, next_line_help: bool, longest: usize) {
|
||||
let width = sc_str.display_width();
|
||||
|
||||
self.none(TAB);
|
||||
self.literal(sc_str);
|
||||
self.writer.extend(sc_str.into_iter());
|
||||
if !next_line_help {
|
||||
let width = display_width(sc_str);
|
||||
self.spaces(width.max(longest + TAB_WIDTH) - width);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue