Simplify `textwrap::Options` construction

We're constructing `textwrap::Options` in a lot of places, leading to a
lot of duplicated code. Let's simplify it a bit.
This commit is contained in:
Rebecca Turner 2025-10-02 17:15:08 -07:00
parent df7bcfa17d
commit 53374ab324
1 changed files with 30 additions and 47 deletions

View File

@ -262,17 +262,7 @@ impl GraphicalReportHandler {
if let Some(footer) = &self.footer { if let Some(footer) = &self.footer {
writeln!(f)?; writeln!(f)?;
let width = self.termwidth.saturating_sub(2); let width = self.termwidth.saturating_sub(2);
let mut opts = textwrap::Options::new(width) let opts = self.textwrap_options(width);
.initial_indent(" ")
.subsequent_indent(" ")
.break_words(self.break_words);
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, "{}", self.wrap(footer, opts))?; writeln!(f, "{}", self.wrap(footer, opts))?;
} }
Ok(()) Ok(())
@ -340,16 +330,10 @@ impl GraphicalReportHandler {
let initial_indent = format!(" {} ", severity_icon.style(severity_style)); let initial_indent = format!(" {} ", severity_icon.style(severity_style));
let rest_indent = format!(" {} ", self.theme.characters.vbar.style(severity_style)); let rest_indent = format!(" {} ", self.theme.characters.vbar.style(severity_style));
let width = self.termwidth.saturating_sub(2); let width = self.termwidth.saturating_sub(2);
let mut opts = textwrap::Options::new(width) let opts = self
.textwrap_options(width)
.initial_indent(&initial_indent) .initial_indent(&initial_indent)
.subsequent_indent(&rest_indent) .subsequent_indent(&rest_indent);
.break_words(self.break_words);
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, "{}", self.wrap(&diagnostic.to_string(), opts))?; writeln!(f, "{}", self.wrap(&diagnostic.to_string(), opts))?;
@ -386,16 +370,10 @@ impl GraphicalReportHandler {
) )
.style(severity_style) .style(severity_style)
.to_string(); .to_string();
let mut opts = textwrap::Options::new(width) let opts = self
.textwrap_options(width)
.initial_indent(&initial_indent) .initial_indent(&initial_indent)
.subsequent_indent(&rest_indent) .subsequent_indent(&rest_indent);
.break_words(self.break_words);
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 { match error {
ErrorKind::Diagnostic(diag) => { ErrorKind::Diagnostic(diag) => {
@ -428,16 +406,10 @@ impl GraphicalReportHandler {
if let Some(help) = diagnostic.help() { if let Some(help) = diagnostic.help() {
let width = self.termwidth.saturating_sub(2); let width = self.termwidth.saturating_sub(2);
let initial_indent = " help: ".style(self.theme.styles.help).to_string(); let initial_indent = " help: ".style(self.theme.styles.help).to_string();
let mut opts = textwrap::Options::new(width) let opts = self
.textwrap_options(width)
.initial_indent(&initial_indent) .initial_indent(&initial_indent)
.subsequent_indent(" ") .subsequent_indent(" ");
.break_words(self.break_words);
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, "{}", self.wrap(&help.to_string(), opts))?; writeln!(f, "{}", self.wrap(&help.to_string(), opts))?;
} }
@ -489,16 +461,10 @@ impl GraphicalReportHandler {
.style(severity_style) .style(severity_style)
.to_string(); .to_string();
let mut opts = textwrap::Options::new(width) let opts = self
.textwrap_options(width)
.initial_indent(&initial_indent) .initial_indent(&initial_indent)
.subsequent_indent(&rest_indent) .subsequent_indent(&rest_indent);
.break_words(self.break_words);
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);
}
let mut inner = String::new(); let mut inner = String::new();
@ -1358,6 +1324,23 @@ impl GraphicalReportHandler {
} }
Ok((context_data, lines)) Ok((context_data, lines))
} }
fn textwrap_options(&self, width: usize) -> textwrap::Options<'static> {
textwrap::Options::new(width)
.initial_indent(" ")
.subsequent_indent(" ")
.word_separator(
self.word_separator
.clone()
.unwrap_or(textwrap::WordSeparator::AsciiSpace),
)
.word_splitter(
self.word_splitter
.clone()
.unwrap_or(textwrap::WordSplitter::NoHyphenation),
)
.break_words(self.break_words)
}
} }
impl ReportHandler for GraphicalReportHandler { impl ReportHandler for GraphicalReportHandler {