summaryrefslogtreecommitdiff
path: root/alot/widgets
diff options
context:
space:
mode:
authorAnton Khirnov <anton@khirnov.net>2021-01-16 09:09:16 +0100
committerAnton Khirnov <anton@khirnov.net>2021-01-16 09:09:16 +0100
commit55171ecf0bd62c3b83aea85ac3c7e1d4967ebda4 (patch)
tree663e44c786a527fcc80bf4d982b3c2695defc044 /alot/widgets
parent6f88bda385a0cc2d10b19fde5eefdd9265319c8d (diff)
widgets/thread: color git-send-email patches with pygments
Diffstat (limited to 'alot/widgets')
-rw-r--r--alot/widgets/thread.py37
1 files changed, 33 insertions, 4 deletions
diff --git a/alot/widgets/thread.py b/alot/widgets/thread.py
index 68e0ff34..455815f4 100644
--- a/alot/widgets/thread.py
+++ b/alot/widgets/thread.py
@@ -8,9 +8,16 @@ import logging
import re
import urwid
+try:
+ import pygments.lexers, pygments.formatters, pygments.util
+ have_pygments = True
+except ImportError:
+ have_pygments = False
+
from .globals import TagWidget
from .globals import AttachmentWidget
from ..settings.const import settings
+from ..utils import ansi_term
class MessageSummaryWidget(urwid.WidgetWrap):
"""
@@ -244,14 +251,36 @@ class _TextPart(_MIMEPartWidget):
_lines = None
_fold = None
- def __init__(self, text):
+ def __init__(self, text, part):
attr_text = settings.get_theming_attribute('thread', 'body')
attrs_quote = settings.get_quote_theming()
self._fold_context = settings.get('thread_fold_context')
- self._lines, self._fold, self._max_level = \
- self._parse_quotes(text, attr_text, attrs_quote)
+ # color git patches using pygments
+ # FIXME this is quite ad-hoc
+ # make it more configurable, detect other patch formats
+ # and possibly other kinds of text
+ if ('x-mailer' in part.headers and
+ part.headers['x-mailer'][0].startswith('git-send-email')):
+ # process as patch
+ if have_pygments:
+ try:
+ lexer = pygments.lexers.get_lexer_by_name('diff')
+ formatter = pygments.formatters.get_formatter_by_name('terminal')
+ text = pygments.highlight(text, lexer, formatter)
+ except pygments.util.ClassNotFound:
+ logging.debug('Could not get a lexer/formatter for diff highlighting')
+
+ self._lines = ansi_term.parse_escapes_to_urwid(text, attr_text)
+ self._fold = _Fold(0, 0, len(self._lines))
+ self._max_level = 0
+
+ if self._lines is None:
+ # process as a normal text mail body:
+ # detect quoted blocks
+ self._lines, self._fold, self._max_level = \
+ self._parse_quotes(text, attr_text, attrs_quote)
super().__init__(urwid.Text(''))
@@ -396,7 +425,7 @@ def _render_mime_tree(mime_tree, alternative_pref):
# try rendering the message
text = mime_tree.render_str()
if text is not None:
- return _TextPart(text) if len(text) else _EmptyPartWidget()
+ return _TextPart(text, mime_tree) if len(text) else _EmptyPartWidget()
body_wgt = urwid.Text('Undisplayable "%s" MIME part.' % mime_tree.content_type,
align = 'center')