summaryrefslogtreecommitdiff
path: root/src
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
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')
-rw-r--r--src/fruits/cmsrv/ui.c84
-rw-r--r--src/fruits/cmsrv/ui.h6
-rw-r--r--src/liana/list.c8
-rw-r--r--src/liana/list.h6
-rw-r--r--src/liana/list_cmp.h51
-rw-r--r--src/server/server.c7
6 files changed, 92 insertions, 70 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;
};
diff --git a/src/liana/list.c b/src/liana/list.c
index fc34aca..df91140 100644
--- a/src/liana/list.c
+++ b/src/liana/list.c
@@ -435,7 +435,7 @@ static void handle_clear(struct lia_list *list)
{
struct lia_list_entry *entry;
al_array_foreach(list->entries, i, entry) {
- al_str_free(&entry->name);
+ al_wstr_free(&entry->name);
al_free(entry);
}
list->entries.size = 0;
@@ -532,7 +532,7 @@ void lia_list_remove_sink(struct lia_list *list, void *userdata)
pump_queue(list);
}
-void lia_list_add(struct lia_list *list, void *opaque, u64 duration, str *name)
+void lia_list_add(struct lia_list *list, void *opaque, u64 duration, wstr *name)
{
struct lia_list_entry *entry = al_alloc_object(struct lia_list_entry);
entry->opaque = opaque;
@@ -541,7 +541,7 @@ void lia_list_add(struct lia_list *list, void *opaque, u64 duration, str *name)
entry->offset = 0;
entry->ended = false;
entry->duration = duration;
- al_str_clone(&entry->name, name);
+ al_wstr_clone(&entry->name, name);
entry->list = list;
struct lia_list_cmd *cmd = al_alloc_object(struct lia_list_cmd);
cmd->op = ADD;
@@ -643,7 +643,7 @@ void lia_list_free(struct lia_list *list)
{
struct lia_list_entry *entry;
al_array_foreach(list->entries, i, entry) {
- al_str_free(&entry->name);
+ al_wstr_free(&entry->name);
al_free(entry);
}
al_array_free(list->entries);
diff --git a/src/liana/list.h b/src/liana/list.h
index 270ef98..4f56641 100644
--- a/src/liana/list.h
+++ b/src/liana/list.h
@@ -1,7 +1,7 @@
#pragma once
#include <al/types.h>
-#include <al/str.h>
+#include <al/wstr.h>
#include <al/array.h>
#define LIANA_SEQUENCE_ANY -1
@@ -56,7 +56,7 @@ struct lia_list_entry {
u64 offset;
bool ended;
u64 duration;
- str name;
+ wstr name;
struct lia_list *list;
};
@@ -98,7 +98,7 @@ void lia_list_pump(struct lia_list *list);
void lia_list_add_sink(struct lia_list *list, void (*callback)(void *, u8, struct lia_list_entry *, s32, struct lia_timing *), void *userdata);
void lia_list_remove_sink(struct lia_list *list, void *userdata);
-void lia_list_add(struct lia_list *list, void *opaque, u64 duration, str *name);
+void lia_list_add(struct lia_list *list, void *opaque, u64 duration, wstr *name);
void lia_list_unset(struct lia_list *list);
void lia_list_skipto(struct lia_list *list, s32 sequence, s32 i);
void lia_list_skip(struct lia_list *list, s32 sequence, s32 n);
diff --git a/src/liana/list_cmp.h b/src/liana/list_cmp.h
index 1f77b46..ef923fb 100644
--- a/src/liana/list_cmp.h
+++ b/src/liana/list_cmp.h
@@ -4,32 +4,39 @@
#include "list.h"
-static void camu_db_num_from_path(str *path, s64 *id, s64 *index)
+AL_UNUSED_FUNCTION_PUSH
+
+static void camu_db_num_from_path(wstr *path, s64 *id, s64 *index)
{
- s32 index0 = al_str_rfind(path, '/');
+ s32 index0 = al_wstr_rfind(path, L'/');
if (index0 < 0) return;
- str s = *al_str_substr(path, index0 + 1, path->len);
- index0 = al_str_find(&s, '_');
+ wstr w = *al_wstr_substr(path, index0 + 1, path->len);
+ index0 = al_wstr_find(&w, L'_');
if (index0 < 0) return;
- s = *al_str_substr(&s, index0 + 1, s.len);
- index0 = al_str_find(&s, '_');
+ w = *al_wstr_substr(&w, index0 + 1, w.len);
+ index0 = al_wstr_find(&w, L'_');
if (index0 < 0) return;
- s = *al_str_substr(&s, index0 + 1, s.len);
- index0 = al_str_find(&s, '_');
+ w = *al_wstr_substr(&w, index0 + 1, w.len);
+ index0 = al_wstr_find(&w, L'_');
if (index0 < 0) return;
- s = *al_str_substr(&s, 0, index0);
+ w = *al_wstr_substr(&w, 0, index0);
+ str s;
+ al_wstr_to_str(&w, &s);
s64 num = al_str_to_long(&s, 10);
+ al_str_free(&s);
if (num == INT64_MIN || num == INT64_MAX) return;
*id = num;
- index0 = al_str_rfind(path, '.');
- s32 index1 = al_str_rfind(path, 'a');
+ index0 = al_wstr_rfind(path, L'.');
+ s32 index1 = al_wstr_rfind(path, 'a');
if (index0 < 0 || index1 < 0) return;
- s = *al_str_substr(path, index1 + 1, index0);
+ w = *al_wstr_substr(path, index1 + 1, index0);
+ al_wstr_to_str(&w, &s);
num = al_str_to_long(&s, 10);
+ al_str_free(&s);
if (num == INT64_MIN || num == INT64_MAX) return;
*index = num;
}
@@ -38,16 +45,18 @@ static s32 camu_db_compare(const void *a, const void *b)
{
struct lia_list_entry *aa = *((struct lia_list_entry **)a);
struct lia_list_entry *bb = *((struct lia_list_entry **)b);
- s64 anum = -1, aindex = -1;
- s64 bnum = -1, bindex = -1;
- camu_db_num_from_path(&aa->name, &anum, &aindex);
- camu_db_num_from_path(&bb->name, &bnum, &bindex);
- if (anum == bnum) {
- if (aindex > bindex) return 1;
- else if (aindex < bindex) return -1;
+ s64 a_id = -1, a_index = -1;
+ s64 b_id = -1, b_index = -1;
+ camu_db_num_from_path(&aa->name, &a_id, &a_index);
+ camu_db_num_from_path(&bb->name, &b_id, &b_index);
+ if (a_id == b_id) {
+ if (a_index > b_index) return 1;
+ else if (a_index < b_index) return -1;
} else {
- if (anum > bnum) return -1;
- else if (anum < bnum) return 1;
+ if (a_id > b_id) return -1;
+ else if (a_id < b_id) return 1;
}
return 0;
}
+
+AL_UNUSED_FUNCTION_PUSH
diff --git a/src/server/server.c b/src/server/server.c
index a76fb77..a77046b 100644
--- a/src/server/server.c
+++ b/src/server/server.c
@@ -310,7 +310,10 @@ static void handle_add_command(struct camu_server *server, struct lia_list *list
resource->r.node->userdata = (struct camu_resource *)resource;
resource->r.duration = LIANA_TIMESTAMP_INVALID;
al_array_init(resource->r.pending);
- lia_list_add(list, resource, resource->r.duration, &path);
+ wstr name;
+ al_wstr_from_str(&name, &path);
+ lia_list_add(list, resource, resource->r.duration, &name);
+ al_wstr_free(&name);
break;
}
case CAMU_RESOURCE_PORTAL: {
@@ -341,7 +344,7 @@ static void handle_add_command(struct camu_server *server, struct lia_list *list
resource->r.node->userdata = (struct camu_resource *)resource;
resource->r.duration = LIANA_TIMESTAMP_INVALID;
al_array_init(resource->r.pending);
- lia_list_add(list, resource, resource->r.duration, al_str_c("dfdd"));
+ lia_list_add(list, resource, resource->r.duration, &resource->post->title);
break;
}
}