Debian Codegraph @

src/utils/formatting/content_split/mod.rs

raw symbols SCIP
1 use craterust-analyzer cargo comfy-table 7.2.2 crate/::utilsrust-analyzer cargo comfy-table 7.2.2 utils/::ColumnDisplayInforust-analyzer cargo comfy-table 7.2.2 utils/ColumnDisplayInfo#;
2
3 #[cfg(feature = "custom_styling")]
4 mod custom_stylingrust-analyzer cargo comfy-table 7.2.2 utils/formatting/content_split/custom_styling/;
5 #[cfg(not(feature = "custom_styling"))]
6 mod normal;
7
8 #[cfg(feature = "custom_styling")]
9 pub use custom_stylingrust-analyzer cargo comfy-table 7.2.2 utils/formatting/content_split/custom_styling/::*;
10 #[cfg(not(feature = "custom_styling"))]
11 pub use normal::*;
12
13 /// Split a line if it's longer than the allowed columns (width - padding).
14 ///
15 /// This function tries to do this in a smart way, by splitting the content
16 /// with a given delimiter at the very beginning.
17 /// These "elements" then get added one-by-one to the lines, until a line is full.
18 /// As soon as the line is full, we add it to the result set and start a new line.
19 ///
20 /// This is repeated until there are no more "elements".
21 ///
22 /// Mid-element splits only occurs if an element doesn't fit in a single line by itself.
23 pub fn split_linerust-analyzer cargo comfy-table 7.2.2 utils/formatting/content_split/split_line().(line: &str, info: &ColumnDisplayInforust-analyzer cargo comfy-table 7.2.2 utils/ColumnDisplayInfo#, delimiter: char) -> Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> {
24 let mut lines = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().();
25 let content_width = usize::fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/num/impl#[usize][`From<u16>`]from().(info.content_widthrust-analyzer cargo comfy-table 7.2.2 utils/ColumnDisplayInfo#content_width.);
26
27 // Split the line by the given deliminator and turn the content into a stack.
28 // Also clone it and convert it into a Vec<String>. Otherwise, we get some burrowing problems
29 // due to early drops of borrowed values that need to be inserted into `Vec<&str>`
30 let mut elements = split_line_by_delimiterrust-analyzer cargo comfy-table 7.2.2 utils/formatting/content_split/custom_styling/split_line_by_delimiter().(line, delimiter);
31
32 // Reverse it, since we want to push/pop without reversing the text.
33 elements.reverserust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]reverse().();
34
35 let mut current_line = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]new().();
36 while let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(next) = elements.poprust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]pop().() {
37 let current_length = measure_text_widthrust-analyzer cargo comfy-table 7.2.2 utils/formatting/content_split/custom_styling/measure_text_width().(&current_line);
38 let next_length = measure_text_widthrust-analyzer cargo comfy-table 7.2.2 utils/formatting/content_split/custom_styling/measure_text_width().(&next);
39
40 // Some helper variables
41 // The length of the current line when combining it with the next element
42 // Add 1 for the delimiter if we are on a non-empty line.
43 let mut added_length = next_length rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().current_length;
44 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().current_line.is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]is_empty().() {
45 added_length rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().1;
46 }
47 // The remaining width for this column. If we are on a non-empty line, subtract 1 for the
48 // delimiter.
49 let mut remaining_width = content_width rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().current_length;
50 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().current_line.is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]is_empty().() {
51 remaining_width = remaining_width.saturating_subrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]saturating_sub().(1);
52 }
53
54 // The next element fits into the current line
55 if added_length rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]le().<=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]le(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]le().content_width {
56 // Only add delimiter, if we're not on a fresh line
57 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().current_line.is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]is_empty().() {
58 current_line.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().(delimiter);
59 }
60 current_line rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/AddAssign#add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/AddAssign#add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/AddAssign#add_assign().&next;
61
62 // Already complete the current line, if there isn't space for more than two chars
63 current_line = check_if_fullrust-analyzer cargo comfy-table 7.2.2 utils/formatting/content_split/check_if_full().(&mut lines, content_width, current_line);
64 continue;
65 }
66
67 // The next element doesn't fit in the current line
68
69 // Check, if there's enough space in the current line in case we decide to split the
70 // element and only append a part of it to the current line.
71 // If there isn't enough space, we simply push the current line, put the element back
72 // on stack and start with a fresh line.
73 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().current_line.is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]is_empty().() && remaining_width rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]le().<=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]le(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]le().MIN_FREE_CHARSrust-analyzer cargo comfy-table 7.2.2 utils/formatting/content_split/MIN_FREE_CHARS. {
74 elements.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(next);
75 lines.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(current_line);
76 current_line = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]new().();
77
78 continue;
79 }
80
81 // Ok. There's still enough space to fit something in (more than MIN_FREE_CHARS characters)
82 // There are two scenarios:
83 //
84 // 1. The word is too long for a single line. In this case, we have to split the element
85 // anyway. Let's fill the remaining space on the current line with, start a new line and
86 // push the remaining part on the stack.
87 // 2. The word is short enough to fit as a whole into a line In that case we simply push the
88 // current line and start a new one with the current element
89
90 // Case 1
91 // The element is longer than the specified content_width
92 // Split the word, push the remaining string back on the stack
93 if next_length rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().content_width {
94 let new_line = current_line.is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]is_empty().();
95
96 // Only add delimiter, if we're not on a fresh line
97 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().new_line {
98 current_line.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().(delimiter);
99 }
100
101 let (mut next, mut remaining) = split_long_wordrust-analyzer cargo comfy-table 7.2.2 utils/formatting/content_split/custom_styling/split_long_word().(remaining_width, &next);
102
103 // This is an ugly hack, but it's needed for now.
104 //
105 // Scenario: The current column has to have a width of 1, and we work with a new line.
106 // However, the next char is a multi-character UTF-8 symbol.
107 //
108 // Since a multi-character wide symbol doesn't fit into a 1-character column,
109 // this code would loop endlessly. (There's no legitimate way to split that character.)
110 // Hence, we have to live with the fact, that this line will look broken, as we put a
111 // two-character wide symbol into it, despite the line being formatted for 1 character.
112 if new_line && next.is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]is_empty().() {
113 let mut chars = remaining.charsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]chars().();
114 next.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().(chars.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`Chars<'a>`][Iterator]next().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().());
115 remaining = chars.collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().();
116 }
117
118 current_line rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/AddAssign#add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/AddAssign#add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/AddAssign#add_assign().&next;
119 elements.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(remaining);
120
121 // Push the finished line, and start a new one
122 lines.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(current_line);
123 current_line = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]new().();
124
125 continue;
126 }
127
128 // Case 2
129 // The element fits into a single line.
130 // Push the current line and initialize the next line with the element.
131 lines.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(current_line);
132 current_line = next.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().();
133 current_line = check_if_fullrust-analyzer cargo comfy-table 7.2.2 utils/formatting/content_split/check_if_full().(&mut lines, content_width, current_line);
134 }
135
136 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().current_line.is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]is_empty().() {
137 lines.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(current_line);
138 }
139
140 lines
141 }
142
143 /// This is the minimum of available characters per line.
144 /// It's used to check, whether another element can be added to the current line.
145 /// Otherwise, the line will simply be left as it is, and we start with a new one.
146 /// Two chars seems like a reasonable approach, since this would require next element to be
147 /// a single char + delimiter.
148 const MIN_FREE_CHARSrust-analyzer cargo comfy-table 7.2.2 utils/formatting/content_split/MIN_FREE_CHARS.: usize = 2;
149
150 /// Check if the current line is too long and whether we should start a new one
151 /// If it's too long, we add the current line to the list of lines and return a new [String].
152 /// Otherwise, we simply return the current line and basically don't do anything.
153 fn check_if_fullrust-analyzer cargo comfy-table 7.2.2 utils/formatting/content_split/check_if_full().(lines: &mut Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>, content_width: usize, current_line: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# {
154 // Already complete the current line, if there isn't space for more than two chars
155 if measure_text_widthrust-analyzer cargo comfy-table 7.2.2 utils/formatting/content_split/custom_styling/measure_text_width().(&current_line) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().content_width.saturating_subrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]saturating_sub().(MIN_FREE_CHARSrust-analyzer cargo comfy-table 7.2.2 utils/formatting/content_split/MIN_FREE_CHARS.) {
156 lines.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(current_line);
157 return Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]new().();
158 }
159
160 current_line
161 }
162
163 #[cfg(test)]
164 mod testsrust-analyzer cargo comfy-table 7.2.2 utils/formatting/content_split/tests/ {
165 use unicode_widthrust-analyzer cargo unicode-width 0.2.2 crate/::UnicodeWidthStrrust-analyzer cargo unicode-width 0.2.2 UnicodeWidthStr#;
166
167 use superrust-analyzer cargo comfy-table 7.2.2 utils/formatting/content_split/::*;
168
169 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
170 fn test_split_long_wordrust-analyzer cargo comfy-table 7.2.2 utils/formatting/content_split/tests/test_split_long_word().() {
171 let emoji = "🙂‍↕️"; // U+1F642 U+200D U+2195 U+FE0F head shaking vertically
172 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(emoji.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().(), 13);
173 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(emoji.charsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]chars().().countrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`Chars<'a>`][Iterator]count().(), 4);
174 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(emoji.widthrust-analyzer cargo unicode-width 0.2.2 impl#[str][UnicodeWidthStr]width().(), 2);
175
176 let (word, remaining) = split_long_wordrust-analyzer cargo comfy-table 7.2.2 utils/formatting/content_split/custom_styling/split_long_word().(emoji.widthrust-analyzer cargo unicode-width 0.2.2 impl#[str][UnicodeWidthStr]width().(), emoji);
177
178 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(word, "\u{1F642}\u{200D}\u{2195}\u{FE0F}");
179 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(word.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]len().(), 13);
180 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(word.charsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]chars().().countrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`Chars<'a>`][Iterator]count().(), 4);
181 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(word.widthrust-analyzer cargo unicode-width 0.2.2 impl#[str][UnicodeWidthStr]width().(), 2);
182
183 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(remaining.is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]is_empty().());
184 }
185 }