Skip to content

Commit

Permalink
[tools] Fix heapsnapshot console crashes
Browse files Browse the repository at this point in the history
`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.

[email protected]

TEST=manually tested

Change-Id: Ie1a064e37b90bc4529ac4f5c1259642ad5680ca6
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/388340
Commit-Queue: Slava Egorov <[email protected]>
Reviewed-by: Martin Kustermann <[email protected]>
  • Loading branch information
mraleph authored and Commit Queue committed Oct 7, 2024
1 parent 06223ff commit d345fba
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,4 @@ logs/logs.json
logs/results.json
.dart_tool/bisect_dart/
doc/api/
runtime/tools/heapsnapshot/.dart_tool
13 changes: 6 additions & 7 deletions runtime/tools/heapsnapshot/lib/src/console.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -67,16 +68,14 @@ class SmartConsole extends Console {
}
}

cursorPosition = Coordinate(promptRow, 0);
drawPrompt(buffer);
}
}

void drawPrompt(LineEditBuffer buffer) {
const prefix = '(hsa) ';

final row = cursorPosition!.row;

moveCursorToColumn(0);
setForegroundColor(ConsoleColor.brightBlue);
write(prefix);
resetColorAttributes();
Expand All @@ -93,7 +92,7 @@ class SmartConsole extends Console {
write(buffer.text);
}

cursorPosition = Coordinate(row, prefix.length + buffer.index);
moveCursorToColumn(prefix.length + buffer.index);
}
}

Expand Down

0 comments on commit d345fba

Please sign in to comment.