Change array item keys to be strings instead of numbers which are

painful to work with.
This commit is contained in:
nicm
2026-07-06 14:29:10 +00:00
parent 79b83600af
commit 1117caaf09
7 changed files with 53 additions and 48 deletions

View File

@@ -31,7 +31,7 @@
static enum cmd_retval cmd_show_options_exec(struct cmd *, struct cmdq_item *); static enum cmd_retval cmd_show_options_exec(struct cmd *, struct cmdq_item *);
static void cmd_show_options_print(struct cmd *, struct cmdq_item *, static void cmd_show_options_print(struct cmd *, struct cmdq_item *,
struct options_entry *, int, int); struct options_entry *, const char *, int);
static void cmd_show_hooks_print_monitor(struct cmdq_item *, static void cmd_show_hooks_print_monitor(struct cmdq_item *,
struct options_entry *); struct options_entry *);
static enum cmd_retval cmd_show_hooks_monitor(struct cmd *, struct cmdq_item *, static enum cmd_retval cmd_show_hooks_monitor(struct cmd *, struct cmdq_item *,
@@ -85,7 +85,8 @@ cmd_show_options_exec(struct cmd *self, struct cmdq_item *item)
struct cmd_find_state *target = cmdq_get_target(item); struct cmd_find_state *target = cmdq_get_target(item);
struct options *oo; struct options *oo;
char *argument, *name = NULL, *cause; char *argument, *name = NULL, *cause;
int window, idx, ambiguous, parent, scope; char *array_key = NULL;
int window, ambiguous, parent, scope;
struct options_entry *o; struct options_entry *o;
window = (cmd_get_entry(self) == &cmd_show_window_options_entry); window = (cmd_get_entry(self) == &cmd_show_window_options_entry);
@@ -107,7 +108,7 @@ cmd_show_options_exec(struct cmd *self, struct cmdq_item *item)
} }
argument = format_single_from_target(item, args_string(args, 0)); argument = format_single_from_target(item, args_string(args, 0));
name = options_match(argument, &idx, &ambiguous); name = options_match(argument, &array_key, &ambiguous);
if (name == NULL) { if (name == NULL) {
if (args_has(args, 'q')) if (args_has(args, 'q'))
goto out; goto out;
@@ -137,7 +138,7 @@ cmd_show_options_exec(struct cmd *self, struct cmdq_item *item)
args_has(args, 'B')) args_has(args, 'B'))
cmd_show_hooks_print_monitor(item, o); cmd_show_hooks_print_monitor(item, o);
else else
cmd_show_options_print(self, item, o, idx, parent); cmd_show_options_print(self, item, o, array_key, parent);
} }
else if (*name == '@') { else if (*name == '@') {
if (args_has(args, 'q')) if (args_has(args, 'q'))
@@ -148,26 +149,28 @@ cmd_show_options_exec(struct cmd *self, struct cmdq_item *item)
out: out:
free(name); free(name);
free(array_key);
free(argument); free(argument);
return (CMD_RETURN_NORMAL); return (CMD_RETURN_NORMAL);
fail: fail:
free(name); free(name);
free(array_key);
free(argument); free(argument);
return (CMD_RETURN_ERROR); return (CMD_RETURN_ERROR);
} }
static void static void
cmd_show_options_print(struct cmd *self, struct cmdq_item *item, cmd_show_options_print(struct cmd *self, struct cmdq_item *item,
struct options_entry *o, int idx, int parent) struct options_entry *o, const char *array_key, int parent)
{ {
struct args *args = cmd_get_args(self); struct args *args = cmd_get_args(self);
struct options_array_item *a; struct options_array_item *a;
const char *name = options_name(o); const char *name = options_name(o);
char *value, *tmp = NULL, *escaped; char *value, *tmp = NULL, *escaped;
if (idx != -1) { if (array_key != NULL) {
xasprintf(&tmp, "%s[%d]", name, idx); xasprintf(&tmp, "%s[%s]", name, array_key);
name = tmp; name = tmp;
} else { } else {
if (options_is_array(o)) { if (options_is_array(o)) {
@@ -178,8 +181,8 @@ cmd_show_options_print(struct cmd *self, struct cmdq_item *item,
return; return;
} }
while (a != NULL) { while (a != NULL) {
idx = options_array_item_index(a); array_key = options_array_item_key(a);
cmd_show_options_print(self, item, o, idx, cmd_show_options_print(self, item, o, array_key,
parent); parent);
a = options_array_next(a); a = options_array_next(a);
} }
@@ -187,7 +190,7 @@ cmd_show_options_print(struct cmd *self, struct cmdq_item *item,
} }
} }
value = options_to_string(o, idx, 0); value = options_to_string(o, array_key, 0);
if (args_has(args, 'v')) if (args_has(args, 'v'))
cmdq_print(item, "%s", value); cmdq_print(item, "%s", value);
else if (options_is_string(o)) { else if (options_is_string(o)) {
@@ -243,15 +246,14 @@ cmd_show_options_all(struct cmd *self, struct cmdq_item *item, int scope,
const struct options_table_entry *oe; const struct options_table_entry *oe;
struct options_entry *o; struct options_entry *o;
struct options_array_item *a; struct options_array_item *a;
const char *name; const char *name, *array_key;
u_int idx;
int parent; int parent;
if (cmd_get_entry(self) != &cmd_show_hooks_entry) { if (cmd_get_entry(self) != &cmd_show_hooks_entry) {
o = options_first(oo); o = options_first(oo);
while (o != NULL) { while (o != NULL) {
if (options_table_entry(o) == NULL) if (options_table_entry(o) == NULL)
cmd_show_options_print(self, item, o, -1, 0); cmd_show_options_print(self, item, o, NULL, 0);
o = options_next(o); o = options_next(o);
} }
} }
@@ -278,7 +280,7 @@ cmd_show_options_all(struct cmd *self, struct cmdq_item *item, int scope,
parent = 0; parent = 0;
if (!options_is_array(o)) if (!options_is_array(o))
cmd_show_options_print(self, item, o, -1, parent); cmd_show_options_print(self, item, o, NULL, parent);
else if ((a = options_array_first(o)) == NULL) { else if ((a = options_array_first(o)) == NULL) {
if (!args_has(args, 'v')) { if (!args_has(args, 'v')) {
name = options_name(o); name = options_name(o);
@@ -289,8 +291,8 @@ cmd_show_options_all(struct cmd *self, struct cmdq_item *item, int scope,
} }
} else { } else {
while (a != NULL) { while (a != NULL) {
idx = options_array_item_index(a); array_key = options_array_item_key(a);
cmd_show_options_print(self, item, o, idx, cmd_show_options_print(self, item, o, array_key,
parent); parent);
a = options_array_next(a); a = options_array_next(a);
} }

View File

@@ -1294,7 +1294,8 @@ colour_palette_from_option(struct colour_palette *p, struct options *oo)
{ {
struct options_entry *o; struct options_entry *o;
struct options_array_item *a; struct options_array_item *a;
u_int i, n; union options_value *ov;
u_int i;
int c; int c;
if (p == NULL) if (p == NULL)
@@ -1312,12 +1313,11 @@ colour_palette_from_option(struct colour_palette *p, struct options *oo)
p->default_palette = xcalloc(256, sizeof *p->default_palette); p->default_palette = xcalloc(256, sizeof *p->default_palette);
for (i = 0; i < 256; i++) for (i = 0; i < 256; i++)
p->default_palette[i] = -1; p->default_palette[i] = -1;
while (a != NULL) { for (i = 0; i < 256; i++) {
n = options_array_item_index(a); ov = options_array_getv(o, "%u", i);
if (n < 256) { if (ov != NULL) {
c = options_array_item_value(a)->number; c = ov->number;
p->default_palette[n] = c; p->default_palette[i] = c;
} }
a = options_array_next(a);
} }
} }

View File

@@ -2781,7 +2781,7 @@ input_exit_rename(struct input_ctx *ictx)
if (ictx->input_len == 0) { if (ictx->input_len == 0) {
o = options_get_only(w->options, "automatic-rename"); o = options_get_only(w->options, "automatic-rename");
if (o != NULL) if (o != NULL)
options_remove_or_default(o, -1, NULL); options_remove_or_default(o, NULL, NULL);
if (!options_get_number(w->options, "automatic-rename")) if (!options_get_number(w->options, "automatic-rename"))
window_set_name(w, "", 1); window_set_name(w, "", 1);
} else { } else {

View File

@@ -274,7 +274,7 @@ status_redraw(struct client *c)
for (i = 0; i < lines; i++) { for (i = 0; i < lines; i++) {
screen_write_cursormove(&ctx, 0, i, 0); screen_write_cursormove(&ctx, 0, i, 0);
ov = options_array_get(o, i); ov = options_array_getv(o, "%u", i);
if (ov == NULL) { if (ov == NULL) {
for (n = 0; n < width; n++) for (n = 0; n < width; n++)
screen_write_putc(&ctx, &gc, ' '); screen_write_putc(&ctx, &gc, ' ');

12
tmux.1
View File

@@ -4574,6 +4574,10 @@ If
.Fl g .Fl g
is given, the global session or window option is set. is given, the global session or window option is set.
.Pp .Pp
Array options may be set by giving a key in square brackets after the option
name, for example
.Ql command-alias[zoom] .
.Pp
.Fl F .Fl F
expands formats in the option value. expands formats in the option value.
The The
@@ -4685,7 +4689,7 @@ it is replaced with
.Ar value . .Ar value .
For example, after: For example, after:
.Pp .Pp
.Dl set \-s command\-alias[100] zoom=\[aq]resize\-pane \-Z\[aq] .Dl set \-s command\-alias[zoom] zoom=\[aq]resize\-pane \-Z\[aq]
.Pp .Pp
Using: Using:
.Pp .Pp
@@ -6286,11 +6290,11 @@ or
.Fl H . .Fl H .
The following two commands are equivalent: The following two commands are equivalent:
.Bd -literal -offset indent. .Bd -literal -offset indent.
set\-hook \-g pane\-mode\-changed[42] \[aq]set \-g status\-left\-style bg=red\[aq] set\-hook \-g pane\-mode\-changed[style] \[aq]display\-message changed\[aq]
set\-option \-g pane\-mode\-changed[42] \[aq]set \-g status\-left\-style bg=red\[aq] set\-option \-g pane\-mode\-changed[style] \[aq]display\-message changed\[aq]
.Ed .Ed
.Pp .Pp
Setting a hook without specifying an array index clears the hook and sets the Setting a hook without specifying an array key clears the hook and sets the
first member of the array. first member of the array.
.Pp .Pp
A command's after A command's after

26
tmux.h
View File

@@ -2653,25 +2653,27 @@ const struct options_table_entry *options_table_entry(struct options_entry *);
struct options_entry *options_get_only(struct options *, const char *); struct options_entry *options_get_only(struct options *, const char *);
struct options_entry *options_get(struct options *, const char *); struct options_entry *options_get(struct options *, const char *);
void options_array_clear(struct options_entry *); void options_array_clear(struct options_entry *);
union options_value *options_array_get(struct options_entry *, u_int); union options_value *options_array_get(struct options_entry *, const char *);
int options_array_set(struct options_entry *, u_int, const char *, union options_value * printflike(2, 3) options_array_getv(
int, char **); struct options_entry *, const char *, ...);
int options_array_set(struct options_entry *, const char *,
const char *, int, char **);
int options_array_assign(struct options_entry *, const char *, int options_array_assign(struct options_entry *, const char *,
char **); char **);
struct options_array_item *options_array_first(struct options_entry *); struct options_array_item *options_array_first(struct options_entry *);
struct options_array_item *options_array_next(struct options_array_item *); struct options_array_item *options_array_next(struct options_array_item *);
u_int options_array_item_index(struct options_array_item *); const char *options_array_item_key(struct options_array_item *);
union options_value *options_array_item_value(struct options_array_item *); union options_value *options_array_item_value(struct options_array_item *);
int options_is_array(struct options_entry *); int options_is_array(struct options_entry *);
int options_is_string(struct options_entry *); int options_is_string(struct options_entry *);
char *options_to_string(struct options_entry *, int, int); char *options_to_string(struct options_entry *, const char *, int);
char *options_parse(const char *, int *); char *options_parse(const char *, char **);
struct options_entry *options_parse_get(struct options *, const char *, int *, struct options_entry *options_parse_get(struct options *, const char *,
int); char **, int);
const struct options_table_entry *options_search(const char *); const struct options_table_entry *options_search(const char *);
char *options_match(const char *, int *, int *); char *options_match(const char *, char **, int *);
struct options_entry *options_match_get(struct options *, const char *, int *, struct options_entry *options_match_get(struct options *, const char *,
int, int *); char **, int, int *);
const char *options_get_string(struct options *, const char *); const char *options_get_string(struct options *, const char *);
long long options_get_number(struct options *, const char *); long long options_get_number(struct options *, const char *);
struct cmd_list *options_get_command(struct options *, const char *); struct cmd_list *options_get_command(struct options *, const char *);
@@ -2694,7 +2696,7 @@ int options_from_string(struct options *,
int options_find_choice(const struct options_table_entry *, int options_find_choice(const struct options_table_entry *,
const char *, char **); const char *, char **);
void options_push_changes(const char *); void options_push_changes(const char *);
int options_remove_or_default(struct options_entry *, int, int options_remove_or_default(struct options_entry *, const char *,
char **); char **);
/* options-table.c */ /* options-table.c */

View File

@@ -495,7 +495,6 @@ tty_keys_build(struct tty *tty)
u_int i, j; u_int i, j;
const char *s; const char *s;
struct options_entry *o; struct options_entry *o;
struct options_array_item *a;
union options_value *ov; union options_value *ov;
char copy[16]; char copy[16];
key_code key; key_code key;
@@ -532,12 +531,10 @@ tty_keys_build(struct tty *tty)
o = options_get(global_options, "user-keys"); o = options_get(global_options, "user-keys");
if (o != NULL) { if (o != NULL) {
a = options_array_first(o); for (i = 0; i <= KEYC_NUSER; i++) {
while (a != NULL) { ov = options_array_getv(o, "%u", i);
i = options_array_item_index(a); if (ov != NULL)
ov = options_array_item_value(a); tty_keys_add(tty, ov->string, KEYC_USER + i);
tty_keys_add(tty, ov->string, KEYC_USER + i);
a = options_array_next(a);
} }
} }
} }