Hook mktridactylrc up to native messenger

Still need to doublecheck subconfigs and fallbacks.
Also need to make sure that ! overwrites and error out if it isn't
supplied.
This commit is contained in:
Anton Vilhelm Ásgeirsson 2019-05-30 16:55:49 +00:00 committed by Oliver Blanthorn
parent 66de3e815d
commit bdc6a39180
No known key found for this signature in database
GPG key ID: 2BB8C36BB504BFF3
4 changed files with 107 additions and 57 deletions

View file

@ -468,7 +468,8 @@ def handleMessage(message):
reply["code"] = 2
elif cmd == "write":
with open(message["file"], "w") as file:
path = os.path.expanduser(message["file"])
with open(path, "w") as file:
file.write(message["content"])
elif cmd == "temp":

View file

@ -13,12 +13,14 @@ export async function source(filename = "auto") {
return true
}
export async function write(conf: string, force = false, filename = "auto") {
export async function writeRc(conf: string, force = false, filename = "auto") {
let path: string
if (filename === "auto") {
path = await Native.getrcpath()
} else {
path = filename
}
return await Native.write(path, conf)
}
export async function runRc(rc: string) {

View file

@ -670,7 +670,7 @@ export async function nativeinstall() {
TODO: Write documentation.
Available flags:
- `--force` will overwrite the config file if it exists.
- `-f` will overwrite the config file if it exists.
@param string[] argArr an optional string of arguments to be parsed.
@returns the parsed config.
@ -680,7 +680,7 @@ export async function mktridactylrc(...argArr: string[]) {
let overwrite = false
const argParse = (args: string[]): string[] => {
if (args[0] === "--force") {
if (args[0] === "-f") {
overwrite = true
args.shift()
argParse(args)
@ -689,13 +689,12 @@ export async function mktridactylrc(...argArr: string[]) {
}
const file = argParse(argArr).join(" ") || undefined
//TODO: Add actual native messenger code here.
//if (await Native.nativegate("0.1.3")) if (!await rc.source(file)) logger.error("Could not find RC file")
const conf = config.parseConfig()
console.log(conf)
console.log(file)
console.log(overwrite)
if (await Native.nativegate("0.1.3") && (!await rc.writeRc(conf, overwrite, file))) logger.error("Could not write RC file")
return conf
}

View file

@ -1377,71 +1377,119 @@ export function removeChangeListener<P extends keyof default_config>(
*/
export function parseConfig(): string {
// Parse the config into sections, this is my last resort.
const parsedConf = []
const parsedBinds = []
const parsedAliases = []
const parsedAucmds = []
const parsedAucons = []
const parsedLogging = []
let p = {
conf: [],
binds: [],
aliases: [],
subconfigs: [],
aucmds: [],
aucons: [],
logging: [],
}
for (const i in USERCONFIG) {
if (typeof USERCONFIG[i] !== "object")
parsedConf.push(`set ${i} ${USERCONFIG[i]}`)
p = parseConfigHelper(USERCONFIG, p)
const s = {
general: ``,
binds: ``,
aliases: ``,
aucmds: ``,
aucons: ``,
subconfigs: ``,
logging: ``,
}
if (p.conf.length > 0)
s.general = `" General Settings\n${p.conf.join("\n")}\n\n`
if (p.binds.length > 0)
s.binds = `" Binds\n${p.binds.join("\n")}\n\n`
if (p.aliases.length > 0)
s.aliases = `" Aliases\n${p.aliases.join("\n")}\n\n`
if (p.aucmds.length > 0)
s.aucmds = `" Autocmds\n${p.aucmds.join("\n")}\n\n`
if (p.aucons.length > 0)
s.aucons = `" Autocontainers\n${p.aucons.join("\n")}\n\n`
if (p.subconfigs.length > 0)
s.subconfigs = `" Subconfig Settings\n${p.subconfigs.join("\n")}\n\n`
if (p.logging.length > 0)
s.logging = `" Logging\n" + p.logging.join("\n")}\n\n`
return `${s.general}${s.binds}${s.subconfigs}${s.aliases}${s.aucmds}${s.aucons}${s.logging}`
}
const parseConfigHelper = (pconf, parseobj) => {
for (const i in pconf) {
if (typeof pconf[i] !== "object")
parseobj.conf.push(`set ${i} ${pconf[i]}`)
else {
for (const e of Object.keys(USERCONFIG[i])) {
if (i === "nmaps") {
parsedBinds.push(`bind ${e} ${USERCONFIG[i][e]}`)
} else if (i === "exaliases") {
// Only really useful if mapping the entire config and not just USERCONFIG.
if (e === "alias") {
parsedAliases.push(`command ${e} ${USERCONFIG[i][e]}`)
for (const e of Object.keys(pconf[i])) {
if (i === "nmaps" ) {
if (pconf[i][e].length > 0) {
parseobj.binds.push(`bind ${e} "${pconf[i][e]}"`)
} else {
parsedAliases.push(`alias ${e} ${USERCONFIG[i][e]}`)
parseobj.binds.push(`unbind ${e}`)
}
} else if (i === "exmaps" ) {
if (pconf[i][e].length > 0) {
parseobj.binds.push(`bind --mode=ex ${e} "${pconf[i][e]}"`)
} else {
parseobj.binds.push(`unbind --mode=ex ${e}`)
}
} else if (i === "ignoremaps" ) {
if (pconf[i][e].length > 0) {
parseobj.binds.push(`bind --mode=ignore ${e} "${pconf[i][e]}"`)
} else {
parseobj.binds.push(`unbind --mode=ignore ${e}`)
}
} else if (i === "imaps" ) {
if (pconf[i][e].length > 0) {
parseobj.binds.push(`bind --mode=insert ${e} "${pconf[i][e]}"`)
} else {
parseobj.binds.push(`unbind --mode=insert ${e}`)
}
} else if (i === "inputmaps" ) {
if (pconf[i][e].length > 0) {
parseobj.binds.push(`bind --mode=input ${e} "${pconf[i][e]}"`)
} else {
parseobj.binds.push(`unbind --mode=input ${e}`)
}
} else if (i === "hintmaps" ) {
if (pconf[i][e].length > 0) {
parseobj.binds.push(`bind --mode=hint ${e} "${pconf[i][e]}"`)
} else {
parseobj.binds.push(`unbind --mode=hint ${e}`)
}
} else if (i === "subconfigs") {
parseobj.subconfigs.push(`js tri.config.set("${i}, {"${e}": ${JSON.stringify(pconf[i][e])}})`)
} else if (i === "exaliases") {
// Only really useful if mapping the entire config and not just pconf.
if (e === "alias") {
parseobj.aliases.push(`command ${e} ${pconf[i][e]}`)
} else {
parseobj.aliases.push(`alias ${e} ${pconf[i][e]}`)
}
} else if (i === "autocmds") {
for (const a of Object.keys(USERCONFIG[i][e])) {
parsedAucmds.push(`autocmd ${e} ${a} ${USERCONFIG[i][e][a]}`)
for (const a of Object.keys(pconf[i][e])) {
parseobj.aucmds.push(`autocmd ${e} ${a} ${pconf[i][e][a]}`)
}
} else if (i === "autocontain") {
parsedAucmds.push(`autocontain ${e} ${USERCONFIG[i][e]}`)
parseobj.aucmds.push(`autocontain ${e} ${pconf[i][e]}`)
} else if (i === "logging") {
// Map the int values in e to a log level
let level
if (USERCONFIG[i][e] === 0) level = "never"
if (USERCONFIG[i][e] === 1) level = "error"
if (USERCONFIG[i][e] === 2) level = "warning"
if (USERCONFIG[i][e] === 3) level = "info"
if (USERCONFIG[i][e] === 4) level = "debug"
parsedLogging.push(`set logging.${e} ${level}`)
if (pconf[i][e] === 0) level = "never"
if (pconf[i][e] === 1) level = "error"
if (pconf[i][e] === 2) level = "warning"
if (pconf[i][e] === 3) level = "info"
if (pconf[i][e] === 4) level = "debug"
parseobj.logging.push(`set logging.${e} ${level}`)
} else {
parsedConf.push(`set ${i}.${e} ${USERCONFIG[i][e]}`)
parseobj.conf.push(`set ${i}.${e} ${pconf[i][e]}`)
}
}
}
}
let genSettings = ``
let bindSettings = ``
let aliasSettings = ``
let aucmdSettings = ``
let auconSettings = ``
let logSettings = ``
if (parsedConf.length > 0)
genSettings = `"General Settings\n${parsedConf.join("\n")}\n\n`
if (parsedBinds.length > 0)
bindSettings = `"Binds\n${parsedBinds.join("\n")}\n\n`
if (parsedAliases.length > 0)
aliasSettings = `"Aliases\n${parsedAliases.join("\n")}\n\n`
if (parsedAucmds.length > 0)
aucmdSettings = `"Autocmds\n${parsedAucmds.join("\n")}\n\n`
if (parsedAucons.length > 0)
auconSettings = `"Autocontainers\n${parsedAucons.join("\n")}\n\n`
if (parsedLogging.length > 0)
logSettings = `"Logging\n" + parsedLogging.join("\n")}\n\n`
return `${genSettings}${bindSettings}${aliasSettings}${aucmdSettings}${auconSettings}${logSettings}`
return parseobj
}
// Listen for changes to the storage and update the USERCONFIG if appropriate.