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) context_lines: Option<usize>,
pub(crate) tab_width: Option<usize>, pub(crate) tab_width: Option<usize>,
pub(crate) with_cause_chain: Option<bool>, pub(crate) with_cause_chain: Option<bool>,
pub(crate) break_words: Option<bool>,
} }
impl MietteHandlerOpts { impl MietteHandlerOpts {
@ -86,6 +87,16 @@ impl MietteHandlerOpts {
self 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. /// Include the cause chain of the top-level error in the report.
pub fn with_cause_chain(mut self) -> Self { pub fn with_cause_chain(mut self) -> Self {
self.with_cause_chain = Some(true); self.with_cause_chain = Some(true);
@ -233,6 +244,9 @@ impl MietteHandlerOpts {
if let Some(w) = self.tab_width { if let Some(w) = self.tab_width {
handler = handler.tab_width(w); handler = handler.tab_width(w);
} }
if let Some(b) = self.break_words {
handler = handler.with_break_words(b)
}
MietteHandler { MietteHandler {
inner: Box::new(handler), inner: Box::new(handler),
} }

View File

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

View File

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