tracing_human_layer/
textwrap.rs1use std::borrow::Cow;
4
5use textwrap::Options;
6use textwrap::WordSeparator;
7use textwrap::WordSplitter;
8
9pub fn options<'a>() -> Options<'a> {
11 let opts = Options::with_termwidth()
12 .break_words(false)
13 .word_separator(WordSeparator::AsciiSpace)
14 .word_splitter(WordSplitter::NoHyphenation);
15
16 if cfg!(test) {
18 opts.with_width(80)
19 } else {
20 opts
21 }
22}
23
24pub trait TextWrapOptionsExt {
26 fn subtract_width(self, decrease: usize) -> Self;
28
29 fn with_width(self, width: usize) -> Self;
31
32 fn wrap<'s>(&self, text: &'s str) -> Vec<Cow<'s, str>>;
34
35 fn fill(&self, text: &str) -> String;
39}
40
41impl<'a> TextWrapOptionsExt for Options<'a> {
42 fn subtract_width(mut self, decrease: usize) -> Self {
43 self.width -= decrease;
44 self
45 }
46
47 fn with_width(mut self, width: usize) -> Self {
48 self.width = width;
49 self
50 }
51
52 fn wrap<'s>(&self, text: &'s str) -> Vec<Cow<'s, str>> {
53 textwrap::wrap(text, self)
54 }
55
56 fn fill(&self, text: &str) -> String {
57 textwrap::fill(text, self)
58 }
59}