diff options
author | Sébastien Helleu <flashcode@flashtux.org> | 2022-05-25 21:48:26 +0200 |
---|---|---|
committer | Sébastien Helleu <flashcode@flashtux.org> | 2022-05-27 22:16:50 +0200 |
commit | 007958e82c272a63255f4389155bd4dd58c461c3 (patch) | |
tree | 3c1b0f44c796b6a7c803bb2777d4c0744ffd798c /src | |
parent | 5876a8335f00faaf0c25259f4a7712b45e040be0 (diff) | |
download | weechat-007958e82c272a63255f4389155bd4dd58c461c3.zip |
core: add display conditions in custom bar items (issue #808)
Diffstat (limited to 'src')
-rw-r--r-- | src/core/wee-command.c | 53 | ||||
-rw-r--r-- | src/core/wee-config.c | 12 | ||||
-rw-r--r-- | src/gui/gui-bar-item-custom.c | 90 | ||||
-rw-r--r-- | src/gui/gui-bar-item-custom.h | 4 |
4 files changed, 126 insertions, 33 deletions
diff --git a/src/core/wee-command.c b/src/core/wee-command.c index bc72efa76..4c48fe472 100644 --- a/src/core/wee-command.c +++ b/src/core/wee-command.c @@ -3395,8 +3395,8 @@ COMMAND_CALLBACK(input) COMMAND_CALLBACK(item) { struct t_gui_bar_item_custom *ptr_bar_item_custom; - char str_command[4096], str_pos[16]; - int i, update; + char str_command[4096], str_pos[16], **sargv; + int i, update, sargc; /* make C compiler happy */ (void) pointer; @@ -3415,9 +3415,19 @@ COMMAND_CALLBACK(item) ptr_bar_item_custom = ptr_bar_item_custom->next_item) { gui_chat_printf ( - NULL, " %s -> \"%s\"", - ptr_bar_item_custom->bar_item->name, - CONFIG_STRING(ptr_bar_item_custom->options[GUI_BAR_ITEM_CUSTOM_OPTION_CONTENT])); + NULL, " %s:", ptr_bar_item_custom->bar_item->name); + gui_chat_printf (NULL, _(" conditions: %s\"%s%s%s\"%s"), + GUI_COLOR(GUI_COLOR_CHAT_DELIMITERS), + GUI_COLOR(GUI_COLOR_CHAT), + CONFIG_STRING(ptr_bar_item_custom->options[GUI_BAR_ITEM_CUSTOM_OPTION_CONDITIONS]), + GUI_COLOR(GUI_COLOR_CHAT_DELIMITERS), + GUI_COLOR(GUI_COLOR_CHAT)); + gui_chat_printf (NULL, _(" content: %s\"%s%s%s\"%s"), + GUI_COLOR(GUI_COLOR_CHAT_DELIMITERS), + GUI_COLOR(GUI_COLOR_CHAT), + CONFIG_STRING(ptr_bar_item_custom->options[GUI_BAR_ITEM_CUSTOM_OPTION_CONTENT]), + GUI_COLOR(GUI_COLOR_CHAT_DELIMITERS), + GUI_COLOR(GUI_COLOR_CHAT)); } } else @@ -3432,12 +3442,18 @@ COMMAND_CALLBACK(item) if ((string_strcasecmp (argv[1], "add") == 0) || (string_strcasecmp (argv[1], "addreplace") == 0)) { - COMMAND_MIN_ARGS(4, argv[1]); + sargv = string_split_shell (argv_eol[2], &sargc); + if (!sargv || (sargc < 3)) + { + if (sargv) + string_free_split (sargv); + COMMAND_ERROR; + } update = 0; if (string_strcasecmp (argv[1], "addreplace") == 0) { - ptr_bar_item_custom = gui_bar_item_custom_search (argv[2]); + ptr_bar_item_custom = gui_bar_item_custom_search (sargv[0]); if (ptr_bar_item_custom) { gui_bar_item_custom_free (ptr_bar_item_custom); @@ -3445,23 +3461,26 @@ COMMAND_CALLBACK(item) } } - ptr_bar_item_custom = gui_bar_item_custom_new (argv[2], argv_eol[3]); + ptr_bar_item_custom = gui_bar_item_custom_new (sargv[0], sargv[1], + sargv[2]); if (ptr_bar_item_custom) { gui_chat_printf (NULL, (update) ? _("Custom bar item \"%s\" updated") : _("Custom bar item \"%s\" added"), - argv[2]); + sargv[0]); } else { gui_chat_printf (NULL, _("%sUnable to add custom bar item \"%s\""), gui_chat_prefix[GUI_CHAT_PREFIX_ERROR], - argv[2]); + sargv[0]); } + string_free_split (sargv); + return WEECHAT_RC_OK; } @@ -3483,8 +3502,9 @@ COMMAND_CALLBACK(item) if (ptr_bar_item_custom) { snprintf (str_command, sizeof (str_command), - "/item addreplace %s %s", + "/item addreplace %s \"%s\" \"%s\"", ptr_bar_item_custom->bar_item->name, + CONFIG_STRING(ptr_bar_item_custom->options[GUI_BAR_ITEM_CUSTOM_OPTION_CONDITIONS]), CONFIG_STRING(ptr_bar_item_custom->options[GUI_BAR_ITEM_CUSTOM_OPTION_CONTENT])); gui_buffer_set (buffer, "input", str_command); snprintf (str_pos, sizeof (str_pos), @@ -8021,7 +8041,7 @@ command_init () NULL, "item", N_("manage custom bar items"), N_("list" - " || add|addreplace <name> <content>" + " || add|addreplace <name> \"<conditions>\" \"<content>\"" " || rename <name> <new_name>" " || refresh <name> [<name>...]" " || recreate <name>" @@ -8030,6 +8050,8 @@ command_init () " add: add a custom bar item\n" "addreplace: add or replace an existing custom bar item\n" " name: custom bar item name\n" + "conditions: evaluated conditions to display the bar item " + "(for example to display the bar item only in specific buffers)\n" " content: content (evaluated, see /help eval)\n" " rename: rename a custom bar item\n" " refresh: update content of item in all bars where the item is " @@ -8041,9 +8063,10 @@ command_init () " -all: delete all custom bar items\n" "\n" "Examples:\n" - " /item add terminfo term:${info:term_width}x${info:term_height}\n" - " /item add bufinfo ${buffer.number}:${buffer.name}" - "${if:${buffer.zoomed}?(Z)}"), + " /item add terminfo \"${buffer.number} == 1\" " + "\"term:${info:term_width}x${info:term_height}\"\n" + " /item add bufinfo \"\" \"${buffer.number}:${buffer.name}" + "${if:${buffer.zoomed}?(Z)}\""), "list" " || add %(custom_bar_items_names)" " || addreplace %(custom_bar_items_names) %(custom_bar_item_content)" diff --git a/src/core/wee-config.c b/src/core/wee-config.c index dc8d74975..071cbfe91 100644 --- a/src/core/wee-config.c +++ b/src/core/wee-config.c @@ -1939,10 +1939,10 @@ config_weechat_bar_read_cb (const void *pointer, void *data, */ int -config_weechat_bar_item_read_cb (const void *pointer, void *data, - struct t_config_file *config_file, - struct t_config_section *section, - const char *option_name, const char *value) +config_weechat_custom_bar_item_read_cb (const void *pointer, void *data, + struct t_config_file *config_file, + struct t_config_section *section, + const char *option_name, const char *value) { char *pos_option, *item_name; struct t_gui_bar_item_custom *ptr_temp_item; @@ -1997,7 +1997,7 @@ config_weechat_bar_item_read_cb (const void *pointer, void *data, if (index_option >= 0) { gui_bar_item_custom_create_option_temp (ptr_temp_item, index_option, - value); + value); } else { @@ -4746,7 +4746,7 @@ config_weechat_init_options () ptr_section = config_file_new_section ( weechat_config_file, "custom_bar_item", 0, 0, - &config_weechat_bar_item_read_cb, NULL, NULL, + &config_weechat_custom_bar_item_read_cb, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, diff --git a/src/gui/gui-bar-item-custom.c b/src/gui/gui-bar-item-custom.c index c901515a3..599476acd 100644 --- a/src/gui/gui-bar-item-custom.c +++ b/src/gui/gui-bar-item-custom.c @@ -39,7 +39,10 @@ char *gui_bar_item_custom_option_string[GUI_BAR_ITEM_CUSTOM_NUM_OPTIONS] = -{ "content" }; +{ "conditions", "content" }; +char *gui_bar_item_custom_option_default[GUI_BAR_ITEM_CUSTOM_NUM_OPTIONS] = +{ "", "" }; + struct t_gui_bar_item_custom *gui_custom_bar_items = NULL; struct t_gui_bar_item_custom *last_gui_custom_bar_item = NULL; @@ -49,7 +52,6 @@ struct t_gui_bar_item_custom *gui_temp_custom_bar_items = NULL; struct t_gui_bar_item_custom *last_gui_temp_custom_bar_item = NULL; - /* * Checks if a custom bar item name is valid: it must not have any * space/period. @@ -163,12 +165,12 @@ gui_bar_item_custom_search_with_option_name (const char *option_name) } /* - * Callback called when option "content" is changed. + * Callback called when option "conditions" or "content" is changed. */ void -gui_bar_item_custom_config_change_content (const void *pointer, void *data, - struct t_config_option *option) +gui_bar_item_custom_config_change (const void *pointer, void *data, + struct t_config_option *option) { struct t_gui_bar_item_custom *ptr_item; @@ -208,6 +210,17 @@ gui_bar_item_custom_create_option (const char *item_name, int index_option, switch (index_option) { + case GUI_BAR_ITEM_CUSTOM_OPTION_CONDITIONS: + ptr_option = config_file_new_option ( + weechat_config_file, weechat_config_section_custom_bar_item, + option_name, "string", + N_("condition(s) to display the bar item " + "(evaluated, see /help eval)"), + NULL, 0, 0, value, NULL, 0, + NULL, NULL, NULL, + &gui_bar_item_custom_config_change, NULL, NULL, + NULL, NULL, NULL); + break; case GUI_BAR_ITEM_CUSTOM_OPTION_CONTENT: ptr_option = config_file_new_option ( weechat_config_file, weechat_config_section_custom_bar_item, @@ -215,7 +228,7 @@ gui_bar_item_custom_create_option (const char *item_name, int index_option, N_("content of bar item (evaluated, see /help eval)"), NULL, 0, 0, value, NULL, 0, NULL, NULL, NULL, - &gui_bar_item_custom_config_change_content, NULL, NULL, + &gui_bar_item_custom_config_change, NULL, NULL, NULL, NULL, NULL); break; case GUI_BAR_ITEM_CUSTOM_NUM_OPTIONS: @@ -263,14 +276,18 @@ gui_bar_item_custom_callback (const void *pointer, struct t_hashtable *extra_info) { struct t_gui_bar_item_custom *ptr_item; - struct t_hashtable *pointers; + struct t_hashtable *pointers, *options; + const char *ptr_conditions; char *result; + int rc; /* make C compiler happy */ (void) data; (void) item; (void) extra_info; + result = NULL; + ptr_item = (struct t_gui_bar_item_custom *)pointer; if (!ptr_item) return NULL; @@ -286,12 +303,41 @@ gui_bar_item_custom_callback (const void *pointer, hashtable_set (pointers, "buffer", buffer); } + options = hashtable_new (32, + WEECHAT_HASHTABLE_STRING, + WEECHAT_HASHTABLE_STRING, + NULL, + NULL); + if (options) + hashtable_set (options, "type", "condition"); + + /* check conditions */ + ptr_conditions = CONFIG_STRING(ptr_item->options[GUI_BAR_ITEM_CUSTOM_OPTION_CONDITIONS]); + if (ptr_conditions && ptr_conditions[0]) + { + result = eval_expression ( + CONFIG_STRING(ptr_item->options[GUI_BAR_ITEM_CUSTOM_OPTION_CONDITIONS]), + pointers, NULL, options); + rc = eval_is_true (result); + if (result) + { + free (result); + result = NULL; + } + if (!rc) + goto end; + } + + /* evaluate content */ result = eval_expression ( CONFIG_STRING(ptr_item->options[GUI_BAR_ITEM_CUSTOM_OPTION_CONTENT]), pointers, NULL, NULL); +end: if (pointers) hashtable_free (pointers); + if (options) + hashtable_free (options); return result; } @@ -349,6 +395,7 @@ gui_bar_item_custom_create_bar_item (struct t_gui_bar_item_custom *item) struct t_gui_bar_item_custom * gui_bar_item_custom_new_with_options (const char *name, + struct t_config_option *conditions, struct t_config_option *content) { struct t_gui_bar_item_custom *new_bar_item_custom; @@ -358,6 +405,7 @@ gui_bar_item_custom_new_with_options (const char *name, if (!new_bar_item_custom) return NULL; + new_bar_item_custom->options[GUI_BAR_ITEM_CUSTOM_OPTION_CONDITIONS] = conditions; new_bar_item_custom->options[GUI_BAR_ITEM_CUSTOM_OPTION_CONTENT] = content; new_bar_item_custom->bar_item = NULL; @@ -380,9 +428,10 @@ gui_bar_item_custom_new_with_options (const char *name, */ struct t_gui_bar_item_custom * -gui_bar_item_custom_new (const char *name, const char *content) +gui_bar_item_custom_new (const char *name, const char *conditions, + const char *content) { - struct t_config_option *option_content; + struct t_config_option *option_conditions, *option_content; struct t_gui_bar_item_custom *new_bar_item_custom; if (!gui_bar_item_custom_name_valid (name)) @@ -391,13 +440,19 @@ gui_bar_item_custom_new (const char *name, const char *content) if (gui_bar_item_custom_search (name)) return NULL; + option_conditions = gui_bar_item_custom_create_option ( + name, + GUI_BAR_ITEM_CUSTOM_OPTION_CONDITIONS, + conditions); option_content = gui_bar_item_custom_create_option ( name, GUI_BAR_ITEM_CUSTOM_OPTION_CONTENT, content); - new_bar_item_custom = gui_bar_item_custom_new_with_options (name, - option_content); + new_bar_item_custom = gui_bar_item_custom_new_with_options ( + name, + option_conditions, + option_content); if (new_bar_item_custom) { gui_bar_item_custom_create_bar_item (new_bar_item_custom); @@ -405,6 +460,8 @@ gui_bar_item_custom_new (const char *name, const char *content) } else { + if (option_conditions) + config_file_option_free (option_conditions, 0); if (option_content) config_file_option_free (option_content, 0); } @@ -420,10 +477,21 @@ void gui_bar_item_custom_use_temp_items () { struct t_gui_bar_item_custom *ptr_temp_item; + int i; for (ptr_temp_item = gui_temp_custom_bar_items; ptr_temp_item; ptr_temp_item = ptr_temp_item->next_item) { + for (i = 0; i < GUI_BAR_ITEM_CUSTOM_NUM_OPTIONS; i++) + { + if (!ptr_temp_item->options[i]) + { + ptr_temp_item->options[i] = gui_bar_item_custom_create_option ( + ptr_temp_item->name, + i, + gui_bar_item_custom_option_default[i]); + } + } gui_bar_item_custom_create_bar_item (ptr_temp_item); } diff --git a/src/gui/gui-bar-item-custom.h b/src/gui/gui-bar-item-custom.h index efb8b70f3..b08d306c5 100644 --- a/src/gui/gui-bar-item-custom.h +++ b/src/gui/gui-bar-item-custom.h @@ -27,7 +27,8 @@ struct t_weechat_plugin; enum t_gui_bar_item_custom_option { - GUI_BAR_ITEM_CUSTOM_OPTION_CONTENT = 0, /* item content */ + GUI_BAR_ITEM_CUSTOM_OPTION_CONDITIONS = 0, /* condition(s) to display */ + GUI_BAR_ITEM_CUSTOM_OPTION_CONTENT, /* item content */ /* number of bar options */ GUI_BAR_ITEM_CUSTOM_NUM_OPTIONS, }; @@ -60,6 +61,7 @@ extern void gui_bar_item_custom_create_option_temp (struct t_gui_bar_item_custom const char *value); extern struct t_gui_bar_item_custom *gui_bar_item_custom_alloc (const char *name); extern struct t_gui_bar_item_custom *gui_bar_item_custom_new (const char *name, + const char *conditions, const char *content); extern void gui_bar_item_custom_use_temp_items (); extern int gui_bar_item_custom_rename (struct t_gui_bar_item_custom *item, |