1
0
mirror of https://github.com/weechat/weechat.git synced 2026-07-03 16:23:14 +02:00

fset: store type as enum, translate ${type}, add ${type_en}, ${type_short} and ${type_tiny}

This commit is contained in:
Sébastien Helleu
2017-05-31 22:21:16 +02:00
parent f555d588b8
commit 3211a73599
5 changed files with 146 additions and 70 deletions
+54 -9
View File
@@ -104,7 +104,7 @@ fset_buffer_display_line (int y, struct t_fset_option *fset_option)
const char *ptr_field, *ptr_parent_value;
int selected_line;
int default_value_undef, value_undef, value_changed;
int add_quotes, add_quotes_parent;
int type, add_quotes, add_quotes_parent;
struct t_config_option *ptr_option_color_value;
selected_line = (y == fset_buffer_selected_line) ? 1 : 0;
@@ -150,25 +150,70 @@ fset_buffer_display_line (int y, struct t_fset_option *fset_option)
"parent_name", str_field);
/* type */
ptr_field = weechat_hdata_string (fset_hdata_fset_option,
fset_option, "type");
snprintf (str_field, sizeof (str_field), "%s", ptr_field);
type = weechat_hdata_integer (fset_hdata_fset_option, fset_option, "type");
snprintf (str_field, sizeof (str_field),
"%s", _(fset_option_type_string[type]));
weechat_hashtable_set (fset_buffer_hashtable_extra_vars,
"__type", str_field);
snprintf (str_field, sizeof (str_field),
"%s%s",
weechat_color (weechat_config_string (fset_config_color_type[selected_line])),
(ptr_field) ? ptr_field : "");
_(fset_option_type_string[type]));
weechat_hashtable_set (fset_buffer_hashtable_extra_vars,
"_type", str_field);
fset_buffer_fills_field (str_field, sizeof (str_field), "type", 8);
weechat_hashtable_set (fset_buffer_hashtable_extra_vars,
"type", str_field);
/* type_en */
snprintf (str_field, sizeof (str_field),
"%s", fset_option_type_string[type]);
weechat_hashtable_set (fset_buffer_hashtable_extra_vars,
"__type_en", str_field);
snprintf (str_field, sizeof (str_field),
"%s%s",
weechat_color (weechat_config_string (fset_config_color_type[selected_line])),
fset_option_type_string[type]);
weechat_hashtable_set (fset_buffer_hashtable_extra_vars,
"_type_en", str_field);
fset_buffer_fills_field (str_field, sizeof (str_field), "type_en", 8);
weechat_hashtable_set (fset_buffer_hashtable_extra_vars,
"type_en", str_field);
/* type_short */
snprintf (str_field, sizeof (str_field),
"%s", fset_option_type_string_short[type]);
weechat_hashtable_set (fset_buffer_hashtable_extra_vars,
"__type_short", str_field);
snprintf (str_field, sizeof (str_field),
"%s%s",
weechat_color (weechat_config_string (fset_config_color_type[selected_line])),
fset_option_type_string_short[type]);
weechat_hashtable_set (fset_buffer_hashtable_extra_vars,
"_type_short", str_field);
fset_buffer_fills_field (str_field, sizeof (str_field), "type_short", 4);
weechat_hashtable_set (fset_buffer_hashtable_extra_vars,
"type_short", str_field);
/* type_tiny */
snprintf (str_field, sizeof (str_field),
"%s", fset_option_type_string_tiny[type]);
weechat_hashtable_set (fset_buffer_hashtable_extra_vars,
"__type_tiny", str_field);
snprintf (str_field, sizeof (str_field),
"%s%s",
weechat_color (weechat_config_string (fset_config_color_type[selected_line])),
fset_option_type_string_tiny[type]);
weechat_hashtable_set (fset_buffer_hashtable_extra_vars,
"_type_tiny", str_field);
fset_buffer_fills_field (str_field, sizeof (str_field), "type_tiny", 1);
weechat_hashtable_set (fset_buffer_hashtable_extra_vars,
"type_tiny", 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;
add_quotes = (ptr_field && (fset_option->type == FSET_OPTION_TYPE_STRING)) ? 1 : 0;
if (default_value_undef)
ptr_option_color_value = fset_config_color_value_undef[selected_line];
else
@@ -194,7 +239,7 @@ fset_buffer_display_line (int y, struct t_fset_option *fset_option)
/* 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;
add_quotes = (ptr_field && (fset_option->type == FSET_OPTION_TYPE_STRING)) ? 1 : 0;
if (value_undef)
ptr_option_color_value = fset_config_color_value_undef[selected_line];
else if (value_changed)
@@ -224,7 +269,7 @@ fset_buffer_display_line (int y, struct t_fset_option *fset_option)
fset_option, "parent_value");
if (value_undef && ptr_parent_value)
{
add_quotes_parent = (ptr_parent_value && (strcmp (fset_option->type, "string") == 0)) ? 1 : 0;
add_quotes_parent = (ptr_parent_value && (fset_option->type == FSET_OPTION_TYPE_STRING)) ? 1 : 0;
snprintf (str_field, sizeof (str_field),
"%s -> %s",
(ptr_field) ? ptr_field : FSET_OPTION_VALUE_NULL,
@@ -277,7 +322,7 @@ fset_buffer_display_line (int y, struct t_fset_option *fset_option)
/* parent_value (set only if value is NULL and inherited from parent) */
if (value_undef && ptr_parent_value)
{
add_quotes_parent = (ptr_parent_value && (strcmp (fset_option->type, "string") == 0)) ? 1 : 0;
add_quotes_parent = (ptr_parent_value && (fset_option->type == FSET_OPTION_TYPE_STRING)) ? 1 : 0;
snprintf (str_field, sizeof (str_field),
"%s",
(ptr_parent_value) ? ptr_parent_value : FSET_OPTION_VALUE_NULL);
+4 -4
View File
@@ -164,15 +164,15 @@ fset_command_fset (const void *pointer, void *data,
if (weechat_strcasecmp (argv[1], "-toggle") == 0)
{
if (strcmp (ptr_fset_option->type, "boolean") == 0)
if (ptr_fset_option->type == FSET_OPTION_TYPE_BOOLEAN)
weechat_config_option_set (ptr_option, "toggle", 1);
return WEECHAT_RC_OK;
}
if (weechat_strcasecmp (argv[1], "-add") == 0)
{
if ((strcmp (ptr_fset_option->type, "integer") == 0)
|| (strcmp (ptr_fset_option->type, "color") == 0))
if ((ptr_fset_option->type == FSET_OPTION_TYPE_INTEGER)
|| (ptr_fset_option->type == FSET_OPTION_TYPE_COLOR))
{
value = 1;
if (argc > 2)
@@ -212,7 +212,7 @@ fset_command_fset (const void *pointer, void *data,
append = (weechat_strcasecmp (argv[1], "-append") == 0) ? 1 : 0;
use_mute = weechat_config_boolean (fset_config_look_use_mute);
add_quotes = (ptr_fset_option->value
&& strcmp (ptr_fset_option->type, "string") == 0) ? 1 : 0;
&& (ptr_fset_option->type == FSET_OPTION_TYPE_STRING)) ? 1 : 0;
snprintf (str_input, sizeof (str_input),
"%s/set %s %s%s%s",
(use_mute) ? "/mute " : "",
+6 -6
View File
@@ -84,7 +84,7 @@ fset_focus_cb (const void *pointer, void *data, struct t_hashtable *info)
weechat_hashtable_set (info, "fset_option", str_value);
weechat_hashtable_set (info, "fset_option_name", ptr_fset_option->name);
weechat_hashtable_set (info, "fset_option_parent_name", ptr_fset_option->parent_name);
weechat_hashtable_set (info, "fset_option_type", ptr_fset_option->type);
weechat_hashtable_set (info, "fset_option_type", fset_option_type_string[ptr_fset_option->type]);
weechat_hashtable_set (info, "fset_option_default_value", ptr_fset_option->default_value);
weechat_hashtable_set (info, "fset_option_value", ptr_fset_option->value);
weechat_hashtable_set (info, "fset_option_parent_value", ptr_fset_option->parent_value);
@@ -173,14 +173,14 @@ fset_hsignal_cb (const void *pointer, void *data, const char *signal,
{
snprintf (str_command, sizeof (str_command),
"/fset %s",
(strcmp (ptr_fset_option->type, "boolean") == 0) ? "-toggle" : "-set");
(ptr_fset_option->type == FSET_OPTION_TYPE_BOOLEAN) ? "-toggle" : "-set");
weechat_command (fset_buffer, str_command);
}
else if (weechat_string_match (ptr_key, "button2-gesture-left*", 1))
{
distance = fset_mouse_get_distance_x (hashtable);
if ((strcmp (ptr_fset_option->type, "integer") == 0)
|| (strcmp (ptr_fset_option->type, "color") == 0))
if ((ptr_fset_option->type == FSET_OPTION_TYPE_INTEGER)
|| (ptr_fset_option->type == FSET_OPTION_TYPE_COLOR))
{
snprintf (str_command, sizeof (str_command),
"/fset -add -%d",
@@ -191,8 +191,8 @@ fset_hsignal_cb (const void *pointer, void *data, const char *signal,
else if (weechat_string_match (ptr_key, "button2-gesture-right*", 1))
{
distance = fset_mouse_get_distance_x (hashtable);
if ((strcmp (ptr_fset_option->type, "integer") == 0)
|| (strcmp (ptr_fset_option->type, "color") == 0))
if ((ptr_fset_option->type == FSET_OPTION_TYPE_INTEGER)
|| (ptr_fset_option->type == FSET_OPTION_TYPE_COLOR))
{
snprintf (str_command, sizeof (str_command),
"/fset -add %d",
+68 -50
View File
@@ -34,6 +34,13 @@ struct t_arraylist *fset_options = NULL;
struct t_hashtable *fset_option_max_length_field = NULL;
char *fset_option_filter = NULL;
char *fset_option_type_string[FSET_OPTION_NUM_TYPES] =
{ N_("boolean"), N_("integer"), N_("string"), N_("color") };
char *fset_option_type_string_short[FSET_OPTION_NUM_TYPES] =
{ "bool", "int", "str", "col" };
char *fset_option_type_string_tiny[FSET_OPTION_NUM_TYPES] =
{ "b", "i", "s", "c" };
/*
* Checks if a fset option pointer is valid.
@@ -128,7 +135,7 @@ fset_option_value_is_changed (struct t_fset_option *fset_option)
void
fset_option_set_value_string (struct t_config_option *option,
const char *type, void *value,
enum t_fset_option_type type, void *value,
int default_value,
char **value_string)
{
@@ -139,38 +146,38 @@ fset_option_set_value_string (struct t_config_option *option,
{
*value_string = NULL;
}
else if (strcmp (type, "boolean") == 0)
{
*value_string = strdup (*((int *)value) ? "on" : "off");
}
else if (strcmp (type, "integer") == 0)
{
ptr_string_values = weechat_config_option_get_pointer (
option, "string_values");
if (ptr_string_values)
{
*value_string = strdup (
(default_value) ? weechat_config_string_default (option) : weechat_config_string (option));
}
else
{
snprintf (str_value, sizeof (str_value), "%d", *((int *)value));
*value_string = strdup (str_value);
}
}
else if (strcmp (type, "string") == 0)
{
*value_string = strdup (
(default_value) ? weechat_config_string_default (option) : weechat_config_string (option));
}
else if (strcmp (type, "color") == 0)
{
*value_string = strdup (
(default_value) ? weechat_config_color_default (option) : weechat_config_color (option));
}
else
{
*value_string = strdup ("");
switch (type)
{
case FSET_OPTION_TYPE_BOOLEAN:
*value_string = strdup (*((int *)value) ? "on" : "off");
break;
case FSET_OPTION_TYPE_INTEGER:
ptr_string_values = weechat_config_option_get_pointer (
option, "string_values");
if (ptr_string_values)
{
*value_string = strdup (
(default_value) ? weechat_config_string_default (option) : weechat_config_string (option));
}
else
{
snprintf (str_value, sizeof (str_value), "%d", *((int *)value));
*value_string = strdup (str_value);
}
break;
case FSET_OPTION_TYPE_STRING:
*value_string = strdup (
(default_value) ? weechat_config_string_default (option) : weechat_config_string (option));
break;
case FSET_OPTION_TYPE_COLOR:
*value_string = strdup (
(default_value) ? weechat_config_color_default (option) : weechat_config_color (option));
break;
case FSET_OPTION_NUM_TYPES:
break;
}
}
}
@@ -287,11 +294,11 @@ void
fset_option_set_values (struct t_fset_option *fset_option,
struct t_config_option *option)
{
const char *ptr_parent_name, *ptr_type, *ptr_description;
const char *ptr_parent_name, *ptr_description;
const char **ptr_string_values;
void *ptr_default_value, *ptr_value;
struct t_config_option *ptr_parent_option;
int *ptr_min, *ptr_max;
int *ptr_type, *ptr_min, *ptr_max;
char str_value[64];
/* parent name */
@@ -304,13 +311,8 @@ fset_option_set_values (struct t_fset_option *fset_option,
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 : "");
ptr_type = weechat_config_option_get_pointer (option, "type");
fset_option->type = *ptr_type;
/* default value */
if (fset_option->default_value)
@@ -442,13 +444,29 @@ fset_option_set_max_length_fields_option (struct t_fset_option *fset_option)
/* type */
fset_option_set_max_length_field (
"type", weechat_strlen_screen (fset_option->type));
"type",
weechat_strlen_screen (_(fset_option_type_string[fset_option->type])));
/* type_en */
fset_option_set_max_length_field (
"type_en",
weechat_strlen_screen (fset_option_type_string[fset_option->type]));
/* type_short */
fset_option_set_max_length_field (
"type_short",
weechat_strlen_screen (fset_option_type_string_short[fset_option->type]));
/* type_tiny */
fset_option_set_max_length_field (
"type_tiny",
weechat_strlen_screen (fset_option_type_string_tiny[fset_option->type]));
/* default_value */
if (fset_option->default_value)
{
length = weechat_strlen_screen (fset_option->default_value);
if (strcmp (fset_option->type, "string") == 0)
if (fset_option->type == FSET_OPTION_TYPE_STRING)
length += 2;
}
else
@@ -461,7 +479,7 @@ fset_option_set_max_length_fields_option (struct t_fset_option *fset_option)
if (fset_option->value)
{
length_value = weechat_strlen_screen (fset_option->value);
if (strcmp (fset_option->type, "string") == 0)
if (fset_option->type == FSET_OPTION_TYPE_STRING)
length_value += 2;
}
else
@@ -474,7 +492,7 @@ fset_option_set_max_length_fields_option (struct t_fset_option *fset_option)
if (fset_option->parent_value)
{
length_parent_value = weechat_strlen_screen (fset_option->parent_value);
if (strcmp (fset_option->type, "string") == 0)
if (fset_option->type == FSET_OPTION_TYPE_STRING)
length_parent_value += 2;
}
else
@@ -571,7 +589,7 @@ fset_option_alloc (struct t_config_file *config_file,
{
new_fset_option->name = option_name;
new_fset_option->parent_name = NULL;
new_fset_option->type = NULL;
new_fset_option->type = 0;
new_fset_option->default_value = NULL;
new_fset_option->value = NULL;
new_fset_option->parent_value = NULL;
@@ -691,8 +709,6 @@ fset_option_free (struct t_fset_option *fset_option)
free (fset_option->name);
if (fset_option->parent_name)
free (fset_option->parent_name);
if (fset_option->type)
free (fset_option->type);
if (fset_option->default_value)
free (fset_option->default_value);
if (fset_option->value)
@@ -931,7 +947,7 @@ fset_option_hdata_option_cb (const void *pointer, void *data,
{
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, type, INTEGER, 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);
@@ -968,7 +984,7 @@ fset_option_add_to_infolist (struct t_infolist *infolist,
return 0;
if (!weechat_infolist_new_var_string (ptr_item, "parent_name", fset_option->parent_name))
return 0;
if (!weechat_infolist_new_var_string (ptr_item, "type", fset_option->type))
if (!weechat_infolist_new_var_string (ptr_item, "type", fset_option_type_string[fset_option->type]))
return 0;
if (!weechat_infolist_new_var_string (ptr_item, "default_value", fset_option->default_value))
return 0;
@@ -1006,7 +1022,9 @@ fset_option_print_log ()
weechat_log_printf ("[fset option (addr:0x%lx)]", ptr_fset_option);
weechat_log_printf (" name. . . . . . . . . : '%s'", ptr_fset_option->name);
weechat_log_printf (" parent_name . . . . . : '%s'", ptr_fset_option->parent_name);
weechat_log_printf (" type. . . . . . . . . : '%s'", ptr_fset_option->type);
weechat_log_printf (" type. . . . . . . . . : %d ('%s')",
ptr_fset_option->type,
fset_option_type_string[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);
+14 -1
View File
@@ -22,11 +22,21 @@
#define FSET_OPTION_VALUE_NULL "null"
enum t_fset_option_type
{
FSET_OPTION_TYPE_BOOLEAN = 0,
FSET_OPTION_TYPE_INTEGER,
FSET_OPTION_TYPE_STRING,
FSET_OPTION_TYPE_COLOR,
/* number of option types */
FSET_OPTION_NUM_TYPES,
};
struct t_fset_option
{
char *name; /* option name */
char *parent_name; /* parent option name */
char *type; /* option type */
enum t_fset_option_type type; /* option type */
char *default_value; /* option default value */
char *value; /* option value */
char *parent_value; /* parent option value */
@@ -41,6 +51,9 @@ struct t_fset_option
extern struct t_arraylist *fset_options;
extern struct t_hashtable *fset_option_max_length_field;
extern char *fset_option_filter;
extern char *fset_option_type_string[];
extern char *fset_option_type_string_short[];
extern char *fset_option_type_string_tiny[];
extern int fset_option_valid (struct t_fset_option *option);
extern struct t_fset_option *fset_option_search_by_name (const char *name,