Skip to content

Commit

Permalink
Refactor rz_core_theme_list() to return RzPVector
Browse files Browse the repository at this point in the history
  • Loading branch information
pelijah committed May 12, 2024
1 parent 0a05e42 commit 9f4d76b
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 78 deletions.
115 changes: 44 additions & 71 deletions librz/core/cmd/cmd_eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <stddef.h>
#include <stdbool.h>
#include <rz_core.h>
#include <rz_util/set.h>
#include "../core_private.h"

static bool load_theme(RzCore *core, const char *path) {
Expand All @@ -19,40 +20,6 @@ static bool load_theme(RzCore *core, const char *path) {
return res;
}

static bool pal_seek(RzCore *core, RzConsPalSeekMode mode, const char *file, RzListIter /*<char *>*/ *iter) {
const char *fn = rz_str_lchr(file, '/');
if (!fn) {
fn = file;
}
switch (mode) {
case RZ_CONS_PAL_SEEK_PREVIOUS: {
const char *next_fn = rz_list_iter_get_next_data(iter);
if (!core->curtheme) {
return true;
}
if (next_fn && !strcmp(next_fn, core->curtheme)) {
free(core->curtheme);
core->curtheme = strdup(fn);
return false;
}
break;
}
case RZ_CONS_PAL_SEEK_NEXT: {
const char *prev_fn = rz_list_iter_get_prev_data(iter);
if (!core->curtheme) {
return true;
}
if (prev_fn && !strcmp(prev_fn, core->curtheme)) {
free(core->curtheme);
core->curtheme = strdup(fn);
return false;
}
break;
}
}
return true;
}

RZ_API bool rz_core_theme_load(RzCore *core, const char *name) {
bool failed = false;
if (RZ_STR_ISEMPTY(name)) {
Expand Down Expand Up @@ -108,13 +75,13 @@ RZ_API bool rz_core_theme_load(RzCore *core, const char *name) {
return !failed;
}

static void list_themes_in_path(HtSU *themes, const char *path) {
static void list_themes_in_path(SetS *themes, const char *path) {
RzListIter *iter;
const char *fn;
RzList *files = rz_sys_dir(path);
rz_list_foreach (files, iter, fn) {
if (*fn && *fn != '.') {
ht_su_insert(themes, fn, 1);
set_s_add(themes, fn);
}
}
rz_list_free(files);
Expand All @@ -124,22 +91,16 @@ RZ_API char *rz_core_theme_get(RzCore *core) {
return core->curtheme;
}

static bool dict2keylist(void *user, const char *key, const ut64 value) {
RzList *list = (RzList *)user;
rz_list_append(list, strdup(key));
return true;
}

/**
* \brief Returns the list of the rizin themes.
*
* \param core The RzCore struct to use
* \return On success, an RzList pointer, otherwise NULL.
* \return On success, an RzPVector pointer, otherwise NULL.
*/
RZ_API RZ_OWN RzList /*<char *>*/ *rz_core_theme_list(RZ_NONNULL RzCore *core) {
RZ_API RZ_OWN RzPVector /*<char *>*/ *rz_core_theme_list(RZ_NONNULL RzCore *core) {
rz_return_val_if_fail(core, NULL);

HtSU *themes = ht_su_new(HT_STR_DUP);
SetS *themes = set_s_new(HT_STR_DUP);
if (!themes) {
return NULL;
}
Expand All @@ -162,32 +123,45 @@ RZ_API RZ_OWN RzList /*<char *>*/ *rz_core_theme_list(RZ_NONNULL RzCore *core) {
RZ_FREE(path);
}

RzList *list = rz_list_newf(free);
rz_list_append(list, strdup("default"));
ht_su_foreach(themes, dict2keylist, list);

rz_list_sort(list, (RzListComparator)strcmp, NULL);
ht_su_free(themes);
return list;
RzPVector *vec = set_s_to_vector(themes);
if (!vec) {
set_s_free(themes);
return NULL;
}
rz_pvector_push_front(vec, strdup("default"));
set_s_free(themes);
return vec;
}

RZ_API void rz_core_theme_nextpal(RzCore *core, RzConsPalSeekMode mode) {
RzListIter *iter;
const char *fn;
RzList *files = rz_core_theme_list(core);

rz_list_foreach (files, iter, fn) {
if (*fn && *fn != '.') {
if (!pal_seek(core, mode, fn, iter)) {
goto done;
rz_return_if_fail(core && core->curtheme);

void **iter;
size_t idx;
RzPVector *files = rz_core_theme_list(core);
rz_pvector_enumerate (files, iter, idx) {
const char *fn = *iter;
if (strcmp(fn, core->curtheme)) {
continue;
}
if (mode == RZ_CONS_PAL_SEEK_PREVIOUS) {
if (idx == 0) {
break;
}
fn = rz_pvector_at(files, idx - 1);
} else if (mode == RZ_CONS_PAL_SEEK_NEXT) {
if (idx == rz_pvector_len(files) - 1) {
break;
}
fn = rz_pvector_at(files, idx + 1);
} else {
rz_warn_if_reached();
break;
}
rz_core_theme_load(core, fn);
break;
}
rz_list_free(files);
files = NULL;
done:
rz_core_theme_load(core, core->curtheme);
rz_list_free(files);
rz_pvector_free(files);
}

RZ_IPI RzCmdStatus rz_cmd_eval_color_list_handler(RzCore *core, int argc, const char **argv, RzOutputMode mode) {
Expand Down Expand Up @@ -260,21 +234,20 @@ RZ_IPI RzCmdStatus rz_cmd_eval_color_highlight_list_handler(RzCore *core, int ar
}

RZ_IPI RzCmdStatus rz_cmd_eval_color_load_theme_handler(RzCore *core, int argc, const char **argv, RzCmdStateOutput *state) {
RzList *themes_list = NULL;
RzListIter *th_iter;
PJ *pj = state->d.pj;
const char *th;
if (argc == 2) {
return bool2status(rz_core_theme_load(core, argv[1]));
}
themes_list = rz_core_theme_list(core);
RzPVector *themes_list = rz_core_theme_list(core);
if (!themes_list) {
return RZ_CMD_STATUS_ERROR;
}
if (state->mode == RZ_OUTPUT_MODE_JSON) {
pj_a(pj);
}
rz_list_foreach (themes_list, th_iter, th) {
void **iter;
rz_pvector_foreach (themes_list, iter) {
const char *th = *iter;
switch (state->mode) {
case RZ_OUTPUT_MODE_JSON: {
pj_s(pj, th);
Expand All @@ -294,7 +267,7 @@ RZ_IPI RzCmdStatus rz_cmd_eval_color_load_theme_handler(RzCore *core, int argc,
if (state->mode == RZ_OUTPUT_MODE_JSON) {
pj_end(pj);
}
rz_list_free(themes_list);
rz_pvector_free(themes_list);
return RZ_CMD_STATUS_OK;
}

Expand Down
2 changes: 1 addition & 1 deletion librz/core/core_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ typedef struct rz_panels_root_t {
RzPVector /*<RzPanelsTab *>*/ tabs;
RzPanelsTab *active_tab; // Seems redudant since we have cur_tab index
RzPanelsRootState root_state;
RzList /*<char *>*/ *theme_list; ///< List of themes
RzPVector /*<char *>*/ *theme_list; ///< List of themes
bool from_visual;
} RzPanelsRoot;

Expand Down
10 changes: 5 additions & 5 deletions librz/core/tui/panels.c
Original file line number Diff line number Diff line change
Expand Up @@ -4374,11 +4374,11 @@ void __init_menu_color_settings_layout(void *_core, const char *parent) {
char *now = strdup(curtheme);
rz_str_split(now, '\n');
parent = "Settings.Colors";
RzListIter *iter;
char *pos;
RzStrBuf *buf = rz_strbuf_new(NULL);
RzCoreVisual *visual = core->visual;
rz_list_foreach (visual->panels_root->theme_list, iter, pos) {
void **iter;
rz_pvector_foreach (visual->panels_root->theme_list, iter) {
const char *pos = *iter;
if (pos && !strcmp(now, pos)) {
rz_strbuf_setf(buf, "%s%s", color, pos);
__add_menu(core, parent, rz_strbuf_get(buf), __settings_colors_cb);
Expand Down Expand Up @@ -6145,7 +6145,7 @@ RZ_IPI void rz_panels_root_free(RZ_NULLABLE RzPanelsRoot *panels_root) {
return;
}
rz_pvector_fini(&panels_root->tabs);
rz_list_free(panels_root->theme_list);
rz_pvector_free(panels_root->theme_list);
free(panels_root);
}

Expand All @@ -6162,7 +6162,7 @@ RZ_IPI bool rz_core_visual_panels_root(RzCore *core, RzPanelsRoot *panels_root)
panels_root->cur_tab = 0;
__set_root_state(core, DEFAULT);
panels_root->theme_list = rz_core_theme_list(core);
rz_list_sort(panels_root->theme_list, cmpstr, NULL);
rz_pvector_sort(panels_root->theme_list, cmpstr, NULL);
__init_new_panels_root(core);
} else {
if (rz_pvector_len(&panels_root->tabs) == 0) {
Expand Down
2 changes: 1 addition & 1 deletion librz/include/rz_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ RZ_API bool rz_core_plugin_del(RzCore *core, RZ_NONNULL RzCorePlugin *plugin);
RZ_API bool rz_core_plugin_fini(RzCore *core);

// #define rz_core_ncast(x) (RzCore*)(size_t)(x)
RZ_API RZ_OWN RzList /*<char *>*/ *rz_core_theme_list(RZ_NONNULL RzCore *core);
RZ_API RZ_OWN RzPVector /*<char *>*/ *rz_core_theme_list(RZ_NONNULL RzCore *core);
RZ_API char *rz_core_theme_get(RzCore *core);
RZ_API bool rz_core_theme_load(RzCore *core, const char *name);
RZ_API void rz_core_theme_nextpal(RzCore *core, RzConsPalSeekMode mode);
Expand Down

0 comments on commit 9f4d76b

Please sign in to comment.