diff options
author | Drew DeVault <sir@cmpwn.com> | 2015-12-17 19:48:55 -0500 |
---|---|---|
committer | Drew DeVault <sir@cmpwn.com> | 2015-12-17 19:48:55 -0500 |
commit | f994f00d0059039d952d7dcfb72a683f303878bc (patch) | |
tree | 32c96595026c36cbb88a7c57544396b74c9128f0 /sway | |
parent | 89341c0c702573065676eb5cf1ac2fcc6091eacf (diff) | |
parent | 76c520a04b09490591c8ca7f854592f2a7a50042 (diff) |
Merge pull request #352 from progandy/workspace-numbers
Workspace numbers
Diffstat (limited to 'sway')
-rw-r--r-- | sway/commands.c | 15 | ||||
-rw-r--r-- | sway/container.c | 20 | ||||
-rw-r--r-- | sway/workspace.c | 22 |
3 files changed, 52 insertions, 5 deletions
diff --git a/sway/commands.c b/sway/commands.c index b40cdb6a..24d56052 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -649,12 +649,15 @@ static struct cmd_results *cmd_move(int argc, char **argv) { } const char *ws_name = argv[3]; - if (argc == 5) { + swayc_t *ws; + if (argc == 5 && strcasecmp(ws_name, "number") == 0) { // move "container to workspace number x" ws_name = argv[4]; + ws = workspace_by_number(ws_name); + } else { + ws = workspace_by_name(ws_name); } - swayc_t *ws = workspace_by_name(ws_name); if (ws == NULL) { ws = workspace_create(ws_name); } @@ -1435,13 +1438,17 @@ static struct cmd_results *cmd_workspace(int argc, char **argv) { if ((error = checkarg(argc, "workspace", EXPECTED_AT_LEAST, 1))) { return error; } - if (argc == 1) { + if (argc == 1 || (argc == 2 && strcasecmp(argv[0], "number") == 0) ) { if (config->reading || !config->active) { return cmd_results_new(CMD_DEFER, "workspace", NULL); } // Handle workspace next/prev swayc_t *ws = NULL; - if (strcasecmp(argv[0], "next") == 0) { + if (argc == 2) { + if (!(ws=workspace_by_number(argv[1]))) { + ws = workspace_create(argv[1]); + } + }else if (strcasecmp(argv[0], "next") == 0) { ws = workspace_next(); } else if (strcasecmp(argv[0], "prev") == 0) { ws = workspace_prev(); diff --git a/sway/container.c b/sway/container.c index 8165bbad..36056ff7 100644 --- a/sway/container.c +++ b/sway/container.c @@ -1,3 +1,4 @@ +#include <ctype.h> #include <stdlib.h> #include <stdbool.h> #include <strings.h> @@ -168,7 +169,24 @@ swayc_t *new_workspace(swayc_t *output, const char *name) { workspace->visible = false; workspace->floating = create_list(); - add_child(output, workspace); + if (isdigit(workspace->name[0])) { + // find position for numbered workspace + // order: ascending numbers, insert before same number + // numbers before unnumbered + int num = strtol(workspace->name, NULL, 10); + int i; + for (i = 0; i < output->children->length; ++i) { + char *name = ((swayc_t *)output->children->items[i])->name; + if (!isdigit(name[0]) || num <= strtol(name, NULL, 10)) { + break; + } + } + insert_child(output, workspace, i); + + } else { + // append new unnumbered to the end + add_child(output, workspace); + } return workspace; } diff --git a/sway/workspace.c b/sway/workspace.c index 5e6ea799..761a5f4e 100644 --- a/sway/workspace.c +++ b/sway/workspace.c @@ -17,6 +17,11 @@ #include "ipc.h" char *prev_workspace_name = NULL; +struct workspace_by_number_data { + int len; + const char *cset; + const char *name; +}; char *workspace_next_name(void) { sway_log(L_DEBUG, "Workspace: Generating new name"); @@ -133,6 +138,23 @@ swayc_t *workspace_by_name(const char* name) { } } +static bool _workspace_by_number(swayc_t *view, void *data) { + if (view->type != C_WORKSPACE) { + return false; + } + struct workspace_by_number_data *wbnd = data; + int a = strspn(view->name, wbnd->cset); + return a == wbnd->len && strncmp(view->name, wbnd->name, a) == 0; +} +swayc_t *workspace_by_number(const char* name) { + struct workspace_by_number_data wbnd = {0, "1234567890", name}; + wbnd.len = strspn(name, wbnd.cset); + if (wbnd.len <= 0) { + return NULL; + } + return swayc_by_test(&root_container, _workspace_by_number, (void *) &wbnd); +} + /** * Get the previous or next workspace on the specified output. * Wraps around at the end and beginning. |