This commit is contained in:
Mars 2024-06-09 20:14:51 -04:00
parent 94d08a02d6
commit 4d1e69bbe5
Signed by: pupbrained
GPG key ID: 874E22DF2F9DFCB5
8 changed files with 167 additions and 133 deletions

View file

@ -5,11 +5,13 @@
#include <utility>
#include <variant>
#include "util/numtypes.h"
// Define an error type
class Error {
public:
explicit Error(std::string message) : m_Message(std::move(message)) {}
[[nodiscard]] const std::string& message() const { return m_Message; }
[[nodiscard]] fn message() const -> const std::string& { return m_Message; }
private:
std::string m_Message;
@ -29,10 +31,10 @@ class Result<void> {
Result(Error&& error) : m_Result(std::move(error)) {}
// Check if the result is an error
[[nodiscard]] bool isOk() const {
[[nodiscard]] fn isOk() const -> bool {
return std::holds_alternative<std::monostate>(m_Result);
}
[[nodiscard]] bool isErr() const {
[[nodiscard]] fn isErr() const -> bool {
return std::holds_alternative<Error>(m_Result);
}
@ -44,7 +46,7 @@ class Result<void> {
}
// Get the error or throw an exception if it is a value
[[nodiscard]] const Error& error() const {
[[nodiscard]] fn error() const -> const Error& {
if (isOk()) {
throw std::logic_error("Attempted to access error of an ok Result");
}
@ -66,15 +68,15 @@ class Result {
Result(Error&& error) : m_Result(std::move(error)) {}
// Check if the result is an error
[[nodiscard]] bool isOk() const {
[[nodiscard]] fn isOk() const -> bool {
return std::holds_alternative<T>(m_Result);
}
[[nodiscard]] bool isErr() const {
[[nodiscard]] fn isErr() const -> bool {
return std::holds_alternative<Error>(m_Result);
}
// Get the value or throw an exception if it is an error
const T& value() const {
fn value() const -> const T& {
if (isErr()) {
throw std::logic_error("Attempted to access value of an error Result");
}
@ -82,7 +84,7 @@ class Result {
}
// Get the error or throw an exception if it is a value
[[nodiscard]] const Error& error() const {
[[nodiscard]] fn error() const -> const Error& {
if (isOk()) {
throw std::logic_error("Attempted to access error of an ok Result");
}
@ -90,7 +92,7 @@ class Result {
}
// Optional: Get the value or provide a default
T valueOr(const T& defaultValue) const {
fn valueOr(const T& defaultValue) const -> T {
return isOk() ? std::get<T>(m_Result) : defaultValue;
}
@ -99,8 +101,8 @@ class Result {
};
template <typename T>
auto Ok(T&& value) {
fn Ok(T&& value) {
return Result<std::decay_t<T>>(std::forward<T>(value));
}
inline auto Ok() { return Result<void>(); }
inline fn Ok() -> Result<void> { return {}; }

View file

@ -1,7 +1,7 @@
#include "config.h"
#define DEFINE_GETTER(class_name, type, name) \
type class_name::get##name() const { return m_##name; }
fn class_name::get##name() const->type { return m_##name; }
DEFINE_GETTER(Config, const General, General)
DEFINE_GETTER(Config, const NowPlaying, NowPlaying)
@ -12,7 +12,7 @@ DEFINE_GETTER(Weather, const Weather::Location, Location)
DEFINE_GETTER(Weather, const std::string, ApiKey)
DEFINE_GETTER(Weather, const std::string, Units)
const Config& Config::getInstance() {
fn Config::getInstance() -> const Config& {
static const auto* INSTANCE =
new Config(rfl::toml::load<Config>("./config.toml").value());
return *INSTANCE;
@ -25,14 +25,14 @@ Config::Config(General general, NowPlaying now_playing, Weather weather)
General::General(std::string name) : m_Name(std::move(name)) {}
NowPlaying::NowPlaying(bool enable) : m_Enabled(enable) {}
NowPlaying::NowPlaying(bool enabled) : m_Enabled(enabled) {}
Weather::Weather(Location location, std::string api_key, std::string units)
: m_Location(std::move(location)),
m_ApiKey(std::move(api_key)),
m_Units(std::move(units)) {}
WeatherImpl WeatherImpl::from_class(const Weather& weather) noexcept {
fn WeatherImpl::from_class(const Weather& weather) noexcept -> WeatherImpl {
return {
.location = weather.getLocation(),
.api_key = weather.getApiKey(),
@ -40,22 +40,27 @@ WeatherImpl WeatherImpl::from_class(const Weather& weather) noexcept {
};
}
Weather WeatherImpl::to_class() const { return {location, api_key, units}; }
fn WeatherImpl::to_class() const -> Weather {
return {location, api_key, units};
}
GeneralImpl GeneralImpl::from_class(const General& general) noexcept {
fn GeneralImpl::from_class(const General& general) noexcept -> GeneralImpl {
return {general.getName()};
}
General GeneralImpl::to_class() const { return {name}; }
fn GeneralImpl::to_class() const -> General { return {name}; }
NowPlayingImpl NowPlayingImpl::from_class(
const NowPlaying& now_playing) noexcept {
// clang-format off
fn NowPlayingImpl::from_class(
const NowPlaying& now_playing
) noexcept -> NowPlayingImpl {
return {.enabled = now_playing.getEnabled()};
}
//clang-format on
NowPlaying NowPlayingImpl::to_class() const { return {enabled}; }
fn NowPlayingImpl::to_class() const -> NowPlaying { return {enabled}; }
ConfigImpl ConfigImpl::from_class(const Config& config) noexcept {
fn ConfigImpl::from_class(const Config& config) noexcept -> ConfigImpl {
return {
.general = config.getGeneral(),
.now_playing = config.getNowPlaying(),
@ -63,4 +68,6 @@ ConfigImpl ConfigImpl::from_class(const Config& config) noexcept {
};
}
Config ConfigImpl::to_class() const { return {general, now_playing, weather}; }
fn ConfigImpl::to_class() const -> Config {
return {general, now_playing, weather};
}

View file

@ -10,30 +10,30 @@
class Weather {
public:
using percentage = rfl::Validator<i8, rfl::Minimum<0>, rfl::Maximum<100>>;
using degrees = rfl::Validator<u16, rfl::Minimum<0>, rfl::Maximum<360>>;
using percentage = rfl::Validator<i8, rfl::Minimum<0>, rfl::Maximum<100>>;
struct Condition {
usize id;
rfl::Rename<"main", std::string> group;
std::string description;
rfl::Rename<"icon", std::string> icon_id;
std::string icon;
std::string main;
usize id;
};
struct Main {
f64 feels_like;
f64 temp;
f64 temp_max;
f64 temp_min;
f64 feels_like;
isize pressure;
std::optional<isize> sea_level;
std::optional<isize> grnd_level;
percentage humidity;
std::optional<isize> grnd_level;
std::optional<isize> sea_level;
};
struct Wind {
f64 speed;
degrees deg;
f64 speed;
std::optional<f64> gust;
};
@ -60,21 +60,21 @@ class Weather {
};
struct WeatherOutput {
Clouds clouds;
Main main;
Sys sys;
Wind wind;
isize timezone;
isize visibility;
Main main;
Clouds clouds;
rfl::Rename<"coord", Coords> coords;
std::optional<Precipitation> rain;
std::optional<Precipitation> snow;
std::vector<Condition> weather;
std::string base;
std::string name;
Sys sys;
std::vector<Condition> weather;
usize cod;
usize dt;
usize id;
Wind wind;
};
using Location = std::variant<std::string, Coords>;
@ -87,10 +87,10 @@ class Weather {
public:
Weather(Location location, std::string api_key, std::string units);
[[nodiscard]] WeatherOutput getWeatherInfo() const;
[[nodiscard]] const Location getLocation() const;
[[nodiscard]] const std::string getApiKey() const;
[[nodiscard]] const std::string getUnits() const;
[[nodiscard]] fn getWeatherInfo() const -> WeatherOutput;
[[nodiscard]] fn getLocation() const -> const Location;
[[nodiscard]] fn getApiKey() const -> const std::string;
[[nodiscard]] fn getUnits() const -> const std::string;
};
struct WeatherImpl {
@ -98,9 +98,9 @@ struct WeatherImpl {
std::string api_key;
std::string units;
static WeatherImpl from_class(const Weather& weather) noexcept;
static fn from_class(const Weather& weather) noexcept -> WeatherImpl;
[[nodiscard]] Weather to_class() const;
[[nodiscard]] fn to_class() const -> Weather;
};
class General {
@ -110,15 +110,15 @@ class General {
public:
General(std::string name);
[[nodiscard]] const std::string getName() const;
[[nodiscard]] fn getName() const -> const std::string;
};
struct GeneralImpl {
std::string name;
static GeneralImpl from_class(const General& general) noexcept;
static fn from_class(const General& general) noexcept -> GeneralImpl;
[[nodiscard]] General to_class() const;
[[nodiscard]] fn to_class() const -> General;
};
class NowPlaying {
@ -128,15 +128,16 @@ class NowPlaying {
public:
NowPlaying(bool enabled);
[[nodiscard]] bool getEnabled() const;
[[nodiscard]] fn getEnabled() const -> bool;
};
struct NowPlayingImpl {
bool enabled;
static NowPlayingImpl from_class(const NowPlaying& now_playing) noexcept;
static fn from_class(const NowPlaying& now_playing
) noexcept -> NowPlayingImpl;
[[nodiscard]] NowPlaying to_class() const;
[[nodiscard]] fn to_class() const -> NowPlaying;
};
class Config {
@ -148,11 +149,11 @@ class Config {
public:
Config(General general, NowPlaying now_playing, Weather weather);
static const Config& getInstance();
static fn getInstance() -> const Config&;
[[nodiscard]] const Weather getWeather() const;
[[nodiscard]] const General getGeneral() const;
[[nodiscard]] const NowPlaying getNowPlaying() const;
[[nodiscard]] fn getWeather() const -> const Weather;
[[nodiscard]] fn getGeneral() const -> const General;
[[nodiscard]] fn getNowPlaying() const -> const NowPlaying;
};
struct ConfigImpl {
@ -160,16 +161,17 @@ struct ConfigImpl {
NowPlaying now_playing;
Weather weather;
static ConfigImpl from_class(const Config& config) noexcept;
static fn from_class(const Config& config) noexcept -> ConfigImpl;
[[nodiscard]] Config to_class() const;
[[nodiscard]] fn to_class() const -> Config;
};
// Parsers for Config classes
namespace rfl::parsing {
template <class ReaderType, class WriterType, class ProcessorsType>
struct Parser<ReaderType, WriterType, Weather, ProcessorsType>
: public CustomParser<ReaderType,
: public CustomParser<
ReaderType,
WriterType,
ProcessorsType,
Weather,
@ -177,7 +179,8 @@ namespace rfl::parsing {
template <class ReaderType, class WriterType, class ProcessorsType>
struct Parser<ReaderType, WriterType, General, ProcessorsType>
: public CustomParser<ReaderType,
: public CustomParser<
ReaderType,
WriterType,
ProcessorsType,
General,
@ -185,7 +188,8 @@ namespace rfl::parsing {
template <class ReaderType, class WriterType, class ProcessorsType>
struct Parser<ReaderType, WriterType, NowPlaying, ProcessorsType>
: public CustomParser<ReaderType,
: public CustomParser<
ReaderType,
WriterType,
ProcessorsType,
NowPlaying,
@ -193,7 +197,8 @@ namespace rfl::parsing {
template <class ReaderType, class WriterType, class ProcessorsType>
struct Parser<ReaderType, WriterType, Config, ProcessorsType>
: public CustomParser<ReaderType,
: public CustomParser<
ReaderType,
WriterType,
ProcessorsType,
Config,

View file

@ -8,11 +8,12 @@
using WeatherOutput = Weather::WeatherOutput;
// Function to read cache from file
Result<WeatherOutput> ReadCacheFromFile() {
fn ReadCacheFromFile() -> Result<WeatherOutput> {
const std::string cacheFile = "/tmp/weather_cache.json";
std::ifstream ifs(cacheFile);
if (!ifs.is_open()) return Error("Cache file not found.");
if (!ifs.is_open())
return Error("Cache file not found.");
fmt::println("Reading from cache file...");
@ -32,12 +33,13 @@ Result<WeatherOutput> ReadCacheFromFile() {
}
// Function to write cache to file
Result<> WriteCacheToFile(const WeatherOutput& data) {
fn WriteCacheToFile(const WeatherOutput& data) -> Result<> {
const std::string cacheFile = "/tmp/weather_cache.json";
fmt::println("Writing to cache file...");
std::ofstream ofs(cacheFile);
if (!ofs.is_open()) return Error("Failed to open cache file for writing.");
if (!ofs.is_open())
return Error("Failed to open cache file for writing.");
ofs << rfl::json::write(data);
@ -46,10 +48,8 @@ Result<> WriteCacheToFile(const WeatherOutput& data) {
return Ok();
}
size_t WriteCallback(void* contents,
size_t size,
size_t nmemb,
std::string* buffer) {
fn WriteCallback(void* contents, size_t size, size_t nmemb, std::string* buffer)
-> size_t {
size_t realsize = size * nmemb;
buffer->append(static_cast<char*>(contents), realsize);
@ -58,7 +58,7 @@ size_t WriteCallback(void* contents,
}
// Function to make API request
Result<WeatherOutput> MakeApiRequest(const std::string& url) {
fn MakeApiRequest(const std::string& url) -> Result<WeatherOutput> {
fmt::println("Making API request...");
CURL* curl = curl_easy_init();
@ -73,8 +73,9 @@ Result<WeatherOutput> MakeApiRequest(const std::string& url) {
curl_easy_cleanup(curl);
if (res != CURLE_OK)
return Error(fmt::format("Failed to perform cURL request: {}",
curl_easy_strerror(res)));
return Error(fmt::format(
"Failed to perform cURL request: {}", curl_easy_strerror(res)
));
fmt::println("Received response from API.");
// Parse the JSON response
@ -88,7 +89,7 @@ Result<WeatherOutput> MakeApiRequest(const std::string& url) {
}
// Core function to get weather information
WeatherOutput Weather::getWeatherInfo() const {
fn Weather::getWeatherInfo() const -> WeatherOutput {
using namespace std::chrono;
const Location loc = m_Location;
@ -116,15 +117,19 @@ WeatherOutput Weather::getWeatherInfo() const {
if (holds_alternative<std::string>(loc)) {
const std::string city = get<std::string>(loc);
const char* location = curl_easy_escape(nullptr, city.c_str(),
static_cast<int>(city.length()));
const char* location = curl_easy_escape(
nullptr, city.c_str(), static_cast<int>(city.length())
);
fmt::println("City: {}", location);
const std::string apiUrl = fmt::format(
"https://api.openweathermap.org/data/2.5/"
"weather?q={}&appid={}&units={}",
location, apiKey, units);
location,
apiKey,
units
);
result = MakeApiRequest(apiUrl).value();
} else {
@ -135,7 +140,11 @@ WeatherOutput Weather::getWeatherInfo() const {
const std::string apiUrl = fmt::format(
"https://api.openweathermap.org/data/2.5/"
"weather?lat={:.3f}&lon={:.3f}&appid={}&units={}",
lat, lon, apiKey, units);
lat,
lon,
apiKey,
units
);
result = MakeApiRequest(apiUrl).value();
}

View file

@ -11,13 +11,14 @@ struct BytesToGiB {
u64 value;
};
constexpr u64 GIB = 1'073'741'824;
template <>
struct fmt::formatter<BytesToGiB> : formatter<double> {
template <typename FmtCtx>
fn format(const BytesToGiB BTG, FmtCtx& ctx) -> typename FmtCtx::iterator {
typename FmtCtx::iterator out = formatter<double>::format(
static_cast<double>(BTG.value) / pow(1024, 3), ctx
);
typename FmtCtx::iterator out =
formatter<double>::format(static_cast<double>(BTG.value) / GIB, ctx);
*out++ = 'G';
*out++ = 'i';
*out++ = 'B';

View file

@ -5,7 +5,7 @@
#include "macos/bridge.h"
#include "os.h"
u64 GetMemInfo() {
fn GetMemInfo() -> u64 {
u64 mem = 0;
usize size = sizeof(mem);
@ -14,7 +14,7 @@ u64 GetMemInfo() {
return mem;
}
std::string GetNowPlaying() {
fn GetNowPlaying() -> std::string {
if (const char* title = GetCurrentPlayingTitle();
const char* artist = GetCurrentPlayingArtist())
return "Now Playing: " + std::string(artist) + " - " + std::string(title);
@ -22,6 +22,6 @@ std::string GetNowPlaying() {
return "No song playing";
}
const char* GetOSVersion() { return GetMacOSVersion(); };
fn GetOSVersion() -> const char* { return GetMacOSVersion(); };
#endif

View file

@ -9,10 +9,12 @@
+ (NSString*)macOSVersion;
@end
#else
#include "util/numtypes.h"
extern "C" {
const char* GetCurrentPlayingTitle();
const char* GetCurrentPlayingArtist();
const char* GetMacOSVersion();
fn GetCurrentPlayingTitle() -> const char*;
fn GetCurrentPlayingArtist() -> const char*;
fn GetMacOSVersion() -> const char*;
}
#endif
#endif

View file

@ -79,7 +79,8 @@ using MRMediaRemoteGetNowPlayingInfoFunction = void (*)(
NSNumber* majorVersionNumber = @(osVersion.majorVersion);
NSString* versionName = versionNames[majorVersionNumber];
if (versionName == nil) versionName = @"Unknown";
if (versionName == nil)
versionName = @"Unknown";
NSString* fullVersion =
[NSString stringWithFormat:@"macOS %@ %@", version, versionName];
@ -88,37 +89,44 @@ using MRMediaRemoteGetNowPlayingInfoFunction = void (*)(
}
@end
#include "util/numtypes.h"
extern "C" {
const char* GetCurrentPlayingTitle() {
fn GetCurrentPlayingTitle() -> const char* {
NSDictionary* metadata = [Bridge currentPlayingMetadata];
if (metadata == nil) return nullptr;
if (metadata == nil)
return nullptr;
NSString* title =
[metadata objectForKey:@"kMRMediaRemoteNowPlayingInfoTitle"];
if (title) return strdup([title UTF8String]);
if (title)
return strdup([title UTF8String]);
return nullptr;
}
const char* GetCurrentPlayingArtist() {
fn GetCurrentPlayingArtist() -> const char* {
NSDictionary* metadata = [Bridge currentPlayingMetadata];
if (metadata == nil) return nullptr;
if (metadata == nil)
return nullptr;
NSString* artist =
[metadata objectForKey:@"kMRMediaRemoteNowPlayingInfoArtist"];
if (artist) return strdup([artist UTF8String]);
if (artist)
return strdup([artist UTF8String]);
return nullptr;
}
const char* GetMacOSVersion() {
fn GetMacOSVersion() -> const char* {
NSString* version = [Bridge macOSVersion];
if (version) return strdup([version UTF8String]);
if (version)
return strdup([version UTF8String]);
return nullptr;
}