Abscissa / scriptlike

Utility library to help you write script-like programs in D
Other
93 stars 10 forks source link

userInput for passwords #24

Open magicl opened 8 years ago

magicl commented 8 years ago

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

JesseKPhillips commented 7 years ago

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;
}