Skip to content

Commit c7144ee

Browse files
authoredFeb 5, 2024
feat(fancy): Add option to change the link display text (#335)
This option allows to globally change the default `(link)` display text with any other text provider by users.
1 parent cf2d8c0 commit c7144ee

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed
 

‎src/handlers/graphical.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ pub struct GraphicalReportHandler {
3636
pub(crate) word_separator: Option<textwrap::WordSeparator>,
3737
pub(crate) word_splitter: Option<textwrap::WordSplitter>,
3838
pub(crate) highlighter: MietteHighlighter,
39+
pub(crate) link_display_text: Option<String>,
3940
}
4041

4142
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
@@ -62,6 +63,7 @@ impl GraphicalReportHandler {
6263
word_separator: None,
6364
word_splitter: None,
6465
highlighter: MietteHighlighter::default(),
66+
link_display_text: None,
6567
}
6668
}
6769

@@ -80,6 +82,7 @@ impl GraphicalReportHandler {
8082
word_separator: None,
8183
word_splitter: None,
8284
highlighter: MietteHighlighter::default(),
85+
link_display_text: None,
8386
}
8487
}
8588

@@ -190,6 +193,13 @@ impl GraphicalReportHandler {
190193
self.highlighter = MietteHighlighter::nocolor();
191194
self
192195
}
196+
197+
/// Sets the display text for links.
198+
/// Miette displays `(link)` if this option is not set.
199+
pub fn with_link_display_text(mut self, text: impl Into<String>) -> Self {
200+
self.link_display_text = Some(text.into());
201+
self
202+
}
193203
}
194204

195205
impl Default for GraphicalReportHandler {
@@ -246,11 +256,12 @@ impl GraphicalReportHandler {
246256
} else {
247257
"".to_string()
248258
};
259+
let display_text = self.link_display_text.as_deref().unwrap_or("(link)");
249260
let link = format!(
250261
"\u{1b}]8;;{}\u{1b}\\{}{}\u{1b}]8;;\u{1b}\\",
251262
url,
252263
code.style(severity_style),
253-
"(link)".style(self.theme.styles.link)
264+
display_text.style(self.theme.styles.link)
254265
);
255266
write!(header, "{}", link)?;
256267
writeln!(f, "{}", header)?;

‎tests/graphical.rs

+22
Original file line numberDiff line numberDiff line change
@@ -1341,6 +1341,28 @@ fn disable_url_links() -> Result<(), MietteError> {
13411341
Ok(())
13421342
}
13431343

1344+
#[test]
1345+
fn url_links_with_display_text() -> Result<(), MietteError> {
1346+
#[derive(Debug, Diagnostic, Error)]
1347+
#[error("oops!")]
1348+
#[diagnostic(
1349+
code(oops::my::bad),
1350+
help("try doing it better next time?"),
1351+
url("https://example.com")
1352+
)]
1353+
struct MyBad;
1354+
let err = MyBad;
1355+
let out = fmt_report_with_settings(err.into(), |handler| {
1356+
handler.with_link_display_text("Read the documentation")
1357+
});
1358+
1359+
println!("Error: {}", out);
1360+
assert!(out.contains("https://example.com"));
1361+
assert!(out.contains("Read the documentation"));
1362+
assert!(out.contains("oops::my::bad"));
1363+
Ok(())
1364+
}
1365+
13441366
#[test]
13451367
fn related() -> Result<(), MietteError> {
13461368
#[derive(Debug, Diagnostic, Error)]

0 commit comments

Comments
 (0)
Please sign in to comment.