about summary refs log tree commit diff stats
path: root/src/config
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2021-11-19 21:42:39 +0100
committerbptato <nincsnevem662@gmail.com>2021-11-19 21:52:31 +0100
commit8bbff1f79920fa8175da7425f8f23ad08b97f79e (patch)
tree830abcc6918f8c96bce5538eff5384518876e378 /src/config
parent42aacf6bf1a52a8ebad902d8ee5adeef57f8822a (diff)
downloadchawan-8bbff1f79920fa8175da7425f8f23ad08b97f79e.tar.gz
User stylesheets and applyStylesheets optimizations
Diffstat (limited to 'src/config')
-rw-r--r--src/config/config.nim61
1 files changed, 51 insertions, 10 deletions
diff --git a/src/config/config.nim b/src/config/config.nim
index d055af53..89f80fae 100644
--- a/src/config/config.nim
+++ b/src/config/config.nim
@@ -37,10 +37,12 @@ type
   StaticConfig = object
     nmap: ActionMap
     lemap: ActionMap
+    stylesheet*: string
 
   Config = object
     nmap*: ActionMap
     lemap*: ActionMap
+    stylesheet*: string
 
 func getConfig(s: StaticConfig): Config =
   return Config(nmap: s.nmap, lemap: s.lemap)
@@ -105,15 +107,53 @@ func constructActionTable*(origTable: ActionMap): ActionMap =
     newTable[realk] = v
   return newTable
 
+proc readUserStylesheet(dir: string, file: string): string =
+  if file.len == 0:
+    return ""
+  if file[0] == '~' or file[0] == '/':
+    var f: File
+    if f.open(expandPath(file)):
+      result = f.readAll()
+      f.close()
+  else:
+    var f: File
+    if f.open(dir / file):
+      result = f.readAll()
+      f.close()
+
 proc parseConfigLine[T](line: string, config: var T) =
   if line.len == 0 or line[0] == '#':
     return
-  let cmd = line.split(' ')
+  var cmd: seq[string]
+  var s = ""
+  var quote = false
+  var escape = false
+  for c in line:
+    if escape:
+      escape = false
+      s &= c
+      continue
+
+    if not quote and c == ' ' and s.len > 0:
+      cmd.add(s)
+      s = ""
+    elif c == '"':
+      quote = not quote
+    elif c == '\\' and not quote:
+      escape = true
+    else:
+      s &= c
+  if s.len > 0:
+    cmd.add(s)
+
   if cmd.len == 3:
     if cmd[0] == "nmap":
       config.nmap[getRealKey(cmd[1])] = parseEnum[TwtAction]("ACTION_" & cmd[2])
     elif cmd[0] == "lemap":
       config.lemap[getRealKey(cmd[1])] = parseEnum[TwtAction]("ACTION_" & cmd[2])
+  elif cmd.len == 2:
+    if cmd[0] == "stylesheet":
+      config.stylesheet = cmd[1]
 
 proc staticReadConfig(): StaticConfig =
   let default = staticRead"res/config"
@@ -124,25 +164,26 @@ proc staticReadConfig(): StaticConfig =
   result.lemap = constructActionTable(result.lemap)
 
 const defaultConfig = staticReadConfig()
-var gconfig* = getConfig(defaultConfig)
+var gconfig*: Config
 
-proc readConfig(filename: string) =
+proc readConfig(dir: string) =
   var f: File
-  let status = f.open(filename, fmRead)
-  var nmap: ActionMap
-  var lemap: ActionMap
+  let status = f.open(dir / "config", fmRead)
   if status:
     var line: TaintedString
     while f.readLine(line):
       parseConfigLine(line, gconfig)
 
-    gconfig.nmap = constructActionTable(nmap)
-    gconfig.lemap = constructActionTable(lemap)
+    gconfig.nmap = constructActionTable(gconfig.nmap)
+    gconfig.lemap = constructActionTable(gconfig.lemap)
+    gconfig.stylesheet = readUserStylesheet(dir, gconfig.stylesheet)
+    f.close()
 
 proc readConfig*() =
+  gconfig = getConfig(defaultConfig)
   when defined(debug):
-    readConfig("res" / "config")
-  readConfig(getConfigDir() / "twt" / "config")
+    readConfig(getCurrentDir() / "res")
+  readConfig(getConfigDir() / "twt")
 
 proc getNormalAction*(s: string): TwtAction =
   if gconfig.nmap.hasKey(s):