sway: enable workspace selection by number

This commit is contained in:
progandy 2015-12-17 22:28:41 +01:00
parent 89341c0c70
commit 8f1ac1ef2c
3 changed files with 34 additions and 4 deletions

View file

@ -10,6 +10,7 @@ extern char *prev_workspace_name;
char *workspace_next_name(void);
swayc_t *workspace_create(const char*);
swayc_t *workspace_by_name(const char*);
swayc_t *workspace_by_number(const char*);
bool workspace_switch(swayc_t*);
swayc_t *workspace_output_next();
swayc_t *workspace_next();

View file

@ -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();

View file

@ -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.