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