From a36d2bfe644cd441cdc257a7a31026a8daec2d6e Mon Sep 17 00:00:00 2001 From: pelijah Date: Sat, 11 May 2024 17:22:35 +0300 Subject: [PATCH] Refactor rz_core_theme_list() to return RzPVector --- librz/core/cmd/cmd_eval.c | 115 +++++++++++++++----------------------- librz/core/core_private.h | 2 +- librz/core/tui/panels.c | 10 ++-- librz/include/rz_core.h | 2 +- 4 files changed, 51 insertions(+), 78 deletions(-) diff --git a/librz/core/cmd/cmd_eval.c b/librz/core/cmd/cmd_eval.c index babf3c8828d..d87ecf87350 100644 --- a/librz/core/cmd/cmd_eval.c +++ b/librz/core/cmd/cmd_eval.c @@ -4,6 +4,7 @@ #include #include #include +#include #include "../core_private.h" static bool load_theme(RzCore *core, const char *path) { @@ -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 /**/ *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)) { @@ -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); @@ -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 /**/ *rz_core_theme_list(RZ_NONNULL RzCore *core) { +RZ_API RZ_OWN RzPVector /**/ *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; } @@ -162,32 +123,45 @@ RZ_API RZ_OWN RzList /**/ *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) { @@ -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); @@ -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; } diff --git a/librz/core/core_private.h b/librz/core/core_private.h index 5d732cd88b1..81a46953a6c 100644 --- a/librz/core/core_private.h +++ b/librz/core/core_private.h @@ -330,7 +330,7 @@ typedef struct rz_panels_root_t { RzPVector /**/ tabs; RzPanelsTab *active_tab; // Seems redudant since we have cur_tab index RzPanelsRootState root_state; - RzList /**/ *theme_list; ///< List of themes + RzPVector /**/ *theme_list; ///< List of themes bool from_visual; } RzPanelsRoot; diff --git a/librz/core/tui/panels.c b/librz/core/tui/panels.c index 295778c798f..52bf0ea4a5a 100644 --- a/librz/core/tui/panels.c +++ b/librz/core/tui/panels.c @@ -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); @@ -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); } @@ -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) { diff --git a/librz/include/rz_core.h b/librz/include/rz_core.h index 0e43e8deb41..61b78096019 100644 --- a/librz/include/rz_core.h +++ b/librz/include/rz_core.h @@ -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 /**/ *rz_core_theme_list(RZ_NONNULL RzCore *core); +RZ_API RZ_OWN RzPVector /**/ *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);