From 752860cf24fc22208d7e9138d0a13ea5d616b4ca Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Wed, 10 Feb 2021 10:52:50 +0100 Subject: widgets/search: display the number of matching messages in the thread --- alot/buffers/search.py | 3 ++- alot/db/thread.py | 11 +++++++++++ alot/widgets/search.py | 24 +++++++++++++----------- 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/alot/buffers/search.py b/alot/buffers/search.py index 700ce69f..a17e6db3 100644 --- a/alot/buffers/search.py +++ b/alot/buffers/search.py @@ -167,7 +167,8 @@ class SearchBuffer(Buffer): threads = self.dbman.get_threads(self.querystring, self.sort_order, exclude_tags) - wgt_factory = partial(ThreadlineWidget, dbman = self.dbman) + wgt_factory = partial(ThreadlineWidget, dbman = self.dbman, + query = self.querystring) threadlist = IterWalker(threads, wgt_factory) # check that the query is well-formed diff --git a/alot/db/thread.py b/alot/db/thread.py index 13ddabda..b6634b57 100644 --- a/alot/db/thread.py +++ b/alot/db/thread.py @@ -214,3 +214,14 @@ class Thread: thread_query = 'thread:%s AND (%s)' % (self.id, query) num_matches = self._dbman.count_messages(thread_query) return num_matches > 0 + + def count_matches(self, query): + """ + Count the number of messags in this thread that match the given notmuch + query. + + :param query: The query to check against + :type query: string + """ + thread_query = 'thread:%s AND (%s)' % (self.id, query) + return self._dbman.count_messages(thread_query) diff --git a/alot/widgets/search.py b/alot/widgets/search.py index dda171ac..a921146a 100644 --- a/alot/widgets/search.py +++ b/alot/widgets/search.py @@ -21,13 +21,15 @@ class ThreadlineWidget(urwid.WidgetPlaceholder): tid = None _dbman = None + _query = None _thread = None _widgets = None - def __init__(self, tid, dbman): + def __init__(self, tid, dbman, query): self.tid = tid self._dbman = dbman + self._query = query super().__init__(urwid.Text('')) @@ -63,7 +65,7 @@ class ThreadlineWidget(urwid.WidgetPlaceholder): widgets.append(w) else: width, part = build_text_part(partname, thread, - structure[partname]) + structure[partname], self._query) add_column(width, part) widgets.append(part) @@ -120,7 +122,7 @@ def build_tags_part(tags, attr_normal, attr_focus): return width, part_w -def build_text_part(name, thread, struct): +def build_text_part(name, thread, struct, query): """ create an urwid.Text widget (wrapped in approproate Attributes) to display a plain text parts in a threadline. @@ -149,7 +151,7 @@ def build_text_part(name, thread, struct): if width_tuple[0] == 'fit': minw, maxw = width_tuple[1:] - content = prepare_string(name, thread, maxw) + content = prepare_string(name, thread, query, maxw) # pad content if not long enough if minw: @@ -169,26 +171,26 @@ def build_text_part(name, thread, struct): return width, part_w -def prepare_date_string(thread): +def prepare_date_string(thread, query): newest = thread.newest_date if newest is not None: datestring = settings.represent_datetime(newest) return datestring -def prepare_mailcount_string(thread): - return "(%d)" % thread.total_messages +def prepare_mailcount_string(thread, query): + return "(%d/%d)" % (thread.count_matches(query), thread.total_messages) -def prepare_authors_string(thread): +def prepare_authors_string(thread, query): return thread.get_authors_string() or '(None)' -def prepare_subject_string(thread): +def prepare_subject_string(thread, query): return thread.subject or ' ' -def prepare_string(partname, thread, maxw): +def prepare_string(partname, thread, query, maxw): """ extract a content string for part 'partname' from 'thread' of maximal length 'maxw'. @@ -207,7 +209,7 @@ def prepare_string(partname, thread, maxw): content, shortener = prep[partname] # get string - s = content(thread) + s = content(thread, query) # sanitize s = s.replace('\n', ' ') -- cgit v1.2.3