Skip to content

Commit

Permalink
commands: envelope: better attachment removal
Browse files Browse the repository at this point in the history
This renames the "unattach" command to "detach" and makes it accept a glob
pattern for consistency with "attach".

GitHub: closes #1489
  • Loading branch information
pacien authored and pazz committed May 6, 2020
1 parent 867e30c commit c5cfed5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 26 deletions.
31 changes: 16 additions & 15 deletions alot/commands/envelope.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import datetime
import email
import email.policy
import fnmatch
import glob
import logging
import os
Expand Down Expand Up @@ -37,7 +38,7 @@

@registerCommand(
MODE, 'attach',
arguments=[(['path'], {'help': 'file(s) to attach (accepts wildcads)'})])
arguments=[(['path'], {'help': 'file(s) to attach (accepts wildcards)'})])
class AttachCommand(Command):
"""attach files to the mail"""
repeatable = True
Expand Down Expand Up @@ -65,30 +66,30 @@ def apply(self, ui):
ui.current_buffer.rebuild()


@registerCommand(MODE, 'unattach', arguments=[
(['hint'], {'nargs': '?', 'help': 'which attached file to remove'}),
@registerCommand(MODE, 'detach', arguments=[
(['files'], {
'nargs': '?',
'help': 'name of the attachment to remove (accepts wildcards)'
}),
])
class UnattachCommand(Command):
class DetachCommand(Command):
"""remove attachments from current envelope"""
repeatable = True

def __init__(self, hint=None, **kwargs):
def __init__(self, files=None, **kwargs):
"""
:param hint: which attached file to remove
:type hint: str
:param files: attached file glob to remove
:type files: str
"""
Command.__init__(self, **kwargs)
self.hint = hint
self.files = files or '*'

def apply(self, ui):
envelope = ui.current_buffer.envelope

if self.hint is not None:
for a in envelope.attachments:
if self.hint in a.get_filename():
envelope.attachments.remove(a)
else:
envelope.attachments = []
envelope.attachments = [
attachment for attachment in envelope.attachments
if not fnmatch.fnmatch(attachment.get_filename(), self.files)
]
ui.current_buffer.rebuild()


Expand Down
22 changes: 11 additions & 11 deletions docs/source/usage/modes/envelope.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,17 @@ The following commands are available in envelope mode:
attach files to the mail

argument
file(s) to attach (accepts wildcads)
file(s) to attach (accepts wildcards)


.. _cmd.envelope.detach:

.. describe:: detach

remove attachments from current envelope

argument
name of the attachment to remove (accepts wildcards)


.. _cmd.envelope.display:
Expand Down Expand Up @@ -192,16 +202,6 @@ The following commands are available in envelope mode:
converter command to use


.. _cmd.envelope.unattach:

.. describe:: unattach

remove attachments from current envelope

argument
which attached file to remove


.. _cmd.envelope.unencrypt:

.. describe:: unencrypt
Expand Down

0 comments on commit c5cfed5

Please sign in to comment.