layoutText function
Text laid out in rows of at most width columns, with where the cursor
falls.
Implementation
({List<String> rows, int row, int column}) layoutText(List<String> chars, int cursor, int width) {
final rows = <StringBuffer>[StringBuffer()];
var column = 0;
var cursorRow = 0;
var cursorColumn = 0;
for (var index = 0; index <= chars.length; index++) {
if (index == cursor) {
if (column >= width) {
rows.add(StringBuffer());
column = 0;
}
cursorRow = rows.length - 1;
cursorColumn = column;
}
if (index == chars.length) {
break;
}
final char = chars[index];
if (char == '\n') {
rows.add(StringBuffer());
column = 0;
continue;
}
final charWidth = runeWidth(char.runes.first);
if (column + charWidth > width) {
rows.add(StringBuffer());
column = 0;
}
rows.last.write(char);
column += charWidth;
}
return (rows: rows.map((row) => row.toString()).toList(), row: cursorRow, column: cursorColumn);
}