From 54b95f619665317fc54b355349dd2880b7fa24b8 Mon Sep 17 00:00:00 2001 From: codegax <14095200+codegax@users.noreply.github.com> Date: Sat, 2 May 2026 17:25:00 -0600 Subject: [PATCH] swaysavetree: match workspace by number too --workspace was documented as accepting a name or a number, but the lookup only compared against the IPC name, so common workspaces such as "1: web" (num=1) could not be selected with --workspace 1. Parse a numeric argument and also compare against the num field. Update the manpage accordingly. --- swaysavetree/main.c | 24 ++++++++++++++++++++---- swaysavetree/sway-save-tree.1.scd | 4 +++- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/swaysavetree/main.c b/swaysavetree/main.c index da64cce69..3dc47e1dc 100644 --- a/swaysavetree/main.c +++ b/swaysavetree/main.c @@ -2,6 +2,7 @@ // append_layout`. Counterpart to i3-save-tree(1). #include +#include #include #include #include @@ -168,9 +169,9 @@ static struct json_object *build_layout_node(struct json_object *src) { return out; } -// Recursive search of the tree for the requested workspace. +// Matches by name; if numeric >= 0, also matches num so "1" finds "1: web". static struct json_object *find_workspace(struct json_object *node, - const char *name) { + const char *name, int numeric) { struct json_object *type; if (json_object_object_get_ex(node, "type", &type) && json_object_get_type(type) == json_type_string && @@ -180,6 +181,13 @@ static struct json_object *find_workspace(struct json_object *node, strcmp(json_object_get_string(ws_name), name) == 0) { return node; } + if (numeric >= 0) { + struct json_object *num; + if (json_object_object_get_ex(node, "num", &num) && + json_object_get_int(num) == numeric) { + return node; + } + } } struct json_object *nodes; if (json_object_object_get_ex(node, "nodes", &nodes) && @@ -187,7 +195,7 @@ static struct json_object *find_workspace(struct json_object *node, size_t n = json_object_array_length(nodes); for (size_t i = 0; i < n; i++) { struct json_object *child = json_object_array_get_idx(nodes, i); - struct json_object *match = find_workspace(child, name); + struct json_object *match = find_workspace(child, name, numeric); if (match) { return match; } @@ -245,7 +253,15 @@ int main(int argc, char **argv) { return 1; } - struct json_object *ws = find_workspace(root, workspace); + int numeric = -1; + if (workspace[0] != '\0') { + char *end = NULL; + long n = strtol(workspace, &end, 10); + if (end != workspace && *end == '\0' && n >= 0 && n <= INT_MAX) { + numeric = (int)n; + } + } + struct json_object *ws = find_workspace(root, workspace, numeric); if (!ws) { fprintf(stderr, "sway-save-tree: workspace '%s' not found\n", workspace); diff --git a/swaysavetree/sway-save-tree.1.scd b/swaysavetree/sway-save-tree.1.scd index 7d00902ae..90d5aefed 100644 --- a/swaysavetree/sway-save-tree.1.scd +++ b/swaysavetree/sway-save-tree.1.scd @@ -25,7 +25,9 @@ This is the sway counterpart of i3-save-tree(1). # OPTIONS *--workspace* - Name (or number, as a string) of the workspace to dump. Required. + Name or number of the workspace to dump. Required. If the argument is + all digits, it is also matched against the workspace's number, so + *--workspace 1* finds a workspace named "1: web". *-h, --help* Show help and exit.