From 9370f51b7fa1d0fa5723d3c7cee9fc00d9cf3261 Mon Sep 17 00:00:00 2001 From: Steven Chan Date: Fri, 10 May 2019 13:25:52 -0700 Subject: [PATCH] Draw comment text in help view in alignment Add code to align comment text when shown in the help screen. Comment text are drawn on the right-hand side of an 'action' field. Only key bindings with comment text will participate in calculating the field width; key bindings with no comments but with unusually long action names may overflow the field. 1. keys.h Add run_request.name field, analogous to req_info.name field. 2. keys.c a. add_run_request() Load run_request.name with a displayable version of **argv, using new function argv_to_string_alloc_prefix(). Include run_request.flags as a string prefix. 3. help.c a. help_keys_visitor() If run_request.help text is present, update help_state.name_width with strlen(run_request.name), so that drawing run_request.help will be aligned. b. help_draw() Analogous to drawing req_info.name as a LINE_HELP_ACTION, draw run_request.name using draw_field(), which replaces the current method of drawing **argv using draw_formatted(). We do this only if run_request.help text is present, otherwise, we draw run_request.name as free-form text. --- include/tig/keys.h | 1 + src/help.c | 18 ++++++++++++------ src/keys.c | 9 ++++++++- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/include/tig/keys.h b/include/tig/keys.h index 06158284d..bad427ca5 100644 --- a/include/tig/keys.h +++ b/include/tig/keys.h @@ -90,6 +90,7 @@ struct run_request { struct keymap *keymap; struct run_request_flags flags; const char **argv; + char *name; char *help; }; diff --git a/src/help.c b/src/help.c index d6d6a537a..7443c1787 100644 --- a/src/help.c +++ b/src/help.c @@ -51,18 +51,19 @@ help_draw(struct view *view, struct line *line, unsigned int lineno) } else if (help->request > REQ_RUN_REQUESTS) { struct run_request *req = get_run_request(help->request); const char *key = get_keys(keymap, help->request, true); - const char *sep = format_run_request_flags(req); - int i; if (draw_field(view, LINE_DEFAULT, key, state->keys_width + 2, ALIGN_RIGHT, false)) return true; - for (i = 0; req->argv[i]; i++) { - if (draw_formatted(view, LINE_HELP_ACTION, "%s%s", sep, req->argv[i])) + /* If there is req->help text to draw, then first draw req->name as a fixed-width field */ + if (req->help) { + if (draw_field(view, LINE_HELP_ACTION, req->name, state->name_width, ALIGN_LEFT, false)) return true; - sep = " "; + draw_text(view, LINE_DEFAULT, req->help); } - if (req->help) draw_text(view, LINE_DEFAULT, req->help); + + /* Else just draw req->name as free-form text */ + else draw_text(view, LINE_HELP_ACTION, req->name); } else { const struct request_info *req_info = help->data.req_info; @@ -170,6 +171,11 @@ help_keys_visitor(void *data, const char *group, struct keymap *keymap, help->data.req_info = req_info; } + /* Include run_req->name in the MAX calculation but only if there is run_req->help text */ + if (run_req && run_req->help) { + state->name_width = MAX(state->name_width, strlen(run_req->name)); + } + return true; } diff --git a/src/keys.c b/src/keys.c index c746e9168..8c22d484c 100644 --- a/src/keys.c +++ b/src/keys.c @@ -490,8 +490,8 @@ add_run_request(struct keymap *keymap, const struct key key[], { struct run_request *req; struct run_request_flags flags = {0}; - enum status_code code = parse_run_request_flags(&flags, argv); + enum status_code code = parse_run_request_flags(&flags, argv); if (code != SUCCESS) return code; @@ -505,6 +505,13 @@ add_run_request(struct keymap *keymap, const struct key key[], req->flags = flags; req->keymap = keymap; + /* Duplicate a displayable version of **argv into the run_request struct */ + req->name = NULL; + if (argv) { + if ((req->name = argv_to_string_alloc_prefix(argv, " ", format_run_request_flags(req))) == NULL) + return ERROR_OUT_OF_MEMORY; + } + /* If there is help text, then dupe it into the run_request struct */ req->help = NULL; if (help)