diff options
Diffstat (limited to 'src/core/ephraim_config.erl')
-rw-r--r-- | src/core/ephraim_config.erl | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/src/core/ephraim_config.erl b/src/core/ephraim_config.erl new file mode 100644 index 0000000..6c49351 --- /dev/null +++ b/src/core/ephraim_config.erl @@ -0,0 +1,55 @@ +-module(ephraim_config). +-compile([debug_info, export_all]). + +-spec init() -> ok. +init() -> + register(ephraim_config, self()), + ephraim ! {self(), started}, + + {ok, Terms} = file:consult(config_path() ++ "/ephraim.cfg"), + Config = dict:from_list(lists:map(fun(Term) -> {element(1, Term), Term} end, Terms)), + + loop(Config). + + +-spec config_path() -> string(). +config_path() -> + Path = case os:getenv("XDG_CONFIG_HOME") of + false -> + os:getenv("HOME") ++ "/.config"; + ConfigHome -> + ConfigHome + end ++ "/ephraim", + case file:read_file_info(Path) of + {ok, _} -> + ok; + {error, _} -> + file:make_dir(Path) + end, + Path. + + +-spec get(atom()) -> tuple() | error. +get(Key) -> + ephraim_config ! {self(), get, Key}, + receive + {config, ok, Key, Value} -> + Value; + {config, error, Key} -> + error + end. + +-spec loop(dict()) -> ok. +loop(Config) -> + receive + stop -> + ok; + {From, get, Key} -> + case dict:find(Key, Config) of + {ok, Value} -> + From ! {config, ok, Key, Value}; + error -> + From ! {config, error, Key} + end, + loop(Config) + end. |