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

feat(fancy): Add option to change the link display texts #335

Merged
merged 1 commit into from
Feb 5, 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
13 changes: 12 additions & 1 deletion src/handlers/graphical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub struct GraphicalReportHandler {
pub(crate) word_separator: Option<textwrap::WordSeparator>,
pub(crate) word_splitter: Option<textwrap::WordSplitter>,
pub(crate) highlighter: MietteHighlighter,
pub(crate) link_display_text: Option<String>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
Expand All @@ -62,6 +63,7 @@ impl GraphicalReportHandler {
word_separator: None,
word_splitter: None,
highlighter: MietteHighlighter::default(),
link_display_text: None,
}
}

Expand All @@ -80,6 +82,7 @@ impl GraphicalReportHandler {
word_separator: None,
word_splitter: None,
highlighter: MietteHighlighter::default(),
link_display_text: None,
}
}

Expand Down Expand Up @@ -190,6 +193,13 @@ impl GraphicalReportHandler {
self.highlighter = MietteHighlighter::nocolor();
self
}

/// Sets the display text for links.
/// Miette displays `(link)` if this option is not set.
pub fn with_link_display_text(mut self, text: impl Into<String>) -> Self {
self.link_display_text = Some(text.into());
self
}
}

impl Default for GraphicalReportHandler {
Expand Down Expand Up @@ -246,11 +256,12 @@ impl GraphicalReportHandler {
} else {
"".to_string()
};
let display_text = self.link_display_text.as_deref().unwrap_or("(link)");
let link = format!(
"\u{1b}]8;;{}\u{1b}\\{}{}\u{1b}]8;;\u{1b}\\",
url,
code.style(severity_style),
"(link)".style(self.theme.styles.link)
display_text.style(self.theme.styles.link)
);
write!(header, "{}", link)?;
writeln!(f, "{}", header)?;
Expand Down
22 changes: 22 additions & 0 deletions tests/graphical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1341,6 +1341,28 @@ fn disable_url_links() -> Result<(), MietteError> {
Ok(())
}

#[test]
fn url_links_with_display_text() -> Result<(), MietteError> {
#[derive(Debug, Diagnostic, Error)]
#[error("oops!")]
#[diagnostic(
code(oops::my::bad),
help("try doing it better next time?"),
url("https://example.com")
)]
struct MyBad;
let err = MyBad;
let out = fmt_report_with_settings(err.into(), |handler| {
handler.with_link_display_text("Read the documentation")
});

println!("Error: {}", out);
assert!(out.contains("https://example.com"));
assert!(out.contains("Read the documentation"));
assert!(out.contains("oops::my::bad"));
Ok(())
}

#[test]
fn related() -> Result<(), MietteError> {
#[derive(Debug, Diagnostic, Error)]
Expand Down
Loading