decode static method
Decodes a single chunk of input data into logical keys.
Terminals deliver complete escape sequences within a single read in raw mode, so a lone escape byte at the end of a chunk is interpreted as the Escape key.
Implementation
static List<TuiKey> decode(final List<int> data) {
final keys = <TuiKey>[];
var i = 0;
while (i < data.length) {
final byte = data[i];
if (byte == _esc) {
i = _decodeEscapeSequence(data, i, keys);
continue;
}
// Windows console prefixes special keys with 0x00 or 0xE0.
if ((byte == 0x00 || byte == 0xe0) && i + 1 < data.length) {
final key = _windowsSpecialKey(data[i + 1]);
if (key != null) {
keys.add(key);
i += 2;
continue;
}
}
final controlKey = _controlKey(byte);
if (controlKey != null) {
keys.add(controlKey);
i += 1;
continue;
}
// Printable characters, including multi-byte UTF-8 sequences.
if (byte > 0x20 && byte != 0x7f) {
final end = _printableRunEnd(data, i);
final text = utf8.decode(data.sublist(i, end), allowMalformed: true);
for (final rune in text.runes) {
keys.add(
TuiKey(TuiKeyType.character, character: String.fromCharCode(rune)),
);
}
i = end;
continue;
}
keys.add(const TuiKey(TuiKeyType.unknown));
i += 1;
}
return keys;
}