#!/usr/bin/python # -*- coding: utf-8 -*- # Copyright (C) René 'Necoro' Neumann # # Some helper functions for managing workspaces in i3. # import sys from os.path import realpath, dirname, join cwd = realpath(dirname(__file__)) sys.path.insert(1, join(cwd, "libs")) import i3 import sh from functools import partial DEFAULT = "switch" switch = i3.workspace move = partial(i3.move, "container to workspace") def new_ws(cmd): """Create a new workspace by using the first free number > 0.""" nums = (w["num"] for w in i3.get_workspaces()) nums = filter(lambda n: n is not None and n >= 0, nums) for i,n in enumerate(sorted(nums)): if i != n: cmd(str(i)) break else: cmd(str(i+1)) def to_ws(cmd, prompt): """Use `dmenu` to switch or move to a workspace.""" ws = sorted(w["name"] for w in i3.get_workspaces()) try: sel = (sh.dmenu("-b", _in = "\n".join(ws), p=prompt).strip() or None) except sh.ErrorReturnCode: sel = None if sel is not None: cmd(sel) if __name__ == "__main__": try: arg = sys.argv[1] except IndexError: arg = DEFAULT if arg == "switch": to_ws(switch, "Switch to:") elif arg == "move": to_ws(move, "Move to:") elif arg == "new": new_ws(switch) elif arg == "move_new": new_ws(move) else: print("Unknown arg: %s" % arg) sys.exit(1)