From fee95aa8fe0c08edce18b9aca9c59813b769e65a Mon Sep 17 00:00:00 2001 From: Steven Chan Date: Sat, 6 Jul 2019 14:29:01 -0700 Subject: [PATCH] Improve how a toggle option value is shown on the status line Whenever a view column option is toggled, the new setting is echoed on the status line, but it does not mimic what should be typed on the status line using the set command to achieve the same thing. For example, the message 'set commit-title-graph = v1' is shown but the user needs to type ':set main-view-commit-title-graph = v1' on the status line to achieve the same setting, ie, the user needs to type colon to initiate prompt mode, and then type the view name as a prefix to the option name. The PR modifies prompt_toggle_option() to show exactly what needs to be typed on the status line. The function called success() with a format string, an option name, and an option value to generate a status line for each type of option value. The format string is modified to include view->name as a prefix to option name. A macro wrapper for success() is used to reduce repetition. Fixes issue #879. --- src/prompt.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/prompt.c b/src/prompt.c index 831e4c9c6..506b32b6f 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -718,6 +718,11 @@ static enum status_code prompt_toggle_option(struct view *view, const char *argv[], const char *prefix, struct option_info *toggle, enum view_flag *flags) { +/* Define a partial version of success() to format the option name as a suffix to the view name, + * thereby reducing the arguments needed by success() to only the option value and its format string. + */ +#define Success(opt_fmt, opt_val) success(":set %s-view-%s = " opt_fmt, view->name, name, opt_val) + char name[SIZEOF_STR]; if (!enum_name_prefixed(name, sizeof(name), prefix, toggle->name)) @@ -731,7 +736,7 @@ prompt_toggle_option(struct view *view, const char *argv[], const char *prefix, *opt = !*opt; if (opt == &opt_mouse) enable_mouse(*opt); - return success("set %s = %s", name, *opt ? "yes" : "no"); + return Success("%s", *opt ? "yes" : "no"); } else if (!strncmp(toggle->type, "enum", 4)) { const char *type = toggle->type + STRING_SIZE("enum "); @@ -739,7 +744,7 @@ prompt_toggle_option(struct view *view, const char *argv[], const char *prefix, const struct enum_map *map = find_enum_map(type); *opt = (*opt + 1) % map->size; - return success("set %s = %s", name, enum_name(map->entries[*opt].name)); + return Success("%s", enum_name(map->entries[*opt].name)); } else if (!strcmp(toggle->type, "int")) { const char *arg = argv[2] ? argv[2] : "1"; @@ -761,12 +766,12 @@ prompt_toggle_option(struct view *view, const char *argv[], const char *prefix, if (strstr(name, "commit-title-overflow")) { *opt = *opt ? -*opt : 50; if (*opt < 0) - return success("set %s = no", name); + return Success("%s", "no"); diff = 0; } *opt += diff; - return success("set %s = %d", name, *opt); + return Success("%d", *opt); } else if (!strcmp(toggle->type, "double")) { const char *arg = argv[2] ? argv[2] : "1.0"; @@ -783,7 +788,7 @@ prompt_toggle_option(struct view *view, const char *argv[], const char *prefix, diff = strtod(arg, NULL); *opt += sign * diff; - return success("set %s = %.2f", name, *opt); + return Success("%.2f", *opt); } else if (!strcmp(toggle->type, "const char **")) { const char ***opt = toggle->value; @@ -826,6 +831,7 @@ prompt_toggle_option(struct view *view, const char *argv[], const char *prefix, } else { return error("Unsupported `:toggle %s` (%s)", name, toggle->type); } +#undef Success } static enum status_code