about summary refs log tree commit diff stats
path: root/src/main.nim
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-10-28 20:08:09 +0100
committerbptato <nincsnevem662@gmail.com>2024-10-28 20:08:09 +0100
commit6c5922d5f0b2d9d7b6e4c39593c6892a65f74e54 (patch)
tree2485ae5f313f0dd017ba85e86404393e5945b470 /src/main.nim
parentbd4386923056c53e97f0865d846ef52d0666ca56 (diff)
downloadchawan-6c5922d5f0b2d9d7b6e4c39593c6892a65f74e54.tar.gz
config: refactor, accept more possible config dirs
I'm starting to favor dotfiles over XDG basedirs, but there's no reason
why we couldn't have both. So now the search path is:

0. if config was set through -C, use that
1. $CHA_CONFIG_DIR is set -> $CHA_CONFIG_DIR/config.toml
2. $XDG_CONFIG_HOME is set -> $XDG_CONFIG_HOME/chawan/config.toml
3. ~/.config/chawan/config.toml exists -> use that
4. ~/.chawan/config.toml exists -> use that

Notably, this makes it so the default directory is ~/.chawan *if* you
don't have an existing config.toml file. So in that case known_hosts
will be placed in ~/.chawan/known_hosts. However, configurations with a
config in ~/.config/chawan/config.toml continue to work as expected, as
for those the known_hosts file remains inside ~/.config/chawan/.

Finally, I've added a default user CGI directory to reduce friction in
setting CGI up. (Like known_hosts, it's also relative to whatever config
dir you have.)
Diffstat (limited to 'src/main.nim')
-rw-r--r--src/main.nim44
1 files changed, 26 insertions, 18 deletions
diff --git a/src/main.nim b/src/main.nim
index 4f39a5f3..f7d6a87a 100644
--- a/src/main.nim
+++ b/src/main.nim
@@ -3,6 +3,7 @@ import version
 import std/options
 import std/os
 import std/posix
+import std/streams
 
 import chagashi/charset
 import config/chapath
@@ -200,6 +201,28 @@ proc parse(ctx: var ParamParseContext) =
       ctx.pages.add(param)
     inc ctx.i
 
+const defaultConfig = staticRead"res/config.toml"
+
+proc initConfig(ctx: ParamParseContext; config: Config;
+    warnings: var seq[string]): Err[string] =
+  let fs = openConfig(config.configdir, ctx.configPath)
+  if fs == nil and ctx.configPath.isSome:
+    # The user specified a non-existent config file.
+    return err("Failed to open config file " & ctx.configPath.get)
+  putEnv("CHA_CONFIG_DIR", config.configdir)
+  ?config.parseConfig("res", defaultConfig, warnings)
+  when defined(debug):
+    if (let fs = newFileStream(getCurrentDir() / "res/config.toml"); fs != nil):
+      ?config.parseConfig(getCurrentDir(), fs.readAll(), warnings)
+  if fs != nil:
+    ?config.parseConfig(config.configdir, fs.readAll(), warnings)
+  for opt in ctx.opts:
+    ?config.parseConfig(getCurrentDir(), opt, warnings, laxnames = true)
+  config.css.stylesheet &= ctx.stylesheet
+  ?config.initCommands()
+  isCJKAmbiguous = config.display.double_width_ambiguous
+  return ok()
+
 proc main() =
   putEnv("CHA_LIBEXEC_DIR", ChaPath"${%CHA_LIBEXEC_DIR}".unquoteGet())
   let forkserver = newForkServer()
@@ -208,25 +231,10 @@ proc main() =
   let jsrt = newJSRuntime()
   let jsctx = jsrt.newJSContext()
   var warnings = newSeq[string]()
-  let (config, res) = readConfig(ctx.configPath, jsctx)
-  if not res.success:
-    stderr.writeLine(res.errorMsg)
+  let config = Config(jsctx: jsctx)
+  if (let res = ctx.initConfig(config, warnings); res.isNone):
+    stderr.writeLine(res.error)
     quit(1)
-  warnings.add(res.warnings)
-  for opt in ctx.opts:
-    let res = config.parseConfig(getCurrentDir(), opt, laxnames = true,
-      setdir = false)
-    if not res.success:
-      stderr.writeLine(res.errorMsg)
-      quit(1)
-    warnings.add(res.warnings)
-  config.css.stylesheet &= ctx.stylesheet
-  block commands:
-    let res = config.initCommands()
-    if res.isNone:
-      stderr.writeLine("Error parsing commands: " & res.error)
-      quit(1)
-  isCJKAmbiguous = config.display.double_width_ambiguous
   if ctx.pages.len == 0 and stdin.isatty():
     if ctx.visual:
       ctx.pages.add(config.start.visual_home)