-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwinmenu.py
62 lines (57 loc) · 2.03 KB
/
winmenu.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python
# dmenu script to jump to windows in i3.
#
# using ziberna's i3-py library: https://github.com/ziberna/i3-py
# depends: dmenu (vertical patch), i3.
# released by joepd under WTFPLv2-license:
# http://sam.zoy.org/wtfpl/COPYING
#
# edited by Jure Ziberna for i3-py's examples section
#
# Trying to mimic ruby-wmii script behauvior
# I used this on my config as follows:
#
# bindsym $mod+Shift+K exec python ~/.i3/scripts/winmenu.py
import i3
import subprocess
def i3clients():
"""
Returns a dictionary of key-value pairs of a window text and window id.
Each window text is of format "[workspace] window title (instance number)"
"""
clients = {}
for ws_num in range(1,11):
workspace = i3.filter(num=ws_num)
if not workspace:
continue
workspace = workspace[0]
windows = i3.filter(workspace, nodes=[])
instances = {}
# Adds windows and their ids to the clients dictionary
for window in windows:
win_str = '[%s] %s' % (workspace['name'], window['name'])
# Appends an instance number if other instances are present
if win_str in instances:
instances[win_str] += 1
win_str = '%s (%d)' % (win_str, instances[win_str])
else:
instances[win_str] = 1
clients[win_str] = window['id']
return clients
def win_menu(clients, l=10):
"""
Displays a window menu using dmenu. Returns window id.
"""
#dmenu = subprocess.Popen(['/usr/bin/dmenu','-i','-l', str(l)],
dmenu = subprocess.Popen(['/usr/bin/dmenu','-i','-b'],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
menu_str = '\n'.join(sorted(clients.keys()))
# Popen.communicate returns a tuple stdout, stderr
win_str = dmenu.communicate(menu_str.encode('utf-8'))[0].decode('utf-8').rstrip()
return clients.get(win_str, None)
if __name__ == '__main__':
clients = i3clients()
win_id = win_menu(clients)
if win_id:
i3.focus(con_id=win_id)