Unverified Commit 6d46ff7e authored by includecmath's avatar includecmath Committed by GitHub

[flutter_tools] flutter run should allow exit when make choose for devices. (#64403)

parent 0b4dad65
......@@ -285,6 +285,7 @@ class AnsiTerminal implements Terminal {
if (displayAcceptedCharacters) {
logger.printStatus(' [${charactersToDisplay.join("|")}]', newline: false);
}
// prompt ends with ': '
logger.printStatus(': ', emphasis: true, newline: false);
}
choice = await keystrokes.first;
......
......@@ -256,7 +256,7 @@ class UserMessages {
'Found $count devices with name or id matching $deviceId:';
String get flutterMultipleDevicesFound => 'Multiple devices found:';
String flutterChooseDevice(int option, String name, String deviceId) => '[$option]: $name ($deviceId)';
String get flutterChooseOne => 'Please choose one:';
String get flutterChooseOne => 'Please choose one (To quit, press "q/Q")';
String get flutterSpecifyDeviceWithAllOption =>
'More than one device connected; please specify a device with '
"the '-d <deviceId>' flag, or use '-d all' to act on all devices.";
......
......@@ -14,6 +14,7 @@ import 'android/android_sdk.dart';
import 'android/android_workflow.dart';
import 'application_package.dart';
import 'artifacts.dart';
import 'base/common.dart';
import 'base/config.dart';
import 'base/context.dart';
import 'base/dds.dart';
......@@ -283,6 +284,9 @@ abstract class DeviceManager {
Future<Device> _chooseOneOfAvailableDevices(List<Device> devices) async {
_displayDeviceOptions(devices);
final String userInput = await _readUserInput(devices.length);
if (userInput.toLowerCase() == 'q') {
throwToolExit('');
}
return devices[int.parse(userInput)];
}
......@@ -297,7 +301,8 @@ abstract class DeviceManager {
Future<String> _readUserInput(int deviceCount) async {
globals.terminal.usesTerminalUi = true;
final String result = await globals.terminal.promptForCharInput(
<String>[ for (int i = 0; i < deviceCount; i++) '$i' ],
<String>[ for (int i = 0; i < deviceCount; i++) '$i', 'q', 'Q'],
displayAcceptedCharacters: false,
logger: globals.logger,
prompt: userMessages.flutterChooseOne);
return result;
......
......@@ -4,6 +4,7 @@
import 'dart:async';
import 'package:flutter_tools/src/base/common.dart';
import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/base/terminal.dart';
import 'package:flutter_tools/src/artifacts.dart';
......@@ -208,7 +209,8 @@ void main() {
];
when(mockStdio.stdinHasTerminal).thenReturn(true);
when(globals.terminal.promptForCharInput(<String>['0', '1'],
when(globals.terminal.promptForCharInput(<String>['0', '1', 'q', 'Q'],
displayAcceptedCharacters: false,
logger: globals.logger,
prompt: globals.userMessages.flutterChooseOne)
).thenAnswer((Invocation invocation) async => '0');
......@@ -232,8 +234,9 @@ void main() {
];
when(mockStdio.stdinHasTerminal).thenReturn(true);
when(globals.terminal.promptForCharInput(<String>['0', '1'],
logger: globals.logger,
when(globals.terminal.promptForCharInput(<String>['0', '1', 'q', 'Q'],
displayAcceptedCharacters: false,
logger: globals.logger,
prompt: globals.userMessages.flutterChooseOne)
).thenAnswer((Invocation invocation) async => '1');
......@@ -256,8 +259,9 @@ void main() {
];
when(mockStdio.stdinHasTerminal).thenReturn(true);
when(globals.terminal.promptForCharInput(<String>['0', '1'],
logger: globals.logger,
when(globals.terminal.promptForCharInput(<String>['0', '1', 'q', 'Q'],
displayAcceptedCharacters: false,
logger: globals.logger,
prompt: globals.userMessages.flutterChooseOne)
).thenAnswer((Invocation invocation) async => '0');
......@@ -280,8 +284,9 @@ void main() {
];
when(mockStdio.stdinHasTerminal).thenReturn(true);
when(globals.terminal.promptForCharInput(<String>['0', '1'],
logger: globals.logger,
when(globals.terminal.promptForCharInput(<String>['0', '1', 'q', 'Q'],
displayAcceptedCharacters: false,
logger: globals.logger,
prompt: globals.userMessages.flutterChooseOne)
).thenAnswer((Invocation invocation) async => '1');
......@@ -307,7 +312,8 @@ void main() {
];
when(mockStdio.stdinHasTerminal).thenReturn(true);
when(globals.terminal.promptForCharInput(<String>['0', '1', '2', '3'],
when(globals.terminal.promptForCharInput(<String>['0', '1', '2', '3', 'q', 'Q'],
displayAcceptedCharacters: false,
logger: globals.logger,
prompt: globals.userMessages.flutterChooseOne)
).thenAnswer((Invocation invocation) async => '2');
......@@ -325,6 +331,33 @@ void main() {
Cache: () => cache,
});
testUsingContext('exit from choose one of available devices', () async {
final List<Device> devices = <Device>[
ephemeralOne,
ephemeralTwo,
];
when(mockStdio.stdinHasTerminal).thenReturn(true);
when(globals.terminal.promptForCharInput(<String>['0', '1', 'q', 'Q'],
displayAcceptedCharacters: false,
logger: globals.logger,
prompt: globals.userMessages.flutterChooseOne)
).thenAnswer((Invocation invocation) async => 'q');
try {
final DeviceManager deviceManager = TestDeviceManager(devices);
await deviceManager.findTargetDevices(FlutterProject.current());
} on ToolExit catch (e) {
expect(e.exitCode, null);
expect(e.message, '');
}
}, overrides: <Type, Generator>{
Stdio: () => mockStdio,
AnsiTerminal: () => MockTerminal(),
Artifacts: () => Artifacts.test(),
Cache: () => cache,
});
testUsingContext('Removes a single unsupported device', () async {
final List<Device> devices = <Device>[
unsupported,
......
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