#!/usr/bin/php name; } sort($ordered); foreach ($ordered as $k => $v) { foreach ($workspaces as $x => $w) { if ($w->name == $v) { $ordered[$k] = $w; unset($workspaces[$x]); } } } return $ordered; } function getMoveOption() { global $argv; if (!isset($argv[1])) { $argv[1] = 'next'; } return in_array($argv[1], ['next', 'previous']) ? $argv[1] : 'next'; } function getNextWorkspace($workspaces) { $key = 0; foreach ($workspaces as $k => $v) { if ($v->focused) { $key = $k; } } return $workspaces[($key + 1) % count($workspaces)]; } function getPreviousWorkspace($workspaces) { $count = count($workspaces); $key = 0; foreach ($workspaces as $k => $v) { if ($v->focused) { $key = $k; } } return $workspaces[--$key >= 0 ? $key : $count - 1]; } function moveToWorkspace($workspace) { shell_exec(sprintf("i3-msg 'workspace \"%s\"'", $workspace->name)); } $workspaces = getOrderedWorkspaces(); moveToWorkspace('next' === getMoveOption() ? getNextWorkspace($workspaces) : getPreviousWorkspace($workspaces));