mirror of https://github.com/zkat/miette.git
feat(report): make header line as wide as terminal
This commit is contained in:
parent
bc72532465
commit
eaebde92cf
|
|
@ -20,6 +20,7 @@ atty = "0.2.14"
|
||||||
ci_info = "0.14.2"
|
ci_info = "0.14.2"
|
||||||
textwrap = "0.14.2"
|
textwrap = "0.14.2"
|
||||||
term_size = "0.3.2"
|
term_size = "0.3.2"
|
||||||
|
unicode-width = "0.1.8"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
semver = "1.0.4"
|
semver = "1.0.4"
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
use std::fmt;
|
use std::fmt::{self, Write};
|
||||||
|
|
||||||
use owo_colors::{OwoColorize, Style};
|
use owo_colors::{OwoColorize, Style};
|
||||||
|
use unicode_width::UnicodeWidthStr;
|
||||||
|
|
||||||
use crate::chain::Chain;
|
use crate::chain::Chain;
|
||||||
use crate::handlers::theme::*;
|
use crate::handlers::theme::*;
|
||||||
|
|
@ -96,7 +97,14 @@ impl GraphicalReportHandler {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render_header(&self, f: &mut impl fmt::Write, diagnostic: &(dyn Diagnostic)) -> fmt::Result {
|
fn render_header(&self, f: &mut impl fmt::Write, diagnostic: &(dyn Diagnostic)) -> fmt::Result {
|
||||||
write!(f, "{}", self.theme.characters.hbar.to_string().repeat(4))?;
|
let mut header = String::new();
|
||||||
|
let mut width = 0;
|
||||||
|
write!(
|
||||||
|
header,
|
||||||
|
"{}",
|
||||||
|
self.theme.characters.hbar.to_string().repeat(4)
|
||||||
|
)?;
|
||||||
|
width += UnicodeWidthStr::width(&header[..]);
|
||||||
if self.linkify_code && diagnostic.url().is_some() && diagnostic.code().is_some() {
|
if self.linkify_code && diagnostic.url().is_some() && diagnostic.code().is_some() {
|
||||||
let url = diagnostic.url().unwrap(); // safe
|
let url = diagnostic.url().unwrap(); // safe
|
||||||
let code = format!(
|
let code = format!(
|
||||||
|
|
@ -106,11 +114,24 @@ impl GraphicalReportHandler {
|
||||||
.expect("MIETTE BUG: already got checked for None")
|
.expect("MIETTE BUG: already got checked for None")
|
||||||
);
|
);
|
||||||
let link = format!("\u{1b}]8;;{}\u{1b}\\{}\u{1b}]8;;\u{1b}\\", url, code);
|
let link = format!("\u{1b}]8;;{}\u{1b}\\{}\u{1b}]8;;\u{1b}\\", url, code);
|
||||||
write!(f, "[{}]", link.style(self.theme.styles.code))?;
|
write!(header, "[{}]", link.style(self.theme.styles.code))?;
|
||||||
|
width += UnicodeWidthStr::width(&url.to_string()[..])
|
||||||
|
+ UnicodeWidthStr::width(&code[..])
|
||||||
|
+ 2;
|
||||||
} else if let Some(code) = diagnostic.code() {
|
} else if let Some(code) = diagnostic.code() {
|
||||||
write!(f, "[{}]", code.style(self.theme.styles.code))?;
|
write!(header, "[{}]", code.style(self.theme.styles.code))?;
|
||||||
|
width += UnicodeWidthStr::width(&code.to_string()[..]) + 2;
|
||||||
}
|
}
|
||||||
writeln!(f, "{}", self.theme.characters.hbar.to_string().repeat(20),)?;
|
writeln!(
|
||||||
|
f,
|
||||||
|
"{}{}",
|
||||||
|
header,
|
||||||
|
self.theme
|
||||||
|
.characters
|
||||||
|
.hbar
|
||||||
|
.to_string()
|
||||||
|
.repeat(self.termwidth.saturating_sub(width)),
|
||||||
|
)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -48,7 +48,7 @@ fn single_line_highlight() -> Result<(), MietteError> {
|
||||||
let out = fmt_report(err.into());
|
let out = fmt_report(err.into());
|
||||||
println!("{}", out);
|
println!("{}", out);
|
||||||
let expected = r#"
|
let expected = r#"
|
||||||
────[oops::my::bad]────────────────────
|
────[oops::my::bad]─────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
× oops!
|
× oops!
|
||||||
|
|
||||||
|
|
@ -91,7 +91,7 @@ fn single_line_highlight_offset_zero() -> Result<(), MietteError> {
|
||||||
let out = fmt_report(err.into());
|
let out = fmt_report(err.into());
|
||||||
println!("{}", out);
|
println!("{}", out);
|
||||||
let expected = r#"
|
let expected = r#"
|
||||||
────[oops::my::bad]────────────────────
|
────[oops::my::bad]─────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
× oops!
|
× oops!
|
||||||
|
|
||||||
|
|
@ -134,7 +134,7 @@ fn single_line_highlight_with_empty_span() -> Result<(), MietteError> {
|
||||||
let out = fmt_report(err.into());
|
let out = fmt_report(err.into());
|
||||||
println!("{}", out);
|
println!("{}", out);
|
||||||
let expected = r#"
|
let expected = r#"
|
||||||
────[oops::my::bad]────────────────────
|
────[oops::my::bad]─────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
× oops!
|
× oops!
|
||||||
|
|
||||||
|
|
@ -177,7 +177,7 @@ fn single_line_highlight_no_label() -> Result<(), MietteError> {
|
||||||
let out = fmt_report(err.into());
|
let out = fmt_report(err.into());
|
||||||
println!("{}", out);
|
println!("{}", out);
|
||||||
let expected = r#"
|
let expected = r#"
|
||||||
────[oops::my::bad]────────────────────
|
────[oops::my::bad]─────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
× oops!
|
× oops!
|
||||||
|
|
||||||
|
|
@ -222,7 +222,7 @@ fn multiple_same_line_highlights() -> Result<(), MietteError> {
|
||||||
let out = fmt_report(err.into());
|
let out = fmt_report(err.into());
|
||||||
println!("{}", out);
|
println!("{}", out);
|
||||||
let expected = r#"
|
let expected = r#"
|
||||||
────[oops::my::bad]────────────────────
|
────[oops::my::bad]─────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
× oops!
|
× oops!
|
||||||
|
|
||||||
|
|
@ -266,7 +266,7 @@ fn multiline_highlight_adjacent() -> Result<(), MietteError> {
|
||||||
let out = fmt_report(err.into());
|
let out = fmt_report(err.into());
|
||||||
println!("{}", out);
|
println!("{}", out);
|
||||||
let expected = r#"
|
let expected = r#"
|
||||||
────[oops::my::bad]────────────────────
|
────[oops::my::bad]─────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
× oops!
|
× oops!
|
||||||
|
|
||||||
|
|
@ -317,7 +317,7 @@ line5
|
||||||
let out = fmt_report(err.into());
|
let out = fmt_report(err.into());
|
||||||
println!("{}", out);
|
println!("{}", out);
|
||||||
let expected = r#"
|
let expected = r#"
|
||||||
────[oops::my::bad]────────────────────
|
────[oops::my::bad]─────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
× oops!
|
× oops!
|
||||||
|
|
||||||
|
|
@ -382,7 +382,7 @@ line5
|
||||||
let out = fmt_report(err.into());
|
let out = fmt_report(err.into());
|
||||||
println!("{}", out);
|
println!("{}", out);
|
||||||
let expected = "
|
let expected = "
|
||||||
────[oops::my::bad]────────────────────
|
────[oops::my::bad]─────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
× wtf?!
|
× wtf?!
|
||||||
│ it broke :(
|
│ it broke :(
|
||||||
|
|
@ -436,7 +436,7 @@ fn multiple_multiline_highlights_adjacent() -> Result<(), MietteError> {
|
||||||
let out = fmt_report(err.into());
|
let out = fmt_report(err.into());
|
||||||
println!("{}", out);
|
println!("{}", out);
|
||||||
let expected = r#"
|
let expected = r#"
|
||||||
────[oops::my::bad]────────────────────
|
────[oops::my::bad]─────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
× oops!
|
× oops!
|
||||||
|
|
||||||
|
|
@ -582,7 +582,7 @@ fn unnamed_snippet_shows_message() {
|
||||||
let out = fmt_report(err.into());
|
let out = fmt_report(err.into());
|
||||||
println!("{}", out);
|
println!("{}", out);
|
||||||
let expected = r#"
|
let expected = r#"
|
||||||
────[oops::my::bad]────────────────────
|
────[oops::my::bad]─────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
× oops!
|
× oops!
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue