confirm method
Prompts user with a yes/no question.
This method requires a terminal to be attached to stdout. See https://api.dart.dev/stable/dart-io/Stdout/hasTerminal.html.
Implementation
bool confirm(String? message, {bool defaultValue = false}) {
final suffix = ' ${darkGray.wrap('(${defaultValue.toYesNo()})')}';
final resolvedMessage = '$message$suffix ';
_stdout.write(resolvedMessage);
String? input;
try {
input = _readLineSync();
} on FormatException catch (_) {
// FormatExceptions can occur due to utf8 decoding errors
// so we treat them as the user pressing enter (e.g. use `defaultValue`).
_stdout.writeln();
}
final response = input == null || input.isEmpty
? defaultValue
: input.toBoolean() ?? defaultValue;
final lines = resolvedMessage.split('\n').length - 1;
final prefix =
lines > 1 ? '\x1b[A\u001B[2K\u001B[${lines}A' : '\x1b[A\u001B[2K';
_stdout.writeln(
'''$prefix$resolvedMessage${styleDim.wrap(lightCyan.wrap(response ? 'Yes' : 'No'))}''',
);
return response;
}