From c186d79b39abe91439542a822bdb48ad39e45a0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20=27Necoro=27=20Neumann?= Date: Fri, 7 Jun 2013 12:51:19 +0200 Subject: i3: Overhaul workspaces.py --- .i3/scripts/workspaces.py | 46 +++++++++++++++++++--------------------------- 1 file changed, 19 insertions(+), 27 deletions(-) (limited to '.i3') diff --git a/.i3/scripts/workspaces.py b/.i3/scripts/workspaces.py index 4c0ec87..44de01f 100755 --- a/.i3/scripts/workspaces.py +++ b/.i3/scripts/workspaces.py @@ -15,46 +15,36 @@ sys.path.insert(1, join(cwd, "libs")) import i3 import sh +from functools import partial DEFAULT = "switch" -def ws_dmenu(**kw): - """Call `dmenu` with the names of the current workspaces. - Arguments are directly passed to `dmenu`. +switch = i3.workspace +move = partial(i3.move, "container to workspace") - Returns the stripped output of dmenu or `None`.""" - - ws = sorted(w["name"] for w in i3.get_workspaces()) - - try: - return sh.dmenu("-b", _in = "\n".join(ws), **kw).strip() or None - except sh.ErrorReturnCode: - return None - -def new_ws(): +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: - i3.workspace(str(i)) + cmd(str(i)) break else: - i3.workspace(str(i+1)) + 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()) -def switch_ws(): - """Use `dmenu` to switch to a workspace.""" - sel = ws_dmenu(p="Switch to:") - if sel is not None: - i3.workspace(sel) + try: + sel = (sh.dmenu("-b", _in = "\n".join(ws), p=prompt).strip() or None) + except sh.ErrorReturnCode: + sel = None -def move_to_ws(): - """Use `dmenu` to move the current container to a workspace.""" - sel = ws_dmenu(p="Move to:") if sel is not None: - i3.move("container to workspace", sel) + cmd(sel) if __name__ == "__main__": try: @@ -63,11 +53,13 @@ if __name__ == "__main__": arg = DEFAULT if arg == "switch": - switch_ws() + to_ws(switch, "Switch to:") elif arg == "move": - move_to_ws() + to_ws(move, "Move to:") elif arg == "new": - new_ws() + new_ws(switch) + elif arg == "move_new": + new_ws(move) else: print("Unknown arg: %s" % arg) sys.exit(1) -- cgit v1.2.3