[tools] Fix heapsnapshot console crashes

`Console.cursorPosition` can return null when you are typing too
fast because it is implemented by sending an escape code to
the terminal and reading what it responds. This response is
naturally intermingled with normal input and the implementation
does nothing to guard against that.

We do not really need to know the cursor position though
we are simply interested in keeping cursor on the same row.
But this can be achieved by sending _horizontal position absolute_
which only changes column rather than _cursor position_
which sets both row and column.

R=kustermann@google.com

TEST=manually tested

Change-Id: Ie1a064e37b90bc4529ac4f5c1259642ad5680ca6
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/388340
Commit-Queue: Slava Egorov <vegorov@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
This commit is contained in:
Vyacheslav Egorov 2024-10-07 10:53:42 +00:00 committed by Commit Queue
parent 06223ff9f0
commit d345fbadd4
2 changed files with 7 additions and 7 deletions

1
.gitignore vendored
View file

@ -112,3 +112,4 @@ logs/logs.json
logs/results.json
.dart_tool/bisect_dart/
doc/api/
runtime/tools/heapsnapshot/.dart_tool

View file

@ -21,12 +21,13 @@ class SmartConsole extends Console {
];
}
void moveCursorToColumn(int column) {
write('\x1b[${column + 1}`');
}
ReadLineResult smartReadLine() {
final buffer = LineEditBuffer();
int promptRow = cursorPosition!.row;
cursorPosition = Coordinate(promptRow, 0);
drawPrompt(buffer);
while (true) {
@ -67,7 +68,6 @@ class SmartConsole extends Console {
}
}
cursorPosition = Coordinate(promptRow, 0);
drawPrompt(buffer);
}
}
@ -75,8 +75,7 @@ class SmartConsole extends Console {
void drawPrompt(LineEditBuffer buffer) {
const prefix = '(hsa) ';
final row = cursorPosition!.row;
moveCursorToColumn(0);
setForegroundColor(ConsoleColor.brightBlue);
write(prefix);
resetColorAttributes();
@ -93,7 +92,7 @@ class SmartConsole extends Console {
write(buffer.text);
}
cursorPosition = Coordinate(row, prefix.length + buffer.index);
moveCursorToColumn(prefix.length + buffer.index);
}
}