Merge pull request #100 from tsoding/config

Improve config experience
pull/102/head
Alexey Kutepov 4 years ago committed by GitHub
commit cdf951b50e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -15,6 +15,7 @@ import opengl, opengl/glx
import la
import strutils
import math
import options
type Shader = tuple[path, content: string]
@ -175,8 +176,11 @@ proc xElevenErrorHandler(display: PDisplay, errorEvent: PXErrorEvent): cint{.cde
echo "X ELEVEN ERROR: ", $(addr errorMessage)
proc main() =
let boomerDir = getConfigDir() / "boomer"
var configFile = boomerDir / "config"
var windowed = false
var delaySec = 0.0
# TODO(#95): Make boomer optionally wait for some kind of event (for example, key press)
block:
proc versionQuit() =
@ -184,36 +188,76 @@ proc main() =
quit "boomer-$#" % [hash[0 .. 7]]
proc usageQuit() =
quit """Usage: boomer [OPTIONS]
-d, --delay <seconds: float> delay execution of the program by provided seconds
-d, --delay <seconds: float> delay execution of the program by provided <seconds>
-h, --help show this help and exit
--new-config [filepath] generate a new default config at [filepath]
-c, --config <filepath> use config at <filepath>
-V, --version show the current version and exit
-w, --windowed windowed mode instead of fullscreen"""
var i = 1
while i <= paramCount():
let arg = paramStr(i)
case arg
of "-d", "--delay":
template asParam(paramVar: untyped, body: untyped) =
if i + 1 > paramCount():
echo "No value is provided for $#" % [arg]
usageQuit()
delaySec = parseFloat(paramStr(i + 1))
let paramVar = paramStr(i + 1)
body
i += 2
of "-w", "--windowed":
windowed = true
template asFlag(body: untyped) =
body
i += 1
template asOptionalParam(paramVar: untyped, body: untyped) =
let paramVar = block:
var resultVal = none(string)
if i + 1 <= paramCount():
let param = paramStr(i + 1)
if len(param) > 0 and param[0] != '-':
resultVal = some(param)
resultVal
body
if paramVar.isNone:
i += 1
else:
i += 2
case arg
of "-d", "--delay":
asParam(delayParam):
delaySec = parseFloat(delayParam)
of "-w", "--windowed":
asFlag():
windowed = true
of "-h", "--help":
usageQuit()
asFlag():
usageQuit()
of "-V", "--version":
versionQuit()
asFlag():
versionQuit()
of "--new-config":
asOptionalParam(configName):
let newConfigPath = configName.get(configFile)
createDir(newConfigPath.splitFile.dir)
if newConfigPath.existsFile:
stdout.write("File ", newConfigPath, " already exists. Replace it? [yn] ")
if stdin.readChar != 'y':
quit "Disaster prevented"
generateDefaultConfig(newConfigPath)
quit "Generated config at $#" % [newConfigPath]
of "-c", "--config":
asParam(configParam):
configFile = configParam
else:
echo "Unknown flag `$#`" % [arg]
usageQuit()
sleep(floor(delaySec * 1000).int)
var config = defaultConfig
let
boomerDir = getConfigDir() / "boomer"
configFile = boomerDir / "config"
if existsFile configFile:
config = loadConfig(configFile)

@ -1,36 +1,16 @@
import macros, strutils
type Config* = object
scrollSpeed*: float
dragFriction*: float
scaleFriction*: float
scroll_speed*: float
drag_friction*: float
scale_friction*: float
const defaultConfig* = Config(
scrollSpeed: 1.5,
dragFriction: 6.0,
scaleFriction: 4.0,
scroll_speed: 1.5,
drag_friction: 6.0,
scale_friction: 4.0,
)
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 rawLine in filePath.lines:
@ -40,4 +20,19 @@ proc loadConfig*(filePath: string): Config =
let pair = line.split('=', 1)
let key = pair[0].strip
let value = pair[1].strip
result.parseObject key, value
case key
of "scroll_speed":
result.scroll_speed = parseFloat(value)
of "drag_friction":
result.drag_friction = parseFloat(value)
of "scale_friction":
result.scale_friction = parseFloat(value)
else:
quit "Unknown config key `$#`" % [key]
proc generateDefaultConfig*(filePath: string) =
var f = open(filePath, fmWrite)
defer: f.close
f.write("scroll_speed = ", defaultConfig.scroll_speed, "\n")
f.write("drag_friction = ", defaultConfig.drag_friction, "\n")
f.write("scale_friction = ", defaultConfig.scale_friction, "\n")

Loading…
Cancel
Save