My i3 configurations and scripts.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

i3_switch_workspace.php 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #!/usr/bin/php5
  2. <?php
  3. function getOrderedWorkspaces()
  4. {
  5. $json = shell_exec('i3-msg -t get_workspaces');
  6. $workspaces = json_decode($json);
  7. $ordered = array();
  8. foreach ($workspaces as $w) {
  9. $ordered[] = $w->name;
  10. }
  11. sort($ordered);
  12. foreach ($ordered as $k => $v) {
  13. foreach ($workspaces as $x => $w) {
  14. if ($w->name == $v) {
  15. $ordered[$k] = $w;
  16. unset($workspaces[$x]);
  17. }
  18. }
  19. }
  20. return $ordered;
  21. }
  22. function getMoveOption()
  23. {
  24. global $argv;
  25. if (!isset($argv[1])) {
  26. $argv[1] = 'next';
  27. }
  28. return in_array($argv[1], array('next', 'previous')) ? $argv[1] : 'next';
  29. }
  30. function getNextWorkspace($workspaces)
  31. {
  32. $key = 0;
  33. foreach ($workspaces as $k => $v) {
  34. if ($v->focused) {
  35. $key = $k;
  36. }
  37. }
  38. return $workspaces[($key + 1) % count($workspaces)];
  39. }
  40. function getPreviousWorkspace($workspaces)
  41. {
  42. $count = count($workspaces);
  43. $key = 0;
  44. foreach ($workspaces as $k => $v) {
  45. if ($v->focused) {
  46. $key = $k;
  47. }
  48. }
  49. return $workspaces[ --$key >= 0 ? $key : $count - 1 ];
  50. }
  51. function moveToWorkspace($workspace)
  52. {
  53. shell_exec(sprintf("i3-msg 'workspace \"%s\"'", $workspace->name));
  54. }
  55. $workspaces = getOrderedWorkspaces();
  56. moveToWorkspace(getMoveOption() === 'next' ? getNextWorkspace($workspaces) : getPreviousWorkspace($workspaces));