Repository navigation
Expand file tree
/
Copy path__init__.py
More file actions
82 lines (65 loc) · 3.23 KB
/
Copy path__init__.py
File metadata and controls
82 lines (65 loc) · 3.23 KB
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
# -*- coding: utf-8 -*-
import subprocess
from collections import namedtuple
from albert import *
md_iid = "5.0"
md_version = "0.7.2"
md_name = "X Window Switcher"
md_description = "Switch X11 Windows"
md_license = "MIT"
md_url = "https://github.com/albertlauncher/albert-plugin-python-x-window-switcher"
md_bin_dependencies = ["wmctrl"]
md_authors = ["@edjoperez", "@ManuelSchneid3r", "@dshoreman", "@nopsqi"]
md_maintainers = ["@ronskons"]
Window = namedtuple("Window", ["wid", "desktop", "wm_class", "host", "wm_name"])
class Plugin(PluginInstance, RankedQueryHandler):
def __init__(self):
PluginInstance.__init__(self)
RankedQueryHandler.__init__(self)
# Check for X session and wmctrl availability
try:
subprocess.check_call(["wmctrl", "-m"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
except FileNotFoundError:
raise Exception("wmctrl not found. Please install wmctrl.")
except subprocess.CalledProcessError:
raise Exception("Unable to communicate with X11 window manager. This plugin requires a running X session.")
def defaultTrigger(self):
return 'w '
def rankItems(self, ctx):
rank_items = []
try:
for line in subprocess.check_output(['wmctrl', '-l', '-x']).splitlines():
win = Window(*parseWindow(line))
if win.desktop == "-1":
continue
win_instance, win_class = win.wm_class.replace(' ', '-').split('.', 1)
matcher = Matcher(ctx.query)
if m := matcher.match(win_instance + ' ' + win_class + ' ' + win.wm_name):
rank_items.append(
RankItem(
StandardItem(
id="%s%s" % (md_name, win.wm_class),
icon_factory=lambda w_inst=win_instance: Icon.theme(w_inst),
text="%s - Desktop %s" % (win_class.replace('-', ' '), win.desktop),
subtext=win.wm_name,
actions=[Action("switch",
"Switch Window",
lambda w=win: runDetachedProcess(["wmctrl", '-i', '-a', w.wid])),
Action("move",
"Move window to this desktop",
lambda w=win: runDetachedProcess(["wmctrl", '-i', '-R', w.wid])),
Action("close",
"Close the window gracefully.",
lambda w=win: runDetachedProcess(["wmctrl", '-c', w.wid]))]
),
m
)
)
except subprocess.CalledProcessError as e:
warning(f"Error executing wmctrl: {str(e)}")
return rank_items
def parseWindow(line):
win_id, desktop, rest = line.decode().split(None, 2)
win_class, rest = rest.split(' ', 1)
host, title = rest.strip().split(None, 1)
return [win_id, desktop, win_class, host, title]