Skip to content

Commit

Permalink
feat: improve options and update miette
Browse files Browse the repository at this point in the history
  • Loading branch information
aripiprazole committed Oct 3, 2024
1 parent a998931 commit a10b147
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "bupropion"
description = "Fancy error handler implementation for Miette"
version = "0.0.18"
version = "0.0.19"
edition = "2021"
authors = ["Gabrielle Guimarães de Oliveira"]
documentation = "https://github.com/aripiprazole/bupropion"
Expand All @@ -13,9 +13,9 @@ categories = ["parsing", "text-editors"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
miette = { version = "5.10.0", features = ["fancy"] }
miette = { version = "7.2.0", features = ["fancy"] }
thiserror = "1.0.46"
owo-colors = "3.5.0"
owo-colors = "4.1.0"
textwrap = "0.16.0"
supports-unicode = "2.0.0"
supports-color = "2.0.0"
Expand Down
38 changes: 24 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ impl ReportHandler for MietteHandler {
/// printer.
#[derive(Debug, Clone)]
pub struct GraphicalReportHandler {
pub(crate) spacious: bool,
pub(crate) links: LinkStyle,
pub(crate) termwidth: usize,
pub(crate) theme: GraphicalTheme,
Expand All @@ -414,6 +415,7 @@ impl GraphicalReportHandler {
/// [`GraphicalTheme`]. This will use both unicode characters and colors.
pub fn new() -> Self {
Self {
spacious: false,
links: LinkStyle::Link,
termwidth: 200,
theme: GraphicalTheme::default(),
Expand All @@ -427,6 +429,7 @@ impl GraphicalReportHandler {
///Create a new `GraphicalReportHandler` with a given [`GraphicalTheme`].
pub fn new_themed(theme: GraphicalTheme) -> Self {
Self {
spacious: false,
links: LinkStyle::Link,
termwidth: 200,
theme,
Expand All @@ -437,6 +440,12 @@ impl GraphicalReportHandler {
}
}

/// Make the output more spacious.
pub fn spacious(mut self) -> Self {
self.spacious = true;
self
}

/// Set the displayed tab width in spaces.
pub fn tab_width(mut self, width: usize) -> Self {
self.tab_width = width;
Expand Down Expand Up @@ -525,7 +534,7 @@ impl GraphicalReportHandler {
Some(Severity::Warning) => self.theme.styles.warning,
Some(Severity::Advice) => self.theme.styles.advice,
};
write!(f, "{}", "Failure".style(severity_style))?;
write!(f, "{}", "failure".style(severity_style))?;
self.render_header(f, diagnostic)?;
self.render_causes(f, diagnostic)?;
let src = diagnostic.source_code();
Expand Down Expand Up @@ -573,7 +582,7 @@ impl GraphicalReportHandler {
let url = diagnostic.url().unwrap(); // safe
write!(header, " ({})", url.style(self.theme.styles.link))?;
}
write!(header, "{}", format!("[{code}]: ").style(severity_style))?;
write!(header, "{}", format!("[{code}]:").style(severity_style))?;
write!(f, "{}", header)?;
}
Ok(())
Expand Down Expand Up @@ -640,7 +649,6 @@ impl GraphicalReportHandler {
write!(f, "{}", textwrap::fill(&help.to_string(), opts))?;
}
writeln!(f)?;
writeln!(f)?;
Ok(())
}

Expand All @@ -654,13 +662,13 @@ impl GraphicalReportHandler {
for rel in related {
match rel.severity() {
Some(Severity::Error) | None => {
write!(f, "{}", "Error".style(self.theme.styles.error))?
write!(f, "{}", "error".style(self.theme.styles.error))?
}
Some(Severity::Warning) => {
write!(f, "{}", "Warning".style(self.theme.styles.warning))?
write!(f, "{}", "warning".style(self.theme.styles.warning))?
}
Some(Severity::Advice) => {
write!(f, "{}", "Advice".style(self.theme.styles.advice))?
write!(f, "{}", "advice".style(self.theme.styles.advice))?
}
};
self.render_header(f, rel)?;
Expand Down Expand Up @@ -807,8 +815,10 @@ impl GraphicalReportHandler {
writeln!(f, "{}:{}", contents.line() + 1, contents.column() + 1)?;
}

self.write_no_linum(f, linum_width)?;
writeln!(f)?;
if self.spacious {
self.write_no_linum(f, linum_width)?;
writeln!(f)?;
}

// Now it's time for the fun part--actually rendering everything!
for line in &lines {
Expand Down Expand Up @@ -841,8 +851,6 @@ impl GraphicalReportHandler {
&single_line,
&labels,
)?;
self.write_no_linum(f, linum_width)?;
writeln!(f)?;
}
for hl in multi_line {
if hl.label().is_some() && line.span_ends(hl) && !line.span_starts(hl) {
Expand All @@ -855,8 +863,10 @@ impl GraphicalReportHandler {
}
}

self.write_no_linum(f, linum_width)?;
writeln!(f)?;
if self.spacious {
self.write_no_linum(f, linum_width)?;
writeln!(f)?;
}

write!(f, "{}", " ".repeat(linum_width + 2),)?;
Ok(())
Expand Down Expand Up @@ -1517,7 +1527,7 @@ mod tests {
#[diagnostic(code(test::failure))]
struct Failure {
#[source_code]
source_code: miette::NamedSource,
source_code: miette::NamedSource<String>,

#[label = "here"]
here: miette::SourceSpan,
Expand All @@ -1531,7 +1541,7 @@ mod tests {
let handler = BupropionHandlerOpts::new().build_inner().unwrap();
let mut output = String::new();
let failure = Failure {
source_code: NamedSource::new("Example.zu", include_str!("../Example.zu")),
source_code: NamedSource::new("Example.zu", include_str!("../Example.zu").to_string()),
here: miette::SourceSpan::from(7..10),
inner: vec![Inner {
here: miette::SourceSpan::from(7..10),
Expand Down

0 comments on commit a10b147

Please sign in to comment.