From 039f1cf853448de43e9cdb6d9e62c3d401679e52 Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Mon, 14 Sep 2020 17:57:04 -0700 Subject: [PATCH] [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 --- packages/flutter_tools/lib/src/base/terminal.dart | 9 ++++++--- .../test/general.shard/base/terminal_test.dart | 7 +++---- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/packages/flutter_tools/lib/src/base/terminal.dart b/packages/flutter_tools/lib/src/base/terminal.dart index 4d329af3ba..cbb4a47eb3 100644 --- a/packages/flutter_tools/lib/src/base/terminal.dart +++ b/packages/flutter_tools/lib/src/base/terminal.dart @@ -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 promptForCharInput( List acceptedCharacters, { @@ -278,7 +281,7 @@ class AnsiTerminal implements Terminal { assert(defaultChoiceIndex >= 0 && defaultChoiceIndex < acceptedCharacters.length); charactersToDisplay = List.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; diff --git a/packages/flutter_tools/test/general.shard/base/terminal_test.dart b/packages/flutter_tools/test/general.shard/base/terminal_test.dart index 66870a3690..6267c407dc 100644 --- a/packages/flutter_tools/test/general.shard/base/terminal_test.dart +++ b/packages/flutter_tools/test/general.shard/base/terminal_test.dart @@ -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', () {