Skip to content

Commit

Permalink
refactor(shell): Use alternate to close links
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Mar 8, 2024
1 parent 6b27055 commit af9f134
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 36 deletions.
4 changes: 1 addition & 3 deletions src/cargo/core/compiler/job_queue/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -830,9 +830,7 @@ impl<'gctx> DrainState<'gctx> {
"https://doc.rust-lang.org/cargo/reference/profiles.html#default-profiles",
);
let message = format!(
"{}`{profile_name}` profile [{opt_type}]{} target(s) in {time_elapsed}",
profile_link.open(),
profile_link.close()
"{profile_link}`{profile_name}` profile [{opt_type}]{profile_link:#} target(s) in {time_elapsed}",
);
if !build_runner.bcx.build_config.build_plan {
// It doesn't really matter if this fails.
Expand Down
7 changes: 1 addition & 6 deletions src/cargo/core/compiler/timings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,12 +350,7 @@ impl<'gctx> Timings<'gctx> {
let mut shell = self.gctx.shell();
let timing_path = std::env::current_dir().unwrap_or_default().join(&filename);
let link = shell.err_file_hyperlink(&timing_path);
let msg = format!(
"report saved to {}{}{}",
link.open(),
timing_path.display(),
link.close()
);
let msg = format!("report saved to {link}{}{link:#}", timing_path.display(),);
shell.status_with_color("Timing", msg, &style::NOTE)?;

Ok(())
Expand Down
21 changes: 8 additions & 13 deletions src/cargo/core/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -539,20 +539,15 @@ impl<D: fmt::Display> Default for Hyperlink<D> {
}
}

impl<D: fmt::Display> Hyperlink<D> {
pub fn open(&self) -> impl fmt::Display {
if let Some(url) = self.url.as_ref() {
format!("\x1B]8;;{url}\x1B\\")
} else {
String::new()
}
}

pub fn close(&self) -> impl fmt::Display {
if self.url.is_some() {
"\x1B]8;;\x1B\\"
impl<D: fmt::Display> fmt::Display for Hyperlink<D> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let Some(url) = self.url.as_ref() else {
return Ok(());
};
if f.alternate() {
write!(f, "\x1B]8;;\x1B\\")
} else {
""
write!(f, "\x1B]8;;{url}\x1B\\")
}
}
}
Expand Down
17 changes: 3 additions & 14 deletions src/cargo/ops/cargo_doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,7 @@ pub fn doc(ws: &Workspace<'_>, options: &DocOptions) -> CargoResult<()> {
};
let mut shell = ws.gctx().shell();
let link = shell.err_file_hyperlink(&path);
shell.status(
"Opening",
format!("{}{}{}", link.open(), path.display(), link.close()),
)?;
shell.status("Opening", format!("{link}{}{link:#}", path.display()))?;
open_docs(&path, &mut shell, config_browser, ws.gctx())?;
}
} else if ws.gctx().shell().verbosity() == Verbosity::Verbose {
Expand All @@ -85,10 +82,7 @@ pub fn doc(ws: &Workspace<'_>, options: &DocOptions) -> CargoResult<()> {
if path.exists() {
let mut shell = ws.gctx().shell();
let link = shell.err_file_hyperlink(&path);
shell.status(
"Generated",
format!("{}{}{}", link.open(), path.display(), link.close()),
)?;
shell.status("Generated", format!("{link}{}{link:#}", path.display()))?;
}
}
}
Expand All @@ -114,12 +108,7 @@ pub fn doc(ws: &Workspace<'_>, options: &DocOptions) -> CargoResult<()> {
let link = shell.err_file_hyperlink(&first_path);
shell.status(
"Generated",
format!(
"{}{}{}{remaining}",
link.open(),
first_path.display(),
link.close()
),
format!("{link}{}{link:#}{remaining}", first_path.display(),),
)?;
}
}
Expand Down

0 comments on commit af9f134

Please sign in to comment.