From 7b9b25988c8971fc22848142c005b6cbb8ad3ce5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Helleu?= Date: Fri, 26 May 2017 14:03:35 +0200 Subject: [PATCH] fset: add support of parent options, add color for quotes around strings, fix auto size of columns --- src/plugins/fset/fset-buffer.c | 135 +++++++++++++++++++++++---------- src/plugins/fset/fset-config.c | 36 ++++++++- src/plugins/fset/fset-config.h | 2 + src/plugins/fset/fset-option.c | 115 ++++++++++++++++++++++++---- src/plugins/fset/fset-option.h | 10 +-- 5 files changed, 238 insertions(+), 60 deletions(-) diff --git a/src/plugins/fset/fset-buffer.c b/src/plugins/fset/fset-buffer.c index b98434dff..c2a1dd96a 100644 --- a/src/plugins/fset/fset-buffer.c +++ b/src/plugins/fset/fset-buffer.c @@ -39,6 +39,35 @@ char *fset_buffer_columns[] = { "name", "parent_name", "type", "default_value", int fset_buffer_columns_default_size[] = { 64, 64, 8, 16, 16 }; +/* + * Fills a field with spaces (according to max length in hashtable + * "fset_option_max_length_field" for this field. + */ + +void +fset_buffer_fills_field (char *field, int size, + const char *field_name, int default_max_length) +{ + int length, length_screen, *ptr_length, num_spaces; + + length = strlen (field); + length_screen = weechat_strlen_screen (field); + + ptr_length = (int *)weechat_hashtable_get (fset_option_max_length_field, + field_name); + if (!ptr_length) + ptr_length = &default_max_length; + + num_spaces = *ptr_length - length_screen; + if (num_spaces <= 0) + return; + if (length + num_spaces >= size) + num_spaces = size - length - 1; + + memset (field + length, ' ', num_spaces); + field[length + num_spaces] = '\0'; +} + /* * Displays a line with an fset option. */ @@ -46,13 +75,15 @@ int fset_buffer_columns_default_size[] = { 64, 64, 8, 16, 16 }; void fset_buffer_display_line (int y, struct t_fset_option *fset_option) { - char *line, str_format[32], str_value[1024]; - const char *ptr_value; - int i, selected_line, *ptr_length, value_undef, value_diff; + char *line, str_field[4096]; + const char *ptr_field; + int selected_line; + int default_value_undef, value_undef, value_diff, add_quotes; struct t_config_option *ptr_option_color_value; selected_line = (y == fset_buffer_selected_line) ? 1 : 0; + default_value_undef = (fset_option->default_value == NULL) ? 1 : 0; value_undef = (fset_option->value == NULL) ? 1 : 0; value_diff = (fset_option_value_different_from_default (fset_option)) ? 1 : 0; @@ -60,52 +91,80 @@ fset_buffer_display_line (int y, struct t_fset_option *fset_option) weechat_hashtable_set (fset_buffer_hashtable_pointers, "fset_option", fset_option); - /* set column variables */ - for (i = 0; fset_buffer_columns[i]; i++) - { - ptr_length = (int *)weechat_hashtable_get (fset_option_max_length_field, - fset_buffer_columns[i]); - snprintf (str_format, sizeof (str_format), - "%%-%ds", - (ptr_length) ? *ptr_length : fset_buffer_columns_default_size[i]); - ptr_value = weechat_hdata_string (fset_hdata_fset_option, - fset_option, - fset_buffer_columns[i]); - snprintf (str_value, sizeof (str_value), - str_format, - (ptr_value) ? ptr_value : "null"); - weechat_hashtable_set (fset_buffer_hashtable_extra_vars, - fset_buffer_columns[i], - str_value); - } + /* name */ + ptr_field = weechat_hdata_string (fset_hdata_fset_option, + fset_option, "name"); + snprintf (str_field, sizeof (str_field), + "%s%s", + weechat_color (weechat_config_string (fset_config_color_name[selected_line])), + ptr_field); + fset_buffer_fills_field (str_field, sizeof (str_field), "name", 64); + weechat_hashtable_set (fset_buffer_hashtable_extra_vars, + "name", str_field); - /* set colors */ - snprintf (str_value, sizeof (str_value), - "%s", - weechat_color (weechat_config_string (fset_config_color_name[selected_line]))); + /* parent_name */ + ptr_field = weechat_hdata_string (fset_hdata_fset_option, + fset_option, "parent_name"); + snprintf (str_field, sizeof (str_field), + "%s%s", + weechat_color (weechat_config_string (fset_config_color_parent_name[selected_line])), + ptr_field); + fset_buffer_fills_field (str_field, sizeof (str_field), "parent_name", 64); weechat_hashtable_set (fset_buffer_hashtable_extra_vars, - "color_name", str_value); - snprintf (str_value, sizeof (str_value), - "%s", - weechat_color (weechat_config_string (fset_config_color_type[selected_line]))); + "parent_name", str_field); + + /* type */ + ptr_field = weechat_hdata_string (fset_hdata_fset_option, + fset_option, "type"); + snprintf (str_field, sizeof (str_field), + "%s%s", + weechat_color (weechat_config_string (fset_config_color_type[selected_line])), + ptr_field); + fset_buffer_fills_field (str_field, sizeof (str_field), "type", 8); weechat_hashtable_set (fset_buffer_hashtable_extra_vars, - "color_type", str_value); - snprintf (str_value, sizeof (str_value), - "%s", - weechat_color (weechat_config_string (fset_config_color_default_value[selected_line]))); + "type", str_field); + + /* default_value */ + ptr_field = weechat_hdata_string (fset_hdata_fset_option, + fset_option, "default_value"); + add_quotes = (ptr_field && (strcmp (fset_option->type, "string") == 0)) ? 1 : 0; + if (default_value_undef) + ptr_option_color_value = fset_config_color_value_undef[selected_line]; + else + ptr_option_color_value = fset_config_color_value[selected_line]; + snprintf (str_field, sizeof (str_field), + "%s%s%s%s%s%s", + (add_quotes) ? weechat_color (weechat_config_string (fset_config_color_quotes[selected_line])) : "", + (add_quotes) ? "\"" : "", + weechat_color (weechat_config_string (ptr_option_color_value)), + (ptr_field) ? ptr_field : FSET_OPTION_VALUE_NULL, + (add_quotes) ? weechat_color (weechat_config_string (fset_config_color_quotes[selected_line])) : "", + (add_quotes) ? "\"" : ""); + fset_buffer_fills_field (str_field, sizeof (str_field), "default_value", 16); weechat_hashtable_set (fset_buffer_hashtable_extra_vars, - "color_default_value", str_value); + "default_value", str_field); + + /* value */ + ptr_field = weechat_hdata_string (fset_hdata_fset_option, + fset_option, "value"); + add_quotes = (ptr_field && (strcmp (fset_option->type, "string") == 0)) ? 1 : 0; if (value_undef) ptr_option_color_value = fset_config_color_value_undef[selected_line]; else if (value_diff) ptr_option_color_value = fset_config_color_value_diff[selected_line]; else ptr_option_color_value = fset_config_color_value[selected_line]; - snprintf (str_value, sizeof (str_value), - "%s", - weechat_color (weechat_config_string (ptr_option_color_value))); + snprintf (str_field, sizeof (str_field), + "%s%s%s%s%s%s", + (add_quotes) ? weechat_color (weechat_config_string (fset_config_color_quotes[selected_line])) : "", + (add_quotes) ? "\"" : "", + weechat_color (weechat_config_string (ptr_option_color_value)), + (ptr_field) ? ptr_field : FSET_OPTION_VALUE_NULL, + (add_quotes) ? weechat_color (weechat_config_string (fset_config_color_quotes[selected_line])) : "", + (add_quotes) ? "\"" : ""); + fset_buffer_fills_field (str_field, sizeof (str_field), "value", 16); weechat_hashtable_set (fset_buffer_hashtable_extra_vars, - "color_value", str_value); + "value", str_field); /* set other variables depending on the value */ weechat_hashtable_set (fset_buffer_hashtable_extra_vars, diff --git a/src/plugins/fset/fset-config.c b/src/plugins/fset/fset-config.c index ced2973ec..a2a6fddd9 100644 --- a/src/plugins/fset/fset-config.c +++ b/src/plugins/fset/fset-config.c @@ -44,6 +44,8 @@ struct t_config_option *fset_config_format_option_current; struct t_config_option *fset_config_color_default_value[2]; struct t_config_option *fset_config_color_name[2]; +struct t_config_option *fset_config_color_parent_name[2]; +struct t_config_option *fset_config_color_quotes[2]; struct t_config_option *fset_config_color_type[2]; struct t_config_option *fset_config_color_value[2]; struct t_config_option *fset_config_color_value_diff[2]; @@ -179,7 +181,7 @@ fset_config_init () N_("format of each line with an option " "(note: content is evaluated, see /help fset)"), NULL, 0, 0, - " ${color_name}${name} ${color_type}${type} ${color_value}${value}", + " ${name} ${type} ${value}", NULL, 0, NULL, NULL, NULL, &fset_config_change_format, NULL, NULL, @@ -240,6 +242,38 @@ fset_config_init () NULL, NULL, NULL, &fset_config_change_color, NULL, NULL, NULL, NULL, NULL); + fset_config_color_parent_name[0] = weechat_config_new_option ( + fset_config_file, ptr_section, + "parent_name", "color", + N_("color for parent name"), + NULL, 0, 0, "default", NULL, 0, + NULL, NULL, NULL, + &fset_config_change_color, NULL, NULL, + NULL, NULL, NULL); + fset_config_color_parent_name[1] = weechat_config_new_option ( + fset_config_file, ptr_section, + "parent_name_selected", "color", + N_("color for parent name on the selected line"), + NULL, 0, 0, "white", NULL, 0, + NULL, NULL, NULL, + &fset_config_change_color, NULL, NULL, + NULL, NULL, NULL); + fset_config_color_quotes[0] = weechat_config_new_option ( + fset_config_file, ptr_section, + "quote", "color", + N_("color for quotes around string values"), + NULL, 0, 0, "darkgray", NULL, 0, + NULL, NULL, NULL, + &fset_config_change_color, NULL, NULL, + NULL, NULL, NULL); + fset_config_color_quotes[1] = weechat_config_new_option ( + fset_config_file, ptr_section, + "quotes_selected", "color", + N_("color for quotes around string values on the selected line"), + NULL, 0, 0, "default", NULL, 0, + NULL, NULL, NULL, + &fset_config_change_color, NULL, NULL, + NULL, NULL, NULL); fset_config_color_type[0] = weechat_config_new_option ( fset_config_file, ptr_section, "type", "color", diff --git a/src/plugins/fset/fset-config.h b/src/plugins/fset/fset-config.h index e5bec0eb5..6194c1f06 100644 --- a/src/plugins/fset/fset-config.h +++ b/src/plugins/fset/fset-config.h @@ -32,6 +32,8 @@ extern struct t_config_option *fset_config_format_option_current; extern struct t_config_option *fset_config_color_default_value[2]; extern struct t_config_option *fset_config_color_name[2]; +extern struct t_config_option *fset_config_color_parent_name[2]; +extern struct t_config_option *fset_config_color_quotes[2]; extern struct t_config_option *fset_config_color_type[2]; extern struct t_config_option *fset_config_color_value[2]; extern struct t_config_option *fset_config_color_value_diff[2]; diff --git a/src/plugins/fset/fset-option.c b/src/plugins/fset/fset-option.c index fb4bda19f..16c9ce50e 100644 --- a/src/plugins/fset/fset-option.c +++ b/src/plugins/fset/fset-option.c @@ -134,7 +134,6 @@ fset_option_set_value_string (struct t_config_option *option, { char str_value[64]; void *ptr_string_values; - int length; if (!value) { @@ -161,10 +160,8 @@ fset_option_set_value_string (struct t_config_option *option, } else if (strcmp (type, "string") == 0) { - length = 1 + strlen ((const char *)value) + 1 + 1; - *value_string = malloc (length); - if (*value_string) - snprintf (*value_string, length, "\"%s\"", (const char *)value); + *value_string = strdup ( + (default_value) ? weechat_config_string_default (option) : weechat_config_string (option)); } else if (strcmp (type, "color") == 0) { @@ -207,22 +204,32 @@ fset_option_set_values (struct t_fset_option *fset_option, { const char *ptr_parent_name, *ptr_type; void *ptr_default_value, *ptr_value; + struct t_config_option *ptr_parent_option; /* parent name */ if (fset_option->parent_name) + { free (fset_option->parent_name); + fset_option->parent_name = NULL; + } ptr_parent_name = weechat_config_option_get_string (option, "parent"); fset_option->parent_name = (ptr_parent_name) ? strdup (ptr_parent_name) : NULL; /* type */ if (fset_option->type) + { free (fset_option->type); + fset_option->type = NULL; + } ptr_type = weechat_config_option_get_string (option, "type"); fset_option->type = strdup ((ptr_type) ? ptr_type : ""); /* default value */ if (fset_option->default_value) + { free (fset_option->default_value); + fset_option->default_value = NULL; + } ptr_default_value = weechat_config_option_get_pointer (option, "default_value"); fset_option_set_value_string (option, @@ -233,13 +240,37 @@ fset_option_set_values (struct t_fset_option *fset_option, /* value */ if (fset_option->value) + { free (fset_option->value); + fset_option->value = NULL; + } ptr_value = weechat_config_option_get_pointer (option, "value"); fset_option_set_value_string (option, fset_option->type, ptr_value, 0, &fset_option->value); + + /* parent_value */ + if (fset_option->parent_value) + { + free (fset_option->parent_value); + fset_option->parent_value = NULL; + } + if (ptr_parent_name) + { + ptr_parent_option = weechat_config_get (ptr_parent_name); + if (ptr_parent_option) + { + ptr_value = weechat_config_option_get_pointer (ptr_parent_option, + "value"); + fset_option_set_value_string (option, + fset_option->type, + ptr_value, + 0, + &fset_option->parent_value); + } + } } /* @@ -263,19 +294,58 @@ fset_option_set_max_length_field (const char *field, int length) void fset_option_set_max_length_fields_option (struct t_fset_option *fset_option) { + int length; + + /* name */ fset_option_set_max_length_field ("name", strlen (fset_option->name)); + + /* parent_name */ fset_option_set_max_length_field ( "parent_name", (fset_option->parent_name) ? strlen (fset_option->parent_name) : 0); + + /* type */ fset_option_set_max_length_field ("type", strlen (fset_option->type)); - fset_option_set_max_length_field ( - "default_value", - strlen ((fset_option->default_value) ? - fset_option->default_value : "null")); - fset_option_set_max_length_field ( - "value", - strlen ((fset_option->value) ? - fset_option->value : "null")); + + /* default_value */ + if (fset_option->default_value) + { + length = strlen (fset_option->default_value); + if (strcmp (fset_option->type, "string") == 0) + length += 2; + } + else + { + length = strlen (FSET_OPTION_VALUE_NULL); + } + fset_option_set_max_length_field ("default_value", length); + + /* value */ + if (fset_option->value) + { + length = strlen (fset_option->value); + if (strcmp (fset_option->type, "string") == 0) + length += 2; + } + else + { + length = strlen (FSET_OPTION_VALUE_NULL); + } + fset_option_set_max_length_field ("value", length); + + /* parent_value */ + if (fset_option->parent_value) + { + length = strlen (fset_option->parent_value); + if (strcmp (fset_option->type, "string") == 0) + length += 2; + } + else + { + length = strlen (FSET_OPTION_VALUE_NULL); + } + fset_option_set_max_length_field ("parent_value", length); + } /* @@ -352,6 +422,7 @@ fset_option_alloc (struct t_config_file *config_file, new_fset_option->type = NULL; new_fset_option->default_value = NULL; new_fset_option->value = NULL; + new_fset_option->parent_value = NULL; fset_option_set_values (new_fset_option, option); fset_option_set_max_length_fields_option (new_fset_option); } @@ -468,6 +539,8 @@ fset_option_free_cb (void *data, struct t_arraylist *arraylist, void *pointer) free (fset_option->default_value); if (fset_option->value) free (fset_option->value); + if (fset_option->parent_value) + free (fset_option->parent_value); free (fset_option); } @@ -622,9 +695,11 @@ fset_option_hdata_option_cb (const void *pointer, void *data, if (hdata) { WEECHAT_HDATA_VAR(struct t_fset_option, name, STRING, 0, NULL, NULL); + WEECHAT_HDATA_VAR(struct t_fset_option, parent_name, STRING, 0, NULL, NULL); WEECHAT_HDATA_VAR(struct t_fset_option, type, STRING, 0, NULL, NULL); WEECHAT_HDATA_VAR(struct t_fset_option, default_value, STRING, 0, NULL, NULL); WEECHAT_HDATA_VAR(struct t_fset_option, value, STRING, 0, NULL, NULL); + WEECHAT_HDATA_VAR(struct t_fset_option, parent_value, STRING, 0, NULL, NULL); } return hdata; } @@ -660,6 +735,8 @@ fset_option_add_to_infolist (struct t_infolist *infolist, return 0; if (!weechat_infolist_new_var_string (ptr_item, "value", fset_option->value)) return 0; + if (!weechat_infolist_new_var_string (ptr_item, "parent_value", fset_option->parent_value)) + return 0; return 1; } @@ -685,6 +762,7 @@ fset_option_print_log () weechat_log_printf (" type. . . . . . . . . : '%s'", ptr_fset_option->type); weechat_log_printf (" default_value . . . . : '%s'", ptr_fset_option->default_value); weechat_log_printf (" value . . . . . . . . : '%s'", ptr_fset_option->value); + weechat_log_printf (" parent_value. . . . . : '%s'", ptr_fset_option->parent_value); } } @@ -699,6 +777,12 @@ fset_option_print_log () int fset_option_init () { + fset_option_max_length_field = weechat_hashtable_new ( + 32, + WEECHAT_HASHTABLE_STRING, + WEECHAT_HASHTABLE_INTEGER, + NULL, NULL); + fset_options = weechat_arraylist_new (100, 1, 0, &fset_option_compare_options_cb, NULL, &fset_option_free_cb, NULL); @@ -718,4 +802,9 @@ fset_option_end () weechat_arraylist_free (fset_options); fset_options = NULL; } + if (fset_option_max_length_field) + { + weechat_hashtable_free (fset_option_max_length_field); + fset_option_max_length_field = NULL; + } } diff --git a/src/plugins/fset/fset-option.h b/src/plugins/fset/fset-option.h index e4381777e..93d581470 100644 --- a/src/plugins/fset/fset-option.h +++ b/src/plugins/fset/fset-option.h @@ -20,14 +20,7 @@ #ifndef WEECHAT_FSET_OPTION_H #define WEECHAT_FSET_OPTION_H 1 -#include - -/* status for fset */ -#define FSET_STATUS_INSTALLED 1 -#define FSET_STATUS_AUTOLOADED 2 -#define FSET_STATUS_HELD 4 -#define FSET_STATUS_RUNNING 8 -#define FSET_STATUS_NEW_VERSION 16 +#define FSET_OPTION_VALUE_NULL "null" struct t_fset_option { @@ -36,6 +29,7 @@ struct t_fset_option char *type; /* option type */ char *default_value; /* option default value */ char *value; /* option value */ + char *parent_value; /* parent option value */ struct t_fset_option *prev_option; /* link to previous option */ struct t_fset_option *next_option; /* link to next option */ };