| 1 | use crate::utils::ColumnDisplayInfo; |
| 2 | |
| 3 | #[cfg(feature = "custom_styling")] |
| 4 | mod custom_styling; |
| 5 | #[cfg(not(feature = "custom_styling"))] |
| 6 | mod normal; |
| 7 | |
| 8 | #[cfg(feature = "custom_styling")] |
| 9 | pub use 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_line(line: &str, info: &ColumnDisplayInfo, delimiter: char) -> Vec<String> { |
| 24 | let mut lines = Vec::new(); |
| 25 | let content_width = usize::from(info.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_delimiter(line, delimiter); |
| 31 | |
| 32 | // Reverse it, since we want to push/pop without reversing the text. |
| 33 | elements.reverse(); |
| 34 | |
| 35 | let mut current_line = String::new(); |
| 36 | while let Some(next) = elements.pop() { |
| 37 | let current_length = measure_text_width(¤t_line); |
| 38 | let next_length = 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 + current_length; |
| 44 | if !current_line.is_empty() { |
| 45 | added_length += 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 - current_length; |
| 50 | if !current_line.is_empty() { |
| 51 | remaining_width = remaining_width.saturating_sub(1); |
| 52 | } |
| 53 | |
| 54 | // The next element fits into the current line |
| 55 | if added_length <= content_width { |
| 56 | // Only add delimiter, if we're not on a fresh line |
| 57 | if !current_line.is_empty() { |
| 58 | current_line.push(delimiter); |
| 59 | } |
| 60 | current_line += &next; |
| 61 | |
| 62 | // Already complete the current line, if there isn't space for more than two chars |
| 63 | current_line = 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 !current_line.is_empty() && remaining_width <= MIN_FREE_CHARS { |
| 74 | elements.push(next); |
| 75 | lines.push(current_line); |
| 76 | current_line = 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 > content_width { |
| 94 | let new_line = current_line.is_empty(); |
| 95 | |
| 96 | // Only add delimiter, if we're not on a fresh line |
| 97 | if !new_line { |
| 98 | current_line.push(delimiter); |
| 99 | } |
| 100 | |
| 101 | let (mut next, mut remaining) = 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_empty() { |
| 113 | let mut chars = remaining.chars(); |
| 114 | next.push(chars.next().unwrap()); |
| 115 | remaining = chars.collect(); |
| 116 | } |
| 117 | |
| 118 | current_line += &next; |
| 119 | elements.push(remaining); |
| 120 | |
| 121 | // Push the finished line, and start a new one |
| 122 | lines.push(current_line); |
| 123 | current_line = 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.push(current_line); |
| 132 | current_line = next.to_string(); |
| 133 | current_line = check_if_full(&mut lines, content_width, current_line); |
| 134 | } |
| 135 | |
| 136 | if !current_line.is_empty() { |
| 137 | lines.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_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_full(lines: &mut Vec<String>, content_width: usize, current_line: String) -> String { |
| 154 | // Already complete the current line, if there isn't space for more than two chars |
| 155 | if measure_text_width(¤t_line) > content_width.saturating_sub(MIN_FREE_CHARS) { |
| 156 | lines.push(current_line); |
| 157 | return String::new(); |
| 158 | } |
| 159 | |
| 160 | current_line |
| 161 | } |
| 162 | |
| 163 | #[cfg(test)] |
| 164 | mod tests { |
| 165 | use unicode_width::UnicodeWidthStr; |
| 166 | |
| 167 | use super::*; |
| 168 | |
| 169 | #[test] |
| 170 | fn test_split_long_word() { |
| 171 | let emoji = "🙂↕️"; // U+1F642 U+200D U+2195 U+FE0F head shaking vertically |
| 172 | assert_eq!(emoji.len(), 13); |
| 173 | assert_eq!(emoji.chars().count(), 4); |
| 174 | assert_eq!(emoji.width(), 2); |
| 175 | |
| 176 | let (word, remaining) = split_long_word(emoji.width(), emoji); |
| 177 | |
| 178 | assert_eq!(word, "\u{1F642}\u{200D}\u{2195}\u{FE0F}"); |
| 179 | assert_eq!(word.len(), 13); |
| 180 | assert_eq!(word.chars().count(), 4); |
| 181 | assert_eq!(word.width(), 2); |
| 182 | |
| 183 | assert!(remaining.is_empty()); |
| 184 | } |
| 185 | } |