From bf9d9494db89e7de45653e4797e2977c6e185c13 Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Sun, 12 Jun 2016 15:26:07 +0200 Subject: Add a CHOICE type to the settings system. This is useful to let the user choose an option between a finite set of valid alternatives. --- src/fe-common/core/completion.c | 17 ++++++++++++++--- src/fe-common/core/fe-settings.c | 5 +++++ 2 files changed, 19 insertions(+), 3 deletions(-) (limited to 'src/fe-common') diff --git a/src/fe-common/core/completion.c b/src/fe-common/core/completion.c index 861054b5..0e9982d5 100644 --- a/src/fe-common/core/completion.c +++ b/src/fe-common/core/completion.c @@ -690,9 +690,20 @@ static void sig_complete_set(GList **list, WINDOW_REC *window, else if (*line != '\0' && *word == '\0') { SETTINGS_REC *rec = settings_get_record(line); if (rec != NULL) { - char *value = settings_get_print(rec); - if (value != NULL) - *list = g_list_append(*list, value); + /* show the whole list of valid options */ + if (rec->type == SETTING_TYPE_CHOICE) { + char **tmp = rec->choices; + + while (*tmp) + *list = g_list_append(*list, g_strdup(*tmp++)); + } + /* show the current option */ + else { + char *value = settings_get_print(rec); + + if (value != NULL) + *list = g_list_append(*list, value); + } } } diff --git a/src/fe-common/core/fe-settings.c b/src/fe-common/core/fe-settings.c index b689cbf9..e25886ec 100644 --- a/src/fe-common/core/fe-settings.c +++ b/src/fe-common/core/fe-settings.c @@ -142,6 +142,11 @@ static void cmd_set(char *data) else set_int(key, value); break; + case SETTING_TYPE_CHOICE: + settings_set_choice(key, clear ? "" : + set_default ? rec->choices[rec->default_value.v_int] : + value); + break; case SETTING_TYPE_STRING: settings_set_str(key, clear ? "" : set_default ? rec->default_value.v_string : -- cgit v1.2.3 From 7307b48bd6928207ded9e186af3f3b97625f00bb Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Sun, 12 Jun 2016 22:58:35 +0200 Subject: Sort the completion results Make sure the current option is shown first. --- src/fe-common/core/completion.c | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) (limited to 'src/fe-common') diff --git a/src/fe-common/core/completion.c b/src/fe-common/core/completion.c index 0e9982d5..c87fdb50 100644 --- a/src/fe-common/core/completion.c +++ b/src/fe-common/core/completion.c @@ -690,19 +690,20 @@ static void sig_complete_set(GList **list, WINDOW_REC *window, else if (*line != '\0' && *word == '\0') { SETTINGS_REC *rec = settings_get_record(line); if (rec != NULL) { + char *value = settings_get_print(rec); + + /* show the current option first */ + if (value != NULL) + *list = g_list_append(*list, value); + /* show the whole list of valid options */ if (rec->type == SETTING_TYPE_CHOICE) { - char **tmp = rec->choices; - - while (*tmp) - *list = g_list_append(*list, g_strdup(*tmp++)); - } - /* show the current option */ - else { - char *value = settings_get_print(rec); + char **tmp; - if (value != NULL) - *list = g_list_append(*list, value); + for (tmp = rec->choices; *tmp; tmp++) { + if (g_ascii_strcasecmp(*tmp, value) != 0) + *list = g_list_append(*list, g_strdup(*tmp)); + } } } } -- cgit v1.2.3 From 6f795f020d5c374023dc9ca647b267aef0a76950 Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Sun, 12 Jun 2016 23:39:22 +0200 Subject: Strip the surrounding whitespace. --- src/fe-common/core/fe-settings.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) (limited to 'src/fe-common') diff --git a/src/fe-common/core/fe-settings.c b/src/fe-common/core/fe-settings.c index e25886ec..ca1f871d 100644 --- a/src/fe-common/core/fe-settings.c +++ b/src/fe-common/core/fe-settings.c @@ -67,6 +67,7 @@ static void set_print_pattern(const char *pattern) static void set_boolean(const char *key, const char *value) { char *stripped_value; + stripped_value = g_strdup(value); g_strstrip(stripped_value); @@ -79,7 +80,7 @@ static void set_boolean(const char *key, const char *value) else printformat(NULL, NULL, MSGLEVEL_CLIENTERROR, TXT_NOT_TOGGLE); - g_free(stripped_value); + g_free(stripped_value); } static void set_int(const char *key, const char *value) @@ -99,6 +100,16 @@ static void set_int(const char *key, const char *value) settings_set_int(key, (int)longval); } +static void set_choice(const char *key, const char *value) +{ + char *stripped_value; + + stripped_value = g_strdup(value); + g_strstrip(stripped_value); + settings_set_choice(key, stripped_value); + g_free(stripped_value); +} + /* SYNTAX: SET [-clear | -default] [ []] */ static void cmd_set(char *data) { @@ -143,9 +154,10 @@ static void cmd_set(char *data) set_int(key, value); break; case SETTING_TYPE_CHOICE: - settings_set_choice(key, clear ? "" : - set_default ? rec->choices[rec->default_value.v_int] : - value); + if (clear || set_default) + settings_set_choice(key, rec->choices[rec->default_value.v_int]); + else + set_choice(key, value); break; case SETTING_TYPE_STRING: settings_set_str(key, clear ? "" : -- cgit v1.2.3 From 9a30ab53df226acb4586a2f214f3994a7b8b32ea Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Mon, 13 Jun 2016 14:03:00 +0200 Subject: Move the validation of the CHOICE setting value Also, use a FORMAT to show the error message. --- src/fe-common/core/fe-settings.c | 10 +++++++++- src/fe-common/core/module-formats.c | 1 + src/fe-common/core/module-formats.h | 1 + 3 files changed, 11 insertions(+), 1 deletion(-) (limited to 'src/fe-common') diff --git a/src/fe-common/core/fe-settings.c b/src/fe-common/core/fe-settings.c index ca1f871d..6f69d930 100644 --- a/src/fe-common/core/fe-settings.c +++ b/src/fe-common/core/fe-settings.c @@ -106,7 +106,15 @@ static void set_choice(const char *key, const char *value) stripped_value = g_strdup(value); g_strstrip(stripped_value); - settings_set_choice(key, stripped_value); + + if (settings_set_choice(key, stripped_value) == FALSE) { + SETTINGS_REC *rec = settings_get_record(key); + char *msg = g_strjoinv(",", rec->choices); + + printformat(NULL, NULL, MSGLEVEL_CLIENTERROR, TXT_INVALID_CHOICE, msg); + g_free(msg); + } + g_free(stripped_value); } diff --git a/src/fe-common/core/module-formats.c b/src/fe-common/core/module-formats.c index b897b0c6..5c07f14c 100644 --- a/src/fe-common/core/module-formats.c +++ b/src/fe-common/core/module-formats.c @@ -221,6 +221,7 @@ FORMAT_REC fecommon_core_formats[] = { { "invalid_level", "Invalid message level", 0 }, { "invalid_size", "Invalid size", 0 }, { "invalid_charset", "Invalid charset: $0", 1, { 0 } }, + { "invalid_choice", "Invalid choice, must be one of $0", 1, { 0 } }, { "eval_max_recurse", "/eval hit maximum recursion limit", 0 }, { "program_not_found", "Could not find file or file is not executable", 0 }, { "no_server_defined", "No servers defined for this network, see /help server for how to add one", 0 }, diff --git a/src/fe-common/core/module-formats.h b/src/fe-common/core/module-formats.h index de1e13f2..2b45ff6b 100644 --- a/src/fe-common/core/module-formats.h +++ b/src/fe-common/core/module-formats.h @@ -190,6 +190,7 @@ enum { TXT_INVALID_LEVEL, TXT_INVALID_SIZE, TXT_INVALID_CHARSET, + TXT_INVALID_CHOICE, TXT_EVAL_MAX_RECURSE, TXT_PROGRAM_NOT_FOUND, TXT_NO_SERVER_DEFINED, -- cgit v1.2.3 From 5c8423a08cd9ffbe3363f3b0e5722e4a32157914 Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Mon, 13 Jun 2016 20:27:37 +0200 Subject: Add a space after the comma when listing the options. --- src/fe-common/core/fe-settings.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/fe-common') diff --git a/src/fe-common/core/fe-settings.c b/src/fe-common/core/fe-settings.c index 6f69d930..abbd45a8 100644 --- a/src/fe-common/core/fe-settings.c +++ b/src/fe-common/core/fe-settings.c @@ -109,7 +109,7 @@ static void set_choice(const char *key, const char *value) if (settings_set_choice(key, stripped_value) == FALSE) { SETTINGS_REC *rec = settings_get_record(key); - char *msg = g_strjoinv(",", rec->choices); + char *msg = g_strjoinv(", ", rec->choices); printformat(NULL, NULL, MSGLEVEL_CLIENTERROR, TXT_INVALID_CHOICE, msg); g_free(msg); -- cgit v1.2.3