feat: utf8 support & partial ui.c refactor

This commit is contained in:
2025-06-20 21:31:03 +02:00
parent d5688f5c5c
commit 787b412005
17 changed files with 366 additions and 264 deletions

275
src/ui.c
View File

@@ -19,19 +19,22 @@
#include "auth.h"
#include "efield.h"
#include "keys.h"
#include "ofield.h"
#include "sessions.h"
#include "ui.h"
#include "ui_state.h"
#include "users.h"
#include "util.h"
const u_char inputs_n = 3;
const uint boxw = 50;
const uint boxh = 12;
static void print_box();
static void print_footer();
static void restore_all();
static void signal_handler(int);
const uint boxw = 50;
const uint boxh = 12;
struct uint_point {
uint x;
uint y;
@@ -41,9 +44,6 @@ static void print_session(struct uint_point, struct session, bool);
static void print_user(struct uint_point, struct user, bool);
static void print_passwd(struct uint_point, uint, bool);
enum input { SESSION, USER, PASSWD };
static u_char inputs_n = 3;
// ansi resource: https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797
static struct termios orig_term;
static struct termios term;
@@ -57,7 +57,7 @@ void setup(struct config* config) {
// 2 padding top and bottom for footer and vertical compensation
// 2 padding left & right to not overflow footer width
if (window.ws_row < boxh + 4 || window.ws_col < boxw + 4) {
fprintf(stderr, "\x1b[1;31mScreen too small\x1b[0m\n");
(void)fprintf(stderr, "\x1b[1;31mScreen too small\x1b[0m\n");
exit(1);
}
@@ -73,243 +73,82 @@ void setup(struct config* config) {
g_config->colors.fg);
print_footer();
atexit(restore_all);
signal(SIGINT, signal_handler);
(void)atexit(restore_all);
(void)signal(SIGINT, signal_handler);
}
static struct uint_point box_start() {
struct uint_point __start;
__start.x = (window.ws_col - boxw) / 2 + 1;
__start.y = (window.ws_row - boxh) / 2 + 1;
return __start;
return (struct uint_point){
.x = ((window.ws_col - boxw) / 2) + 1,
.y = ((window.ws_row - boxh) / 2) + 1,
};
}
static char* fmt_time() {
time_t t = time(NULL);
struct tm tm = *localtime(&t);
time_t tme = time(NULL);
struct tm tm = *localtime(&tme);
size_t bsize =
snprintf(NULL, 0, "%d-%02d-%02d %02d:%02d:%02d", tm.tm_year + 1900,
tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec) +
1;
char* buf = malloc(bsize);
snprintf(buf, bsize, "%d-%02d-%02d %02d:%02d:%02d", tm.tm_year + 1900,
// TODO: use strftime and a cfg template string
char* buf;
asprintf(&buf, "%d-%02d-%02d %02d:%02d:%02d", tm.tm_year + 1900,
tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
return buf;
}
// TODO: handle buffers longer than the buffer (cut str to the end, change
// cursor pos...) should just overlap for now
// ugh, this represent a field which might have options
// opts is the amount of other options possible (0 will behave as a passwd)
// aaaand (it's an abstract idea, letme think), also holds the status of a
// custom content, like custom launch command or user or smth
struct opt_field {
uint opts;
uint current_opt; // 0 is edit mode btw
struct editable_field efield;
};
void print_ofield(struct opt_field* focused_input);
static struct opt_field ofield_new(uint opts) {
struct opt_field __field;
__field.opts = opts;
__field.current_opt = 1;
if (opts == 0) {
__field.current_opt = 0;
__field.efield = field_new("");
}
return __field;
}
static void ofield_toedit(struct opt_field* ofield, char* init) {
ofield->current_opt = 0;
ofield->efield = field_new(init);
}
static void ofield_type(struct opt_field* ofield, char* new, char* startstr) {
if (ofield->current_opt != 0) ofield_toedit(ofield, startstr);
field_update(&ofield->efield, new);
}
// true if it changed anything, single opt fields return false
static bool ofield_opt_seek(struct opt_field* ofield, char seek) {
// TODO: think this
if (ofield->opts == 0 || (ofield->opts == 1 && ofield->current_opt != 0))
return false;
ofield->current_opt =
1 + ((ofield->current_opt - 1 + seek + ofield->opts) % ofield->opts);
print_ofield(ofield);
return true;
}
// true in case it was able to "use" the seek (a empty only editable field
// wouldn't)
static bool ofield_seek(struct opt_field* ofield, char seek) {
if (ofield->current_opt == 0) {
if (field_seek(&ofield->efield, seek)) {
return true;
}
}
if (ofield->opts == 0) return false;
ofield_opt_seek(ofield, seek);
return true;
}
static u_char ofield_max_displ_pos(struct opt_field* ofield) {
// TODO: set max cursor pos too
// keep in mind that also have to keep in mind scrolling and ughhh, mentally
// blocked, but this is complex
if (ofield->current_opt == 0)
return ofield->efield.pos;
else
return 0;
}
enum input focused_input = PASSWD;
struct opt_field of_session;
struct opt_field of_user;
struct opt_field of_passwd;
struct Vector* gusers;
struct Vector* gsessions;
// not *that* OF tho
struct opt_field* get_of(enum input from) {
if (from == SESSION) return &of_session;
if (from == USER) return &of_user;
if (from == PASSWD) return &of_passwd;
return NULL;
}
void ffield_cursor_focus() {
void ui_update_cursor_focus() {
struct uint_point bstart = box_start();
u_char line = bstart.y;
u_char row = bstart.x + 15;
u_char col = bstart.x + 15;
struct opts_field* ofield = get_opts_ffield();
col += ofield_display_cursor_col(ofield);
if (ofield->opts > 1) col += utf8len(g_config->strings.opts_pre);
// rows in here quite bodged
if (focused_input == SESSION) {
line += 5;
row += (of_session.opts > 1) * 2;
} else if (focused_input == USER) {
line += 7;
row += (of_user.opts > 1) * 2;
} else if (focused_input == PASSWD)
line += 9;
struct opt_field* ofield = get_of(focused_input);
row += ofield->current_opt == 0 ? ofield_max_displ_pos(ofield) : 0;
printf("\x1b[%d;%dH", line, row);
fflush(stdout);
(void)printf("\x1b[%d;%dH", line, col);
(void)fflush(stdout);
}
struct user get_current_user() {
if (of_user.current_opt != 0)
return *(struct user*)vec_get(gusers, of_user.current_opt - 1);
else {
struct user custom_user;
custom_user.shell = "/usr/bin/bash";
custom_user.username = custom_user.display_name = of_user.efield.content;
return custom_user;
}
}
struct session get_current_session() {
if (of_session.current_opt != 0) {
// this is for the default user shell :P, not the greatest
// implementation but I want to get his done
if (g_config->behavior.include_defshell &&
of_session.current_opt == gsessions->length + 1) {
struct session shell_session;
shell_session.type = SHELL;
shell_session.exec = shell_session.name = get_current_user().shell;
return shell_session;
} else
return *(struct session*)vec_get(gsessions, of_session.current_opt - 1);
} else {
struct session custom_session;
custom_session.type = SHELL;
custom_session.name = custom_session.exec = of_session.efield.content;
return custom_session;
}
}
void print_field(enum input focused_input) {
void ui_update_field(enum input focused_input) {
struct uint_point origin = box_start();
if (focused_input == PASSWD) {
print_passwd(origin, of_passwd.efield.length, false);
print_passwd(origin, utf8len(of_passwd.efield.content), false);
} else if (focused_input == SESSION) {
print_session(origin, get_current_session(), of_session.opts > 1);
print_session(origin, st_session(g_config->behavior.include_defshell),
of_session.opts > 1);
} else if (focused_input == USER) {
print_user(origin, get_current_user(), of_user.opts > 1);
print_field(SESSION);
print_user(origin, st_user(), of_user.opts > 1);
ui_update_field(SESSION);
}
ffield_cursor_focus();
ui_update_cursor_focus();
}
void print_ffield() {
print_field(focused_input);
void ui_update_ffield() {
ui_update_field(focused_input);
}
void print_ofield(struct opt_field* ofield) {
void ui_update_ofield(struct opts_field* self) {
enum input input;
if (ofield == &of_session)
if (self == &of_session)
input = SESSION;
else if (ofield == &of_user)
else if (self == &of_user)
input = USER;
else if (ofield == &of_passwd)
else if (self == &of_passwd)
input = PASSWD;
else
return;
print_field(input);
}
// true = forward, false = backward
void ffield_move(bool direction) {
if (direction)
focused_input = (focused_input + 1 + inputs_n) % inputs_n;
else
focused_input = (focused_input - 1 + inputs_n) % inputs_n;
ffield_cursor_focus();
}
// tf I'm doing
void ffield_change_opt(bool direction) {
struct opt_field* ffield = get_of(focused_input);
if (focused_input == PASSWD) ffield = &of_session;
if (!ofield_opt_seek(ffield, direction ? 1 : -1)) {
if (focused_input == PASSWD || focused_input == SESSION)
ofield_opt_seek(&of_user, direction ? 1 : -1);
else
ofield_opt_seek(&of_session, direction ? 1 : -1);
}
}
void ffield_change_pos(bool direction) {
struct opt_field* ffield = get_of(focused_input);
if (!ofield_seek(ffield, direction ? 1 : -1))
if (!ofield_opt_seek(&of_session, direction ? 1 : -1))
ofield_opt_seek(&of_user, direction ? 1 : -1);
ffield_cursor_focus();
}
void ffield_type(char* text) {
struct opt_field* field = get_of(focused_input);
char* start = "";
if (focused_input == USER && of_user.current_opt != 0)
start = get_current_user().username;
if (focused_input == SESSION && of_session.current_opt != 0 &&
get_current_session().type == SHELL)
start = get_current_session().exec;
ofield_type(field, text, start);
print_ffield();
ui_update_field(input);
}
static char* unknown_str = "unknown";
@@ -351,10 +190,10 @@ int load(struct Vector* users, struct Vector* sessions) {
g_config->colors.e_date, fmtd_time, g_config->colors.fg);
free(fmtd_time);
print_field(SESSION);
print_field(USER);
print_field(PASSWD);
ffield_cursor_focus();
ui_update_field(SESSION);
ui_update_field(USER);
ui_update_field(PASSWD);
ui_update_cursor_focus();
/// INTERACTIVE
u_char len;
@@ -379,23 +218,24 @@ int load(struct Vector* users, struct Vector* sessions) {
reboot(RB_POWER_OFF);
exit(0);
} else if (ansi_code == A_UP || ansi_code == A_DOWN) {
ffield_move(ansi_code == A_DOWN);
st_ch_focus(ansi_code == A_DOWN ? 1 : -1);
} else if (ansi_code == A_RIGHT || ansi_code == A_LEFT) {
if (esc)
ffield_change_opt(ansi_code == A_RIGHT);
st_ch_of_opts(ansi_code == A_RIGHT ? 1 : -1);
else
ffield_change_pos(ansi_code == A_RIGHT);
st_ch_ef_col(ansi_code == A_RIGHT ? 1 : -1);
}
}
} else {
if (len == 1 && *seq == '\n') {
if (!launch(get_current_user().username, of_passwd.efield.content,
get_current_session(), &restore_all, g_config)) {
print_passwd(box_start(), of_passwd.efield.length, true);
ffield_cursor_focus();
if (!launch(st_user().username, of_passwd.efield.content,
st_session(g_config->behavior.include_defshell),
&restore_all, g_config)) {
print_passwd(box_start(), utf8len(of_passwd.efield.content), true);
ui_update_cursor_focus();
}
} else
ffield_type(seq);
st_kbd_type(seq, g_config->behavior.include_defshell);
}
if (esc != 0) esc--;
@@ -439,8 +279,9 @@ static void print_session(struct uint_point origin, struct session session,
}
if (multiple) {
printf("\r\x1b[%dC< \x1b[%sm%s\x1b[%sm >", origin.x + 14, session_color,
session.name, g_config->colors.fg);
printf("\r\x1b[%dC%s\x1b[%sm%s\x1b[%sm%s", origin.x + 14,
g_config->strings.opts_pre, session_color, session.name,
g_config->colors.fg, g_config->strings.opts_post);
} else {
printf("\r\x1b[%dC\x1b[%sm%s\x1b[%sm", origin.x + 14, session_color,
session.name, g_config->colors.fg);