From 2c43740346da954fd71653a079c53a1e9612c06f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kat=20March=C3=A1n?= Date: Thu, 16 Sep 2021 19:04:56 -0700 Subject: [PATCH] feat(theme): more styling changes --- src/handlers/graphical.rs | 20 ++++++++++++-------- src/protocol.rs | 2 +- tests/printer.rs | 16 +++++++++++++++- 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/src/handlers/graphical.rs b/src/handlers/graphical.rs index 32a4efd..7e52b9c 100644 --- a/src/handlers/graphical.rs +++ b/src/handlers/graphical.rs @@ -131,17 +131,21 @@ impl GraphicalReportHandler { let link = format!( "\u{1b}]8;;{}\u{1b}\\{}\u{1b}]8;;\u{1b}\\", url, - "(link)".style(self.theme.styles.filename) + format!( + "{} {}", + code.style(severity_style), + "(link)".style(self.theme.styles.link) + ) ); - write!(header, "[{} {}]", code.style(severity_style), link,)?; + write!(header, "[{}]", link)?; width += "(link)".width() + 3 } else if let Some(code) = diagnostic.code() { write!(header, "[{}", code.style(severity_style),)?; width += &code.to_string()[..].width() + 1; if let Some(link) = diagnostic.url() { - write!(header, " ({})]", link.style(self.theme.styles.filename))?; - width += &link.to_string()[..].width() + 3; + write!(header, " ({})]", link.style(self.theme.styles.link))?; + width += &link.to_string()[..].width() + 4; } else { write!(header, "]")?; width += 1; @@ -162,9 +166,9 @@ impl GraphicalReportHandler { fn render_causes(&self, f: &mut impl fmt::Write, diagnostic: &(dyn Diagnostic)) -> fmt::Result { let (severity_style, severity_icon) = match diagnostic.severity() { - Some(Severity::Error) | None => (self.theme.styles.error, self.theme.characters.error), - Some(Severity::Warning) => (self.theme.styles.warning, self.theme.characters.warning), - Some(Severity::Advice) => (self.theme.styles.advice, self.theme.characters.advice), + Some(Severity::Error) | None => (self.theme.styles.error, &self.theme.characters.error), + Some(Severity::Warning) => (self.theme.styles.warning, &self.theme.characters.warning), + Some(Severity::Advice) => (self.theme.styles.advice, &self.theme.characters.advice), }; let initial_indent = format!(" {} ", severity_icon.style(severity_style)); @@ -294,7 +298,7 @@ impl GraphicalReportHandler { self.theme.characters.hbar, )?; if let Some(source_name) = snippet.source.name() { - let source_name = source_name.style(self.theme.styles.filename); + let source_name = source_name.style(self.theme.styles.link); writeln!( f, "[{}:{}:{}]", diff --git a/src/protocol.rs b/src/protocol.rs index 5df665d..f6fd31a 100644 --- a/src/protocol.rs +++ b/src/protocol.rs @@ -40,7 +40,7 @@ pub trait Diagnostic: std::error::Error { None } - /// URL to visit for a more details explanation/help about this Diagnostic. + /// URL to visit for a more detailed explanation/help about this Diagnostic. fn url<'a>(&'a self) -> Option> { None } diff --git a/tests/printer.rs b/tests/printer.rs index 364ae46..ed673a4 100644 --- a/tests/printer.rs +++ b/tests/printer.rs @@ -629,6 +629,20 @@ fn url_links() -> Result<(), MietteError> { Ok(()) } +#[test] +fn url_links_no_code() -> Result<(), MietteError> { + #[derive(Debug, Diagnostic, Error)] + #[error("oops!")] + #[diagnostic(help("try doing it better next time?"), url("https://example.com"))] + struct MyBad; + let err = MyBad; + let out = fmt_report(err.into()); + println!("{}", out); + assert!(out.contains("https://example.com")); + assert!(out.contains("click for details")); + Ok(()) +} + #[test] fn disable_url_links() -> Result<(), MietteError> { #[derive(Debug, Diagnostic, Error)] @@ -646,7 +660,7 @@ fn disable_url_links() -> Result<(), MietteError> { .render_report(&mut out, &err) .unwrap(); println!("{}", out); - assert!(!out.contains("https://example.com")); + assert!(out.contains("url: https://example.com")); assert!(!out.contains("click for details")); assert!(out.contains("oops::my::bad")); Ok(())