You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
76 lines
2.3 KiB
76 lines
2.3 KiB
7 years ago
|
import itertools
|
||
|
import json
|
||
6 years ago
|
import os.path
|
||
|
import re
|
||
7 years ago
|
|
||
6 years ago
|
KEYMAP_C = """/* {0}
|
||
|
{1}
|
||
|
*/
|
||
|
[{2}] = {3}(
|
||
|
{4})
|
||
|
"""
|
||
7 years ago
|
|
||
6 years ago
|
README_MD = """## {0}
|
||
|
```
|
||
|
{1}
|
||
|
```
|
||
|
"""
|
||
7 years ago
|
|
||
6 years ago
|
base = os.path.dirname(__file__)
|
||
7 years ago
|
|
||
6 years ago
|
layer_names = dict(enumerate(['_QWERTY', '_LOWER', '_RAISE', '_MOVEMENT', '_NUMPAD', '_FUNCTION']))
|
||
|
layer_name = {('MO(%d)' % i): layer_names.get(i).strip('_') for i in layer_names.keys()}
|
||
7 years ago
|
|
||
6 years ago
|
keys = json.load(open(os.path.join(base, 'keys.json')))
|
||
7 years ago
|
|
||
6 years ago
|
d = json.load(open(os.path.join(base, 'guidoism.json')))
|
||
7 years ago
|
|
||
|
def surround(s, a, b, c):
|
||
|
return a + b.join(s) + c
|
||
|
|
||
|
def pattern(cell, table):
|
||
|
return ['─'*cell for i in range(table)]
|
||
|
|
||
6 years ago
|
top = surround(pattern(5, 12), '┌', '┬', '┐')
|
||
|
mid = surround(pattern(5, 12), '├', '┼', '┤')
|
||
|
bottom = surround(pattern(5, 12), '└', '┴', '┘')
|
||
|
|
||
|
from more_itertools import chunked, intersperse, interleave_longest
|
||
|
|
||
|
def uni(k):
|
||
|
return keys.get(k, k).lower().center(5)
|
||
|
|
||
|
def c_layout(i, definition, template):
|
||
|
c_name = layer_names[i]
|
||
|
pretty_name = c_name.strip('_').capitalize()
|
||
|
layout = d['layout']
|
||
|
|
||
|
surround = lambda s: ''.join(interleave_longest(['│']*(len(s)+1), s))
|
||
|
layer = list(map(uni, definition))
|
||
|
layer[41] = layer[41].center(11)
|
||
|
layer = chunked(layer, 12)
|
||
|
rows = intersperse(mid, map(surround, layer))
|
||
|
pretty = '\n'.join(itertools.chain([top], rows, [bottom]))
|
||
|
|
||
|
surround = lambda s: ', '.join(s)
|
||
|
layer = list(map(lambda k: layer_name.get(k, k), definition))
|
||
|
layer = chunked(layer, 12)
|
||
|
rows = map(surround, layer)
|
||
|
c_layer = ',\n '.join(itertools.chain([], rows, []))
|
||
|
|
||
|
return template.format(pretty_name, pretty, c_name, layout, c_layer)
|
||
|
|
||
|
start = '// START_REPLACEMENT\n'
|
||
|
end = '// END_REPLACEMENT\n'
|
||
|
replacement = start + ',\n\n'.join(c_layout(i, l, KEYMAP_C) for i, l in enumerate(d['layers'])) + end
|
||
|
keymap = os.path.join(base, 'keymap.c')
|
||
|
existing = open(keymap).read()
|
||
|
r = re.compile(r'// START_REPLACEMENT.*// END_REPLACEMENT', re.DOTALL)
|
||
|
open(keymap, 'w').write(r.sub(replacement, existing))
|
||
|
|
||
|
replacement = '## Current Configuration\n\n' + '\n\n'.join(c_layout(i, l, README_MD) for i, l in enumerate(d['layers']))
|
||
|
keymap = os.path.join(base, 'readme.md')
|
||
|
existing = open(keymap).read()
|
||
|
r = re.compile(r'## Current Configuration.*', re.DOTALL)
|
||
|
open(keymap, 'w').write(r.sub(replacement, existing))
|