Last active
October 27, 2024 20:59
-
-
Save adsr303/3a1585ee9ecca104a9cb579640b7c634 to your computer and use it in GitHub Desktop.
Search Unicode characters by name
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import argparse | |
import re | |
import unicodedata | |
def gen_chars(rx, cp_max): | |
for i in range(160, cp_max): | |
try: | |
c = chr(i) | |
name = unicodedata.name(c) | |
if rx.match(name): | |
yield c, name | |
except ValueError: | |
pass | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser() | |
parser.add_argument('-n', '--names', action='store_true', | |
help='show Unicode names') | |
parser.add_argument('-u', '--upper-limit', type=int, default=2**16, | |
help='maximum codepoint number') | |
parser.add_argument('match', help='part of Unicode name to match') | |
args = parser.parse_args() | |
rx = re.compile(r'.*%s.*' % re.escape(args.match), re.IGNORECASE) | |
gen = gen_chars(rx, args.upper_limit) | |
if args.names: | |
for char_name in gen: | |
print('%s %s' % char_name) | |
else: | |
print(''.join(c for c, n in gen)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment