Skip to content

Commit 43a33b4

Browse files
committed
feat(cli): Add color to clap help/errors
1 parent 9c61986 commit 43a33b4

File tree

51 files changed

+570
-373
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+570
-373
lines changed

Cargo.lock

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ zstd = { version = "0.13", default-features = false }
105105
# test only (depends on `test` feature)
106106
snapbox = { version = "0.6.22", features = ["term-svg"], optional = true }
107107
walkdir = { version = "2", optional = true }
108+
clap-cargo = "0.18.1"
108109

109110
[target."cfg(windows)".dependencies]
110111
cc = "1"

src/cli/rustup_mode.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ fn handle_epipe(res: Result<ExitCode>) -> Result<ExitCode> {
7575
version = common::version(),
7676
before_help = format!("rustup {}", common::version()),
7777
after_help = RUSTUP_HELP,
78+
styles = clap_cargo::style::CLAP_STYLING,
7879
)]
7980
struct Rustup {
8081
/// Set log level to 'DEBUG' if 'RUSTUP_LOG' is unset
@@ -578,11 +579,11 @@ pub async fn main(
578579
let matches = match Rustup::try_parse_from(process.args_os()) {
579580
Ok(matches) => matches,
580581
Err(err) if err.kind() == DisplayHelp => {
581-
write!(process.stdout().lock(), "{err}")?;
582+
write!(process.stdout().lock(), "{}", err.render().ansi())?;
582583
return Ok(ExitCode(0));
583584
}
584585
Err(err) if err.kind() == DisplayVersion => {
585-
write!(process.stdout().lock(), "{err}")?;
586+
write!(process.stdout().lock(), "{}", err.render().ansi())?;
586587
display_version(current_dir, process).await?;
587588
return Ok(ExitCode(0));
588589
}
@@ -594,9 +595,9 @@ pub async fn main(
594595
]
595596
.contains(&err.kind())
596597
{
597-
write!(process.stdout().lock(), "{err}")?;
598+
write!(process.stdout().lock(), "{}", err.render().ansi())?;
598599
} else {
599-
write!(process.stderr().lock(), "{err}")?;
600+
write!(process.stderr().lock(), "{}", err.render().ansi())?;
600601
}
601602
return Ok(ExitCode(1));
602603
}
@@ -609,7 +610,7 @@ pub async fn main(
609610

610611
let Some(subcmd) = matches.subcmd else {
611612
let help = Rustup::command().render_long_help();
612-
writeln!(process.stderr().lock(), "{help}")?;
613+
writeln!(process.stderr().lock(), "{}", help.ansi())?;
613614
return Ok(ExitCode(1));
614615
};
615616

src/cli/setup_mode.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::path::PathBuf;
22

3-
use anyhow::Result;
3+
use anyhow::{Result, format_err};
44
use clap::Parser;
55
use tracing::warn;
66
use tracing_subscriber::{EnvFilter, Registry, reload::Handle};
@@ -24,6 +24,7 @@ use crate::{
2424
bin_name = "rustup-init[EXE]",
2525
version = common::version(),
2626
before_help = format!("rustup-init {}", common::version()),
27+
styles = clap_cargo::style::CLAP_STYLING
2728
)]
2829
struct RustupInit {
2930
/// Set log level to 'DEBUG' if 'RUSTUP_LOG' is unset
@@ -99,10 +100,10 @@ pub async fn main(
99100
Ok(args) => args,
100101
Err(e) if [ErrorKind::DisplayHelp, ErrorKind::DisplayVersion].contains(&e.kind()) => {
101102
use std::io::Write as _;
102-
write!(process.stdout().lock(), "{e}")?;
103+
write!(process.stdout().lock(), "{}", e.render().ansi())?;
103104
return Ok(utils::ExitCode(0));
104105
}
105-
Err(e) => return Err(e.into()),
106+
Err(e) => return Err(format_err!("{}", e.render().ansi())),
106107
};
107108

108109
if self_replace {

tests/suite/cli_rustup_init_ui/rustup_init_help_flag.stdout.term.svg

Lines changed: 18 additions & 14 deletions
Loading

tests/suite/cli_rustup_ui/rustup_check_cmd_help_flag.stdout.term.svg

Lines changed: 8 additions & 4 deletions
Loading

tests/suite/cli_rustup_ui/rustup_completions_cmd_help_flag.stdout.term.svg

Lines changed: 10 additions & 6 deletions
Loading

tests/suite/cli_rustup_ui/rustup_component_cmd_add_cmd_help_flag.stdout.term.svg

Lines changed: 11 additions & 7 deletions
Loading

tests/suite/cli_rustup_ui/rustup_component_cmd_help_flag.stdout.term.svg

Lines changed: 12 additions & 8 deletions
Loading

0 commit comments

Comments
 (0)