collapseMultilineString method
Returns this multiline string collapsed into a single line, truncated to
cropLength characters.
When appendEllipsis is true (default), an ellipsis is appended if
truncated.
Implementation
@useResult
String collapseMultilineString({
required int cropLength,
bool appendEllipsis = true,
}) {
if (isEmpty) {
return this;
}
final String collapsed = replaceAll(StringExtensions.newLine, ' ').replaceAll(' ', ' ');
if (collapsed.length <= cropLength) {
return collapsed.trim();
}
String cropped = collapsed.substringSafe(0, cropLength + 1);
while (cropped.isNotEmpty && !cropped.endsWithAny(StringExtensions.commonWordEndings)) {
cropped = cropped.substringSafe(0, cropped.length - 1);
}
if (cropped.isNotEmpty) {
cropped = cropped.substringSafe(0, cropped.length - 1).trim();
}
return appendEllipsis ? cropped + StringExtensions.ellipsis : cropped;
}