• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use super::core::display_width;
2 
3 #[derive(Debug)]
4 pub(crate) struct LineWrapper {
5     line_width: usize,
6     hard_width: usize,
7 }
8 
9 impl LineWrapper {
new(hard_width: usize) -> Self10     pub(crate) fn new(hard_width: usize) -> Self {
11         Self {
12             line_width: 0,
13             hard_width,
14         }
15     }
16 
reset(&mut self)17     pub(crate) fn reset(&mut self) {
18         self.line_width = 0;
19     }
20 
wrap<'w>(&mut self, mut words: Vec<&'w str>) -> Vec<&'w str>21     pub(crate) fn wrap<'w>(&mut self, mut words: Vec<&'w str>) -> Vec<&'w str> {
22         let mut i = 0;
23         while i < words.len() {
24             let word = &words[i];
25             let trimmed = word.trim_end();
26             let word_width = display_width(trimmed);
27             let trimmed_delta = word.len() - trimmed.len();
28             if i != 0 && self.hard_width < self.line_width + word_width {
29                 if 0 < i {
30                     let last = i - 1;
31                     let trimmed = words[last].trim_end();
32                     words[last] = trimmed;
33                 }
34                 words.insert(i, "\n");
35                 i += 1;
36                 self.reset();
37             }
38             self.line_width += word_width + trimmed_delta;
39 
40             i += 1;
41         }
42         words
43     }
44 }
45