From 5bdd0c813567401f37505025dd4f1fb82b085278 Mon Sep 17 00:00:00 2001 From: SolitudeSF Date: Fri, 11 Oct 2019 18:01:56 +0300 Subject: [PATCH 1/4] Add user specific config path. --- src/boomer.nim | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/boomer.nim b/src/boomer.nim index 3e9b06b..dc1dacb 100644 --- a/src/boomer.nim +++ b/src/boomer.nim @@ -51,10 +51,13 @@ const proc main() = var config = defaultConfig - var configFile = "" + let configFile = block: + if paramCount() > 0: + paramStr(1) + else: + getConfigDir() / "boomer" / "config" - if paramCount() > 0: - configFile = paramStr(1) + if existsFile configFile: config = loadConfig(configFile) echo "Using config: ", config From 59ea7ab324b0e230edbf6a4c02e3fa27dcb650c6 Mon Sep 17 00:00:00 2001 From: SolitudeSF Date: Fri, 11 Oct 2019 17:51:15 +0300 Subject: [PATCH 2/4] Automatically parse config file according to object fields. --- src/config.nim | 46 ++++++++++++++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/src/config.nim b/src/config.nim index 58046ab..8714183 100644 --- a/src/config.nim +++ b/src/config.nim @@ -1,5 +1,4 @@ -import macros -import strutils +import macros, strutils type Config* = object scrollSpeed*: float @@ -16,22 +15,37 @@ const defaultConfig* = Config( fps: 60 ) +func normIdent(s: string): string = + if s.len > 0: + result.add s[0] + for i in 1..s.high: + if s[i] != '_': + result.add s[i].toLowerAscii + +macro parseObject(obj: typed, key, val: string) = + result = newNimNode(nnkCaseStmt).add(newCall("normIdent", key)) + for c in obj.getType[2]: + let a = case c.getType.typeKind + of ntyFloat: + newCall("parseFloat", val) + of ntyInt: + newCall("parseInt", val) + of ntyString: + val + else: + error "Unsupported type: " & c.getType.`$` + val + result.add newNimNode(nnkOfBranch).add( + newLit normIdent $c, + newStmtList(quote do: `obj`.`c` = `a`) + ) + result.add newNimNode(nnkElse).add(quote do: + raise newException(CatchableError, "Unknown config key " & `key`)) + proc loadConfig*(filePath: string): Config = result = defaultConfig for line in filePath.lines: - let pair = line.split('=') + let pair = line.split('=', 1) let key = pair[0].strip let value = pair[1].strip - case key - of "scroll_speed": - result.scroll_speed = value.parseFloat - of "drag_velocity_factor": - result.drag_velocity_factor = value.parseFloat - of "drag_friction": - result.drag_friction = value.parseFloat - of "scale_friction": - result.scale_friction = value.parseFloat - of "fps": - result.fps = value.parseInt - else: - raise newException(Exception, "Unknown config key " & key) + result.parseObject key, value From 9c1bc4f08fb7619f2ecadec4940a2b4efac9af17 Mon Sep 17 00:00:00 2001 From: SolitudeSF Date: Wed, 16 Oct 2019 19:39:18 +0300 Subject: [PATCH 3/4] Remove style insensitive config handling. --- src/config.nim | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/src/config.nim b/src/config.nim index 8714183..a80a531 100644 --- a/src/config.nim +++ b/src/config.nim @@ -15,15 +15,8 @@ const defaultConfig* = Config( fps: 60 ) -func normIdent(s: string): string = - if s.len > 0: - result.add s[0] - for i in 1..s.high: - if s[i] != '_': - result.add s[i].toLowerAscii - macro parseObject(obj: typed, key, val: string) = - result = newNimNode(nnkCaseStmt).add(newCall("normIdent", key)) + result = newNimNode(nnkCaseStmt).add(key) for c in obj.getType[2]: let a = case c.getType.typeKind of ntyFloat: @@ -36,7 +29,7 @@ macro parseObject(obj: typed, key, val: string) = error "Unsupported type: " & c.getType.`$` val result.add newNimNode(nnkOfBranch).add( - newLit normIdent $c, + newLit $c, newStmtList(quote do: `obj`.`c` = `a`) ) result.add newNimNode(nnkElse).add(quote do: From 63930a7acea0d51b9f0ee5cbb1ec84305604d262 Mon Sep 17 00:00:00 2001 From: SolitudeSF Date: Wed, 16 Oct 2019 19:44:53 +0300 Subject: [PATCH 4/4] Add warning when config file is missing. --- src/boomer.nim | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/boomer.nim b/src/boomer.nim index dc1dacb..c28eb59 100644 --- a/src/boomer.nim +++ b/src/boomer.nim @@ -59,6 +59,8 @@ proc main() = if existsFile configFile: config = loadConfig(configFile) + else: + stderr.writeLine configFile & " doesn't exist. Using default values. " echo "Using config: ", config