Skip to content

Commit

Permalink
Fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
narze committed Jul 14, 2023
1 parent 4901a13 commit 397e5c0
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 56 deletions.
67 changes: 41 additions & 26 deletions generateKcm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Layout, WindowsAttributes } from "./main"

export async function generateKcm(
content: Record<string, unknown>,
outputPath: string
outputPath: string,
): Promise<void> {
const layout = plainToClass(Layout, content)
const errors = await validate(layout)
Expand All @@ -15,21 +15,21 @@ export async function generateKcm(
}

const windowsErrors = await validate(
plainToClass(WindowsAttributes, layout.os.windows)
plainToClass(WindowsAttributes, layout.os.windows),
)

if (windowsErrors.length) {
throw new Error(windowsErrors.map((e) => e.toString()).join(", "))
}

function toHex(str: string) {
var hex, i;
var result = "";
for (i=0; i<str.length; i++) {
hex = str.charCodeAt(i).toString(16);
result += "\\u"+("0000"+hex).slice(-4);
let hex, i
let result = ""
for (i = 0; i < str.length; i++) {
hex = str.charCodeAt(i).toString(16)
result += "\\u" + ("0000" + hex).slice(-4)
}
return result;
return result
}

const klfDefaultLayout = {
Expand Down Expand Up @@ -81,11 +81,11 @@ export async function generateKcm(
".": "key PERIOD {",
"/": "key SLASH {",
" ": "key SPACE {",
"KPDL": "key NUMPAD_COMMA {",
KPDL: "key NUMPAD_COMMA {",
}

const lines = [
`# License: ${layout.license}
`# License: ${layout.license}
\n# ${layout.language} ${layout.name} v${layout.version}
\ntype OVERLAY
map key 2 1
Expand Down Expand Up @@ -136,32 +136,47 @@ map key 51 COMMA
map key 52 PERIOD
map key 53 SLASH
map key 57 SPACE
map key 95 NUMPAD_COMMA`
map key 95 NUMPAD_COMMA`,
]

const layoutLines = [""]
Object.entries(klfDefaultLayout).forEach(([key, value]) =>
{
const extensions = "\n "
+((layout.keys[key][0])?("label: '"+(toHex(layout.keys[key][0]))+"'\n base: '"+(toHex(layout.keys[key][0]))+"'\n "):"")
+((layout.keys[key][1])?("shift: '"+(toHex(layout.keys[key][1]))+"'\n capslock: '"+(toHex(layout.keys[key][1]))+"'\n "):"")
+((layout.keys[key][0])?("capslock+shift: '"+(toHex(layout.keys[key][0]))+"'\n "):"")
+((layout.keys[key][3])?("ralt: '"+(toHex(layout.keys[key][3]))+"'\n "):"")
+((layout.keys[key][5])?("ralt+shift: '"+(toHex(layout.keys[key][5]))+"'\n "):"")
+ "}\n"
Object.entries(klfDefaultLayout).forEach(([key, value]) => {
const extensions =
"\n " +
(layout.keys[key][0]
? "label: '" +
toHex(layout.keys[key][0]) +
"'\n base: '" +
toHex(layout.keys[key][0]) +
"'\n "
: "") +
(layout.keys[key][1]
? "shift: '" +
toHex(layout.keys[key][1]) +
"'\n capslock: '" +
toHex(layout.keys[key][1]) +
"'\n "
: "") +
(layout.keys[key][0]
? "capslock+shift: '" + toHex(layout.keys[key][0]) + "'\n "
: "") +
(layout.keys[key][3]
? "ralt: '" + toHex(layout.keys[key][3]) + "'\n "
: "") +
(layout.keys[key][5]
? "ralt+shift: '" + toHex(layout.keys[key][5]) + "'\n "
: "") +
"}\n"

layoutLines.push([value,...extensions].join(""))
layoutLines.push([value, ...extensions].join(""))
})

fs.writeFileSync(
outputPath,
//"\ufeff" +
[
lines.join("\n"),
layoutLines.join("\n"),
].join("\n\n"),
[lines.join("\n"), layoutLines.join("\n")].join("\n\n"),
{
encoding: "utf8",
}
},
)
}
65 changes: 35 additions & 30 deletions generateKlc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Layout, WindowsAttributes } from "./main"

export async function generateKlc(
content: Record<string, unknown>,
outputPath: string
outputPath: string,
): Promise<void> {
const layout = plainToClass(Layout, content)
const errors = await validate(layout)
Expand All @@ -15,34 +15,33 @@ export async function generateKlc(
}

const windowsErrors = await validate(
plainToClass(WindowsAttributes, layout.os.windows)
plainToClass(WindowsAttributes, layout.os.windows),
)

if (windowsErrors.length) {
throw new Error(windowsErrors.map((e) => e.toString()).join(", "))
}

const klcLocales: any = {
const klcLocales = {
Thai: "th-TH",
Lao: "lo-LA",
}

function toHex(str: string) {
let hex, i;
let result = "";
if(str !== null && str !== undefined){
for (i = 0; i < str.length; i++) {
hex = str.charCodeAt(i).toString(16);
result += ("0000" + hex).slice(-4);
}
return result;
}
else{
return false;
let hex, i
let result = ""
if (str !== null && str !== undefined) {
for (i = 0; i < str.length; i++) {
hex = str.charCodeAt(i).toString(16)
result += ("0000" + hex).slice(-4)
}
return result
} else {
return false
}
}

const toVK: any = {
const toVK: Record<string, string> = {
"1": "1",
"2": "2",
"3": "3",
Expand Down Expand Up @@ -91,9 +90,9 @@ export async function generateKlc(
".": "OEM_PERIOD",
"/": "OEM_2",
" ": "SPACE",
"KPDL": "DECIMAL",
KPDL: "DECIMAL",
}

const klfDefaultLayout = {
"1": "02",
"2": "03",
Expand Down Expand Up @@ -143,41 +142,47 @@ export async function generateKlc(
".": "34",
"/": "35",
" ": "39",
"KPDL": "53",
KPDL: "53",
}

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

const shiftStateLines: Array<string> = []

shiftStateLines.push(`0\t//Column 4 : Base`)
shiftStateLines.push(`1\t//Column 5 : Shft`)
shiftStateLines.push(`2\t//Column 6 : Ctrl`)
shiftStateLines.push(`6\t//Column 7 : Ctrl Alt`)
shiftStateLines.push(`7\t//Column 8 : Shft Ctrl Alt`)

shiftStateLines.unshift("SHIFTSTATE", "")

const layoutLines = ["LAYOUT\t\t;an extra '@' at the end is a dead key\r\n"]

layoutLines.push(`//SC\tVK_\tCap\t0\t1\t2\t6\t7`)
layoutLines.push(`//--\t----\t----\t----\t----\t----\t----\t----\r\n`)

Object.entries(klfDefaultLayout).forEach(([key, value]) => {
const extensions = "\t" + (toVK[(layout.keys[key][4])] || "-1")
+ "\t0"
+ "\t" + (toHex(layout.keys[key][0]) || "-1")
+ "\t" + (toHex(layout.keys[key][1]) || "-1")
+ "\t-1"
+ "\t" + (toHex(layout.keys[key][3]) || "-1")
+ "\t" + (toHex(layout.keys[key][5]) || "-1")
const extensions =
"\t" +
(toVK[layout.keys[key][4]] || "-1") +
"\t0" +
"\t" +
(toHex(layout.keys[key][0]) || "-1") +
"\t" +
(toHex(layout.keys[key][1]) || "-1") +
"\t-1" +
"\t" +
(toHex(layout.keys[key][3]) || "-1") +
"\t" +
(toHex(layout.keys[key][5]) || "-1")

layoutLines.push([value, ...extensions].join(""))
})
Expand All @@ -193,6 +198,6 @@ export async function generateKlc(
].join("\r\n\r\n"),
{
encoding: "utf16le",
}
},
)
}

0 comments on commit 397e5c0

Please sign in to comment.