Add `break_words` setting to disable breaking of long words when wrapping text

This commit is contained in:
Zanie 2023-11-14 13:11:40 -06:00
parent c7ba5b7e52
commit cfa26b3f31
3 changed files with 34 additions and 4 deletions

View File

@ -55,6 +55,7 @@ pub struct MietteHandlerOpts {
pub(crate) context_lines: Option<usize>,
pub(crate) tab_width: Option<usize>,
pub(crate) with_cause_chain: Option<bool>,
pub(crate) break_words: Option<bool>,
}
impl MietteHandlerOpts {
@ -86,6 +87,16 @@ impl MietteHandlerOpts {
self
}
/// If true, long words can be broken when wrapping.
///
/// If false, long words will not be broken when they exceed the width.
///
/// Defaults to true.
pub fn break_words(mut self, break_words: bool) -> Self {
self.break_words = Some(break_words);
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);
@ -233,6 +244,9 @@ impl MietteHandlerOpts {
if let Some(w) = self.tab_width {
handler = handler.tab_width(w);
}
if let Some(b) = self.break_words {
handler = handler.with_break_words(b)
}
MietteHandler {
inner: Box::new(handler),
}

View File

@ -30,6 +30,7 @@ pub struct GraphicalReportHandler {
pub(crate) context_lines: usize,
pub(crate) tab_width: usize,
pub(crate) with_cause_chain: bool,
pub(crate) break_words: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
@ -51,6 +52,7 @@ impl GraphicalReportHandler {
context_lines: 1,
tab_width: 4,
with_cause_chain: true,
break_words: true,
}
}
@ -64,6 +66,7 @@ impl GraphicalReportHandler {
context_lines: 1,
tab_width: 4,
with_cause_chain: true,
break_words: true,
}
}
@ -122,6 +125,12 @@ impl GraphicalReportHandler {
self
}
/// Enables or disables breaking of words during wrapping.
pub fn with_break_words(mut self, break_words: bool) -> Self {
self.break_words = break_words;
self
}
/// Sets the 'global' footer for this handler.
pub fn with_footer(mut self, footer: String) -> Self {
self.footer = Some(footer);
@ -161,7 +170,8 @@ impl GraphicalReportHandler {
let width = self.termwidth.saturating_sub(4);
let opts = textwrap::Options::new(width)
.initial_indent(" ")
.subsequent_indent(" ");
.subsequent_indent(" ")
.break_words(self.break_words);
writeln!(f, "{}", textwrap::fill(footer, opts))?;
}
Ok(())
@ -214,7 +224,8 @@ impl GraphicalReportHandler {
let width = self.termwidth.saturating_sub(2);
let opts = textwrap::Options::new(width)
.initial_indent(&initial_indent)
.subsequent_indent(&rest_indent);
.subsequent_indent(&rest_indent)
.break_words(self.break_words);
writeln!(f, "{}", textwrap::fill(&diagnostic.to_string(), opts))?;
@ -253,7 +264,9 @@ impl GraphicalReportHandler {
.to_string();
let opts = textwrap::Options::new(width)
.initial_indent(&initial_indent)
.subsequent_indent(&rest_indent);
.subsequent_indent(&rest_indent)
.break_words(self.break_words);
match error {
ErrorKind::Diagnostic(diag) => {
let mut inner = String::new();
@ -282,7 +295,9 @@ impl GraphicalReportHandler {
let initial_indent = " help: ".style(self.theme.styles.help).to_string();
let opts = textwrap::Options::new(width)
.initial_indent(&initial_indent)
.subsequent_indent(" ");
.subsequent_indent(" ")
.break_words(self.break_words);
writeln!(f, "{}", textwrap::fill(&help.to_string(), opts))?;
}
Ok(())

View File

@ -593,6 +593,7 @@
//! .unicode(false)
//! .context_lines(3)
//! .tab_width(4)
//! .break_words(true)
//! .build(),
//! )
//! }))