Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(help): Separate command flags like options #4235

Merged
merged 2 commits into from Sep 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions examples/pacman.md
Expand Up @@ -40,9 +40,9 @@ package manager utility
Usage: pacman[EXE] <COMMAND>

Commands:
query -Q --query Query the package database.
sync -S --sync Synchronize packages.
help Print this message or the help of the given subcommand(s)
query, -Q, --query Query the package database.
sync, -S, --sync Synchronize packages.
help Print this message or the help of the given subcommand(s)

Options:
-h, --help Print help information
Expand Down
32 changes: 32 additions & 0 deletions src/builder/styled_str.rs
Expand Up @@ -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 {
Expand All @@ -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,
}
}
}
26 changes: 14 additions & 12 deletions src/output/help.rs
@@ -1,7 +1,6 @@
// Std
use std::borrow::Cow;
use std::cmp;
use std::fmt::Write as _;
use std::usize;

// Internal
Expand Down Expand Up @@ -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)));

Expand All @@ -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 {
Expand All @@ -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,
Expand Down Expand Up @@ -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);
}
}
Expand Down