package main
import (
"io/ioutil"
"fmt"
"encoding/json"
"os"
"sort"
"strings"
"hash/crc64"
)
func main() {
if len(os.Args) < 3 {
fmt.Println("Usage: ./keymap-gen inputfile outfile")
fmt.Println("Outputs dict in current dir")
return
}
data, err := ioutil.ReadFile(os.Args[1])
if err != nil {
panic(err)
}
var FullDict map[string]string
var output []string
json.Unmarshal(data, &FullDict)
for i,v := range FullDict {
hash := crc64.Checksum([]byte(v), crc64.MakeTable(crc64.ECMA))
hashStr := fmt.Sprintf("txt_%x", hash)[:10]
var keys string
for _, k := range(v) {
keys += fmt.Sprintf("KC_%v, ", string(k))
}
keys = keys[:len(keys)-2]
spacer := strings.Repeat(" ", 15-len(i))
output = append(output, fmt.Sprintf("SUBS(%v, %v\"%v\", %v)\n", hashStr, spacer, i, keys))
}
sort.Slice(output, func (i,j int) bool {
return strings.Count(output[i], " ") > strings.Count(output[j], " ")
})
ioutil.WriteFile(os.Args[2], []byte(strings.Join(output, "")), 0555)
}