getCursorPosition method
Implementation
ConsoleCoordinate getCursorPosition([bool flush = false]) {
final bool prevLineMode = _stdin.lineMode;
try {
_stdin.echoMode = false;
_stdin.lineMode = false;
_stdout.write(ConsoleStrings.deviceStatusReport);
final yBytes = <int>[];
final xBytes = <int>[];
int counter = 0;
bool readY = false;
bool readX = false;
// Parsing the response which is in "ESC[y;xR" format
while (counter < 32) {
final char = _stdin.readByteSync();
if (char == -1) {
break; // EOF
}
if (readX) {
if (char == 82) {
// found "R"
break;
}
xBytes.add(char);
} else if (readY) {
if (char == 59) {
// found ";"
readX = true;
continue;
}
yBytes.add(char);
} else if (char == 91) {
// found "["
readY = true;
}
counter++;
}
final int? x = int.tryParse(String.fromCharCodes(xBytes));
final int? y = int.tryParse(String.fromCharCodes(yBytes));
if (x == null || y == null) {
throw Exception(
'Unexpected cursor position report: ${String.fromCharCodes(yBytes)};${String.fromCharCodes(xBytes)}',
);
}
return ConsoleCoordinate(x, y);
} catch (e) {
rethrow;
} finally {
_stdin.echoMode = true;
_stdin.lineMode = prevLineMode;
}
}