Skip to content

Commit 25dfef2

Browse files
epagedjc
authored andcommitted
feat(cli): Add color to clap help/errors
1 parent 2e4faad commit 25dfef2

File tree

51 files changed

+571
-374
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

+571
-374
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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ otel = [
3737
]
3838

3939
# Exports code dependent on private interfaces for the integration test suite
40-
test = ["dep:snapbox", "dep:walkdir"]
40+
test = ["dep:snapbox", "dep:walkdir", "clap-cargo/testing_colors"]
4141

4242
# Sorted by alphabetic order
4343
[dependencies]
@@ -47,6 +47,7 @@ anyhow = "1.0.69"
4747
cfg-if = "1.0"
4848
chrono = { version = "0.4", default-features = false, features = ["std"] }
4949
clap = { version = "4", features = ["derive", "wrap_help"] }
50+
clap-cargo = "0.18.2"
5051
clap_complete = "4"
5152
console = "0.16"
5253
curl = { version = "0.4.44", optional = true }

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)