-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake_layer.rb
186 lines (165 loc) · 4.06 KB
/
make_layer.rb
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# coding: utf-8
require 'csv'
require 'pp'
def read_config()
File.read(ARGV[0])
end
def remove_comments(file)
file.gsub(/\/\/.*$/, '')
end
def parse_csv(file)
CSV.parse(file,
quote_char: "",
col_sep: "\t",
skip_blanks: true)
end
def complete_rows(table)
table.map do |l|
(l + Array.new(6 - l.length)).map do |el|
if el == " " then nil else el end
end
end
end
def but_first(table)
table.drop 1
end
def group_by_two(ary)
collecting_first = true
buf = []
subbuf = [nil, nil]
ary.each do |l|
if collecting_first
buf << [l]
else
buf[-1] << l
end
collecting_first = !collecting_first
end
buf
end
def expand_pairs(table)
buf = []
table.each do |pair|
if pair[1][0] == nil
buf << [pair[0][0] + "0000"] + pair[0][1..-1] <<
["0000" + pair[0][0]] + pair[1][1..-1]
else
buf << [pair[0][0] + pair[1][0]] + pair[0][1..-1]
end
end
buf
end
def convert_to_actions(table)
def type(str)
" Keyboard.press(#{str});\n" +
" Keyboard.release(#{str});"
end
change_map = {
ё: '`', Ё: '~', :'к"' => '@', №: '#', :'к;' => '$', :'к:' => '^', :'к?' => '&',
й: 'q', ц: 'w', у: 'e', к: 'r', е: 't', н: 'y',
г: 'u', ш: 'i', щ: 'o', з: 'p', х: '[', ъ: ']',
ф: 'a', ы: 's', в: 'd', а: 'f', п: 'g', р: 'h',
о: 'j', л: 'k', д: 'l', ж: ';', э: "'",
я: 'z', ч: 'x', с: 'c', м: 'v', и: 'b', т: 'n',
ь: 'm', б: ',', ю: '.', :"к." => "/",
Й: 'Q', Ц: 'W', У: 'E', К: 'R', Е: 'T', Н: 'Y',
Г: 'U', Ш: 'I', Щ: 'O', З: 'P', Х: '[', Ъ: ']',
Ф: 'A', Ы: 'S', В: 'D', А: 'F', П: 'G', Р: 'H',
О: 'J', Л: 'K', Д: 'L', Ж: ':', Э: '"', :"к/" => "|",
Я: 'Z', Ч: 'X', С: 'C', М: 'V', И: 'B', Т: 'N',
Ь: 'M', Б: '<', Ю: '>', :"к," => "?",
}
table.map do |line|
[line[0]] + line[1..-1].map do |val|
if val != nil and change_map.has_key? val.to_sym
val = change_map[val.to_sym]
end
if val == nil
val
elsif val == '!lat'
" Layer::curr = Layer::latin;\n" + (type 'KEY_F19')
elsif val == '!cyr'
" Layer::curr = Layer::cyrillic;\n" + (type 'KEY_F19')
elsif val == '?lat'
" Layer::curr = Layer::latin;\n"
elsif val == '?cyr'
" Layer::curr = Layer::cyrillic;\n"
elsif val == 'mod_stop'
" modif.stop();"
elsif val.start_with? 'mod_lock_'
" modif.start_mode(" +
val.
split('_')[1..-1].
map { |mod| "Mod::" + mod }.
join(' | ') +
");"
elsif val.start_with? 'mod_'
" modif.start_mode_1(" +
val.
split('_')[1..-1].
map { |mod| "Mod::" + mod }.
join(' | ') +
");"
elsif val == '_'
type "'_'"
elsif val.start_with? '_'
" Keyboard.press(KEY#{val.upcase});\n" +
" Keyboard.release(KEY#{val.upcase});"
elsif val == '\\'
type "'\\'"
elsif val == "'"
type "'\\''"
elsif val.length == 1
type "'#{val}'"
else
" Keyboard.print(\"#{val}\");"
end
end
end
end
def wrap_in_case(table)
def cage(action, thumb)
if action == nil
nil
else
"#{thumb}: {\n#{action}\n break;\n}"
end
end
table.map do |line|
[line[0],
cage(line[1], "000"),
cage(line[2], "100"),
cage(line[3], "010"),
cage(line[4], "001"),
cage(line[5], "011")]
end
end
def apply_fingers(table)
def apply_finger(finger, rest)
if rest == nil
nil
else
"case 0b" + finger + rest
end
end
table.map do |line|
line[1..-1].map do |el|
apply_finger line[0], el
end
end
end
puts "case Layer::#{File.basename ARGV[0], '.csv'}: {"
puts " switch (currc) {"
puts (apply_fingers \
wrap_in_case \
convert_to_actions \
expand_pairs \
group_by_two \
but_first \
complete_rows \
parse_csv \
remove_comments \
read_config).join("\n")
puts " }"
puts "break;"
puts "}"