rewrite askUserNameAndPassword() to use tim

This commit is contained in:
2026-01-12 23:00:01 +05:00
parent bfcb2f931f
commit 310e4867d5
4 changed files with 159 additions and 81 deletions

View File

@@ -18,11 +18,11 @@ static const str farewell_art = STR(
"\\(_,J J L l`,)/\n"
);
#define FPS 30
#define is_alias(LITERAL) str_equals(command, STR(LITERAL))
static Result(void) ClientCLI_askUserNameAndPassword(str* username_out, str* password_out);
Result(bool) start_screen(Array(char) username_buf, Array(char) password_buf,
str* out_username, str* out_password);
static Result(void) ClientCLI_openUserDB(ClientCLI* self);
static Result(void) ClientCLI_execCommand(ClientCLI* self, str command, bool* stop);
static Result(SavedServer*) ClientCLI_joinNewServer(ClientCLI* self);
@@ -48,48 +48,29 @@ void ClientCLI_construct(ClientCLI* self){
}
Result(void) ClientCLI_run(ClientCLI* self) {
Deferral(FPS);
Deferral(32);
TimEditState e;
TimEditState_init(&e, 32, "Greetings!");
bool edit_enabled = false;
while(tim_run(30)){
TimStyle c = { .brd = TimColor16_White, .bg = TimColor16_DarkBlue, .fg = TimColor16_White };
tim_frame(0, 0, ~0, ~0, c);
tim_label(e.s, A, 2, A, A, c);
if(edit_enabled){
TimKey key = tim_edit(&e, A, 5, tim->scopes[tim->scope].w - 4, c);
if(key == TimKey_Escape || key == TimKey_Enter)
edit_enabled = false;
}
else {
if(tim_button("[Enter] Edit text", A, 5, tim->scopes[tim->scope].w - 4, A, c) || tim_is_key_press(TimKey_Enter)){
edit_enabled = true;
tim->focus = &e;
tim->event.type = TimEvent_Void; // consume key event
}
}
if(tim_button("[Q] Quit", A, ~1, 10, A, c) || tim_is_key_press('q'))
exit(0);
// ask username and password
Array(char) username_buf = Array_char_alloc(USERNAME_SIZE_MAX + 1);
Array(char) password_buf = Array_char_alloc(PASSWORD_SIZE_MAX + 1);
Defer(
Array_char_destroy(&username_buf);
Array_char_destroy(&password_buf);
);
str username = str_null, password = str_null;
try(bool start, i, start_screen(username_buf, password_buf, &username, &password));
if(!start){
Return RESULT_VOID;
}
tim_reset_terminal();
term_clear();
printf(FMT_str"\n", greeting_art.len, greeting_art.data);
// create Client
str username = str_null, password = str_null;
try_void(ClientCLI_askUserNameAndPassword(&username, &password));
Defer(
str_destroy(username);
str_destroy(password);
);
Client_free(self->client);
// create client
try(self->client, p, Client_create(username, password));
memset(password.data, 0, password.len);
// erase password from memory
Array_char_memset(&password_buf, 0);
// init db
try_void(ClientCLI_openUserDB(self));
@@ -120,49 +101,6 @@ Result(void) ClientCLI_run(ClientCLI* self) {
Return RESULT_VOID;
}
static Result(void) ClientCLI_askUserNameAndPassword(str* username_out, str* password_out){
Deferral(8);
bool success = false;
// ask username
Array(char) username_buf = Array_char_alloc(128);
Defer(if(!success) Array_char_destroy(&username_buf));
str username = str_null;
while(true) {
printf("username: ");
try_void(term_readLine(username_buf.data, username_buf.len));
username = str_from_cstr(username_buf.data);
str_trim(&username, true);
str name_error_str = validateUsername_str(username);
if(name_error_str.data){
printf("ERROR: "FMT_str"\n",
name_error_str.len, name_error_str.data);
}
else break;
}
// ask password
Array(char) password_buf = Array_char_alloc(128);
Defer(if(!success) Array_char_destroy(&password_buf));
str password = str_null;
while(true) {
printf("password: ");
// TODO: hide password
try_void(term_readLineHidden(password_buf.data, password_buf.len));
password = str_from_cstr(password_buf.data);
str_trim(&password, true);
if(password.len < PASSWORD_SIZE_MIN || password.len > PASSWORD_SIZE_MAX){
printf("ERROR: password length (in bytes) must be >= %i and <= %i\n",
PASSWORD_SIZE_MIN, PASSWORD_SIZE_MAX);
}
else break;
}
*username_out = username;
*password_out = password;
success = true;
Return RESULT_VOID;
}
static Result(void) ClientCLI_openUserDB(ClientCLI* self){
Deferral(8);