Skip to content

Instantly share code, notes, and snippets.

@frohoff
Created December 17, 2018 16:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frohoff/f45bd3f01ceba715437d392300268549 to your computer and use it in GitHub Desktop.
Save frohoff/f45bd3f01ceba715437d392300268549 to your computer and use it in GitHub Desktop.
14 segment display ascii renderer
#!/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