From d4765367376c34c8373ee905f70ece3e94c97fe6 Mon Sep 17 00:00:00 2001 From: Gustavo Sousa Date: Tue, 9 May 2023 14:19:11 -0300 Subject: [PATCH] commands/thread: Add option --strip_ansi to pipeto If someone has a mailcap entry that colors the mail content, it is possible that she would want to get the content without the CSI escapes when piping it an external command. Ideally, there should be a way to have the content untouched by the mailcap filter so we could use it when it makes sense. However, let's use ansi.remove_csi() for the time being. --- alot/commands/thread.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/alot/commands/thread.py b/alot/commands/thread.py index ad3839991..d71c3734e 100644 --- a/alot/commands/thread.py +++ b/alot/commands/thread.py @@ -645,6 +645,8 @@ def apply(self, ui): 'help': 'let the shell interpret the command'}), (['--notify_stdout'], {'action': 'store_true', 'help': 'display cmd\'s stdout as notification'}), + (['--strip_ansi'], {'action': 'store_true', + 'help': 'remove ANSI CSI escapes from the content'}), ]) class PipeCommand(Command): @@ -653,7 +655,7 @@ class PipeCommand(Command): def __init__(self, cmd, all=False, separately=False, background=False, shell=False, notify_stdout=False, format='raw', - add_tags=False, noop_msg='no command specified', + add_tags=False, strip_ansi=False, noop_msg='no command specified', confirm_msg='', done_msg=None, **kwargs): """ :param cmd: shellcommand to open @@ -676,6 +678,8 @@ def __init__(self, cmd, all=False, separately=False, background=False, :type format: str :param add_tags: add 'Tags' header to the message :type add_tags: bool + :param strip_ansi: remove ANSI CSI escapes from the content + :type strip_ansi: bool :param noop_msg: error notification to show if `cmd` is empty :type noop_msg: str :param confirm_msg: confirmation question to ask (continues directly if @@ -695,6 +699,7 @@ def __init__(self, cmd, all=False, separately=False, background=False, self.notify_stdout = notify_stdout self.output_format = format self.add_tags = add_tags + self.strip_ansi = strip_ansi self.noop_msg = noop_msg self.confirm_msg = confirm_msg self.done_msg = done_msg @@ -745,6 +750,9 @@ async def apply(self, ui): msgtext = '%s\n\n%s' % (headertext, bodytext) pipestrings.append(msgtext) + if self.strip_ansi: + pipestrings = [ansi.remove_csi(s) for s in pipestrings] + if not self.separately: pipestrings = [separator.join(pipestrings)] if self.shell: