diff --git a/src/boomer.nim b/src/boomer.nim index 3e9b06b..c28eb59 100644 --- a/src/boomer.nim +++ b/src/boomer.nim @@ -51,11 +51,16 @@ 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) + else: + stderr.writeLine configFile & " doesn't exist. Using default values. " echo "Using config: ", config diff --git a/src/config.nim b/src/config.nim index 58046ab..a80a531 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,30 @@ const defaultConfig* = Config( fps: 60 ) +macro parseObject(obj: typed, key, val: string) = + result = newNimNode(nnkCaseStmt).add(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 $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