truncate function
Shortens text to at most width columns, ending it with ellipsis.
Implementation
String truncate(String text, int width, {String ellipsis = '…'}) {
if (displayWidth(text) <= width) {
return text;
}
final limit = width - displayWidth(ellipsis);
if (limit < 0) {
return '';
}
final buffer = StringBuffer();
var used = 0;
for (final cell in _cells(text)) {
if (cell.width > 0 && used + cell.width > limit) {
break;
}
buffer.write(cell.text);
used += cell.width;
}
final reset = Style.enabled && text.contains('\x1B') ? '\x1B[0m' : '';
return '$buffer$reset$ellipsis';
}