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

refactor(shell): Use new fancy anstyle API #13368

Merged
merged 2 commits into from
Jan 29, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 16 additions & 14 deletions src/bin/cargo/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,9 @@ pub fn main(config: &mut LazyConfig) -> CliResult {
.map(String::as_str)
== Some("help")
{
let header = style::HEADER.render();
let literal = style::LITERAL.render();
let placeholder = style::PLACEHOLDER.render();
let reset = anstyle::Reset.render();
let header = style::HEADER;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a subsequent followup, might it make sense to use style::{HEADER, LITERAL, PLACEHOLDER};, and then use the constants directly, rather than first putting them in locals?

let literal = style::LITERAL;
let placeholder = style::PLACEHOLDER;

let options = CliUnstable::help();
let max_length = options
Expand All @@ -72,17 +71,17 @@ pub fn main(config: &mut LazyConfig) -> CliResult {
.map(|(opt, help)| {
let opt = opt.replace("_", "-");
let help = help.unwrap();
format!(" {literal}-Z {opt:<max_length$}{reset} {help}")
format!(" {literal}-Z {opt:<max_length$}{literal:#} {help}")
})
.join("\n");
drop_println!(
config,
"\
{header}Available unstable (nightly-only) flags:{reset}
{header}Available unstable (nightly-only) flags:{header:#}

{z_flags}

Run with `{literal}cargo -Z{reset} {placeholder}[FLAG] [COMMAND]{reset}`",
Run with `{literal}cargo -Z{literal:#} {placeholder}[FLAG] [COMMAND]{placeholder:#}`",
);
if !config.nightly_features_allowed {
drop_println!(
Expand Down Expand Up @@ -136,8 +135,7 @@ Run with `{literal}cargo -Z{reset} {placeholder}[FLAG] [COMMAND]{reset}`",
);
for (name, command) in list_commands(config) {
let known_external_desc = known_external_command_descriptions.get(name.as_str());
let literal = style::LITERAL.render();
let reset = anstyle::Reset.render();
let literal = style::LITERAL;
match command {
CommandInfo::BuiltIn { about } => {
assert!(
Expand All @@ -146,21 +144,25 @@ Run with `{literal}cargo -Z{reset} {placeholder}[FLAG] [COMMAND]{reset}`",
);
let summary = about.unwrap_or_default();
let summary = summary.lines().next().unwrap_or(&summary); // display only the first line
drop_println!(config, " {literal}{name:<20}{reset} {summary}");
drop_println!(config, " {literal}{name:<20}{literal:#} {summary}");
}
CommandInfo::External { path } => {
if let Some(desc) = known_external_desc {
drop_println!(config, " {literal}{name:<20}{reset} {desc}");
drop_println!(config, " {literal}{name:<20}{literal:#} {desc}");
} else if is_verbose {
drop_println!(config, " {literal}{name:<20}{reset} {}", path.display());
drop_println!(
config,
" {literal}{name:<20}{literal:#} {}",
path.display()
);
} else {
drop_println!(config, " {literal}{name}{reset}");
drop_println!(config, " {literal}{name}{literal:#}");
}
}
CommandInfo::Alias { target } => {
drop_println!(
config,
" {literal}{name:<20}{reset} alias: {}",
" {literal}{name:<20}{literal:#} alias: {}",
target.iter().join(" ")
);
}
Expand Down
8 changes: 3 additions & 5 deletions src/cargo/core/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,15 +455,13 @@ impl ShellOut {
style: &Style,
justified: bool,
) -> CargoResult<()> {
let style = style.render();
let bold = (anstyle::Style::new() | anstyle::Effects::BOLD).render();
let reset = anstyle::Reset.render();
let bold = anstyle::Style::new() | anstyle::Effects::BOLD;

let mut buffer = Vec::new();
if justified {
write!(&mut buffer, "{style}{status:>12}{reset}")?;
write!(&mut buffer, "{style}{status:>12}{style:#}")?;
} else {
write!(&mut buffer, "{style}{status}{reset}{bold}:{reset}")?;
write!(&mut buffer, "{style}{status}{style:#}{bold}:{bold:#}")?;
}
match message {
Some(message) => writeln!(buffer, " {message}")?,
Expand Down
9 changes: 4 additions & 5 deletions src/cargo/ops/cargo_add/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1009,9 +1009,8 @@ fn print_dep_table_msg(shell: &mut Shell, dep: &DependencyUI) -> CargoResult<()>
}

let stderr = shell.err();
let good = style::GOOD.render();
let error = style::ERROR.render();
let reset = anstyle::Reset.render();
let good = style::GOOD;
let error = style::ERROR;

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

if total_activated <= MAX_FEATURE_PRINTS {
for feat in activated {
writeln!(stderr, "{prefix}{good}+{reset} {feat}")?;
writeln!(stderr, "{prefix}{good}+{good:#} {feat}")?;
}
} else {
writeln!(stderr, "{prefix}{total_activated} activated features")?;
}

if total_activated + total_deactivated <= MAX_FEATURE_PRINTS {
for feat in deactivated {
writeln!(stderr, "{prefix}{error}-{reset} {feat}")?;
writeln!(stderr, "{prefix}{error}-{error:#} {feat}")?;
}
} else {
writeln!(stderr, "{prefix}{total_deactivated} deactivated features")?;
Expand Down
5 changes: 2 additions & 3 deletions src/cargo/ops/registry/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ pub fn search(

let mut shell = config.shell();
let stdout = shell.out();
let good = style::GOOD.render();
let reset = anstyle::Reset.render();
let good = style::GOOD;

for (name, description) in names.into_iter().zip(descriptions) {
let line = match description {
Expand All @@ -59,7 +58,7 @@ pub fn search(
while let Some(fragment) = fragments.next() {
let _ = write!(stdout, "{fragment}");
if fragments.peek().is_some() {
let _ = write!(stdout, "{good}{query}{reset}");
let _ = write!(stdout, "{good}{query}{good:#}");
}
}
let _ = writeln!(stdout);
Expand Down