diff options
author | René 'Necoro' Neumann <necoro@necoro.net> | 2013-06-06 17:35:45 +0200 |
---|---|---|
committer | René 'Necoro' Neumann <necoro@necoro.net> | 2013-06-06 17:35:45 +0200 |
commit | 93ca7a4258e83996c0075ff5976658a12ffb6e02 (patch) | |
tree | 393833395c7c3237cd162fbbbad48c8cf5b5bc49 /.i3/scripts/workspaces.py | |
parent | 346139a617b044329efcc0a17fdbdf11cf31971e (diff) | |
download | dotfiles-93ca7a4258e83996c0075ff5976658a12ffb6e02.tar.gz dotfiles-93ca7a4258e83996c0075ff5976658a12ffb6e02.tar.bz2 dotfiles-93ca7a4258e83996c0075ff5976658a12ffb6e02.zip |
i3: unify scripts into one
Diffstat (limited to '')
-rwxr-xr-x | .i3/scripts/workspaces.py | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/.i3/scripts/workspaces.py b/.i3/scripts/workspaces.py new file mode 100755 index 0000000..4c0ec87 --- /dev/null +++ b/.i3/scripts/workspaces.py @@ -0,0 +1,74 @@ +#!/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 + +DEFAULT = "switch" + +def ws_dmenu(**kw): + """Call `dmenu` with the names of the current workspaces. + Arguments are directly passed to `dmenu`. + + 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(): + """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)) + break + else: + i3.workspace(str(i+1)) + + +def switch_ws(): + """Use `dmenu` to switch to a workspace.""" + sel = ws_dmenu(p="Switch to:") + if sel is not None: + i3.workspace(sel) + +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) + +if __name__ == "__main__": + try: + arg = sys.argv[1] + except IndexError: + arg = DEFAULT + + if arg == "switch": + switch_ws() + elif arg == "move": + move_to_ws() + elif arg == "new": + new_ws() + else: + print("Unknown arg: %s" % arg) + sys.exit(1) + |