summaryrefslogtreecommitdiff
path: root/layouts/community/planck_mit/guidoism/generate_c.py
blob: 5bee5760847caa698882b5ef0f8e78904b252fb8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import itertools
import json
import os.path
import re

KEYMAP_C = """/* {0}
{1}
*/
[{2}] = {3}(
    {4})
"""

README_MD = """## {0}
```
{1}
```
"""

base = os.path.dirname(__file__)

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()}

keys = json.load(open(os.path.join(base, 'keys.json')))

d = json.load(open(os.path.join(base, 'guidoism.json')))

def surround(s, a, b, c):
    return a + b.join(s) + c

def pattern(cell, table):
    return ['─'*cell for i in range(table)]

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))