ncurses-examples/source/window/password.d

120 lines
3.4 KiB
D
Raw Permalink Normal View History

2024-10-12 22:08:40 +00:00
module window.password;
import std.conv;
import std.string;
import std.stdio;
public import deimos.ncurses;
public import deimos.form;
public import core.stdc.locale;
enum {
COLOR_WINDOW = 1,
COLOR_BACKGROUND = 2
}
void password() {
setlocale(LC_ALL,"");
initscr();
raw();
noecho();
curs_set(2);
start_color();
init_pair(COLOR_WINDOW, COLOR_WHITE, COLOR_RED);
init_pair(COLOR_BACKGROUND, COLOR_WHITE, COLOR_BLUE);
int width, height;
getmaxyx(stdscr, height, width);
bkgd(COLOR_PAIR(COLOR_BACKGROUND));
refresh();
WINDOW *win = newwin(5, 44, height/2 - 3, width/2 - 22);
keypad(win, TRUE);
box(win, 0, 0);
wbkgd(win, COLOR_PAIR(COLOR_WINDOW));
wrefresh(win);
FIELD*[3] fields;
fields[0] = new_field(1, 16, 0, 2, 0, 0);
fields[1] = new_field(1, 20, 0, 18, 0, 0);
fields[2] = null;
set_field_buffer(fields[0], 0, "Введите пароль:");
set_field_opts(fields[0], O_VISIBLE | O_PUBLIC | O_AUTOSKIP);
set_field_type(fields[1], TYPE_REGEXP, `^\**[-0-9A-Za-zА-Яа-я*,./!?%&#:$^_=+@~\]* *$`.ptr);
set_field_opts(fields[1], O_VISIBLE | O_PUBLIC | O_EDIT | O_ACTIVE);
set_field_back(fields[0], COLOR_PAIR(COLOR_WINDOW));
set_field_back(fields[1], COLOR_PAIR(COLOR_WINDOW) | A_UNDERLINE);
FORM *form = new_form(cast(FIELD**)fields);
set_form_win(form, win);
set_form_sub(form, derwin(win, 1, 38, 2, 2));
post_form(form);
wrefresh(win);
dstring password;
bool stop = false;
int status;
dchar ch;
int ret;
while (!stop) {
ret = wget_wch(win, &ch);
switch (ret) {
case KEY_CODE_YES:
switch (ch) {
case KEY_BACKSPACE:
form_driver_w(form, KEY_CODE_YES, REQ_DEL_PREV);
if (password.length)
password = password[0 .. $ - 1];
break;
default:
break;
}
break;
case OK:
switch (ch) {
case KEY_BACKSPACE:
form_driver_w(form, KEY_CODE_YES, REQ_DEL_PREV);
if (password.length)
password = password[0 .. $ - 1];
break;
case 32:
break;
case 10:
case KEY_ENTER:
stop = true;
break;
default:
status = form_driver_w(form, OK, ch);
if( status == OK ) {
status = form_driver_w(form, KEY_CODE_YES, REQ_VALIDATION);
if (status == OK) {
password ~= ch.to!dchar;
form_driver_w(form, KEY_CODE_YES, REQ_DEL_PREV);
form_driver_w(form, OK, '*'.to!int);
} else {
status = form_driver_w(form, KEY_CODE_YES, REQ_DEL_PREV);
}
}
}
break;
default:
break;
}
}
unpost_form(form);
free_form(form);
free_field(fields[0]);
free_field(fields[1]);
endwin();
writefln("Password: %s\n", password);
}