fulvia/modules/unicode_info.py

42 lines
1.1 KiB
Python
Raw Normal View History

2018-03-16 03:13:43 -04:00
#!/usr/bin/env python3
"""
Displays information about unicode endpoints.
"""
import unicodedata
from module import commands, example
@commands('u')
@example('.u ‽', 'U+203D INTERROBANG (‽)')
@example('.u 203D', 'U+203D INTERROBANG (‽)')
def codepoint(bot, trigger):
"""Looks up unicode information."""
arg = trigger.group(2)
if not arg:
return bot.reply('What code point do you want me to look up?')
stripped = arg.strip()
if len(stripped) > 0:
arg = stripped
if len(arg) > 1:
if arg.startswith('U+'):
arg = arg[2:]
try:
arg = chr(int(arg, 16))
except:
return bot.reply("That's not a valid code point.")
# Get the hex value for the code point, and drop the 0x from the front
point = str(hex(ord(u'' + arg)))[2:]
# Make the hex 4 characters long with preceding 0s, and all upper case
point = point.rjust(4, str('0')).upper()
try:
name = unicodedata.name(arg)
except ValueError:
return 'U+%s (No name found)' % point
if not unicodedata.combining(arg):
template = 'U+%s %s (%s)'
else:
template = 'U+%s %s (\xe2\x97\x8c%s)'
2018-05-25 15:21:18 -04:00
bot.msg(template % (point, name, arg))