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 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 *,
struct options_entry *);
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 options *oo;
char *argument, *name = NULL, *cause;
int window, idx, ambiguous, parent, scope;
char *array_key = NULL;
int window, ambiguous, parent, scope;
struct options_entry *o;
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));
name = options_match(argument, &idx, &ambiguous);
name = options_match(argument, &array_key, &ambiguous);
if (name == NULL) {
if (args_has(args, 'q'))
goto out;
@@ -137,7 +138,7 @@ cmd_show_options_exec(struct cmd *self, struct cmdq_item *item)
args_has(args, 'B'))
cmd_show_hooks_print_monitor(item, o);
else
cmd_show_options_print(self, item, o, idx, parent);
cmd_show_options_print(self, item, o, array_key, parent);
}
else if (*name == '@') {
if (args_has(args, 'q'))
@@ -148,26 +149,28 @@ cmd_show_options_exec(struct cmd *self, struct cmdq_item *item)
out:
free(name);
free(array_key);
free(argument);
return (CMD_RETURN_NORMAL);
fail:
free(name);
free(array_key);
free(argument);
return (CMD_RETURN_ERROR);
}
static void
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 options_array_item *a;
const char *name = options_name(o);
char *value, *tmp = NULL, *escaped;
if (idx != -1) {
xasprintf(&tmp, "%s[%d]", name, idx);
if (array_key != NULL) {
xasprintf(&tmp, "%s[%s]", name, array_key);
name = tmp;
} else {
if (options_is_array(o)) {
@@ -178,8 +181,8 @@ cmd_show_options_print(struct cmd *self, struct cmdq_item *item,
return;
}
while (a != NULL) {
idx = options_array_item_index(a);
cmd_show_options_print(self, item, o, idx,
array_key = options_array_item_key(a);
cmd_show_options_print(self, item, o, array_key,
parent);
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'))
cmdq_print(item, "%s", value);
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;
struct options_entry *o;
struct options_array_item *a;
const char *name;
u_int idx;
const char *name, *array_key;
int parent;
if (cmd_get_entry(self) != &cmd_show_hooks_entry) {
o = options_first(oo);
while (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);
}
}
@@ -278,7 +280,7 @@ cmd_show_options_all(struct cmd *self, struct cmdq_item *item, int scope,
parent = 0;
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) {
if (!args_has(args, 'v')) {
name = options_name(o);
@@ -289,8 +291,8 @@ cmd_show_options_all(struct cmd *self, struct cmdq_item *item, int scope,
}
} else {
while (a != NULL) {
idx = options_array_item_index(a);
cmd_show_options_print(self, item, o, idx,
array_key = options_array_item_key(a);
cmd_show_options_print(self, item, o, array_key,
parent);
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_array_item *a;
u_int i, n;
union options_value *ov;
u_int i;
int c;
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);
for (i = 0; i < 256; i++)
p->default_palette[i] = -1;
while (a != NULL) {
n = options_array_item_index(a);
if (n < 256) {
c = options_array_item_value(a)->number;
p->default_palette[n] = c;
for (i = 0; i < 256; i++) {
ov = options_array_getv(o, "%u", i);
if (ov != NULL) {
c = ov->number;
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) {
o = options_get_only(w->options, "automatic-rename");
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"))
window_set_name(w, "", 1);
} else {

View File

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

12
tmux.1
View File

@@ -4574,6 +4574,10 @@ If
.Fl g
is given, the global session or window option is set.
.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
expands formats in the option value.
The
@@ -4685,7 +4689,7 @@ it is replaced with
.Ar value .
For example, after:
.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
Using:
.Pp
@@ -6286,11 +6290,11 @@ or
.Fl H .
The following two commands are equivalent:
.Bd -literal -offset indent.
set\-hook \-g pane\-mode\-changed[42] \[aq]set \-g status\-left\-style bg=red\[aq]
set\-option \-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[style] \[aq]display\-message changed\[aq]
.Ed
.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.
.Pp
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(struct options *, const char *);
void options_array_clear(struct options_entry *);
union options_value *options_array_get(struct options_entry *, u_int);
int options_array_set(struct options_entry *, u_int, const char *,
int, char **);
union options_value *options_array_get(struct options_entry *, const char *);
union options_value * printflike(2, 3) options_array_getv(
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 *,
char **);
struct options_array_item *options_array_first(struct options_entry *);
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 *);
int options_is_array(struct options_entry *);
int options_is_string(struct options_entry *);
char *options_to_string(struct options_entry *, int, int);
char *options_parse(const char *, int *);
struct options_entry *options_parse_get(struct options *, const char *, int *,
int);
char *options_to_string(struct options_entry *, const char *, int);
char *options_parse(const char *, char **);
struct options_entry *options_parse_get(struct options *, const char *,
char **, int);
const struct options_table_entry *options_search(const char *);
char *options_match(const char *, int *, int *);
struct options_entry *options_match_get(struct options *, const char *, int *,
int, int *);
char *options_match(const char *, char **, int *);
struct options_entry *options_match_get(struct options *, const char *,
char **, int, int *);
const char *options_get_string(struct options *, const char *);
long long options_get_number(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 *,
const char *, 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 **);
/* options-table.c */

View File

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