Skip to content

Commit

Permalink
feat(theme): more styling changes
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Sep 17, 2021
1 parent 12a9235 commit 9901030
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
20 changes: 12 additions & 8 deletions src/handlers/graphical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));
Expand Down Expand Up @@ -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,
"[{}:{}:{}]",
Expand Down
2 changes: 1 addition & 1 deletion src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Box<dyn Display + 'a>> {
None
}
Expand Down
16 changes: 15 additions & 1 deletion tests/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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(())
Expand Down

0 comments on commit 9901030

Please sign in to comment.