142 if (desc == null || desc.isBlank()) return builder.toString(); |
142 if (desc == null || desc.isBlank()) return builder.toString(); |
143 |
143 |
144 // Prepare the description by replacing some unwanted spaces |
144 // Prepare the description by replacing some unwanted spaces |
145 final var hdesc = desc.trim() |
145 final var hdesc = desc.trim() |
146 .replaceAll("\\r", "") |
146 .replaceAll("\\r", "") |
147 .replaceAll("\\n", " ") |
|
148 .replaceAll("\\t", " "); |
147 .replaceAll("\\t", " "); |
149 |
148 |
150 // Declare our frame where the description shall be placed |
149 // Declare our frame where the description shall be placed |
151 final int startx = 26; |
150 final int startx = 26; |
152 final int endx = 80; |
151 final int endx = 80; |
163 int x = startx; |
162 int x = startx; |
164 for (int i = 0 ; i < hdesc.length() ;) { |
163 for (int i = 0 ; i < hdesc.length() ;) { |
165 // get the next word and see where we would end |
164 // get the next word and see where we would end |
166 int s = hdesc.indexOf(' ', i); |
165 int s = hdesc.indexOf(' ', i); |
167 if (s < 0) s = hdesc.length(); |
166 if (s < 0) s = hdesc.length(); |
|
167 int n = hdesc.indexOf('\n', i); |
|
168 if (n < 0) n = hdesc.length(); |
|
169 s = Math.min(s, n); |
168 int l = s-i; |
170 int l = s-i; |
169 if (x + l > endx) { |
171 if (x + l > endx) { |
170 // does not fit, break into next line |
172 // does not fit, break into next line |
171 builder.append('\n'); |
173 builder.append('\n'); |
172 builder.append(" ".repeat(startx)); |
174 builder.append(" ".repeat(startx)); |
175 // append the word |
177 // append the word |
176 builder.append(hdesc, i, s); |
178 builder.append(hdesc, i, s); |
177 x += l; |
179 x += l; |
178 i += l; |
180 i += l; |
179 // append the next spaces |
181 // append the next spaces |
180 while (i < hdesc.length() && hdesc.charAt(i) == ' ') { |
182 while (i < hdesc.length()) { |
181 i++; |
183 int c = hdesc.charAt(i); |
182 x++; |
184 if (c == ' ') { |
183 // as long as we don't need to break, add the spaces |
185 // as long as we don't need to break, add the spaces |
184 if (x < endx) builder.append(' '); |
186 i++; |
|
187 x++; |
|
188 if (x < endx) builder.append(' '); |
|
189 } else if (c == '\n') { |
|
190 // if user wants us to break, comply |
|
191 i++; |
|
192 // if we have still space, just move to the end of the line |
|
193 if (x < endx) { |
|
194 x = endx; |
|
195 } else { |
|
196 // otherwise, we need to add an extra blank line |
|
197 builder.append('\n'); |
|
198 } |
|
199 } else { |
|
200 // we have found the next word, so continue with the outer loop |
|
201 break; |
|
202 } |
185 } |
203 } |
186 // break to new line, when spaces moved us outside the frame |
204 // break to new line, when spaces moved us outside the frame |
187 if (x > endx) { |
205 if (x > endx) { |
188 builder.append('\n'); |
206 builder.append('\n'); |
189 builder.append(" ".repeat(startx)); |
207 builder.append(" ".repeat(startx)); |