summaryrefslogtreecommitdiff
path: root/src/fruits
diff options
context:
space:
mode:
authorAndrew Opalach <andrew@akon.city> 2024-11-10 15:40:44 -0500
committerAndrew Opalach <andrew@akon.city> 2024-11-10 15:40:44 -0500
commitb4bcfa1da5c1be9f7999d4d4df4e8d1b73c3061a (patch)
treef8c8c154093a27693ff3fc8c943d9b60388bfcea /src/fruits
parent93908b8c5931c944b1e91c057ef9b55933944814 (diff)
downloadcamu-b4bcfa1da5c1be9f7999d4d4df4e8d1b73c3061a.tar.gz
camu-b4bcfa1da5c1be9f7999d4d4df4e8d1b73c3061a.tar.bz2
camu-b4bcfa1da5c1be9f7999d4d4df4e8d1b73c3061a.zip
Make list entry name a wide string
Signed-off-by: Andrew Opalach <andrew@akon.city>
Diffstat (limited to 'src/fruits')
-rw-r--r--src/fruits/cmsrv/ui.c84
-rw-r--r--src/fruits/cmsrv/ui.h6
2 files changed, 50 insertions, 40 deletions
diff --git a/src/fruits/cmsrv/ui.c b/src/fruits/cmsrv/ui.c
index c5cceef..1a52641 100644
--- a/src/fruits/cmsrv/ui.c
+++ b/src/fruits/cmsrv/ui.c
@@ -13,6 +13,7 @@ static s32 resize_cb(struct ncplane *p)
bool cmsrv_ui_init(struct cmsrv_ui *ui, struct camu_server *server)
{
+ al_memset(ui, 0, sizeof(struct cmsrv_ui));
if (!(ui->nc = notcurses_core_init(NULL, stdin))) {
return false;
}
@@ -56,31 +57,31 @@ static void layout_log(struct cmsrv_ui *ui, struct ncplane *parent)
.x = 0
};
nopts.y = ui->term_rows - nopts.rows;
- ui->log.p = ncplane_create(parent, &nopts);
+ ui->log.n = ncplane_create(parent, &nopts);
}
static void erase_log(struct cmsrv_ui *ui)
{
- struct ncplane *p = ui->log.p;
- ncplane_erase(p);
+ struct ncplane *n = ui->log.n;
+ if (n) ncplane_erase(n);
}
static void render_log(struct cmsrv_ui *ui)
{
- struct ncplane *p = ui->log.p;
- ncplane_erase(p);
+ struct ncplane *n = ui->log.n;
+ ncplane_erase(n);
- u32 max_width = ncplane_dim_x(p) - 2;
- u32 max_height = ncplane_dim_y(p) - 2;
+ u32 max_width = ncplane_dim_x(n) - 2;
+ u32 max_height = ncplane_dim_y(n) - 2;
u64 c = 0;
ncchannels_set_fg_default(&c);
- ncplane_rounded_box(p, NCSTYLE_NONE, c, max_height + 1, max_width + 1, 0);
+ ncplane_rounded_box(n, NCSTYLE_NONE, c, max_height + 1, max_width + 1, 0);
u32 size = ui->log.messages.size;
u32 index = size > max_height ? size - max_height : 0;
for (u32 i = index; i < size; i++) {
- ncplane_putnstr_yx(p, (i - index) + 1, 1, max_width, al_array_at(ui->log.messages, i));
+ ncplane_putnstr_yx(n, (i - index) + 1, 1, max_width, al_array_at(ui->log.messages, i));
}
}
@@ -92,44 +93,49 @@ static void layout_lists(struct cmsrv_ui *ui, struct ncplane *parent)
.x = 0,
.y = 0
};
- ui->lists.p = ncplane_create(parent, &nopts);
+ ui->lists.n = ncplane_create(parent, &nopts);
}
static void erase_lists(struct cmsrv_ui *ui)
{
- struct ncplane *p = ui->lists.p;
- ncplane_erase(p);
+ struct ncplane *n = ui->lists.n;
+ if (n) ncplane_erase(n);
+}
+
+static void putnwstr_maxwidth_yx(struct ncplane *n, u32 y, u32 x, u32 maxwidth, wstr *w)
+{
+ ncplane_cursor_move_yx(n, y, x);
+ u32 end = AL_MIN(w->len, maxwidth);
+ for (u32 i = 0; i < end; i++) {
+ ncplane_putwc(n, al_wstr_at(w, i));
+ }
}
static void render_lists(struct cmsrv_ui *ui)
{
- struct ncplane *p = ui->lists.p;
+ struct ncplane *n = ui->lists.n;
- u32 max_width = ncplane_dim_x(p);
- u32 max_height = ncplane_dim_y(p);
+ u32 max_width = ncplane_dim_x(n);
+ u32 max_height = ncplane_dim_y(n);
u32 current_line = 0;
s32 entries_per_list = 10;
struct lia_list *list;
al_array_foreach(ui->server->lists, i, list) {
if (current_line++ >= max_height) break;
- char *c_str = al_str_to_c_str(&list->name);
- ncplane_putnstr_yx(p, i, 0, max_width, c_str);
- al_free(c_str);
+ ncplane_putnstr_yx(n, i, 0, AL_MIN(max_width, list->name.len), &al_str_at(&list->name, 0));
s32 index = AL_MAX(list->current - (entries_per_list / 2), 0);
s32 size = (s32)list->entries.size;
s32 end = AL_MIN(index + entries_per_list, size);
for (s32 j = index; j < end; j++) {
struct lia_list_entry *entry = al_array_at(list->entries, j);
- c_str = al_str_to_c_str(&entry->name);
u32 y = i + (j - index) + 1;
+ u32 x = 1;
if (j == list->current) {
- ncplane_putchar_yx(p, y, 1, '>');
- ncplane_putnstr_yx(p, y, 3, max_width - 3, c_str);
- } else {
- ncplane_putnstr_yx(p, y, 1, max_width - 1, c_str);
+ ncplane_putchar_yx(n, y, 1, '>');
+ x += 2;
}
- al_free(c_str);
+ putnwstr_maxwidth_yx(n, y, x, max_width - x, &entry->name);
if (++current_line >= max_height) break;
}
}
@@ -143,21 +149,21 @@ static void layout_nodes(struct cmsrv_ui *ui, struct ncplane *parent)
.x = 0,
.y = 0
};
- ui->nodes.p = ncplane_create(parent, &nopts);
+ ui->nodes.n = ncplane_create(parent, &nopts);
}
static void erase_nodes(struct cmsrv_ui *ui)
{
- struct ncplane *p = ui->nodes.p;
- ncplane_erase(p);
+ struct ncplane *n = ui->nodes.n;
+ if (n) ncplane_erase(n);
}
static void render_nodes(struct cmsrv_ui *ui)
{
- struct ncplane *p = ui->lists.p;
+ struct ncplane *n = ui->lists.n;
- u32 max_width = ncplane_dim_x(p);
- u32 max_height = ncplane_dim_y(p);
+ u32 max_width = ncplane_dim_x(n);
+ u32 max_height = ncplane_dim_y(n);
(void)max_height;
char strbuf[16];
@@ -165,27 +171,31 @@ static void render_nodes(struct cmsrv_ui *ui)
struct lia_node *node;
al_array_foreach(ui->server->data.server.nodes, i, node) {
al_snprintf(strbuf, sizeof(strbuf), "%hu", node->id);
- ncplane_putnstr_yx(p, i, 0, max_width, strbuf);
+ ncplane_putnstr_yx(n, i, 0, max_width, strbuf);
struct lia_node_connection *conn;
al_array_foreach(node->connections, j, conn) {
al_snprintf(strbuf, sizeof(strbuf), "%u", conn->pool.ready.size);
- ncplane_putnstr_yx(p, i + j + 1, 2, max_width, strbuf);
+ ncplane_putnstr_yx(n, i + j + 1, 2, max_width, strbuf);
}
}
}
void cmsrv_ui_render(struct cmsrv_ui *ui)
{
+ struct ncplane *stdplane = notcurses_stdplane(ui->nc);
+ erase_log(ui);
+ erase_lists(ui);
+ erase_nodes(ui);
if (ui->pending_layout) {
- ui->pending_layout = false;
- struct ncplane *stdplane = notcurses_stdplane(ui->nc);
+ ncplane_erase(stdplane);
+ notcurses_refresh(ui->nc, &ui->term_rows, &ui->term_cols);
+ notcurses_render(ui->nc);
+ notcurses_stddim_yx(ui->nc, &ui->term_rows, &ui->term_cols);
layout_log(ui, stdplane);
layout_lists(ui, stdplane);
layout_nodes(ui, stdplane);
+ ui->pending_layout = false;
}
- erase_log(ui);
- erase_lists(ui);
- erase_nodes(ui);
render_log(ui);
switch (ui->pane) {
case CMSRV_UI_LISTS:
diff --git a/src/fruits/cmsrv/ui.h b/src/fruits/cmsrv/ui.h
index 08ad956..45fdd85 100644
--- a/src/fruits/cmsrv/ui.h
+++ b/src/fruits/cmsrv/ui.h
@@ -17,16 +17,16 @@ struct cmsrv_ui {
bool pending_layout;
u8 pane;
struct {
- struct ncplane *p;
+ struct ncplane *n;
array(char *) messages;
} log;
struct {
- struct ncplane *p;
+ struct ncplane *n;
} lists;
struct {
} resources;
struct {
- struct ncplane *p;
+ struct ncplane *n;
} nodes;
struct camu_server *server;
};