forked from Answeror/lit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgo.py
More file actions
192 lines (154 loc) · 5.32 KB
/
go.py
File metadata and controls
192 lines (154 loc) · 5.32 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
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import windows as winutils
from datetime import datetime
from utils import Query
from PyQt4.QtCore import (
Qt,
QAbstractListModel,
QMutex,
QMutexLocker
)
import itertools
import logging
from lcs import lcs
NAME_LIMIT = 42
class Task(object):
def __init__(self, hwnd, query, usetime):
self.hwnd = hwnd
self.query = query
self.usetime = usetime
def use(self):
self.usetime = datetime.now()
@property
def digest(self):
if len(self.name) > NAME_LIMIT:
shortname = self.name[:NAME_LIMIT - 3] + '...'
else:
shortname = self.name
if self.filename:
return '%s (%s)' % (shortname, self.filename)
else:
return shortname
@property
def title(self):
return self.name
@property
def fullname(self):
if self.filename:
return self.title + self.filename
else:
return self.title
@property
def filename(self):
if not hasattr(self, '_filename'):
self._filename = winutils.get_app_name(self.hwnd)
return self._filename
@property
def name(self):
return winutils.window_title(self.hwnd)
@property
def icon(self):
if not hasattr(self, '_icon'):
self._icon = winutils.get_window_icon(self.hwnd)
return self._icon
class WindowModel(QAbstractListModel):
NAME_ROLE = Qt.DisplayRole
HWND_ROLE = Qt.UserRole
def __init__(self, items):
self.super.__init__()
self.items = items
@property
def super(self):
return super(WindowModel, self)
def rowCount(self, parent):
return len(self.items)
def columnCount(self, parent):
return 1
def data(self, index, role):
if not index.isValid():
return None
if role == Qt.TextAlignmentRole:
return int(Qt.AlignLeft | Qt.AlignVCenter)
elif role == Qt.DisplayRole:
return self.items[index.row()].digest
elif role == Qt.DecorationRole:
return self.items[index.row()].icon
elif role == Qt.UserRole:
return self.items[index.row()].hwnd
else:
return None
class Go(object):
def __init__(self, worker, client):
self.tasks = {}
self.mutex = QMutex()
self.worker = worker
self.client = client
@property
def name(self):
return 'g'
def lit(self, query, upper_bound, finished, *args, **kargs):
self.worker.do(
make=lambda: WindowModel(
self.sorted_active_runnable(
query,
winutils.top_level_windows()
)[:upper_bound]
),
catch=finished,
main=True
)
def sorted_active_runnable(self, query, hwnds):
with QMutexLocker(self.mutex):
# update query and collect active ones
self._refresh_tasks(hwnds, query)
active_tasks = [self.tasks[h] for h in hwnds]
# sort by last use
if not query:
return sorted(active_tasks, key=lambda t: t.usetime, reverse=True)
titles = [task.fullname.lower() for task in active_tasks]
def f(task, title):
return task.query.distance_to(title)
ds = [f(task, title) * (10 ** len(query)) for task, title in zip(active_tasks, titles)]
best = ds[0]
for i in itertools.takewhile(lambda i: ds[i] == best, range(len(ds))):
ds[i] -= len(lcs(query, titles[i]))
#return sorted(active_tasks, key=f)
return [task for i, task in sorted(enumerate(active_tasks), key=lambda i: ds[i[0]])]
def _refresh_tasks(self, hwnds, query=None):
for hwnd in hwnds:
if not hwnd in self.tasks:
self.tasks[hwnd] = Task(
hwnd=hwnd,
usetime=datetime.now(),
query=Query(
text='' if query is None else query,
insertion_cost=1,
first_insertion_cost=50,
prepend_first_insertion_cost=5,
append_first_insertion_cost=10,
deletion_cost=100,
substitution_cost=100,
transposition_cost=10
)
)
elif not query is None:
self.tasks[hwnd].query.update(query.lower())
def update_usetime(self, hwnd):
"""Update with one time delay."""
if hasattr(self, 'after_select') and self.after_select:
self.after_select()
self.after_select = self.tasks[hwnd].use
def select(self, content, index):
# check content type
if not isinstance(content, WindowModel):
logging.info('wrong content type {}'.format(type(content)))
return
for hwnd in winutils.top_level_windows():
if content.data(index, WindowModel.HWND_ROLE) == hwnd:
self._refresh_tasks([hwnd])
self.client.goto(hwnd=hwnd)
self.update_usetime(hwnd)
return
# remove invalid tasks
del self.tasks[content.data(index, WindowModel.HWND_ROLE)]