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
|
# Copyright (C) 2011-2018 Patrick Totzke <patricktotzke@gmail.com>
# This file is released under the GNU GPL, version 3 or a later revision.
# For further details see the COPYING file
import urwid
from notmuch2 import NotmuchError
from .buffer import Buffer
from ..settings.const import settings
from ..widgets.search import ThreadlineWidget
class IterWalker(urwid.ListWalker):
"""
urwid.ListWalker that reads next items from a generator and wraps them in
ThreadlineWidget widgets for displaying
"""
_pipe = None
_dbman = None
_iter_done = False
# list of the thread IDs
_tids = None
# a dictionary of threadline widgets, indexed by position
_wgts = None
_focus = None
def __init__(self, threads, dbman):
self._threads = threads
self._dbman = dbman
self._tids = []
self._wgts = {}
self._focus = 0
super().__init__()
def __len__(self):
while not self._iter_done:
self._get_next_item()
return len(self._tids)
def __getitem__(self, pos):
self._check_pos(pos)
if not pos in self._wgts:
# make sure an exception while constructing the widget does not get
# swallowed by urwid
try:
self._wgts[pos] = ThreadlineWidget(self._tids[pos], self._dbman)
except (KeyError, IndexError, TypeError) as e:
raise ValueError('Exception while constructing threadline widget') from e
return self._wgts[pos]
def _check_pos(self, pos):
if pos < 0:
raise IndexError
while not self._iter_done and pos >= len(self._tids):
self._get_next_item()
if pos >= len(self._tids):
raise IndexError
return pos
def next_position(self, pos):
return self._check_pos(pos + 1)
def prev_position(self, pos):
return self._check_pos(pos - 1)
@property
def focus(self):
return self._focus
def set_focus(self, pos):
self._check_pos(pos)
self._focus = pos
self._modified()
def _get_next_item(self):
if self._iter_done:
return None
try:
self._tids.append(next(self._threads))
except StopIteration:
self._iter_done = True
class SearchBuffer(Buffer):
"""shows a result list of threads for a query"""
modename = 'search'
_result_count_val = None
_thread_count_val = None
def __init__(self, ui, initialquery='', sort_order=None):
self.dbman = ui.dbman
self.ui = ui
self.querystring = initialquery
default_order = settings.get('search_threads_sort_order')
self.sort_order = sort_order or default_order
self.rebuild()
super().__init__()
def __str__(self):
formatstring = '[search] for "%s" (%d message%s in %d thread%s)'
return formatstring % (self.querystring,
self._result_count, 's' if self._result_count > 1 else '',
self._thread_count, 's' if self._thread_count > 1 else '')
@property
def _result_count(self):
if self._result_count_val is None:
self._result_count_val = self.dbman.count_messages(self.querystring)
return self._result_count_val
@property
def _thread_count(self):
if self._thread_count_val is None:
self._thread_count_val = self.dbman.count_threads(self.querystring)
return self._thread_count_val
def get_info(self):
info = {}
info['querystring'] = self.querystring
info['result_count'] = self._result_count
info['thread_count'] = self._thread_count
info['result_count_positive'] = 's' if info['result_count'] > 1 else ''
return info
def rebuild(self):
exclude_tags = settings.get_notmuch_setting('search', 'exclude_tags')
if exclude_tags:
exclude_tags = frozenset([t for t in exclude_tags.split(';') if t])
else:
exclude_tags = frozenset()
self._result_count_val = None
self._thread_count_val = None
try:
threads = self.dbman.get_threads(self.querystring, self.sort_order,
exclude_tags)
except NotmuchError:
self.ui.notify('malformed query string: %s' % self.querystring,
'error')
self.listbox = urwid.ListBox([])
self.body = self.listbox
return
self.threadlist = IterWalker(threads, self.dbman)
self.listbox = urwid.ListBox(self.threadlist)
self.body = self.listbox
def get_selected_threadline(self):
"""
returns curently focussed :class:`alot.widgets.ThreadlineWidget`
from the result list.
"""
return self.threadlist[self.threadlist.focus]
def get_selected_thread(self):
"""returns currently selected :class:`~alot.db.Thread`"""
threadlinewidget = self.get_selected_threadline()
thread = None
if threadlinewidget:
thread = threadlinewidget.get_thread()
return thread
def focus_first(self):
self.body.set_focus(0)
def focus_last(self):
self.body.set_focus(len(self.threadlist) - 1)
|