Skip to content

Commit

Permalink
feat: Generate layouts in klc
Browse files Browse the repository at this point in the history
  • Loading branch information
narze committed Jul 6, 2021
1 parent b917404 commit fff6e5d
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 5 deletions.
100 changes: 96 additions & 4 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,114 @@ export async function generateKlc(
throw new Error(errors.map((e) => e.toString()).join(", "))
}

const locales = {
const klcLocales = {
Thai: "th-TH",
}

const klcShiftStates = {
Base: 0,
Shift: 1,
Ctrl: 2,
"Shift+Ctrl": 3,
Alt: 4,
"Shift+Alt": 5,
"Alt+Ctrl": 6,
"Shift+Alt+Ctrl": 7,
// From macOS config
Command: 4,
AltGr: 7,
Option: 7,
Control: 2,
}

const klfDefaultLayout = {
"1": "02\t1",
"2": "03\t2",
"3": "04\t3",
"4": "05\t4",
"5": "06\t5",
"6": "07\t6",
"7": "08\t7",
"8": "09\t8",
"9": "0a\t9",
"0": "0b\t0",
"-": "0c\tOEM_MINUS",
"=": "0d\tOEM_PLUS",
q: "10\tQ",
w: "11\tW",
e: "12\tE",
r: "13\tR",
t: "14\tT",
y: "15\tY",
u: "16\tU",
i: "17\tI",
o: "18\tO",
p: "19\tP",
"[": "1a\tOEM_4",
"]": "1b\tOEM_6",
a: "1e\tA",
s: "1f\tS",
d: "20\tD",
f: "21\tF",
g: "22\tG",
h: "23\tH",
j: "24\tJ",
k: "25\tK",
l: "26\tL",
";": "27\tOEM_1",
"'": "28\tOEM_7",
"`": "29\tOEM_3",
"\\": "2b\tOEM_5",
z: "2c\tZ",
x: "2d\tX",
c: "2e\tC",
v: "2f\tV",
b: "30\tB",
n: "31\tN",
m: "32\tM",
",": "33\tOEM_COMMA",
".": "34\tOEM_PERIOD",
"/": "35\tOEM_2",
}

const lines = [
`KBD\t${layout.name}\t"${layout.language} ${layout.name} v${layout.version}"`,
`COPYRIGHT\t"MIT"`,
`COMPANY\t"${layout.os.windows.company}"`,
`LOCALENAME\t"${locales[layout.language]}"`,
`LOCALENAME\t"${klcLocales[layout.language]}"`,
`LOCALEID\t"${layout.os.windows.localeId}"`,
`VERSION\t${layout.version}`,
]

fs.writeFileSync("./output/test.klc", lines.join("\n\n"), {
encoding: "utf8",
const shiftStateLines: Array<string> = layout.layers.map((layer, idx) => {
if (layer in klcShiftStates) {
return `${klcShiftStates[layer]}\t// Column ${idx + 4} : ${layer}`
} else {
throw new Error("Layer not valid")
}
})

shiftStateLines.unshift("SHIFTSTATE", "")

const layoutLines = ["LAYOUT"]

Object.entries(klfDefaultLayout).forEach(([key, value]) => {
const extensions = layout.layers.map((_, idx) => layout.keys[key][idx])
layoutLines.push([value, "0", ...extensions].join("\t"))
})

fs.writeFileSync(
"./output/test.klc",
[
lines.join("\n\n"),
shiftStateLines.join("\n"),
layoutLines.join("\n"),
"ENDKBD",
].join("\n\n"),
{
encoding: "utf8",
}
)
}

export async function generateLayout(
Expand Down
58 changes: 57 additions & 1 deletion output/test.klc
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,60 @@ LOCALENAME "th-TH"

LOCALEID "0000041e"

VERSION 1.0
VERSION 1.0

SHIFTSTATE

0 // Column 4 : Base
1 // Column 5 : Shift

LAYOUT
0b 0 0 0 )
02 1 0 1 !
03 2 0 2 @
04 3 0 3 #
05 4 0 4 $
06 5 0 5 %
07 6 0 6 ^
08 7 0 7 &
09 8 0 8 *
0a 9 0 9 (
0c OEM_MINUS 0 - _
0d OEM_PLUS 0 = +
10 Q 0 ใ ฒ
11 W 0 ต ฏ
12 E 0 ห ซ
13 R 0 ล ญ
14 T 0 ส ฟ
15 Y 0 ป ฉ
16 U 0 ั ึ
17 I 0 ก ธ
18 O 0 ิ ฐ
19 P 0 บ ฎ
1a OEM_4 0 ็ ฆ
1b OEM_6 0 ฬ ฑ
1e A 0 ง ษ
1f S 0 เ ถ
20 D 0 ร แ
21 F 0 น ช
22 G 0 ม พ
23 H 0 อ ผ
24 J 0 า ำ
25 K 0 ่ ข
26 L 0 ้ โ
27 OEM_1 0 ว ภ
28 OEM_7 0 ื "
29 OEM_3 0 ` ~
2b OEM_5 0 ฯ ฌ
2c Z 0 ุ ฤ
2d X 0 ไ ฝ
2e C 0 ท ๆ
2f V 0 ย ณ
30 B 0 จ ๊
31 N 0 ค ๋
32 M 0 ี ์
33 OEM_COMMA 0 ด ศ
34 OEM_PERIOD 0 ะ ฮ
35 OEM_2 0 ู ?

ENDKBD
31 changes: 31 additions & 0 deletions test/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -439,5 +439,36 @@ describe("generateKlc", () => {
expect(lines[3]).toEqual(`LOCALENAME\t"th-TH"`)
expect(lines[4]).toEqual(`LOCALEID\t"0000041e"`)
expect(lines[5]).toEqual(`VERSION\t1.0`)

// Assert Shiftstate
// public enum ShiftState : int {
// Base = 0, // 0
// Shft = 1, // 1
// Ctrl = 2, // 2
// ShftCtrl = Shft | Ctrl, // 3
// Menu = 4, // 4 -- NOT USED
// ShftMenu = Shft | Menu, // 5 -- NOT USED
// MenuCtrl = Menu | Ctrl, // 6
// ShiftMenuCtrl = Shft | Menu | Ctrl, // 7
// }

// 0 //Column 4
// 1 //Column 5 : Shft
// 2 //Column 6 : Ctrl
// 6 //Column 7 : Ctrl Alt
// 7 //Column 8 : Shft Ctrl Alt

expect(lines[6]).toEqual(`SHIFTSTATE`)
expect(lines[7]).toEqual(`0\t// Column 4 : Base`)
expect(lines[8]).toEqual(`1\t// Column 5 : Shift`)

// Assert Layout
expect(lines[9]).toEqual(`LAYOUT`)

expect(lines[10]).toEqual(`0b\t0\t0\t0\t)`)
expect(lines[11]).toEqual(`02\t1\t0\t1\t!`)

// Assert ENDKBD
expect(lines.slice(-1)).toEqual(["ENDKBD"])
})
})

0 comments on commit fff6e5d

Please sign in to comment.