mirror of
https://github.com/javalsai/lidm.git
synced 2025-07-04 06:48:42 +02:00
feat: ithnk full utf8 support & ui 100% consistent
This commit is contained in:
parent
787b412005
commit
e7ef64cd16
@ -7,6 +7,7 @@ Checks: >
|
||||
-bugprone-easily-swappable-parameters,
|
||||
cert-*,
|
||||
modernize-*,
|
||||
-modernize-macro-to-enum,
|
||||
performance-*,
|
||||
portability-*,
|
||||
readability-*,
|
||||
|
@ -14,20 +14,13 @@ enum introspection_type {
|
||||
KEY,
|
||||
STRING_ARRAY,
|
||||
};
|
||||
static const char* const intros_tys_names[] = {
|
||||
static const char* NNULLABLE const intros_tys_names[] = {
|
||||
[STRING] = "STRING",
|
||||
[BOOL] = "BOOL",
|
||||
[KEY] = "KEY",
|
||||
[STRING_ARRAY] = "STRING_ARRAY",
|
||||
};
|
||||
|
||||
union introspection_variant {
|
||||
char* string;
|
||||
bool boolean;
|
||||
enum keys key;
|
||||
char** string_array;
|
||||
};
|
||||
|
||||
struct introspection_item {
|
||||
char* NNULLABLE name;
|
||||
size_t offset;
|
||||
|
@ -18,6 +18,6 @@ void ofield_kbd_type(struct opts_field* self, char* typed, char* empty_default);
|
||||
bool ofield_opts_seek(struct opts_field* self, char seek);
|
||||
bool ofield_seek(struct opts_field* self, char seek);
|
||||
|
||||
u_char ofield_display_cursor_col(struct opts_field* self);
|
||||
u_char ofield_display_cursor_col(struct opts_field* self, u_char maxlen);
|
||||
|
||||
#endif
|
||||
|
38
include/ui.h
38
include/ui.h
@ -5,13 +5,43 @@
|
||||
#include "ofield.h"
|
||||
#include "util.h"
|
||||
|
||||
// [box_start]
|
||||
// ↓
|
||||
// 0 [┌]───────────────────────────────────────────────┐
|
||||
// 1 │ │
|
||||
// 2 │ the-art 2025-06-20 21:40:44 │
|
||||
// 3 │ | │
|
||||
// 4 │ | │
|
||||
// 5 │ xorg < Default > | │
|
||||
// 6 │ | │
|
||||
// 7 │ user javalsai | │
|
||||
// 8 │ | │
|
||||
// 9 │ password ________________________________ │
|
||||
// 10 │ | | │
|
||||
// 11 └────────────────────────────────────────────────┘
|
||||
// 01234567890123456789012345678901234567890123456789
|
||||
// 00000000001111111111222222222233333333334444444444
|
||||
// |--| | ↑ |--|[BOX_HMARGIN]
|
||||
// [BOX_HMARGIN] [VALUES_COL] |
|
||||
// |---|[VALUES_SEPR] |
|
||||
// |--------------------------------|[VALUE_MAXLEN]
|
||||
|
||||
#define HEAD_ROW 2
|
||||
#define SESSION_ROW 5
|
||||
#define USER_ROW 7
|
||||
#define PASSWD_ROW 9
|
||||
|
||||
#define BOX_WIDTH 50
|
||||
#define BOX_HEIGHT 12
|
||||
#define BOX_HMARGIN 2
|
||||
|
||||
#define VALUES_COL 15
|
||||
#define VALUES_SEPR 3
|
||||
#define VALUE_MAXLEN (BOX_WIDTH - VALUES_COL + 1 - BOX_HMARGIN - 2)
|
||||
|
||||
enum input { SESSION, USER, PASSWD };
|
||||
extern const u_char inputs_n;
|
||||
|
||||
// not customizable (for now)
|
||||
extern const uint boxw;
|
||||
extern const uint boxh;
|
||||
|
||||
void setup(struct config* config);
|
||||
int load(struct Vector* users, struct Vector* sessions);
|
||||
void print_err(const char*);
|
||||
|
@ -9,15 +9,16 @@
|
||||
|
||||
#include "keys.h"
|
||||
|
||||
int find_keyname(enum keys* at, char* name);
|
||||
enum keys find_ansi(char*);
|
||||
int find_keyname(enum keys* at, const char* name);
|
||||
enum keys find_ansi(const char*);
|
||||
void read_press(u_char*, char*);
|
||||
|
||||
bool utf8_iscont(char byte);
|
||||
size_t utf8len(char* str);
|
||||
size_t utf8len_until(char* str, char* until);
|
||||
char* utf8back(char* str);
|
||||
char* utf8seek(char* str);
|
||||
size_t utf8len(const char* str);
|
||||
size_t utf8len_until(const char* str, const char* until);
|
||||
const char* utf8back(const char* str);
|
||||
const char* utf8seek(const char* str);
|
||||
const char* utf8seekn(const char* str, size_t n);
|
||||
|
||||
struct Vector {
|
||||
uint32_t length;
|
||||
|
@ -11,7 +11,6 @@
|
||||
#include "log.h"
|
||||
#include "util.h"
|
||||
|
||||
// NOLINTNEXTLINE(modernize-macro-to-enum)
|
||||
#define UPPER_HALF_BYTE 4
|
||||
int parse_hex(char* _at, char x1, char x2) {
|
||||
// make linter happy
|
||||
|
32
src/efield.c
32
src/efield.c
@ -23,7 +23,6 @@ void efield_trim(struct editable_field* self, u_char pos) {
|
||||
self->content[pos + 1] = '\0';
|
||||
}
|
||||
|
||||
// NOLINTNEXTLINE(modernize-macro-to-enum)
|
||||
#define BACKSPACE_CODE 127
|
||||
void efield_update(struct editable_field* self, char* update) {
|
||||
u_char insert_len = strlen(update);
|
||||
@ -61,24 +60,25 @@ void efield_update(struct editable_field* self, char* update) {
|
||||
|
||||
// returns bool depending if it was able to "use" the seek
|
||||
bool efield_seek(struct editable_field* self, char seek) {
|
||||
if (strlen(self->content) == 0) return false;
|
||||
if (*self->content == '\0') return false;
|
||||
if (seek == 0) return false;
|
||||
|
||||
u_char count = seek;
|
||||
if (seek < 0) {
|
||||
count = -seek;
|
||||
u_char count = seek < 0 ? -seek : seek;
|
||||
char* ptr = &self->content[self->pos];
|
||||
char* start = ptr;
|
||||
|
||||
while (count-- > 0) {
|
||||
if (seek < 0) {
|
||||
if (ptr == self->content) break;
|
||||
ptr = (char*)utf8back(ptr);
|
||||
} else {
|
||||
if (*ptr == '\0') break;
|
||||
ptr = (char*)utf8seek(ptr);
|
||||
}
|
||||
}
|
||||
|
||||
char* orig = &self->content[self->pos];
|
||||
char* ptr = orig;
|
||||
while (ptr > self->content && orig > 0 && *ptr != '\0') {
|
||||
if (seek < 0)
|
||||
ptr = utf8back(ptr);
|
||||
else
|
||||
ptr = utf8seek(ptr);
|
||||
count--;
|
||||
}
|
||||
|
||||
return ptr != orig;
|
||||
self->pos = (u_char)(ptr - self->content);
|
||||
return ptr != start;
|
||||
}
|
||||
|
||||
// NOLINTEND(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling)
|
||||
|
25
src/ofield.c
25
src/ofield.c
@ -1,5 +1,6 @@
|
||||
#include <stddef.h>
|
||||
|
||||
#include "config.h"
|
||||
#include "efield.h"
|
||||
#include "ofield.h"
|
||||
#include "ui.h"
|
||||
@ -36,7 +37,7 @@ bool ofield_opts_seek(struct opts_field* self, char seek) {
|
||||
if (self->opts == 0 || (self->opts == 1 && self->current_opt != 0))
|
||||
return false;
|
||||
|
||||
self->current_opt = 1 + ((self->current_opt - 1 + seek) % self->opts);
|
||||
self->current_opt = 1 + ((self->current_opt - 1 + seek + self->opts) % self->opts);
|
||||
ui_update_ofield(self);
|
||||
return true;
|
||||
}
|
||||
@ -53,9 +54,23 @@ bool ofield_seek(struct opts_field* self, char seek) {
|
||||
return true;
|
||||
}
|
||||
|
||||
u_char ofield_display_cursor_col(struct opts_field* self) {
|
||||
if (self->current_opt == 0)
|
||||
return utf8len_until(self->efield.content,
|
||||
&self->efield.content[self->efield.pos]);
|
||||
u_char ofield_display_cursor_col(struct opts_field* self, u_char maxlen) {
|
||||
if (self->current_opt == 0) {
|
||||
u_char display_len = utf8len(self->efield.content);
|
||||
u_char pos = utf8len_until(self->efield.content,
|
||||
&self->efield.content[self->efield.pos]);
|
||||
|
||||
if (display_len > maxlen) {
|
||||
if (pos < maxlen / 2) {
|
||||
return pos;
|
||||
}
|
||||
if (display_len - pos < maxlen / 2) {
|
||||
return maxlen - (display_len - pos);
|
||||
}
|
||||
return maxlen / 2;
|
||||
}
|
||||
|
||||
return pos;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
199
src/ui.c
199
src/ui.c
@ -5,6 +5,7 @@
|
||||
#include <pwd.h>
|
||||
#include <signal.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@ -27,36 +28,39 @@
|
||||
#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);
|
||||
static void signal_handler(int code);
|
||||
|
||||
struct uint_point {
|
||||
uint x;
|
||||
uint y;
|
||||
};
|
||||
|
||||
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);
|
||||
static void print_session(struct uint_point origin, struct session session,
|
||||
bool multiple);
|
||||
static void print_user(struct uint_point origin, struct user user,
|
||||
bool multiple);
|
||||
static void print_passwd(struct uint_point origin, uint length, bool err);
|
||||
|
||||
// ansi resource: https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797
|
||||
static struct termios orig_term;
|
||||
static struct termios term;
|
||||
static struct winsize window;
|
||||
|
||||
#define INNER_BOX_OUT_MARGIN 2
|
||||
struct config* g_config = NULL;
|
||||
void setup(struct config* config) {
|
||||
g_config = config;
|
||||
ioctl(STDOUT_FILENO, TIOCGWINSZ, &window);
|
||||
|
||||
// at least
|
||||
// 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) {
|
||||
if (window.ws_row < BOX_HEIGHT + INNER_BOX_OUT_MARGIN * 2 ||
|
||||
window.ws_col < BOX_WIDTH + INNER_BOX_OUT_MARGIN * 2) {
|
||||
(void)fprintf(stderr, "\x1b[1;31mScreen too small\x1b[0m\n");
|
||||
exit(1);
|
||||
}
|
||||
@ -79,18 +83,19 @@ void setup(struct config* config) {
|
||||
|
||||
static struct uint_point box_start() {
|
||||
return (struct uint_point){
|
||||
.x = ((window.ws_col - boxw) / 2) + 1,
|
||||
.y = ((window.ws_row - boxh) / 2) + 1,
|
||||
.x = ((window.ws_col - BOX_WIDTH) / 2) + 1, // looks better
|
||||
.y = ((window.ws_row - BOX_HEIGHT) / 2), // leave more space under
|
||||
};
|
||||
}
|
||||
|
||||
#define TM_YEAR_EPOCH 1900
|
||||
static char* fmt_time() {
|
||||
time_t tme = time(NULL);
|
||||
struct tm tm = *localtime(&tme);
|
||||
|
||||
// TODO: use strftime and a cfg template string
|
||||
char* buf;
|
||||
asprintf(&buf, "%d-%02d-%02d %02d:%02d:%02d", tm.tm_year + 1900,
|
||||
asprintf(&buf, "%d-%02d-%02d %02d:%02d:%02d", tm.tm_year + TM_YEAR_EPOCH,
|
||||
tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
|
||||
|
||||
return buf;
|
||||
@ -99,19 +104,21 @@ static char* fmt_time() {
|
||||
void ui_update_cursor_focus() {
|
||||
struct uint_point bstart = box_start();
|
||||
u_char line = bstart.y;
|
||||
u_char col = bstart.x + 15;
|
||||
u_char col = bstart.x + VALUES_COL;
|
||||
|
||||
struct opts_field* ofield = get_opts_ffield();
|
||||
col += ofield_display_cursor_col(ofield);
|
||||
col += ofield_display_cursor_col(
|
||||
ofield, VALUE_MAXLEN - utf8len(g_config->strings.opts_pre) -
|
||||
utf8len(g_config->strings.opts_post));
|
||||
if (ofield->opts > 1) col += utf8len(g_config->strings.opts_pre);
|
||||
|
||||
// rows in here quite bodged
|
||||
if (focused_input == SESSION) {
|
||||
line += 5;
|
||||
line += SESSION_ROW;
|
||||
} else if (focused_input == USER) {
|
||||
line += 7;
|
||||
line += USER_ROW;
|
||||
} else if (focused_input == PASSWD)
|
||||
line += 9;
|
||||
line += PASSWD_ROW;
|
||||
|
||||
(void)printf("\x1b[%d;%dH", line, col);
|
||||
(void)fflush(stdout);
|
||||
@ -137,7 +144,7 @@ void ui_update_ffield() {
|
||||
ui_update_field(focused_input);
|
||||
}
|
||||
|
||||
void ui_update_ofield(struct opts_field* self) {
|
||||
void ui_update_ofield(struct opts_field* NNULLABLE self) {
|
||||
enum input input;
|
||||
if (self == &of_session)
|
||||
input = SESSION;
|
||||
@ -152,18 +159,21 @@ void ui_update_ofield(struct opts_field* self) {
|
||||
}
|
||||
|
||||
static char* unknown_str = "unknown";
|
||||
// NOLINTNEXTLINE(readability-function-cognitive-complexity)
|
||||
int load(struct Vector* users, struct Vector* sessions) {
|
||||
/// SETUP
|
||||
gusers = users;
|
||||
gsessions = sessions;
|
||||
|
||||
// hostnames larger won't render properly
|
||||
char* hostname = malloc(16);
|
||||
if (gethostname(hostname, 16) != 0) {
|
||||
char* hostname = malloc(VALUES_COL - VALUES_SEPR - BOX_HMARGIN);
|
||||
if (gethostname(hostname, VALUES_COL - VALUES_SEPR - BOX_HMARGIN) != 0) {
|
||||
free(hostname);
|
||||
hostname = unknown_str;
|
||||
} else {
|
||||
hostname[15] = '\0';
|
||||
char* hidx =
|
||||
(char*)utf8back(&hostname[VALUES_COL - VALUES_SEPR - BOX_HMARGIN - 1]);
|
||||
*hidx = '\0';
|
||||
}
|
||||
|
||||
of_session =
|
||||
@ -178,15 +188,15 @@ int load(struct Vector* users, struct Vector* sessions) {
|
||||
print_box();
|
||||
|
||||
// put hostname
|
||||
printf("\x1b[%d;%dH\x1b[%sm%s\x1b[%sm", boxstart.y + 2,
|
||||
boxstart.x + 12 - (uint)strlen(hostname), g_config->colors.e_hostname,
|
||||
hostname, g_config->colors.fg);
|
||||
printf("\x1b[%d;%dH\x1b[%sm%s\x1b[%sm", boxstart.y + HEAD_ROW,
|
||||
boxstart.x + VALUES_COL - VALUES_SEPR - (uint)utf8len(hostname),
|
||||
g_config->colors.e_hostname, hostname, g_config->colors.fg);
|
||||
if (hostname != unknown_str) free(hostname);
|
||||
|
||||
// put date
|
||||
char* fmtd_time = fmt_time();
|
||||
printf("\x1b[%d;%dH\x1b[%sm%s\x1b[%sm", boxstart.y + 2,
|
||||
boxstart.x + boxw - 3 - (uint)strlen(fmtd_time),
|
||||
printf("\x1b[%d;%dH\x1b[%sm%s\x1b[%sm", boxstart.y + HEAD_ROW,
|
||||
boxstart.x + BOX_WIDTH - 1 - BOX_HMARGIN - (uint)utf8len(fmtd_time),
|
||||
g_config->colors.e_date, fmtd_time, g_config->colors.fg);
|
||||
free(fmtd_time);
|
||||
|
||||
@ -197,7 +207,7 @@ int load(struct Vector* users, struct Vector* sessions) {
|
||||
|
||||
/// INTERACTIVE
|
||||
u_char len;
|
||||
char seq[256];
|
||||
char seq[0xff];
|
||||
uint esc = 0;
|
||||
while (true) {
|
||||
read_press(&len, seq);
|
||||
@ -242,32 +252,46 @@ int load(struct Vector* users, struct Vector* sessions) {
|
||||
}
|
||||
}
|
||||
|
||||
static char* line_cleaner = NULL;
|
||||
static void clean_line(struct uint_point origin, uint line) {
|
||||
void clean_line(struct uint_point origin, uint line) {
|
||||
static char* line_cleaner = NULL;
|
||||
if (line_cleaner == NULL) {
|
||||
line_cleaner = malloc((boxw - 2) * sizeof(char) + 1);
|
||||
memset(line_cleaner, 32, boxw - 2);
|
||||
line_cleaner[boxw - 2] = 0;
|
||||
// - outline + nullbyte
|
||||
line_cleaner = malloc(BOX_WIDTH - 2 + 1);
|
||||
memset(line_cleaner, ' ', BOX_WIDTH - 2);
|
||||
line_cleaner[BOX_WIDTH - 2] = 0;
|
||||
}
|
||||
printf("\x1b[%d;%dH", origin.y + line, origin.x + 1);
|
||||
printf("%s", line_cleaner);
|
||||
printf("\x1b[%d;%dH%s", origin.y + line, origin.x + 1, line_cleaner);
|
||||
}
|
||||
|
||||
u_char get_render_pos_offset(struct opts_field* self, u_char maxlen) {
|
||||
if (self->current_opt != 0) return 0;
|
||||
|
||||
u_char pos = utf8len_until(self->efield.content,
|
||||
&self->efield.content[self->efield.pos]);
|
||||
return pos - ofield_display_cursor_col(self, maxlen);
|
||||
}
|
||||
|
||||
// TODO: session_len > 32
|
||||
static void print_session(struct uint_point origin, struct session session,
|
||||
bool multiple) {
|
||||
clean_line(origin, 5);
|
||||
const char* session_type;
|
||||
clean_line(origin, SESSION_ROW);
|
||||
|
||||
const char* NNULLABLE session_type;
|
||||
if (session.type == XORG) {
|
||||
session_type = g_config->strings.s_xorg;
|
||||
} else if (session.type == WAYLAND) {
|
||||
session_type = g_config->strings.s_wayland;
|
||||
} else {
|
||||
} else if (session.type == SHELL) {
|
||||
session_type = g_config->strings.s_shell;
|
||||
} else {
|
||||
__builtin_unreachable();
|
||||
}
|
||||
printf("\r\x1b[%luC\x1b[%sm%s\x1b[%sm",
|
||||
(ulong)(origin.x + 11 - strlen(session_type)),
|
||||
g_config->colors.e_header, session_type, g_config->colors.fg);
|
||||
|
||||
// already in the box, - 1 bcs no need to step over margin, same reasoning in
|
||||
// other places
|
||||
printf(
|
||||
"\r\x1b[%luC\x1b[%sm%s\x1b[%sm",
|
||||
(ulong)(origin.x + VALUES_COL - VALUES_SEPR - utf8len(session_type) - 1),
|
||||
g_config->colors.e_header, session_type, g_config->colors.fg);
|
||||
|
||||
char* session_color;
|
||||
if (session.type == XORG) {
|
||||
@ -278,42 +302,58 @@ static void print_session(struct uint_point origin, struct session session,
|
||||
session_color = g_config->colors.s_shell;
|
||||
}
|
||||
|
||||
char* toprint = session.name;
|
||||
if (multiple) {
|
||||
printf("\r\x1b[%dC%s\x1b[%sm%s\x1b[%sm%s", origin.x + 14,
|
||||
g_config->strings.opts_pre, session_color, session.name,
|
||||
u_char maxlen = VALUE_MAXLEN - utf8len(g_config->strings.opts_pre) -
|
||||
utf8len(g_config->strings.opts_post);
|
||||
toprint += get_render_pos_offset(&of_session, maxlen);
|
||||
size_t printlen = utf8seekn(toprint, maxlen) - toprint;
|
||||
|
||||
printf("\r\x1b[%dC%s\x1b[%sm%.*s\x1b[%sm%s", origin.x + VALUES_COL - 1,
|
||||
g_config->strings.opts_pre, session_color, (int)printlen, toprint,
|
||||
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);
|
||||
toprint += get_render_pos_offset(&of_session, VALUE_MAXLEN);
|
||||
size_t printlen = utf8seekn(toprint, VALUE_MAXLEN) - toprint;
|
||||
printf("\r\x1b[%dC\x1b[%sm%.*s\x1b[%sm", origin.x + VALUES_COL - 1,
|
||||
session_color, (int)printlen, toprint, g_config->colors.fg);
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: user_len > 32
|
||||
static void print_user(struct uint_point origin, struct user user,
|
||||
bool multiple) {
|
||||
clean_line(origin, 7);
|
||||
clean_line(origin, USER_ROW);
|
||||
printf("\r\x1b[%luC\x1b[%sm%s\x1b[%sm",
|
||||
(ulong)(origin.x + 11 - strlen(g_config->strings.e_user)),
|
||||
(ulong)(origin.x + VALUES_COL - VALUES_SEPR - 1 -
|
||||
strlen(g_config->strings.e_user)),
|
||||
g_config->colors.e_header, g_config->strings.e_user,
|
||||
g_config->colors.fg);
|
||||
|
||||
char* user_color = g_config->colors.e_user;
|
||||
|
||||
char* toprint = user.display_name;
|
||||
if (multiple) {
|
||||
printf("\r\x1b[%dC< \x1b[%sm%s\x1b[%sm >", origin.x + 14, user_color,
|
||||
user.display_name, g_config->colors.fg);
|
||||
u_char maxlen = VALUE_MAXLEN - utf8len(g_config->strings.opts_pre) -
|
||||
utf8len(g_config->strings.opts_post);
|
||||
toprint += get_render_pos_offset(&of_session, maxlen);
|
||||
size_t printlen = utf8seekn(toprint, maxlen) - toprint;
|
||||
|
||||
printf("\r\x1b[%dC< \x1b[%sm%.*s\x1b[%sm >", origin.x + VALUES_COL - 1,
|
||||
user_color, (int)printlen, toprint, g_config->colors.fg);
|
||||
} else {
|
||||
printf("\r\x1b[%dC\x1b[%sm%s\x1b[%sm", origin.x + 14, user_color,
|
||||
user.display_name, g_config->colors.fg);
|
||||
toprint += get_render_pos_offset(&of_user, VALUE_MAXLEN);
|
||||
size_t printlen = utf8seekn(toprint, VALUE_MAXLEN) - toprint;
|
||||
printf("\r\x1b[%dC\x1b[%sm%.*s\x1b[%sm", origin.x + VALUES_COL - 1,
|
||||
user_color, (int)printlen, toprint, g_config->colors.fg);
|
||||
}
|
||||
}
|
||||
|
||||
static char passwd_prompt[33];
|
||||
// TODO: passwd_len > 32
|
||||
static char passwd_prompt[VALUE_MAXLEN + 1];
|
||||
static void print_passwd(struct uint_point origin, uint length, bool err) {
|
||||
clean_line(origin, 9);
|
||||
clean_line(origin, PASSWD_ROW);
|
||||
printf("\r\x1b[%luC\x1b[%sm%s\x1b[%sm",
|
||||
(ulong)(origin.x + 11 - strlen(g_config->strings.e_passwd)),
|
||||
(ulong)(origin.x + VALUES_COL - VALUES_SEPR -
|
||||
utf8len(g_config->strings.e_passwd) - 1),
|
||||
g_config->colors.e_header, g_config->strings.e_passwd,
|
||||
g_config->colors.fg);
|
||||
|
||||
@ -323,47 +363,46 @@ static void print_passwd(struct uint_point origin, uint length, bool err) {
|
||||
else
|
||||
pass_color = g_config->colors.e_passwd;
|
||||
|
||||
ulong prompt_len = sizeof(passwd_prompt);
|
||||
ulong actual_len = length > prompt_len ? prompt_len : length;
|
||||
memset(passwd_prompt, ' ', prompt_len);
|
||||
ulong actual_len = length > VALUE_MAXLEN ? VALUE_MAXLEN : length;
|
||||
memset(passwd_prompt, ' ', VALUE_MAXLEN);
|
||||
memset(passwd_prompt, '*', actual_len);
|
||||
passwd_prompt[32] = 0;
|
||||
passwd_prompt[VALUE_MAXLEN] = '\0';
|
||||
|
||||
printf("\r\x1b[%dC\x1b[%sm", origin.x + 14, pass_color);
|
||||
printf("\r\x1b[%dC\x1b[%sm", origin.x + VALUES_COL - 1, pass_color);
|
||||
printf("%s", passwd_prompt);
|
||||
|
||||
printf("\x1b[%sm", g_config->colors.fg);
|
||||
}
|
||||
|
||||
static void print_empty_row(uint w, uint n, char* edge1, char* edge2) {
|
||||
static void print_empty_row(uint wid, uint n, char* edge1, char* edge2) {
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
printf("%s\x1b[%dC%s\x1b[%dD\x1b[1B", edge1, w, edge2, w + 2);
|
||||
printf("%s\x1b[%dC%s\x1b[%dD\x1b[1B", edge1, wid, edge2, wid + 2);
|
||||
}
|
||||
}
|
||||
|
||||
static void print_row(uint w, uint n, char* edge1, char* edge2, char* filler) {
|
||||
static void print_row(uint wid, uint n, char* edge1, char* edge2,
|
||||
char* filler) {
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
printf("%s", edge1);
|
||||
for (size_t i = 0; i < w; i++) {
|
||||
for (size_t i = 0; i < wid; i++) {
|
||||
printf("%s", filler);
|
||||
}
|
||||
printf("%s\x1b[%dD\x1b[1B", edge2, w + 2);
|
||||
printf("%s\x1b[%dD\x1b[1B", edge2, wid + 2);
|
||||
}
|
||||
}
|
||||
|
||||
static void print_box() {
|
||||
// TODO: check min sizes
|
||||
const struct uint_point bstart = box_start();
|
||||
|
||||
printf("\x1b[%d;%dH\x1b[%sm", bstart.y, bstart.x, g_config->colors.e_box);
|
||||
fflush(stdout);
|
||||
print_row(boxw - 2, 1, g_config->chars.ctl, g_config->chars.ctr,
|
||||
print_row(BOX_WIDTH - 2, 1, g_config->chars.ctl, g_config->chars.ctr,
|
||||
g_config->chars.hb);
|
||||
print_empty_row(boxw - 2, boxh - 2, g_config->chars.vb, g_config->chars.vb);
|
||||
print_row(boxw - 2, 1, g_config->chars.cbl, g_config->chars.cbr,
|
||||
print_empty_row(BOX_WIDTH - 2, BOX_HEIGHT - 2, g_config->chars.vb,
|
||||
g_config->chars.vb);
|
||||
print_row(BOX_WIDTH - 2, 1, g_config->chars.cbl, g_config->chars.cbr,
|
||||
g_config->chars.hb);
|
||||
printf("\x1b[%sm", g_config->colors.fg);
|
||||
fflush(stdout);
|
||||
(void)fflush(stdout);
|
||||
}
|
||||
|
||||
static void print_footer() {
|
||||
@ -383,30 +422,32 @@ static void print_footer() {
|
||||
key_names[g_config->functions.reboot],
|
||||
key_names[g_config->functions.refresh], g_config->strings.f_poweroff,
|
||||
g_config->strings.f_reboot, g_config->strings.f_refresh);
|
||||
fflush(stdout);
|
||||
(void)fflush(stdout);
|
||||
}
|
||||
|
||||
void print_err(const char* msg) {
|
||||
struct uint_point origin = box_start();
|
||||
fprintf(stderr, "\x1b[%d;%dH%s(%d): %s", origin.y - 1, origin.x, msg, errno,
|
||||
strerror(errno));
|
||||
(void)fprintf(stderr, "\x1b[%d;%dH%s(%d): %s", origin.y - 1, origin.x, msg,
|
||||
errno, strerror(errno));
|
||||
}
|
||||
|
||||
void print_errno(const char* descr) {
|
||||
struct uint_point origin = box_start();
|
||||
if (descr == NULL)
|
||||
fprintf(stderr, "\x1b[%d;%dH\x1b[%smunknown error(%d): %s", origin.y - 1,
|
||||
origin.x, g_config->colors.err, errno, strerror(errno));
|
||||
(void)fprintf(stderr, "\x1b[%d;%dH\x1b[%smunknown error(%d): %s",
|
||||
origin.y - 1, origin.x, g_config->colors.err, errno,
|
||||
strerror(errno));
|
||||
else {
|
||||
fprintf(stderr, "\x1b[%d;%dH\x1b[%sm%s(%d): %s", origin.y - 1, origin.x,
|
||||
g_config->colors.err, descr, errno, strerror(errno));
|
||||
(void)fprintf(stderr, "\x1b[%d;%dH\x1b[%sm%s(%d): %s", origin.y - 1,
|
||||
origin.x, g_config->colors.err, descr, errno,
|
||||
strerror(errno));
|
||||
}
|
||||
}
|
||||
|
||||
void restore_all() {
|
||||
// restore cursor pos, restore screen and show cursor
|
||||
printf("\x1b[u\x1b[?47l\x1b[?25h");
|
||||
fflush(stdout);
|
||||
(void)printf("\x1b[u\x1b[?47l\x1b[?25h");
|
||||
(void)fflush(stdout);
|
||||
tcsetattr(STDOUT_FILENO, TCSANOW, &orig_term);
|
||||
}
|
||||
|
||||
|
@ -76,6 +76,7 @@ void st_ch_ef_col(char direction) {
|
||||
ofield_opts_seek(&of_user, direction);
|
||||
|
||||
ui_update_cursor_focus();
|
||||
ui_update_ffield();
|
||||
}
|
||||
|
||||
void st_kbd_type(char* text, bool cfg_include_defshell) {
|
||||
|
@ -1,6 +1,5 @@
|
||||
#include <pwd.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
@ -39,7 +38,6 @@ int build_user(struct user* NNULLABLE user, struct passwd* p) {
|
||||
}
|
||||
|
||||
// This code is designed to be run purely single threaded
|
||||
// NOLINTNEXTLINE(modernize-macro-to-enum)
|
||||
#define LIKELY_BOUND_USERS 4
|
||||
struct Vector get_human_users() {
|
||||
struct Vector users = VEC_NEW;
|
||||
|
24
src/util.c
24
src/util.c
@ -11,7 +11,7 @@
|
||||
|
||||
static int selret_magic();
|
||||
|
||||
int find_keyname(enum keys* at, char* name) {
|
||||
int find_keyname(enum keys* at, const char* name) {
|
||||
for (size_t i = 0; i < sizeof(key_mappings) / sizeof(key_mappings[0]); i++) {
|
||||
if (strcmp(key_names[i], name) == 0) {
|
||||
*at = (enum keys)i;
|
||||
@ -22,7 +22,7 @@ int find_keyname(enum keys* at, char* name) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
enum keys find_ansi(char* seq) {
|
||||
enum keys find_ansi(const char* seq) {
|
||||
for (size_t i = 0; i < sizeof(key_mappings) / sizeof(key_mappings[0]); i++) {
|
||||
struct key_mapping mapping = key_mappings[i];
|
||||
for (size_t j = 0; mapping.sequences[j] != NULL; j++) {
|
||||
@ -34,7 +34,6 @@ enum keys find_ansi(char* seq) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
// https://stackoverflow.com/a/48040042
|
||||
void read_press(u_char* length, char* out) {
|
||||
*length = 0;
|
||||
|
||||
@ -54,6 +53,7 @@ void read_press(u_char* length, char* out) {
|
||||
}
|
||||
}
|
||||
|
||||
// https://stackoverflow.com/a/48040042
|
||||
static int selret_magic() {
|
||||
fd_set set;
|
||||
struct timeval timeout;
|
||||
@ -64,14 +64,14 @@ static int selret_magic() {
|
||||
return select(1, &set, NULL, NULL, &timeout);
|
||||
}
|
||||
|
||||
// UTF-8 shii+
|
||||
// UTF-8 shii
|
||||
#define UTF8_CONT_MSK 0b11000000
|
||||
#define UTF8_CONT_VAL 0b10000000
|
||||
bool utf8_iscont(char byte) {
|
||||
return (byte & UTF8_CONT_MSK) == UTF8_CONT_VAL;
|
||||
}
|
||||
|
||||
size_t utf8len(char* str) {
|
||||
size_t utf8len(const char* str) {
|
||||
size_t len = 0;
|
||||
while (*str != '\0') {
|
||||
if (!utf8_iscont(*(str++))) len++;
|
||||
@ -80,7 +80,7 @@ size_t utf8len(char* str) {
|
||||
return len;
|
||||
}
|
||||
|
||||
size_t utf8len_until(char* str, char* until) {
|
||||
size_t utf8len_until(const char* str, const char* until) {
|
||||
size_t len = 0;
|
||||
while (str < until) {
|
||||
if (!utf8_iscont(*(str++))) len++;
|
||||
@ -89,15 +89,23 @@ size_t utf8len_until(char* str, char* until) {
|
||||
return len;
|
||||
}
|
||||
|
||||
char* utf8back(char* str) {
|
||||
const char* utf8back(const char* str) {
|
||||
while(utf8_iscont(*(--str))) {}
|
||||
return str;
|
||||
}
|
||||
char* utf8seek(char* str) {
|
||||
const char* utf8seek(const char* str) {
|
||||
while(utf8_iscont(*(++str))) {}
|
||||
return str;
|
||||
}
|
||||
|
||||
const char* utf8seekn(const char* str, size_t n) {
|
||||
while(n > 0 && *str != '\0') {
|
||||
str = utf8seek(str);
|
||||
n--;
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
// Vector shii
|
||||
const struct Vector VEC_NEW = {
|
||||
.length = 0,
|
||||
|
Loading…
x
Reference in New Issue
Block a user