Skip to content

Commit

Permalink
toggletags: refactor and fix behaviour in a thread buffer
Browse files Browse the repository at this point in the history
0cd9806 ("db/manager: Handle tag toggling messages", 2020-05-18) moved
the handling of `toggletags` (i.e. the determination of tags to
removeand add) from the search buffer command to the db manager. It did
not move the corresponding code from the thread buffer command, though.

Refactor the thread buffer command so that it calls the db manager
methods as the search buffer command does. Besides reducing reduncancy,
this also fixes an old issue with `toggletags --all` in a thread buffer:
If this is supposed to do something useful and unsursprising then it
should act the same way as `toggletags` in a search buffer for
`thread:tid`, i.e. toggle a tag based on the presence in the thread (not
individually). The refactoring achieves this as an intended side-effect.
  • Loading branch information
mjg authored and pazz committed Mar 29, 2021
1 parent 8fdd720 commit 1ade6de
Showing 1 changed file with 15 additions and 20 deletions.
35 changes: 15 additions & 20 deletions alot/commands/thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -1147,10 +1147,13 @@ async def apply(self, ui):
tbuffer = ui.current_buffer
if self.all:
messagetrees = tbuffer.messagetrees()
testquery = "thread:%s" % \
tbuffer.get_selected_thread().get_thread_id()
else:
messagetrees = [tbuffer.get_selected_messagetree()]
testquery = "mid:%s" % tbuffer.get_selected_mid()

def refresh_widgets():
def refresh():
for mt in messagetrees:
mt.refresh()

Expand All @@ -1164,26 +1167,18 @@ def refresh_widgets():
tbuffer.refresh()

tags = [t for t in self.tagsstring.split(',') if t]

try:
for mt in messagetrees:
m = mt.get_message()
if self.action == 'add':
m.add_tags(tags, afterwards=refresh_widgets)
if self.action == 'set':
m.add_tags(tags, afterwards=refresh_widgets,
remove_rest=True)
elif self.action == 'remove':
m.remove_tags(tags, afterwards=refresh_widgets)
elif self.action == 'toggle':
to_remove = []
to_add = []
for t in tags:
if t in m.get_tags():
to_remove.append(t)
else:
to_add.append(t)
m.remove_tags(to_remove)
m.add_tags(to_add, afterwards=refresh_widgets)
if self.action == 'add':
ui.dbman.tag(testquery, tags, remove_rest=False,
afterwards=refresh)
if self.action == 'set':
ui.dbman.tag(testquery, tags, remove_rest=True,
afterwards=refresh)
elif self.action == 'remove':
ui.dbman.untag(testquery, tags, afterwards=refresh)
elif self.action == 'toggle':
ui.dbman.toggle_tags(testquery, tags, afterwards=refresh)

except DatabaseROError:
ui.notify('index in read-only mode', priority='error')
Expand Down

0 comments on commit 1ade6de

Please sign in to comment.