feat(report): wrap multiline messages to keep formatting

Fixes: https://github.com/zkat/miette/issues/51
This commit is contained in:
Kat Marchán 2021-09-10 08:16:40 -07:00
parent 1ba3f2f5d2
commit f482dcec6a
No known key found for this signature in database
GPG Key ID: AEB529C08A3C7E9E
3 changed files with 26 additions and 9 deletions

View File

@ -18,6 +18,7 @@ once_cell = "1.8.0"
owo-colors = "2.0.0"
atty = "0.2.14"
ci_info = "0.14.2"
textwrap = "0.14.2"
[dev-dependencies]
semver = "1.0.4"

View File

@ -101,8 +101,13 @@ impl GraphicalReportHandler {
}
writeln!(f, "{}", self.theme.characters.hbar.to_string().repeat(20),)?;
writeln!(f)?;
write!(f, " {} ", severity_icon.style(severity_style))?;
writeln!(f, "{}", diagnostic,)?;
// TODO: terminal width support
let initial_indent = format!(" {} ", severity_icon.style(severity_style));
let rest_indent = format!(" {} ", self.theme.characters.vbar.style(severity_style));
let opts = textwrap::Options::new(80)
.initial_indent(&initial_indent)
.subsequent_indent(&rest_indent);
writeln!(f, "{}", textwrap::fill(&diagnostic.to_string(), opts))?;
Ok(())
}
@ -121,13 +126,19 @@ impl GraphicalReportHandler {
} else {
self.theme.characters.lbot
};
let prefix = format!(
" {}{}{}",
let initial_indent = format!(
" {}{}{} ",
char, self.theme.characters.hbar, self.theme.characters.rarrow
)
.style(severity_style)
.to_string();
writeln!(f, "{} {}", prefix, error)?;
let rest_indent = format!(" {} ", self.theme.characters.vbar)
.style(severity_style)
.to_string();
let opts = textwrap::Options::new(80)
.initial_indent(&initial_indent)
.subsequent_indent(&rest_indent);
writeln!(f, "{}", textwrap::fill(&error.to_string(), opts))?;
}
}

View File

@ -340,7 +340,7 @@ line5
#[test]
fn multiline_highlight_no_label() -> Result<(), MietteError> {
#[derive(Debug, Diagnostic, Error)]
#[error("wtf?!")]
#[error("wtf?!\nit broke :(")]
#[diagnostic(code(oops::my::bad), help("try doing it better next time?"))]
struct MyBad {
#[source]
@ -355,7 +355,7 @@ fn multiline_highlight_no_label() -> Result<(), MietteError> {
}
#[derive(Debug, Error)]
#[error("something went wrong")]
#[error("something went wrong\n\nHere's a more detailed explanation of everything that actually went wrong because it's actually important.\n")]
struct Inner(#[source] InnerInner);
#[derive(Debug, Error)]
@ -379,11 +379,16 @@ line5
};
let out = fmt_report(err.into());
println!("{}", out);
let expected = r#"
let expected = "
[oops::my::bad]
× wtf?!
it broke :(
something went wrong
\u{20}\u{20}\u{20}
Here's a more detailed explanation of everything that actually went
wrong because it's actually important.
\u{20}\u{20}\u{20}
very much went wrong
[bad_file.rs:1:1] This is the part that broke:
@ -396,7 +401,7 @@ line5
try doing it better next time?
"#
"
.trim_start()
.to_string();
assert_eq!(expected, out);