Space Panda

🙄 🤦 🙀

I like unicode. Emojis? Well. I guess they are not going away any time soon. As it happens they do not work great in the command line unless you have a font that includes all of them, or at least most.

My font does not, so here is my filter setup to deal with people that write emails that include emojis.

This tells (neo)mutt to refer to the mailcap file when displaying the text/plain part of an email and where to find the mailcap file:

# ~/.config/neomutt/neomuttrc

auto_view text/plain

set mailcap_path = "~/.config/neomutt/mailcap"

And here is how the mailcap filters plain text through the emoji filter:

# ~/.config/neomutt/mailcap

text/plain; ~/bin/emojifilter.py; copiousoutput;

copiousoutput informs (neo)mutt to stream that part of the mail into the script and display the output.

Finally, here is the filter script:

#!/usr/bin/env python3
# ~/bin/emojifilter.py

import unicodedata as ud
import sys


EMOJI_RANGE = set([chr(i) for i in range(0x1f000, 0x1f9ff)
                   if ud.name(chr(i), None) is not None])
EMOJI_REPLACEMENT_TABLE = {
    '🤦': 'm(',
    '♥': '<3',
}


def replace_emojis(text, table=None, template=':{}:', replace=None):
    if table is None:
        table = EMOJI_REPLACEMENT_TABLE
    if replace is None:
        replace = EMOJI_RANGE

    for emoji in replace:
        if emoji in table:
            substitution = table[emoji]
        else:
            substitution = template.format(ud.name(emoji).lower())
        text = text.replace(emoji, substitution)
    return text


if __name__ == '__main__':
    chunk = sys.stdin.buffer.read()
    encoding = None

    for enc  in ['utf_8', 'iso8859_15', 'utf16', 'cp1252', 'cp858', 'utf_32']:
        try:
            text = str(chunk, enc)
            encoding = enc
        except ValueError:
            continue
        break

    if encoding is None:
        sys.stderr.write("Could not detect encoding")
        sys.stdout.buffer.write(chunk)
    else:
        sys.stdout.write(replace_emojis(text).replace('\r\n', '\n'))

It automatically replaces emojis by their unicode names, so 🙄 becomes :face with rolling eyes:. Some emojis might have shorter substitutions, like the facepalm: m(. You can update the EMOJI_REPLACEMENT_TABLE to your liking to use these shorter substitutions.

There is a mild caveat with the script: it does not handle character sets other than unicode and ISO-8859-15 very well.


Tagged as emoji, mail