Skip to content

Commit 36cf66a

Browse files
committedMar 8, 2024
Auto merge of #13562 - epage:hyperlink, r=weihanglo
refactor(shell): Use alternate to close links ### What does this PR try to resolve? Simplifies the code for includign hyperlinks, especially the fact that this allows for including the variable in the format string. ### How should we test and review this PR? Hand tested each case as we don't have SVG support for hyperlinks and we don't have SVG tests for these cases ### Additional information
2 parents 6b27055 + af9f134 commit 36cf66a

File tree

4 files changed

+13
-36
lines changed

4 files changed

+13
-36
lines changed
 

‎src/cargo/core/compiler/job_queue/mod.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -830,9 +830,7 @@ impl<'gctx> DrainState<'gctx> {
830830
"https://doc.rust-lang.org/cargo/reference/profiles.html#default-profiles",
831831
);
832832
let message = format!(
833-
"{}`{profile_name}` profile [{opt_type}]{} target(s) in {time_elapsed}",
834-
profile_link.open(),
835-
profile_link.close()
833+
"{profile_link}`{profile_name}` profile [{opt_type}]{profile_link:#} target(s) in {time_elapsed}",
836834
);
837835
if !build_runner.bcx.build_config.build_plan {
838836
// It doesn't really matter if this fails.

‎src/cargo/core/compiler/timings.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -350,12 +350,7 @@ impl<'gctx> Timings<'gctx> {
350350
let mut shell = self.gctx.shell();
351351
let timing_path = std::env::current_dir().unwrap_or_default().join(&filename);
352352
let link = shell.err_file_hyperlink(&timing_path);
353-
let msg = format!(
354-
"report saved to {}{}{}",
355-
link.open(),
356-
timing_path.display(),
357-
link.close()
358-
);
353+
let msg = format!("report saved to {link}{}{link:#}", timing_path.display(),);
359354
shell.status_with_color("Timing", msg, &style::NOTE)?;
360355

361356
Ok(())

‎src/cargo/core/shell.rs

+8-13
Original file line numberDiff line numberDiff line change
@@ -539,20 +539,15 @@ impl<D: fmt::Display> Default for Hyperlink<D> {
539539
}
540540
}
541541

542-
impl<D: fmt::Display> Hyperlink<D> {
543-
pub fn open(&self) -> impl fmt::Display {
544-
if let Some(url) = self.url.as_ref() {
545-
format!("\x1B]8;;{url}\x1B\\")
546-
} else {
547-
String::new()
548-
}
549-
}
550-
551-
pub fn close(&self) -> impl fmt::Display {
552-
if self.url.is_some() {
553-
"\x1B]8;;\x1B\\"
542+
impl<D: fmt::Display> fmt::Display for Hyperlink<D> {
543+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
544+
let Some(url) = self.url.as_ref() else {
545+
return Ok(());
546+
};
547+
if f.alternate() {
548+
write!(f, "\x1B]8;;\x1B\\")
554549
} else {
555-
""
550+
write!(f, "\x1B]8;;{url}\x1B\\")
556551
}
557552
}
558553
}

‎src/cargo/ops/cargo_doc.rs

+3-14
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,7 @@ pub fn doc(ws: &Workspace<'_>, options: &DocOptions) -> CargoResult<()> {
7171
};
7272
let mut shell = ws.gctx().shell();
7373
let link = shell.err_file_hyperlink(&path);
74-
shell.status(
75-
"Opening",
76-
format!("{}{}{}", link.open(), path.display(), link.close()),
77-
)?;
74+
shell.status("Opening", format!("{link}{}{link:#}", path.display()))?;
7875
open_docs(&path, &mut shell, config_browser, ws.gctx())?;
7976
}
8077
} else if ws.gctx().shell().verbosity() == Verbosity::Verbose {
@@ -85,10 +82,7 @@ pub fn doc(ws: &Workspace<'_>, options: &DocOptions) -> CargoResult<()> {
8582
if path.exists() {
8683
let mut shell = ws.gctx().shell();
8784
let link = shell.err_file_hyperlink(&path);
88-
shell.status(
89-
"Generated",
90-
format!("{}{}{}", link.open(), path.display(), link.close()),
91-
)?;
85+
shell.status("Generated", format!("{link}{}{link:#}", path.display()))?;
9286
}
9387
}
9488
}
@@ -114,12 +108,7 @@ pub fn doc(ws: &Workspace<'_>, options: &DocOptions) -> CargoResult<()> {
114108
let link = shell.err_file_hyperlink(&first_path);
115109
shell.status(
116110
"Generated",
117-
format!(
118-
"{}{}{}{remaining}",
119-
link.open(),
120-
first_path.display(),
121-
link.close()
122-
),
111+
format!("{link}{}{link:#}{remaining}", first_path.display(),),
123112
)?;
124113
}
125114
}

0 commit comments

Comments
 (0)
Please sign in to comment.