Created
December 17, 2018 16:08
-
-
Save frohoff/f45bd3f01ceba715437d392300268549 to your computer and use it in GitHub Desktop.
14 segment display ascii renderer
This file contains 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/python | |
# $ echo bcefgG il | python 14seg.py | |
# | |
# | | | | |
# -- -- | |
# | | | | |
import string | |
import sys | |
CHAR_MATRIX = """ | |
----- | |
|\|/| | |
-- -- | |
|/|\| | |
----- | |
""".strip() | |
SEGS_MATRIX = """ | |
aaaaa | |
fhijb | |
gg GG | |
emlkc | |
ddddd | |
""".strip() | |
MAP = "abcdefgGhijklm" | |
def mask(segs, mapping=MAP): | |
assert not mapping or len(mapping) == len(MAP) | |
tr = string.maketrans(mapping or MAP, MAP) | |
segs = segs.translate(tr) | |
masked_matrix = ''.join(CHAR_MATRIX[i] if SEGS_MATRIX[i] in segs or SEGS_MATRIX[i] == '\n' else ' ' for i in range(len(SEGS_MATRIX))) | |
return masked_matrix | |
def combine(glyphs, space=" "): | |
num_rows = range(len(CHAR_MATRIX.splitlines())) | |
combined = '\n'.join(space.join(g.splitlines()[i] for g in glyphs) for i in num_rows) | |
return combined | |
if __name__ == '__main__': | |
mapping = sys.argv[1] if len(sys.argv) > 1 else None | |
chars = sys.stdin.read().split() | |
glyphs = [mask(segs, mapping=mapping) for segs in chars] | |
combined = combine(glyphs) | |
print combined |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment