Refactor rz_core_theme_list() to return RzPVector (#4486)
* Refactor rz_core_theme_list() to return RzPVector * Rename to rz_core_get_themes() * Use switch in rz_core_theme_nextpal()
This commit is contained in:
parent
706a6bf6c2
commit
e9221853fc
4 changed files with 61 additions and 80 deletions
|
|
@ -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) {
|
||||
|
|
@ -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)) {
|
||||
|
|
@ -108,13 +75,13 @@ fail:
|
|||
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,20 @@ 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;
|
||||
static int compare_strings(const char *s1, const char *s2, RZ_UNUSED void *user) {
|
||||
return strcmp(s1, s2);
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Returns the list of the rizin themes.
|
||||
* \brief Get names of available rizin themes.
|
||||
*
|
||||
* \param core The RzCore struct to use
|
||||
* \return On success, an RzList pointer, otherwise NULL.
|
||||
* \param core The RzCore struct to use
|
||||
* \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_get_themes(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 +127,50 @@ 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(vec, strdup("default"));
|
||||
rz_pvector_sort(vec, (RzPVectorComparator)compare_strings, NULL);
|
||||
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_return_if_fail(core && core->curtheme);
|
||||
|
||||
rz_list_foreach (files, iter, fn) {
|
||||
if (*fn && *fn != '.') {
|
||||
if (!pal_seek(core, mode, fn, iter)) {
|
||||
goto done;
|
||||
}
|
||||
void **iter;
|
||||
size_t idx;
|
||||
RzPVector *files = rz_core_get_themes(core);
|
||||
const char *new_theme = NULL;
|
||||
rz_pvector_enumerate (files, iter, idx) {
|
||||
const char *fn = *iter;
|
||||
if (strcmp(fn, core->curtheme)) {
|
||||
continue;
|
||||
}
|
||||
switch (mode) {
|
||||
case RZ_CONS_PAL_SEEK_PREVIOUS:
|
||||
if (idx > 0) {
|
||||
new_theme = rz_pvector_at(files, idx - 1);
|
||||
}
|
||||
break;
|
||||
case RZ_CONS_PAL_SEEK_NEXT:
|
||||
if (idx < rz_pvector_len(files) - 1) {
|
||||
new_theme = rz_pvector_at(files, idx + 1);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
rz_warn_if_reached();
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
rz_list_free(files);
|
||||
files = NULL;
|
||||
done:
|
||||
rz_core_theme_load(core, core->curtheme);
|
||||
rz_list_free(files);
|
||||
if (new_theme) {
|
||||
rz_core_theme_load(core, new_theme);
|
||||
}
|
||||
rz_pvector_free(files);
|
||||
}
|
||||
|
||||
RZ_IPI RzCmdStatus rz_cmd_eval_color_list_handler(RzCore *core, int argc, const char **argv, RzOutputMode mode) {
|
||||
|
|
@ -260,21 +243,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);
|
||||
if (!themes_list) {
|
||||
RzPVector *themes = rz_core_get_themes(core);
|
||||
if (!themes) {
|
||||
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, iter) {
|
||||
const char *th = *iter;
|
||||
switch (state->mode) {
|
||||
case RZ_OUTPUT_MODE_JSON: {
|
||||
pj_s(pj, th);
|
||||
|
|
@ -294,7 +276,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);
|
||||
return RZ_CMD_STATUS_OK;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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 *>*/ *themes; ///< Available rizin themes
|
||||
bool from_visual;
|
||||
} RzPanelsRoot;
|
||||
|
||||
|
|
|
|||
|
|
@ -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->themes, 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->themes);
|
||||
free(panels_root);
|
||||
}
|
||||
|
||||
|
|
@ -6161,8 +6161,7 @@ RZ_IPI bool rz_core_visual_panels_root(RzCore *core, RzPanelsRoot *panels_root)
|
|||
rz_pvector_reserve(&panels_root->tabs, PANEL_NUM_LIMIT);
|
||||
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);
|
||||
panels_root->themes = rz_core_get_themes(core);
|
||||
__init_new_panels_root(core);
|
||||
} else {
|
||||
if (rz_pvector_len(&panels_root->tabs) == 0) {
|
||||
|
|
|
|||
|
|
@ -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_get_themes(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);
|
||||
|
|
|
|||
Loading…
Reference in a new issue