Add `word_splitter` support

This commit is contained in:
Zanie 2023-11-14 16:13:13 -06:00
parent c7db906e64
commit 69fc5c0ba9
2 changed files with 31 additions and 1 deletions

View File

@ -57,6 +57,7 @@ pub struct MietteHandlerOpts {
pub(crate) with_cause_chain: Option<bool>,
pub(crate) break_words: Option<bool>,
pub(crate) word_separator: Option<textwrap::WordSeparator>,
pub(crate) word_splitter: Option<textwrap::WordSplitter>,
}
impl MietteHandlerOpts {
@ -104,6 +105,11 @@ impl MietteHandlerOpts {
self
}
/// Sets the `textwrap::WordSplitter` to use when determining wrap points.
pub fn word_splitter(mut self, word_splitter: textwrap::WordSplitter) -> Self {
self.word_splitter = Some(word_splitter);
self
}
/// Include the cause chain of the top-level error in the report.
pub fn with_cause_chain(mut self) -> Self {
self.with_cause_chain = Some(true);
@ -257,6 +263,9 @@ impl MietteHandlerOpts {
if let Some(s) = self.word_separator {
handler = handler.with_word_separator(s)
}
if let Some(s) = self.word_splitter {
handler = handler.with_word_splitter(s)
}
MietteHandler {
inner: Box::new(handler),

View File

@ -32,6 +32,7 @@ pub struct GraphicalReportHandler {
pub(crate) with_cause_chain: bool,
pub(crate) break_words: bool,
pub(crate) word_separator: Option<textwrap::WordSeparator>,
pub(crate) word_splitter: Option<textwrap::WordSplitter>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
@ -55,6 +56,7 @@ impl GraphicalReportHandler {
with_cause_chain: true,
break_words: true,
word_separator: None,
word_splitter: None,
}
}
@ -70,6 +72,7 @@ impl GraphicalReportHandler {
with_cause_chain: true,
break_words: true,
word_separator: None,
word_splitter: None,
}
}
@ -134,12 +137,18 @@ impl GraphicalReportHandler {
self
}
/// Sets the word separator when wrapping.
/// Sets the word separator to use when wrapping.
pub fn with_word_separator(mut self, word_separator: textwrap::WordSeparator) -> Self {
self.word_separator = Some(word_separator);
self
}
/// Sets the word splitter to usewhen wrapping.
pub fn with_word_splitter(mut self, word_splitter: textwrap::WordSplitter) -> Self {
self.word_splitter = Some(word_splitter);
self
}
/// Sets the 'global' footer for this handler.
pub fn with_footer(mut self, footer: String) -> Self {
self.footer = Some(footer);
@ -184,6 +193,9 @@ impl GraphicalReportHandler {
if let Some(word_separator) = self.word_separator {
opts = opts.word_separator(word_separator);
}
if let Some(word_splitter) = self.word_splitter.clone() {
opts = opts.word_splitter(word_splitter);
}
writeln!(f, "{}", textwrap::fill(footer, opts))?;
}
@ -242,6 +254,9 @@ impl GraphicalReportHandler {
if let Some(word_separator) = self.word_separator {
opts = opts.word_separator(word_separator);
}
if let Some(word_splitter) = self.word_splitter.clone() {
opts = opts.word_splitter(word_splitter);
}
writeln!(f, "{}", textwrap::fill(&diagnostic.to_string(), opts))?;
@ -285,6 +300,9 @@ impl GraphicalReportHandler {
if let Some(word_separator) = self.word_separator {
opts = opts.word_separator(word_separator);
}
if let Some(word_splitter) = self.word_splitter.clone() {
opts = opts.word_splitter(word_splitter);
}
match error {
ErrorKind::Diagnostic(diag) => {
@ -319,6 +337,9 @@ impl GraphicalReportHandler {
if let Some(word_separator) = self.word_separator {
opts = opts.word_separator(word_separator);
}
if let Some(word_splitter) = self.word_splitter.clone() {
opts = opts.word_splitter(word_splitter);
}
writeln!(f, "{}", textwrap::fill(&help.to_string(), opts))?;
}