Skip to content

Commit 61debf8

Browse files
committed
Auto merge of #13368 - epage:anstyle, r=weihanglo
refactor(shell): Use new fancy anstyle API ### What does this PR try to resolve? Simplifies working with colored output ### How should we test and review this PR? ### Additional information CC `@joshtriplett`
2 parents 3e1a2dd + bf664da commit 61debf8

File tree

5 files changed

+27
-29
lines changed

5 files changed

+27
-29
lines changed

Cargo.lock

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

src/bin/cargo/cli.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,9 @@ pub fn main(config: &mut LazyConfig) -> CliResult {
5454
.map(String::as_str)
5555
== Some("help")
5656
{
57-
let header = style::HEADER.render();
58-
let literal = style::LITERAL.render();
59-
let placeholder = style::PLACEHOLDER.render();
60-
let reset = anstyle::Reset.render();
57+
let header = style::HEADER;
58+
let literal = style::LITERAL;
59+
let placeholder = style::PLACEHOLDER;
6160

6261
let options = CliUnstable::help();
6362
let max_length = options
@@ -72,17 +71,17 @@ pub fn main(config: &mut LazyConfig) -> CliResult {
7271
.map(|(opt, help)| {
7372
let opt = opt.replace("_", "-");
7473
let help = help.unwrap();
75-
format!(" {literal}-Z {opt:<max_length$}{reset} {help}")
74+
format!(" {literal}-Z {opt:<max_length$}{literal:#} {help}")
7675
})
7776
.join("\n");
7877
drop_println!(
7978
config,
8079
"\
81-
{header}Available unstable (nightly-only) flags:{reset}
80+
{header}Available unstable (nightly-only) flags:{header:#}
8281
8382
{z_flags}
8483
85-
Run with `{literal}cargo -Z{reset} {placeholder}[FLAG] [COMMAND]{reset}`",
84+
Run with `{literal}cargo -Z{literal:#} {placeholder}[FLAG] [COMMAND]{placeholder:#}`",
8685
);
8786
if !config.nightly_features_allowed {
8887
drop_println!(
@@ -136,8 +135,7 @@ Run with `{literal}cargo -Z{reset} {placeholder}[FLAG] [COMMAND]{reset}`",
136135
);
137136
for (name, command) in list_commands(config) {
138137
let known_external_desc = known_external_command_descriptions.get(name.as_str());
139-
let literal = style::LITERAL.render();
140-
let reset = anstyle::Reset.render();
138+
let literal = style::LITERAL;
141139
match command {
142140
CommandInfo::BuiltIn { about } => {
143141
assert!(
@@ -146,21 +144,25 @@ Run with `{literal}cargo -Z{reset} {placeholder}[FLAG] [COMMAND]{reset}`",
146144
);
147145
let summary = about.unwrap_or_default();
148146
let summary = summary.lines().next().unwrap_or(&summary); // display only the first line
149-
drop_println!(config, " {literal}{name:<20}{reset} {summary}");
147+
drop_println!(config, " {literal}{name:<20}{literal:#} {summary}");
150148
}
151149
CommandInfo::External { path } => {
152150
if let Some(desc) = known_external_desc {
153-
drop_println!(config, " {literal}{name:<20}{reset} {desc}");
151+
drop_println!(config, " {literal}{name:<20}{literal:#} {desc}");
154152
} else if is_verbose {
155-
drop_println!(config, " {literal}{name:<20}{reset} {}", path.display());
153+
drop_println!(
154+
config,
155+
" {literal}{name:<20}{literal:#} {}",
156+
path.display()
157+
);
156158
} else {
157-
drop_println!(config, " {literal}{name}{reset}");
159+
drop_println!(config, " {literal}{name}{literal:#}");
158160
}
159161
}
160162
CommandInfo::Alias { target } => {
161163
drop_println!(
162164
config,
163-
" {literal}{name:<20}{reset} alias: {}",
165+
" {literal}{name:<20}{literal:#} alias: {}",
164166
target.iter().join(" ")
165167
);
166168
}

src/cargo/core/shell.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -455,15 +455,13 @@ impl ShellOut {
455455
style: &Style,
456456
justified: bool,
457457
) -> CargoResult<()> {
458-
let style = style.render();
459-
let bold = (anstyle::Style::new() | anstyle::Effects::BOLD).render();
460-
let reset = anstyle::Reset.render();
458+
let bold = anstyle::Style::new() | anstyle::Effects::BOLD;
461459

462460
let mut buffer = Vec::new();
463461
if justified {
464-
write!(&mut buffer, "{style}{status:>12}{reset}")?;
462+
write!(&mut buffer, "{style}{status:>12}{style:#}")?;
465463
} else {
466-
write!(&mut buffer, "{style}{status}{reset}{bold}:{reset}")?;
464+
write!(&mut buffer, "{style}{status}{style:#}{bold}:{bold:#}")?;
467465
}
468466
match message {
469467
Some(message) => writeln!(buffer, " {message}")?,

src/cargo/ops/cargo_add/mod.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,9 +1009,8 @@ fn print_dep_table_msg(shell: &mut Shell, dep: &DependencyUI) -> CargoResult<()>
10091009
}
10101010

10111011
let stderr = shell.err();
1012-
let good = style::GOOD.render();
1013-
let error = style::ERROR.render();
1014-
let reset = anstyle::Reset.render();
1012+
let good = style::GOOD;
1013+
let error = style::ERROR;
10151014

10161015
let (activated, deactivated) = dep.features();
10171016
if !activated.is_empty() || !deactivated.is_empty() {
@@ -1026,15 +1025,15 @@ fn print_dep_table_msg(shell: &mut Shell, dep: &DependencyUI) -> CargoResult<()>
10261025

10271026
if total_activated <= MAX_FEATURE_PRINTS {
10281027
for feat in activated {
1029-
writeln!(stderr, "{prefix}{good}+{reset} {feat}")?;
1028+
writeln!(stderr, "{prefix}{good}+{good:#} {feat}")?;
10301029
}
10311030
} else {
10321031
writeln!(stderr, "{prefix}{total_activated} activated features")?;
10331032
}
10341033

10351034
if total_activated + total_deactivated <= MAX_FEATURE_PRINTS {
10361035
for feat in deactivated {
1037-
writeln!(stderr, "{prefix}{error}-{reset} {feat}")?;
1036+
writeln!(stderr, "{prefix}{error}-{error:#} {feat}")?;
10381037
}
10391038
} else {
10401039
writeln!(stderr, "{prefix}{total_deactivated} deactivated features")?;

src/cargo/ops/registry/search.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ pub fn search(
4747

4848
let mut shell = config.shell();
4949
let stdout = shell.out();
50-
let good = style::GOOD.render();
51-
let reset = anstyle::Reset.render();
50+
let good = style::GOOD;
5251

5352
for (name, description) in names.into_iter().zip(descriptions) {
5453
let line = match description {
@@ -59,7 +58,7 @@ pub fn search(
5958
while let Some(fragment) = fragments.next() {
6059
let _ = write!(stdout, "{fragment}");
6160
if fragments.peek().is_some() {
62-
let _ = write!(stdout, "{good}{query}{reset}");
61+
let _ = write!(stdout, "{good}{query}{good:#}");
6362
}
6463
}
6564
let _ = writeln!(stdout);

0 commit comments

Comments
 (0)