generate_c.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import itertools
  2. import json
  3. from pprint import pprint as pp
  4. layers = dict(enumerate(['_QWERTY', '_LOWER', '_RAISE', '_MOVEMENT', '_NUMPAD']))
  5. key_names = {('MO(%d)' % i): layers.get(i).strip('_') for i in layers.keys()}
  6. unicodes = {
  7. "<i class='fa fa-fast-forward'></i>": "next",
  8. "<i class='fa fa-volume-down'></i>": "vol-",
  9. "<i class='fa fa-volume-up'></i>": "vol+",
  10. "<i class='fa fa-play'></i>": "play",
  11. }
  12. d = json.load(open('layouts/community/ortho_4x12/guidoism/guidoism.json'))
  13. def grouper(iterable, n):
  14. args = [iter(iterable)] * n
  15. return itertools.zip_longest(*args, fillvalue='')
  16. def truthy(s):
  17. return [a for a in s if a]
  18. def just(s, n):
  19. return [a.center(n*2+1 if len(s) == 11 and i == 5 else n) for i, a in enumerate(s)]
  20. def replace(s):
  21. return [key_names.get(a, a) for a in s]
  22. def layer(i, l):
  23. n = max(len(s) for s in l)
  24. rows = [', '.join(replace(truthy(row))) for row in grouper(l, 12)]
  25. return '[%s] = %s(\n%s)' % (layers[i], d['layout'], ',\n'.join(rows))
  26. print(',\n\n'.join(layer(i, l) for i, l in enumerate(d['layers'])))
  27. def surround(s, a, b, c):
  28. return a + b.join(s) + c
  29. def pattern(cell, table):
  30. return ['─'*cell for i in range(table)]
  31. keys = json.load(open('layouts/community/ortho_4x12/guidoism/keys.json'))
  32. def layer2(i, l):
  33. def replace(s):
  34. s = [keys.get(a, a) for a in s]
  35. return [unicodes.get(a, a) for a in s]
  36. n = max(len(s) for s in l)
  37. return [surround(just(replace(truthy(row)), 5), '│', '│', '│') for row in grouper(l, 12)]
  38. for i, l in enumerate(d['layers']):
  39. print(surround(pattern(5, 12), '┌', '┬', '┐'))
  40. for n, row in enumerate(layer2(i, l)):
  41. print(row)
  42. if n < 3:
  43. print(surround(pattern(5, 12), '├', '┼', '┤'))
  44. else:
  45. print(surround(pattern(5, 12), '└', '┴', '┘'))