-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconvert-codepoints.js
65 lines (54 loc) · 2.48 KB
/
convert-codepoints.js
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
#!/usr/bin/env osascript -l JavaScript
// INFO JXA-specific method -> replace if not using macOS
function writeToFile(filepath, text) {
const str = $.NSString.alloc.initWithUTF8String(text);
str.writeToFileAtomicallyEncodingError(
filepath,
true,
$.NSUTF8StringEncoding,
null
);
}
// INFO JXA-specific method -> replace if not using macOS
function httpRequest(url) {
const app = Application.currentApplication();
app.includeStandardAdditions = true;
return app.doShellScript(`curl -sL "${url}"`).replace(/\r/g, "\n"); // JXA returns line breaks as `\\\r`
}
//──────────────────────────────────────────────────────────────────────────────
function codePoint2Char(codepoint) {
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCodePoint
try {
const icon = String.fromCodePoint("0x" + codepoint);
return icon;
} catch (error) {
errorCount++;
return false;
}
}
const source =
"https://raw.githubusercontent.com/loichyan/nerdfix/main/src/cached.txt";
const outputPath = "./lua/cmp_nerdfont/items.lua";
//──────────────────────────────────────────────────────────────────────────────
let errorCount = 0;
const newLines = httpRequest(source)
.split("\n")
.slice(1) // first line is just heading
.filter((line) => !line.match(/obsolete$/)) // remove obsolete nerdfont icons (see loichyan/nerdfix)
.map((line) => {
const name = line.split(" ")[0];
const codepoint = line.split(" ")[1];
if (!codepoint) return;
const icon = codePoint2Char(codepoint);
const newLine = `{ word = ":${name}"; label = "${icon} ${name}"; insertText = "${icon}"; filterText = ":${name}" };`;
return newLine;
});
// wrap in first and last line, write to file
newLines.unshift("return function() return {");
newLines.push("} end");
writeToFile(outputPath, newLines.join("\n"));
//──────────────────────────────────────────────────────────────────────────────
// ensure everything went correctly
const directReturn =
errorCount === 0 ? "✅ No errors" : toString(errorCount) + " errors.";
directReturn;