-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
al1-ce
committed
Nov 24, 2022
1 parent
b1119a7
commit 7c5f6f2
Showing
6 changed files
with
209 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
module pkm.config; | ||
|
||
import std.conv: to; | ||
import std.file: exists; | ||
import std.stdio: writeln; | ||
|
||
import dyaml; | ||
|
||
Config getConfig(string[] paths) { | ||
foreach (path; paths) { | ||
if (path.exists) { | ||
return __getConfig(path); | ||
} | ||
} | ||
return Config(); | ||
} | ||
|
||
Config __getConfig(string configPath) { | ||
Node root; | ||
try { | ||
root = Loader.fromFile(configPath).load(); | ||
} catch (YAMLException e) { | ||
return Config(); | ||
} | ||
|
||
Config conf; | ||
|
||
if (root.type != NodeType.mapping) return conf; | ||
|
||
root.getKey!string(&conf.yaypath, "yaypath"); | ||
root.getKey!bool(&conf.yaysearch, "yaysearch"); | ||
root.getKey!bool(&conf.color, "color"); | ||
root.getKey!bool(&conf.auronly, "auronly"); | ||
|
||
return conf; | ||
} | ||
|
||
string configGetGlobal(string configPath, string field) { | ||
Node root = Loader.fromFile(configPath).load(); | ||
|
||
if (root.type != NodeType.mapping) return ""; | ||
if (!root.containsKeyAs!string(field)) return ""; | ||
|
||
return root[field].as!string; | ||
} | ||
|
||
private bool containsKeyType(Node node, string key, NodeType type) { | ||
if (node.containsKey(key)) { | ||
if (node[key].type == type) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
private bool containsKeyAs(T)(Node node, string key) { | ||
if (node.containsKey(key)) { | ||
if (node[key].convertsTo!T) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
private void getKey(T)(Node node, T* variable, string field) { | ||
if (node.containsKeyAs!T(field)) { | ||
*variable = node[field].as!T; | ||
} | ||
} | ||
|
||
|
||
struct Config { | ||
string yaypath = ""; | ||
bool yaysearch = false; | ||
bool color = true; | ||
bool auronly = false; | ||
} |
Oops, something went wrong.