From cfa26b3f31a21336b1b7eea9624930da65564fa7 Mon Sep 17 00:00:00 2001 From: Zanie Date: Tue, 14 Nov 2023 13:11:40 -0600 Subject: [PATCH] Add `break_words` setting to disable breaking of long words when wrapping text --- src/handler.rs | 14 ++++++++++++++ src/handlers/graphical.rs | 23 +++++++++++++++++++---- src/lib.rs | 1 + 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/handler.rs b/src/handler.rs index e983a55..2583c4a 100644 --- a/src/handler.rs +++ b/src/handler.rs @@ -55,6 +55,7 @@ pub struct MietteHandlerOpts { pub(crate) context_lines: Option, pub(crate) tab_width: Option, pub(crate) with_cause_chain: Option, + pub(crate) break_words: Option, } 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), } diff --git a/src/handlers/graphical.rs b/src/handlers/graphical.rs index 8cec88b..3985270 100644 --- a/src/handlers/graphical.rs +++ b/src/handlers/graphical.rs @@ -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(()) diff --git a/src/lib.rs b/src/lib.rs index 3cb021b..08de732 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -593,6 +593,7 @@ //! .unicode(false) //! .context_lines(3) //! .tab_width(4) +//! .break_words(true) //! .build(), //! ) //! }))