Skip to content

Commit

Permalink
Draw comment text in help view in alignment
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
stevenyvr987 committed May 18, 2019
1 parent 557b020 commit 9370f51
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
1 change: 1 addition & 0 deletions include/tig/keys.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ struct run_request {
struct keymap *keymap;
struct run_request_flags flags;
const char **argv;
char *name;
char *help;
};

Expand Down
18 changes: 12 additions & 6 deletions src/help.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down
9 changes: 8 additions & 1 deletion src/keys.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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)
Expand Down

0 comments on commit 9370f51

Please sign in to comment.