From 3885b19161abef297f479c169c90fbd6b1072da0 Mon Sep 17 00:00:00 2001 From: luk3yx Date: Mon, 7 Nov 2022 10:20:51 +1300 Subject: Get character names of control characters --- misc.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/misc.py b/misc.py index ffd0ed4..0447829 100644 --- a/misc.py +++ b/misc.py @@ -192,17 +192,25 @@ def coinflip_cmd(irc, hostmask, is_admin, args): reply(irc, hostmask, args, msg) +def get_char_name(char): + # Hack to get the name of control characters. U+2400 is "SYMBOL FOR NULL" + # and this function will remove the leading "SYMBOL FOR " prefix when a + # null byte is passed to it. + if char <= '\x1f': + char = chr(ord(char) + 0x2400) + return unicodedata.name(char)[11:] + return unicodedata.name(char, 'Unknown') + + @register_command('chr', 'unicode', 'u') def unicode_cmd(irc, hostmask, is_admin, args): chars = args[1] - if len(chars) > 5: - msg = f'You must provide 1-5 characters, not {len(chars)}.' + if len(chars) > 10: + msg = f'You must provide 1-10 characters, not {len(chars)}.' elif not chars: msg = 'Usage: .unicode ' else: - msg = ', '.join( - f'U+{ord(char):04X} - {unicodedata.name(char, "Unknown")}' - for char in chars - ) + msg = ', '.join(f'U+{ord(char):04X} - {get_char_name(char)}' + for char in chars) reply(irc, hostmask, args, msg) -- cgit v1.2.3