-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.