mirror of
https://github.com/tmux/tmux.git
synced 2026-10-11 21:14:21 +00:00
Add a one line description text to each command, originally from someone
in GitHub issue 5438.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
/* $OpenBSD: cmd-list-commands.c,v 1.15 2026/02/24 08:22:13 nicm Exp $ */
|
||||
/* $OpenBSD: cmd-list-commands.c,v 1.16 2026/10/09 13:12:14 nicm Exp $ */
|
||||
|
||||
/*
|
||||
* Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
|
||||
@@ -31,20 +31,43 @@
|
||||
"#{command_list_name}" \
|
||||
"#{?command_list_alias, (#{command_list_alias}),} " \
|
||||
"#{command_list_usage}"
|
||||
#define LIST_COMMANDS_HELP_TEMPLATE \
|
||||
"#{p|#{command_list_name_width}:command_list_name} " \
|
||||
"#{command_list_description}"
|
||||
#define LIST_COMMANDS_VERBOSE_TEMPLATE \
|
||||
LIST_COMMANDS_TEMPLATE "\n #{command_list_description}"
|
||||
|
||||
static enum cmd_retval cmd_list_commands(struct cmd *, struct cmdq_item *);
|
||||
|
||||
const struct cmd_entry cmd_list_commands_entry = {
|
||||
.name = "list-commands",
|
||||
.description = "List commands or show command syntax.",
|
||||
.alias = "lscm",
|
||||
|
||||
.args = { "F:", 0, 1, NULL },
|
||||
.usage = "[-F format] [command]",
|
||||
.args = { "F:h", 0, 1, NULL },
|
||||
.usage = "[-h] [-F format] [command]",
|
||||
|
||||
.flags = CMD_STARTSERVER|CMD_AFTERHOOK,
|
||||
.exec = cmd_list_commands
|
||||
};
|
||||
|
||||
static u_int
|
||||
cmd_list_commands_get_width(const struct cmd_entry *only)
|
||||
{
|
||||
const struct cmd_entry **entryp;
|
||||
u_int width, largest = 0;
|
||||
|
||||
if (only != NULL)
|
||||
return (utf8_cstrwidth(only->name));
|
||||
|
||||
for (entryp = cmd_table; *entryp != NULL; entryp++) {
|
||||
width = utf8_cstrwidth((*entryp)->name);
|
||||
if (width > largest)
|
||||
largest = width;
|
||||
}
|
||||
return (largest);
|
||||
}
|
||||
|
||||
static void
|
||||
cmd_list_single_command(const struct cmd_entry *entry, struct format_tree *ft,
|
||||
const char *template, struct cmdq_item *item)
|
||||
@@ -63,6 +86,11 @@ cmd_list_single_command(const struct cmd_entry *entry, struct format_tree *ft,
|
||||
else
|
||||
s = "";
|
||||
format_add(ft, "command_list_usage", "%s", s);
|
||||
if (entry->description != NULL)
|
||||
s = entry->description;
|
||||
else
|
||||
s = "";
|
||||
format_add(ft, "command_list_description", "%s", s);
|
||||
|
||||
line = format_expand(ft, template);
|
||||
if (*line != '\0')
|
||||
@@ -75,33 +103,44 @@ cmd_list_commands(struct cmd *self, struct cmdq_item *item)
|
||||
{
|
||||
struct args *args = cmd_get_args(self);
|
||||
const struct cmd_entry **entryp;
|
||||
const struct cmd_entry *entry;
|
||||
const struct cmd_entry *entry = NULL;
|
||||
struct format_tree *ft;
|
||||
const char *template, *command;
|
||||
char *cause;
|
||||
int help;
|
||||
u_int width;
|
||||
|
||||
if ((template = args_get(args, 'F')) == NULL)
|
||||
template = LIST_COMMANDS_TEMPLATE;
|
||||
|
||||
ft = format_create(cmdq_get_client(item), item, FORMAT_NONE, 0);
|
||||
format_defaults(ft, NULL, NULL, NULL, NULL);
|
||||
if ((template = args_get(args, 'F')) == NULL) {
|
||||
help = args_has(args, 'h');
|
||||
if (help > 1)
|
||||
template = LIST_COMMANDS_VERBOSE_TEMPLATE;
|
||||
else if (help == 1)
|
||||
template = LIST_COMMANDS_HELP_TEMPLATE;
|
||||
else
|
||||
template = LIST_COMMANDS_TEMPLATE;
|
||||
}
|
||||
|
||||
command = args_string(args, 0);
|
||||
if (command == NULL) {
|
||||
for (entryp = cmd_table; *entryp != NULL; entryp++)
|
||||
cmd_list_single_command(*entryp, ft, template, item);
|
||||
} else {
|
||||
if (command != NULL) {
|
||||
entry = cmd_find(command, &cause);
|
||||
if (entry != NULL)
|
||||
cmd_list_single_command(entry, ft, template, item);
|
||||
else {
|
||||
if (entry == NULL) {
|
||||
cmdq_error(item, "%s", cause);
|
||||
free(cause);
|
||||
format_free(ft);
|
||||
return (CMD_RETURN_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
ft = format_create(cmdq_get_client(item), item, FORMAT_NONE, 0);
|
||||
format_defaults(ft, NULL, NULL, NULL, NULL);
|
||||
width = cmd_list_commands_get_width(entry);
|
||||
format_add(ft, "command_list_name_width", "%u", width);
|
||||
|
||||
if (entry == NULL) {
|
||||
for (entryp = cmd_table; *entryp != NULL; entryp++)
|
||||
cmd_list_single_command(*entryp, ft, template, item);
|
||||
} else
|
||||
cmd_list_single_command(entry, ft, template, item);
|
||||
|
||||
format_free(ft);
|
||||
return (CMD_RETURN_NORMAL);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user