diff options
Diffstat (limited to 'keyboards/gboards/combos/_generator/main.go')
-rw-r--r-- | keyboards/gboards/combos/_generator/main.go | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/keyboards/gboards/combos/_generator/main.go b/keyboards/gboards/combos/_generator/main.go new file mode 100644 index 000000000..043c8b78c --- /dev/null +++ b/keyboards/gboards/combos/_generator/main.go | |||
@@ -0,0 +1,60 @@ | |||
1 | // Package for taking a mapping of words to keys and outputing a | ||
2 | // combo engine commpatible def | ||
3 | |||
4 | package main | ||
5 | |||
6 | import ( | ||
7 | "io/ioutil" | ||
8 | "fmt" | ||
9 | "encoding/json" | ||
10 | "os" | ||
11 | "sort" | ||
12 | "strings" | ||
13 | "hash/crc64" | ||
14 | //"encoding/base64" | ||
15 | ) | ||
16 | |||
17 | func main() { | ||
18 | // Show Usage | ||
19 | if len(os.Args) < 3 { | ||
20 | fmt.Println("Usage: ./keymap-gen inputfile outfile") | ||
21 | fmt.Println("Outputs dict in current dir") | ||
22 | return | ||
23 | } | ||
24 | |||
25 | // Read the source | ||
26 | data, err := ioutil.ReadFile(os.Args[1]) | ||
27 | if err != nil { | ||
28 | panic(err) | ||
29 | } | ||
30 | |||
31 | // Unbundle Data | ||
32 | var FullDict map[string]string | ||
33 | var output []string | ||
34 | json.Unmarshal(data, &FullDict) | ||
35 | |||
36 | // Loop over entries and store | ||
37 | for i,v := range FullDict { | ||
38 | // This checks for colllisions, Generates hash | ||
39 | hash := crc64.Checksum([]byte(v), crc64.MakeTable(crc64.ECMA)) | ||
40 | hashStr := fmt.Sprintf("txt_%x", hash)[:10] | ||
41 | |||
42 | // Format keys into combo | ||
43 | var keys string | ||
44 | for _, k := range(v) { | ||
45 | keys += fmt.Sprintf("KC_%v, ", string(k)) | ||
46 | |||
47 | } | ||
48 | keys = keys[:len(keys)-2] | ||
49 | |||
50 | // Append to output | ||
51 | spacer := strings.Repeat(" ", 15-len(i)) | ||
52 | output = append(output, fmt.Sprintf("SUBS(%v, %v\"%v\", %v)\n", hashStr, spacer, i, keys)) | ||
53 | } | ||
54 | |||
55 | |||
56 | sort.Slice(output, func (i,j int) bool { | ||
57 | return strings.Count(output[i], " ") > strings.Count(output[j], " ") | ||
58 | }) | ||
59 | ioutil.WriteFile(os.Args[2], []byte(strings.Join(output, "")), 0555) | ||
60 | } | ||