Add a parser for a subset of JSON, will be used for new layout strings

(and maybe some other stuff), from Dane Jensen.
This commit is contained in:
nicm
2026-09-08 08:33:10 +00:00
committed by tmux update bot
parent 96f4c7b9fa
commit 01d1d6580a
5 changed files with 1061 additions and 11 deletions

View File

@@ -1,4 +1,4 @@
# $OpenBSD: Makefile,v 1.122 2026/08/01 17:04:12 miod Exp $
# $OpenBSD: Makefile,v 1.123 2026/09/08 08:33:10 nicm Exp $
PROG= tmux
SRCS= alerts.c \
@@ -89,6 +89,7 @@ SRCS= alerts.c \
input-keys.c \
input.c \
job.c \
json.c \
key-bindings.c \
key-string.c \
layout-custom.c \

View File

@@ -1,4 +1,4 @@
/* $OpenBSD: cmd-display-message.c,v 1.65 2026/02/23 08:46:57 nicm Exp $ */
/* $OpenBSD: cmd-display-message.c,v 1.66 2026/09/08 08:33:10 nicm Exp $ */
/*
* Copyright (c) 2009 Tiago Cunha <me@tiagocunha.org>
@@ -39,8 +39,8 @@ const struct cmd_entry cmd_display_message_entry = {
.name = "display-message",
.alias = "display",
.args = { "aCc:d:lINpt:F:v", 0, 1, NULL },
.usage = "[-aCIlNpv] [-c target-client] [-d delay] [-F format] "
.args = { "aCc:d:jlINpt:F:v", 0, 1, NULL },
.usage = "[-aCIjlNpv] [-c target-client] [-d delay] [-F format] "
CMD_TARGET_PANE_USAGE " [message]",
.target = { 't', CMD_FIND_PANE, CMD_FIND_CANFAIL },
@@ -67,14 +67,15 @@ cmd_display_message_exec(struct cmd *self, struct cmdq_item *item)
struct winlink *wl = target->wl;
struct window_pane *wp = target->wp;
const char *template;
char *msg, *cause;
char *msg, *cause = NULL;
int delay = -1, flags, Nflag = args_has(args, 'N');
int Cflag = args_has(args, 'C');
struct format_tree *ft;
u_int count = args_count(args);
struct evbuffer *evb;
struct json_node *jn;
if (args_has(args, 'I')) {
if (args_has(args, 'I') && !args_has(args, 'j')) {
if (wp == NULL)
return (CMD_RETURN_NORMAL);
switch (window_pane_start_input(wp, item, &cause)) {
@@ -107,7 +108,9 @@ cmd_display_message_exec(struct cmd *self, struct cmdq_item *item)
template = args_string(args, 0);
else
template = args_get(args, 'F');
if (template == NULL)
if (args_has(args, 'j') && template == NULL)
template = "";
else if (template == NULL)
template = DISPLAY_MESSAGE_TEMPLATE;
/*
@@ -129,7 +132,7 @@ cmd_display_message_exec(struct cmd *self, struct cmdq_item *item)
ft = format_create(cmdq_get_client(item), item, FORMAT_NONE, flags);
format_defaults(ft, c, s, wl, wp);
if (args_has(args, 'a')) {
if (args_has(args, 'a') && !args_has(args, 'j')) {
format_each(ft, cmd_display_message_each, item);
format_free(ft);
return (CMD_RETURN_NORMAL);
@@ -139,6 +142,19 @@ cmd_display_message_exec(struct cmd *self, struct cmdq_item *item)
msg = xstrdup(template);
else
msg = format_expand_time(ft, template);
if (args_has(args, 'j')) {
jn = json_parse(msg, &cause);
if (jn == NULL) {
cmdq_error(item, "%s", cause);
free(cause);
free(msg);
format_free(ft);
return (CMD_RETURN_ERROR);
}
free(msg);
msg = json_to_string(jn);
json_destroy_node(jn);
}
if (cmdq_get_client(item) == NULL)
cmdq_error(item, "%s", msg);

1001
json.c Normal file

File diff suppressed because it is too large Load Diff

10
tmux.1
View File

@@ -1,4 +1,4 @@
.\" $OpenBSD: tmux.1,v 1.1165 2026/09/08 07:31:59 nicm Exp $
.\" $OpenBSD: tmux.1,v 1.1166 2026/09/08 08:33:10 nicm Exp $
.\"
.\" Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
.\"
@@ -8277,7 +8277,7 @@ The following keys are available in menus:
.El
.Tg display
.It Xo Ic display\-message
.Op Fl aCIlNpv
.Op Fl aCIjlNpv
.Op Fl c Ar target\-client
.Op Fl d Ar delay
.Op Fl t Ar target\-pane
@@ -8317,6 +8317,12 @@ if
.Fl t
is given, otherwise the active pane.
.Pp
If
.Fl j
is given,
.Ar message
is parsed as JSON and printed.
.Pp
.Fl v
prints verbose logging as the format is parsed and
.Fl a

28
tmux.h
View File

@@ -1,4 +1,4 @@
/* $OpenBSD: tmux.h,v 1.1433 2026/09/01 12:49:49 nicm Exp $ */
/* $OpenBSD: tmux.h,v 1.1434 2026/09/08 08:33:10 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
@@ -59,6 +59,7 @@ struct input_ctx;
struct input_request;
struct input_requests;
struct job;
struct json_node;
struct menu_data;
struct mode_tree_data;
struct mouse_event;
@@ -4200,4 +4201,29 @@ struct hyperlinks *hyperlinks_copy(struct hyperlinks *);
void hyperlinks_reset(struct hyperlinks *);
void hyperlinks_free(struct hyperlinks *);
/* json.c */
struct json_node *json_parse(const char *, char **);
void json_destroy_node(struct json_node *);
char *json_to_string(struct json_node *);
struct json_node *json_find(struct json_node *, const char *);
struct json_node *json_array_first(struct json_node *);
struct json_node *json_array_next(struct json_node *);
int json_get_string(struct json_node *, const char **);
int json_get_number(struct json_node *, int64_t *);
int json_get_boolean(struct json_node *, int *);
int json_get_object(struct json_node *,
struct json_node **);
int json_get_array(struct json_node *,
struct json_node **);
int json_find_string(struct json_node *, const char *,
const char **, char **);
int json_find_number(struct json_node *, const char *,
int64_t *, char **);
int json_find_boolean(struct json_node *, const char *,
int *, char **);
int json_find_object(struct json_node *, const char *,
struct json_node **, char **);
int json_find_array(struct json_node *, const char *,
struct json_node **, char **);
#endif /* TMUX_H */