Reputation: 3539
When testing a Dart pre-commit git hook script I wrote, I'm able to type into stdin as input when using a prompt. But when running it through git when committing, it runs through the input calls without prompting the user for input. Apparently, there's a workaround in certain languages, by Calling exec < /dev/tty
which assigns standard input to the keyboard, but what about in Dart?
Upvotes: 0
Views: 107
Reputation: 31209
You can just read directly from /dev/tty as a File instead of redirecting the stdin:
import 'dart:convert';
import 'dart:io';
void main() async {
print('What is your name: ');
final name = await getUserInput();
print('Hello! Your name is $name');
}
Future<String> getUserInput() async =>
File(Platform.isWindows ? r'conIN$' : '/dev/tty')
.openRead()
.transform(utf8.decoder)
.transform(const LineSplitter())
.first;
I found the following documentation for conIN$ which indicates that this always points to the console input on Windows: https://learn.microsoft.com/en-us/windows/console/console-handles
Upvotes: 1