-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.nim
87 lines (65 loc) · 1.96 KB
/
config.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import
os, streams, strutils, strformat
var
configFolder = getHomeDir() / ".config"/"bg-manager"
configFile = configFolder/"config"
type Mode = enum
randSelect=(1, "random"),
single=(2, "single"),
ordered=(3, "manual")
type
Config* = object
folderPath* : string
imgTimer*: int
mode*: Mode
singleImage*: string
#Creates config file
proc createConfig():void =
#if config file dosent exist, create it
discard existsOrCreateDir(configFolder)
#make sure the config file dosent exists before writing to it.
if fileExists(configFile):
return
writeFile(configFile, "# BG-Manager config")
echo "Config file Created"
proc readConfig(defaultPath: string):Config =
#makes sure the config file exists
var userConfig: Config = Config(
folderPath: defaultPath,
imgTimer: 1,
mode: Mode.randSelect,
singleImage: ""
)
if not fileExists(configFile):
createConfig()
return
var
strm = newFileStream(configFile, fmRead)
line = ""
if isNil(strm):
return
while strm.readLine(line):
if startsWith(line, "#") or strip(line).len() == 0:
continue
var confLine: seq[string] = split(strip(line), '=', 2)
if confLine.len < 2:
echo &"BG-Manager: The Following line is invalid. '{line}' Please Update your config File. This line will be ignored"
continue
var value : string = strip(confLine[1])
case strip(confLine[0]):
of "testVar":
echo confLine[1]
of "bgm.folder", "folder","wallpapers":
userConfig.folderPath = value
of "bgm.timer", "timer", "delay":
try:
userConfig.imgTimer = parseInt(value)
except:
echo &"BG-Manager: '{value}' is not a valid int, ignoring."
of "bgm.mode", "mode":
try:
userConfig.mode = parseEnum[Mode](value)
except:
echo &"BG-Manager: '{value}' is not a valid mode name, ignoring."
strm.close()
return userConfig