-
Notifications
You must be signed in to change notification settings - Fork 9
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
1 parent
f1b806d
commit ec22ccb
Showing
66 changed files
with
444 additions
and
294 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#include <karm-app/_embed.h> | ||
#include <karm-app/prefs.h> | ||
#include <karm-json/parse.h> | ||
#include <karm-logger/logger.h> | ||
#include <karm-mime/url.h> | ||
#include <karm-pkg/_embed.h> | ||
#include <karm-sys/dir.h> | ||
#include <karm-sys/file.h> | ||
|
||
namespace Karm::App::_Ember { | ||
|
||
struct XdgConfigPrefs : public Prefs { | ||
Mime::Url _url; | ||
|
||
XdgConfigPrefs(Mime::Url url) | ||
: _url{std::move(url)} {} | ||
|
||
Json::Value _load() { | ||
auto data = Sys::readAllUtf8(_url).unwrapOr("{}"s); | ||
Io::SScan s{data}; | ||
return Json::parse(s).unwrapOr(Json::Object{}); | ||
} | ||
|
||
Res<> _save(Json::Value object) { | ||
auto file = try$(Sys::File::create(_url)); | ||
Io::TextEncoder<> enc{file}; | ||
Io::Emit e{enc}; | ||
return Json::stringify(e, object); | ||
} | ||
|
||
Async::Task<Json::Value> loadAsync(String key, Json::Value defaultValue) { | ||
auto object = _load(); | ||
auto value = object.get(key); | ||
co_return Ok(value == NONE ? defaultValue : value); | ||
} | ||
|
||
Async::Task<> saveAsync(String key, Json::Value value) { | ||
auto object = _load(); | ||
object.set(key, value); | ||
co_return _save(object); | ||
} | ||
}; | ||
|
||
static Opt<XdgConfigPrefs> _globalPrefs; | ||
|
||
static Res<Mime::Url> _resolveConfigDir() { | ||
auto *xdgConfigHome = getenv("XDG_CONFIG_HOME"); | ||
if (xdgConfigHome) | ||
return Ok(Mime::Url::parse(xdgConfigHome) / try$(Pkg::_Embed::currentBundle())); | ||
|
||
auto *home = getenv("HOME"); | ||
if (home) | ||
return Ok(Mime::Url::parse(home) / ".config" / try$(Pkg::_Embed::currentBundle())); | ||
|
||
return Error::notFound("could not find XDG_CONFIG_HOME nor HOME"); | ||
} | ||
|
||
Prefs &globalPrefs() { | ||
if (not _globalPrefs) { | ||
auto url = _resolveConfigDir().unwrap("could not resolve preferences directory"); | ||
Sys::Dir::openOrCreate(url).unwrap("could not create preferences directory"); | ||
logInfo("preferences stored at {}", url); | ||
_globalPrefs = XdgConfigPrefs{url / "configs.json"}; | ||
} | ||
return *_globalPrefs; | ||
} | ||
|
||
} // namespace Karm::App::_Ember |
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
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
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
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,16 @@ | ||
|
||
|
||
#include <karm-app/_embed.h> | ||
#include <karm-app/prefs.h> | ||
|
||
namespace Karm::App::_Ember { | ||
|
||
static Opt<MockPrefs> _globalPrefs; | ||
|
||
Prefs &globalPrefs() { | ||
if (not _globalPrefs) | ||
_globalPrefs = MockPrefs{}; | ||
return *_globalPrefs; | ||
} | ||
|
||
} // namespace Karm::App::_Ember |
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
This file was deleted.
Oops, something went wrong.
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,4 @@ | ||
#pragma once | ||
|
||
#define wasm_import(name) __attribute__((import_module("env"), import_name(#name))) name | ||
#define wasm_export(name) __attribute__((visibility("default"), export_name(#name))) name |
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,13 @@ | ||
#pragma once | ||
|
||
namespace Karm::App { | ||
|
||
struct Prefs; | ||
|
||
} // namespace Karm::App | ||
|
||
namespace Karm::App::_Embed { | ||
|
||
Prefs &globalPrefs(); | ||
|
||
} // namespace Karm::App::_Embed |
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,23 @@ | ||
#include "prefs.h" | ||
|
||
#include "_embed.h" | ||
|
||
namespace Karm::App { | ||
|
||
Async::Task<Json::Value> MockPrefs::loadAsync(String key, Json::Value defaultValue) { | ||
auto item = _store.access(key); | ||
if (item) | ||
co_return *item; | ||
co_return defaultValue; | ||
} | ||
|
||
Async::Task<> MockPrefs::saveAsync(String key, Json::Value value) { | ||
_store.put(key, value); | ||
co_return Ok(); | ||
} | ||
|
||
Prefs &globalPrefs() { | ||
return _Embed::globalPrefs(); | ||
} | ||
|
||
} // namespace Karm::App |
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,26 @@ | ||
#pragma once | ||
|
||
#include <karm-base/async.h> | ||
#include <karm-json/values.h> | ||
|
||
namespace Karm::App { | ||
|
||
struct Prefs { | ||
virtual ~Prefs() = default; | ||
|
||
virtual Async::Task<Json::Value> loadAsync(String key, Json::Value defaultValue = NONE) = 0; | ||
|
||
virtual Async::Task<> saveAsync(String key, Json::Value value) = 0; | ||
}; | ||
|
||
struct MockPrefs : public Prefs { | ||
Json::Object _store; | ||
|
||
Async::Task<Json::Value> loadAsync(String key, Json::Value defaultValue) override; | ||
|
||
Async::Task<> saveAsync(String key, Json::Value value) override; | ||
}; | ||
|
||
Prefs &globalPrefs(); | ||
|
||
} // namespace Karm::App |
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
Oops, something went wrong.