-
-
Notifications
You must be signed in to change notification settings - Fork 398
/
config_rc.ts
78 lines (69 loc) · 2.09 KB
/
config_rc.ts
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
66
67
68
69
70
71
72
73
74
75
76
77
78
import * as controller from "@src/lib/controller"
import * as Native from "@src/lib/native"
export async function source(filename = "auto") {
let rctext = ""
if (filename === "auto") {
rctext = await Native.getrc()
} else {
rctext = (await Native.read(filename)).content
}
if (rctext === undefined) return false
await runRc(rctext)
return true
}
/*
* This should be moved out to a library but I am lazy
*/
export async function fetchText(url: string) {
const response = await fetch(url)
const reader = response.body.getReader()
let rctext = ""
const decoder = new TextDecoder("utf-8")
while (true) {
const { value: chunk, done: isDone } = await reader.read()
if (isDone) return rctext
rctext += decoder.decode(chunk)
}
}
const fetchConfig = fetchText
export async function sourceFromUrl(url: string) {
const rctext = await fetchConfig(url)
if (!rctext) return false
await runRc(rctext)
return true
}
export async function writeRc(conf: string, force = false, filename = "auto") {
let path: string
if (filename === "auto") {
try {
path = await Native.getrcpath()
} catch (e) {
// TODO: respect XDG_CONFIG_DIR without breaking on systems that don't have it set
path = "~/.tridactylrc"
}
} else {
path = filename
}
return await Native.writerc(path, force, conf)
}
export async function runRc(rc: string) {
for (const cmd of rcFileToExCmds(rc)) {
await new Promise(resolve => setTimeout(resolve, 100))
await controller.acceptExCmd(cmd)
}
}
export function rcFileToExCmds(rcText: string): string[] {
// Split into individual excmds
const excmds = rcText.split("\n")
// Remove empty and comment lines
const ex = excmds.filter(
x =>
/\S/.test(x) &&
!x.trim().startsWith('"') &&
!x.trim().startsWith("#"),
)
const res = ex.join("\n")
// string-join lines that end with /
const joined = res.replace(/\\\n/g, "")
return joined.split("\n")
}