Unverified Commit 039f1cf8 authored by Jonah Williams's avatar Jonah Williams Committed by GitHub

[flutter_tools] handle terminals that do not support single char mode in...

[flutter_tools] handle terminals that do not support single char mode in Terminal.promptForCharInput (#65418)

Some terminals stdin do not support single char mode (like an emacs terminal buffer apparently). If this is the case, then the presented choice values would always have an appended newline \n which would prevent us from finding the value in the listed index (or using it as a signal for the default choice). To fix, treat '' as the default choice and always trim the choice value.

While this has seemingly always been broken, it wasn't noticeable until the multi-device selection prompt was added a few months ago.

Fixes #65267
parent 6acea15f
......@@ -123,6 +123,9 @@ abstract class Terminal {
/// will be the character in `acceptedCharacters` at the index given by
/// `defaultChoiceIndex`.
///
/// The accepted characters must be a String with a length of 1, excluding any
/// whitespace characters such as `\t`, `\n`, or ` `.
///
/// If [usesTerminalUi] is false, throws a [StateError].
Future<String> promptForCharInput(
List<String> acceptedCharacters, {
......@@ -278,7 +281,7 @@ class AnsiTerminal implements Terminal {
assert(defaultChoiceIndex >= 0 && defaultChoiceIndex < acceptedCharacters.length);
charactersToDisplay = List<String>.of(charactersToDisplay);
charactersToDisplay[defaultChoiceIndex] = bolden(charactersToDisplay[defaultChoiceIndex]);
acceptedCharacters.add('\n');
acceptedCharacters.add('');
}
String choice;
singleCharMode = true;
......@@ -291,11 +294,11 @@ class AnsiTerminal implements Terminal {
// prompt ends with ': '
logger.printStatus(': ', emphasis: true, newline: false);
}
choice = await keystrokes.first;
choice = (await keystrokes.first).trim();
logger.printStatus(choice);
}
singleCharMode = false;
if (defaultChoiceIndex != null && choice == '\n') {
if (defaultChoiceIndex != null && choice == '') {
choice = acceptedCharacters[defaultChoiceIndex];
}
return choice;
......
......@@ -143,7 +143,6 @@ void main() {
bufferLogger.statusText,
'Please choose something [a|b|c]: d\n'
'Please choose something [a|b|c]: \n'
'\n'
'Please choose something [a|b|c]: b\n');
});
......@@ -166,9 +165,9 @@ void main() {
expect(choice, 'b');
expect(
bufferLogger.statusText,
'Please choose something: \n'
'\n');
bufferLogger.statusText,
'Please choose something: \n'
);
});
testWithoutContext('Does not set single char mode when a terminal is not attached', () {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment