Skip to content

Commit

Permalink
Ability to add comments into help view [jonas#809]
Browse files Browse the repository at this point in the history
1. keys.h

   Add a run_request.help field, analogous to the req_info.help field.

2. options.h

   Add a parameter to set_option() for passing any comment text.

3. options.c

   a. read_option()

      Identify any #comment text string that may have accompanied an option command,
      and pass it into set_option(). The #comment text string is trimmed of any white space.

   b. set_option()

      set_option() dispatches to specific bind commands.
      Pass any given comment text into the option_bind_command(),
      and ignore comment text for the other bind commands.

   c. option_bind_command()

      Pass comment text into add_run_request().

   d. read_repo_config_option()

      Specify the option command to use with "color", "bind", or "set" names
      instead of specific function names.

   e. set_repo_config_option()

      Refactor to call set_option(), the dispatcher for specific bind commands,
      and call with comment text pointer set to NULL,
      because comment text is never available from a repo configuration file.

4. prompt.c

   a. run_prompt_command()

      Call set_option() with comment text pointer set to NULL,
      because comment text is not parsed on a prompt line.

5. keys.c

   a. add_run_request()

      Duplicate comment text into the new run_request.help field.

6. help.c

   a. help_draw()

      Draw the run_request.help text using draw_text(), analogous to drawing req_info.help.
  • Loading branch information
stevenyvr987 committed May 20, 2019
1 parent 36ced9a commit 134547d
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 22 deletions.
3 changes: 2 additions & 1 deletion include/tig/keys.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,11 @@ struct run_request {
struct keymap *keymap;
struct run_request_flags flags;
const char **argv;
char *help;
};

struct run_request *get_run_request(enum request request);
enum status_code add_run_request(struct keymap *keymap, const struct key key[], size_t keys, const char **argv);
enum status_code add_run_request(struct keymap *keymap, const struct key key[], size_t keys, const char **argv, const char *help);
enum status_code parse_run_request_flags(struct run_request_flags *flags, const char **argv);
const char *format_run_request_flags(const struct run_request *req);

Expand Down
2 changes: 1 addition & 1 deletion include/tig/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ struct option_info *find_column_option_info(enum view_column_type type, union vi
const char *option, struct option_info *column_info, const char **column_name);
enum status_code parse_int(int *opt, const char *arg, int min, int max);
enum status_code parse_step(double *opt, const char *arg);
enum status_code set_option(const char *opt, int argc, const char *argv[]);
enum status_code set_option(const char *opt, int argc, const char *argv[], const char *help);
enum status_code load_options(void);
enum status_code load_git_config(void);
enum status_code save_options(const char *path);
Expand Down
1 change: 1 addition & 0 deletions src/help.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ help_draw(struct view *view, struct line *line, unsigned int lineno)
return true;
sep = " ";
}
if (req->help) draw_text(view, LINE_DEFAULT, req->help);

} else {
const struct request_info *req_info = help->data.req_info;
Expand Down
8 changes: 7 additions & 1 deletion src/keys.c
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ parse_run_request_flags(struct run_request_flags *flags, const char **argv)

enum status_code
add_run_request(struct keymap *keymap, const struct key key[],
size_t keys, const char **argv)
size_t keys, const char **argv, const char *help)
{
struct run_request *req;
struct run_request_flags flags = {0};
Expand All @@ -505,6 +505,12 @@ add_run_request(struct keymap *keymap, const struct key key[],
req->flags = flags;
req->keymap = keymap;

/* If there is help text, then dupe it into the run_request struct */
req->help = NULL;
if (help)
if ((req->help = strdup(help)) == NULL)
return ERROR_OUT_OF_MEMORY;

return add_keybinding(keymap, REQ_RUN_REQUESTS + run_requests, key, keys);
}

Expand Down
36 changes: 18 additions & 18 deletions src/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,7 @@ option_set_command(int argc, const char *argv[])

/* Wants: mode request key */
static enum status_code
option_bind_command(int argc, const char *argv[])
option_bind_command(int argc, const char *argv[], const char *help)
{
struct key key[16];
size_t keys = 0;
Expand Down Expand Up @@ -870,7 +870,7 @@ option_bind_command(int argc, const char *argv[])
const char *toggle[] = { ":toggle", mapped, arg, NULL};
const char *other[] = { mapped, NULL };
const char **prompt = *mapped == ':' ? other : toggle;
enum status_code code = add_run_request(keymap, key, keys, prompt);
enum status_code code = add_run_request(keymap, key, keys, prompt, NULL);

if (code == SUCCESS)
code = error("%s has been replaced by `%s%s%s%s'",
Expand All @@ -882,7 +882,7 @@ option_bind_command(int argc, const char *argv[])
}

if (request == REQ_UNKNOWN)
return add_run_request(keymap, key, keys, argv + 2);
return add_run_request(keymap, key, keys, argv + 2, help);

return add_keybinding(keymap, request, key, keys);
}
Expand Down Expand Up @@ -916,7 +916,7 @@ option_source_command(int argc, const char *argv[])
}

enum status_code
set_option(const char *opt, int argc, const char *argv[])
set_option(const char *opt, int argc, const char *argv[], const char *help)
{
if (!strcmp(opt, "color"))
return option_color_command(argc, argv);
Expand All @@ -925,7 +925,7 @@ set_option(const char *opt, int argc, const char *argv[])
return option_set_command(argc, argv);

if (!strcmp(opt, "bind"))
return option_bind_command(argc, argv);
return option_bind_command(argc, argv, help);

if (!strcmp(opt, "source"))
return option_source_command(argc, argv);
Expand Down Expand Up @@ -957,15 +957,16 @@ read_option(char *opt, size_t optlen, char *value, size_t valuelen, void *data)
const char *argv[SIZEOF_ARG];
int argc = 0;

if (len < valuelen) {
bool have_comments = len < valuelen;
if (have_comments) {
valuelen = len;
value[valuelen] = 0;
}

if (!argv_from_string(argv, &argc, value))
status = error("Too many option arguments for %s", opt);
else
status = set_option(opt, argc, argv);
status = set_option(opt, argc, argv, have_comments ? string_trim(value + len + 1) : NULL);
}

if (status != SUCCESS) {
Expand Down Expand Up @@ -1322,16 +1323,15 @@ set_remote_branch(const char *name, const char *value, size_t valuelen)
}

static void
set_repo_config_option(char *name, char *value, enum status_code (*cmd)(int, const char **))
set_repo_config_option(char *name, char *value, const char *cmd)
{
const char *argv[SIZEOF_ARG] = { name, "=" };
int argc = 1 + (cmd == option_set_command);
enum status_code code;
const char *argv[SIZEOF_ARG]; int argc;
if (!strcmp(cmd, "set")) { argv[0] = name; argv[1] = "="; argc = 2; }
else { argv[0] = name; argc = 1; }

if (!argv_from_string(argv, &argc, value))
code = error("Too many arguments");
else
code = cmd(argc, argv);
enum status_code code = !argv_from_string(argv, &argc, value)
? error("Too many arguments")
: set_option(cmd, argc, argv, NULL);

if (code != SUCCESS)
warn("Option 'tig.%s': %s", name, get_status_message(code));
Expand Down Expand Up @@ -1443,13 +1443,13 @@ read_repo_config_option(char *name, size_t namelen, char *value, size_t valuelen
parse_bool(&opt_status_show_untracked_files, value);

else if (!prefixcmp(name, "tig.color."))
set_repo_config_option(name + 10, value, option_color_command);
set_repo_config_option(name + 10, value, "color");

else if (!prefixcmp(name, "tig.bind."))
set_repo_config_option(name + 9, value, option_bind_command);
set_repo_config_option(name + 9, value, "bind");

else if (!prefixcmp(name, "tig."))
set_repo_config_option(name + 4, value, option_set_command);
set_repo_config_option(name + 4, value, "set");

else if (!prefixcmp(name, "color."))
set_git_color_option(name + STRING_SIZE("color."), value);
Expand Down
2 changes: 1 addition & 1 deletion src/prompt.c
Original file line number Diff line number Diff line change
Expand Up @@ -1051,7 +1051,7 @@ run_prompt_command(struct view *view, const char *argv[])
if (request != REQ_UNKNOWN)
return request;

code = set_option(argv[0], argv_size(argv + 1), &argv[1]);
code = set_option(cmd, argv_size(argv + 1), &argv[1], NULL);
if (code != SUCCESS) {
report("%s", get_status_message(code));
return REQ_NONE;
Expand Down

0 comments on commit 134547d

Please sign in to comment.