scriptlike icon indicating copy to clipboard operation
scriptlike copied to clipboard

userInput for passwords

Open magicl opened this issue 9 years ago • 1 comments

Would be great to have a version of userInput that masked what was typed, e.g. using "read -s"

magicl avatar Dec 26 '15 05:12 magicl

I've done this with terminal.d: https://github.com/adamdruppe/arsd/blob/master/terminal.d

/**
 * Requests a user's password without displaying the text entered.
 */
string getPassword() {
	import terminal;
	auto term = Terminal(ConsoleOutputType.linear);
	auto input = RealTimeConsoleInput(&term, ConsoleInputFlags.raw);
	string pass;
	dchar ch;
	ch = input.getch();
	while(ch != '\r' && ch != '\n')
	{
		import std.range;
		if(ch == '\b' && !pass.empty)
			pass = pass[0..$-1];
		else
			pass ~= ch;
		ch = input.getch();
	}

	return pass;
}

JesseKPhillips avatar Mar 03 '17 18:03 JesseKPhillips