From af9f1346a269e1fb14d0fa44852da0daecb22d50 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Fri, 8 Mar 2024 08:15:51 -0600 Subject: [PATCH] refactor(shell): Use alternate to close links --- src/cargo/core/compiler/job_queue/mod.rs | 4 +--- src/cargo/core/compiler/timings.rs | 7 +------ src/cargo/core/shell.rs | 21 ++++++++------------- src/cargo/ops/cargo_doc.rs | 17 +++-------------- 4 files changed, 13 insertions(+), 36 deletions(-) diff --git a/src/cargo/core/compiler/job_queue/mod.rs b/src/cargo/core/compiler/job_queue/mod.rs index 0ad64dc7650..e36faa12fff 100644 --- a/src/cargo/core/compiler/job_queue/mod.rs +++ b/src/cargo/core/compiler/job_queue/mod.rs @@ -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. diff --git a/src/cargo/core/compiler/timings.rs b/src/cargo/core/compiler/timings.rs index b4b23b6590b..f91a020e45a 100644 --- a/src/cargo/core/compiler/timings.rs +++ b/src/cargo/core/compiler/timings.rs @@ -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(()) diff --git a/src/cargo/core/shell.rs b/src/cargo/core/shell.rs index e8ad1874ea5..791e0c35790 100644 --- a/src/cargo/core/shell.rs +++ b/src/cargo/core/shell.rs @@ -539,20 +539,15 @@ impl Default for Hyperlink { } } -impl Hyperlink { - 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 fmt::Display for Hyperlink { + 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\\") } } } diff --git a/src/cargo/ops/cargo_doc.rs b/src/cargo/ops/cargo_doc.rs index a9bdf3cab6c..5f7e9ad131a 100644 --- a/src/cargo/ops/cargo_doc.rs +++ b/src/cargo/ops/cargo_doc.rs @@ -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 { @@ -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()))?; } } } @@ -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(),), )?; } }