-
Notifications
You must be signed in to change notification settings - Fork 40
/
quickswitch.py
executable file
·290 lines (227 loc) · 9.1 KB
/
quickswitch.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# quickswitch for i3 - quickly change to and locate windows in i3.
#
# Author: slowpoke <mail+python at slowpoke dot io>
# This program is Free Software under the terms of the
#
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
# Version 2, December 2004
#
# Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
#
# Everyone is permitted to copy and distribute verbatim or modified
# copies of this license document, and changing it is allowed as long
# as the name is changed.
#
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
# TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
#
# 0. You just DO WHAT THE FUCK YOU WANT TO.
__version__ = '2.2'
import argparse
import subprocess
import os
import re
try:
import i3
except ImportError:
print("quickswitch requires i3-py.")
print("You can install it from the PyPI with ``pip install i3-py''.")
exit(1)
def check_dmenu():
'''Check if dmenu is available.'''
devnull = open(os.devnull)
retcode = subprocess.call(["which", "dmenu"],
stdout=devnull,
stderr=devnull)
return True if retcode == 0 else False
def dmenu(options, dmenu):
'''Call dmenu with a list of options.'''
cmd = subprocess.Popen(dmenu,
shell=True,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stdout, _ = cmd.communicate('\n'.join(options).encode('utf-8'))
return stdout.decode('utf-8').strip('\n')
def get_windows():
'''Get all windows.'''
windows = i3.filter(nodes=[])
return create_lookup_table(windows)
def find_window_by_regex(regex, move=False):
'''Find the first window whose title matches regex and focus or move it.'''
action = move_window_here if move else focus
cr = re.compile(regex)
for title, id in get_windows().items():
if cr.match(title):
action(id)
return True
return False
def get_scratchpad():
'''Get all windows on the scratchpad.'''
scratchpad = i3.filter(name="__i3_scratch")[0]
nodes = scratchpad["floating_nodes"]
windows = i3.filter(tree=nodes, nodes=[])
return create_lookup_table(windows)
def get_workspaces():
'''Returns all workspace names.
NOTE: This returns a map of name → name, which is rather redundant, but
makes it possible to use the result without changing much in main().
'''
workspaces = i3.get_workspaces()
for ws in workspaces:
# create_lookup_table will set the value of all entries in the lookup table
# to the window id. We act as if the workspace name is the window id.
ws['window'] = ws['name']
return create_lookup_table(workspaces)
def next_empty():
'''Return the lowest numbered workspace that is empty.'''
workspaces = sorted([int(ws) for ws in get_workspaces().keys()
if ws.isdecimal()])
for i in range(len(workspaces)):
if workspaces[i] != i + 1:
return str(i + 1)
return str(len(workspaces) + 1)
def next_used(number):
'''Return the next used numbered workspace after the given number.'''
workspaces = sorted([int(ws) for ws in get_workspaces().keys()
if ws.isdecimal()
and int(ws) > number])
return workspaces[0] if workspaces else None
def degap():
'''Remove 'gaps' in the numbered workspaces.
This searches for non-consecutive numbers in the workspace list, and moves
used workspaces as far to the left as possible.
'''
i = 0
while True:
ws = next_used(i)
if ws is None:
break
elif ws - i > 1:
rename_workspace(ws, i + 1)
i += 1
def create_lookup_table(windows):
'''Create a lookup table from the given list of windows.
The returned dict is in the format window title → X window id.
'''
rename_nonunique(windows)
lookup = {}
for window in windows:
name = window.get('name')
id_ = window.get('window')
if id_ is None:
# this is not an X window, ignore it.
continue
if name.startswith("i3bar for output"):
# this is an i3bar, ignore it.
continue
lookup[name] = id_
return lookup
def rename_nonunique(windows):
'''Rename all windows which share a name by appending an index.'''
window_names = [window.get('name') for window in windows]
for name in window_names:
count = window_names.count(name)
if count > 1:
for i in range(count):
index = window_names.index(name)
window_names[index] = "{} [{}]".format(name, i + 1)
for i in range(len(windows)):
windows[i]['name'] = window_names[i]
def get_scratchpad_window(window):
'''Does `scratchpad show` on the specified window.'''
return i3.scratchpad("show", id=window)
def move_window_here(window):
'''Does `move workspace current` on the specified window.'''
return i3.msg(0, "{} move workspace current".format(
i3.container(id=window)))
def rename_workspace(old, new):
'''Rename a given workspace.'''
return i3.msg(0, "rename workspace {} to {}".format(old, new))
def focus(window):
'''Focuses the given window.'''
return i3.focus(id=window)
def goto_workspace(name):
'''Jump to the given workspace.'''
return i3.workspace(name)
def get_current_workspace():
'''Get the name of the currently active workspace.'''
filtered = [ws for ws in i3.get_workspaces() if ws["focused"] is True]
return filtered[0]['name'] if filtered else None
def cycle_numbered_workspaces(backwards=False):
'''Get the next (previous) numbered workspace.'''
current = get_current_workspace()
if not current.isdecimal():
return None
i = int(current)
return str(i + 1) if not backwards else str(i - 1)
def main():
parser = argparse.ArgumentParser(description='''quickswitch for i3''')
parser.add_argument('-m', '--move', default=False, action="store_true",
help="move window to the current workspace")
mutgrp = parser.add_mutually_exclusive_group()
mutgrp.add_argument('-s', '--scratchpad', default=False, action="store_true",
help="list scratchpad windows instead of regular ones")
mutgrp.add_argument('-w', '--workspaces', default=False,
action="store_true",
help="list workspaces instead of windows")
mutgrp.add_argument('-e', '--empty', default=False, action='store_true',
help='go to the next empty, numbered workspace')
mutgrp.add_argument('-r', '--regex',
help='find the first window matching the regex and focus/move it')
mutgrp.add_argument('-g', '--degap', action='store_true',
help='make numbered workspaces consecutive (remove gaps)')
mutgrp.add_argument('-n', '--next', default=False, action='store_true',
help='go to the next (numbered) workspace')
mutgrp.add_argument('-p', '--previous', default=False, action='store_true',
help='go to the previous (numbered) workspace')
parser.add_argument('-d', '--dmenu', default='dmenu -b -i -l 20', help='dmenu command, executed within a shell')
args = parser.parse_args()
if not check_dmenu():
print("quickswitch requires dmenu.")
print("Please install it using your distribution's package manager.")
exit(1)
# jumping to the next empty workspaces doesn't require going through all
# the stuff below, as we don't need to call dmenu etc, so we just call it
# here and exit if the appropriate flag was given.
if args.empty:
exit(*goto_workspace(next_empty()))
# likewise for degapping...
if args.degap:
degap()
exit(0)
# ...and regex search...
if args.regex:
exit(0 if find_window_by_regex(args.regex, args.move) else 1)
# ...as well as workspace cycling
if args.next or args.previous:
if not get_current_workspace().isdecimal:
print("--next and --previous only work on numbered workspaces")
exit(1)
target_ws = cycle_numbered_workspaces(args.previous)
if not args.move:
exit(*goto_workspace(target_ws))
else:
exit(*i3.command("move container to workspace {}".format(target_ws)))
lookup_func = get_windows
if args.scratchpad:
lookup_func = get_scratchpad
if args.workspaces:
lookup_func = get_workspaces
action_func = focus
if args.move:
action_func = move_window_here
else:
if args.scratchpad:
action_func = get_scratchpad_window
if args.workspaces:
action_func = goto_workspace
lookup = lookup_func()
target = dmenu(lookup.keys(), args.dmenu)
id_ = lookup.get(target)
success = action_func(lookup.get(target)) if id_ is not None else False
exit(0 if success else 1)
if __name__ == '__main__':
main()