some cleanup and stuff

This commit is contained in:
Mars 2024-06-14 01:41:59 -04:00
parent 138191227f
commit afbba70f92
Signed by: pupbrained
GPG key ID: 874E22DF2F9DFCB5
13 changed files with 203 additions and 160 deletions

View file

@ -10,6 +10,7 @@ AllowShortLoopsOnASingleLine: true
BasedOnStyle: Chromium
BinPackArguments: false
BinPackParameters: false
FixNamespaceComments: false
IndentAccessModifiers: false
IndentExternBlock: Indent
NamespaceIndentation: All

View file

@ -13,6 +13,7 @@ Checks: >
-hicpp-*,
-llvm-include-order,
-llvm-include-order,
-llvm-namespace-comment,
-llvmlibc-*,
-misc-non-private-member-variables-in-classes,
-readability-braces-around-statements,

View file

@ -2,11 +2,11 @@
"nodes": {
"nixpkgs": {
"locked": {
"lastModified": 1717646450,
"narHash": "sha256-KE+UmfSVk5PG8jdKdclPVcMrUB8yVZHbsjo7ZT1Bm3c=",
"lastModified": 1718149104,
"narHash": "sha256-Ds1QpobBX2yoUDx9ZruqVGJ/uQPgcXoYuobBguyKEh8=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "818dbe2f96df233d2041739d6079bb616d3e5597",
"rev": "e913ae340076bbb73d9f4d3d065c2bca7caafb16",
"type": "github"
},
"original": {
@ -59,11 +59,11 @@
"nixpkgs": "nixpkgs_2"
},
"locked": {
"lastModified": 1717850719,
"narHash": "sha256-npYqVg+Wk4oxnWrnVG7416fpfrlRhp/lQ6wQ4DHI8YE=",
"lastModified": 1718271476,
"narHash": "sha256-35hUMmFesmchb+u7heKHLG5B6c8fBOcSYo0jj0CHLes=",
"owner": "numtide",
"repo": "treefmt-nix",
"rev": "4fc1c45a5f50169f9f29f6a98a438fb910b834ed",
"rev": "e75ba0a6bb562d2ce275db28f6a36a2e4fd81391",
"type": "github"
},
"original": {

3
include/util/macros.h Normal file
View file

@ -0,0 +1,3 @@
#define fn auto
#define DEFINE_GETTER(class_name, type, name) \
fn class_name::get##name() const->type { return m_##name; }

View file

@ -3,8 +3,6 @@
#include <cstddef>
#include <cstdint>
#define fn auto
// Unsigned integers
using u8 = std::uint8_t;
using u16 = std::uint16_t;

View file

@ -4,7 +4,7 @@ project(
default_options: [
'objc_std=c++20',
'objcpp_std=c++20',
'cpp_std=c++23',
'cpp_std=c++20',
'default_library=static',
'warning_level=everything',
'buildtype=debugoptimized'

View file

@ -1,32 +1,12 @@
#include "config.h"
#define DEFINE_GETTER(class_name, type, name) \
fn class_name::get##name() const->type { return m_##name; }
DEFINE_GETTER(Config, const General, General)
DEFINE_GETTER(Config, const NowPlaying, NowPlaying)
DEFINE_GETTER(Config, const Weather, Weather)
DEFINE_GETTER(General, const std::string, Name)
DEFINE_GETTER(NowPlaying, bool, Enabled)
// -------------
// -- Weather --
// -------------
DEFINE_GETTER(Weather, const Weather::Location, Location)
DEFINE_GETTER(Weather, const std::string, ApiKey)
DEFINE_GETTER(Weather, const std::string, Units)
fn Config::getInstance() -> const Config& {
static const auto* INSTANCE =
new Config(rfl::toml::load<Config>("./config.toml").value());
return *INSTANCE;
}
Config::Config(General general, NowPlaying now_playing, Weather weather)
: m_General(std::move(general)),
m_NowPlaying(now_playing),
m_Weather(std::move(weather)) {}
General::General(std::string name) : m_Name(std::move(name)) {}
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)),
@ -43,22 +23,56 @@ fn WeatherImpl::from_class(const Weather& weather) noexcept -> WeatherImpl {
fn WeatherImpl::to_class() const -> Weather {
return {location, api_key, units};
}
// ------------
// -------------
// -- General --
// -------------
DEFINE_GETTER(General, const std::string, Name)
General::General(std::string name) : m_Name(std::move(name)) {}
fn GeneralImpl::from_class(const General& general) noexcept -> GeneralImpl {
return {general.getName()};
}
fn GeneralImpl::to_class() const -> General { return {name}; }
// -------------
// clang-format off
fn NowPlayingImpl::from_class(
const NowPlaying& now_playing
// ----------------
// -- NowPlaying --
// ----------------
DEFINE_GETTER(NowPlaying, bool, Enabled)
NowPlaying::NowPlaying(bool enabled) : m_Enabled(enabled) {}
fn NowPlayingImpl::from_class(const NowPlaying& now_playing
) noexcept -> NowPlayingImpl {
return {.enabled = now_playing.getEnabled()};
}
//clang-format on
fn NowPlayingImpl::to_class() const -> NowPlaying { return {enabled.value_or(false)}; }
fn NowPlayingImpl::to_class() const -> NowPlaying {
return {enabled.value_or(false)};
}
// ----------------
// ------------
// -- Config --
// ------------
DEFINE_GETTER(Config, const General, General)
DEFINE_GETTER(Config, const NowPlaying, NowPlaying)
DEFINE_GETTER(Config, const Weather, Weather)
Config::Config(General general, NowPlaying now_playing, Weather weather)
: m_General(std::move(general)),
m_NowPlaying(now_playing),
m_Weather(std::move(weather)) {}
fn Config::getInstance() -> const Config& {
static const auto* INSTANCE =
new Config(rfl::toml::load<Config>("./config.toml").value());
return *INSTANCE;
}
fn ConfigImpl::from_class(const Config& config) noexcept -> ConfigImpl {
return {
@ -71,3 +85,4 @@ fn ConfigImpl::from_class(const Config& config) noexcept -> ConfigImpl {
fn ConfigImpl::to_class() const -> Config {
return {general, now_playing, weather};
}
// ------------

View file

@ -4,104 +4,11 @@
#include <rfl.hpp>
#include <rfl/toml.hpp>
#include <string>
#include <variant>
#include "util/numtypes.h"
#include "util/macros.h"
#include "weather.h"
class Weather {
public:
using degrees = rfl::Validator<u16, rfl::Minimum<0>, rfl::Maximum<360>>;
using percentage = rfl::Validator<i8, rfl::Minimum<0>, rfl::Maximum<100>>;
struct Condition {
std::string description;
std::string icon;
std::string main;
usize id;
};
struct Main {
f64 feels_like;
f64 temp;
f64 temp_max;
f64 temp_min;
isize pressure;
percentage humidity;
std::optional<isize> grnd_level;
std::optional<isize> sea_level;
};
struct Wind {
degrees deg;
f64 speed;
std::optional<f64> gust;
};
struct Precipitation {
rfl::Rename<"1h", f64> one_hour;
rfl::Rename<"3h", f64> three_hours;
};
struct Sys {
std::string country;
usize id;
usize sunrise;
usize sunset;
usize type;
};
struct Clouds {
percentage all;
};
struct Coords {
double lat;
double lon;
};
struct WeatherOutput {
Clouds clouds;
isize timezone;
isize visibility;
Main main;
rfl::Rename<"coord", Coords> coords;
std::optional<Precipitation> rain;
std::optional<Precipitation> snow;
std::string base;
std::string name;
std::vector<Condition> weather;
Sys sys;
usize cod;
usize dt;
usize id;
Wind wind;
};
using Location = std::variant<std::string, Coords>;
private:
Location m_Location;
std::string m_ApiKey;
std::string m_Units;
public:
Weather(Location location, std::string api_key, std::string units);
[[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 {
Weather::Location location;
std::string api_key;
std::string units;
static fn from_class(const Weather& weather) noexcept -> WeatherImpl;
[[nodiscard]] fn to_class() const -> Weather;
};
// TODO: Make config values optional and supply defaults
class General {
private:
@ -168,15 +75,6 @@ struct ConfigImpl {
// Parsers for Config classes
namespace rfl::parsing {
template <class ReaderType, class WriterType, class ProcessorsType>
struct Parser<ReaderType, WriterType, Weather, ProcessorsType>
: public CustomParser<
ReaderType,
WriterType,
ProcessorsType,
Weather,
WeatherImpl> {};
template <class ReaderType, class WriterType, class ProcessorsType>
struct Parser<ReaderType, WriterType, General, ProcessorsType>
: public CustomParser<
@ -203,4 +101,4 @@ namespace rfl::parsing {
ProcessorsType,
Config,
ConfigImpl> {};
} // namespace rfl::parsing
}

View file

@ -2,16 +2,16 @@
#include <fmt/core.h>
#include <rfl/json.hpp>
#include <rfl/json/load.hpp>
#include <util/result.h>
#include "config.h"
#include "weather.h"
#include "util/result.h"
using WeatherOutput = Weather::WeatherOutput;
// Function to read cache from file
fn ReadCacheFromFile() -> Result<WeatherOutput> {
const std::string cacheFile = "/tmp/weather_cache.json";
std::ifstream ifs(cacheFile);
std::ifstream ifs("/tmp/weather_cache.json");
if (!ifs.is_open())
return Error("Cache file not found.");
@ -35,9 +35,9 @@ fn ReadCacheFromFile() -> Result<WeatherOutput> {
// Function to write cache to file
fn WriteCacheToFile(const WeatherOutput& data) -> Result<> {
const std::string cacheFile = "/tmp/weather_cache.json";
fmt::println("Writing to cache file...");
std::ofstream ofs(cacheFile);
std::ofstream ofs("/tmp/weather_cache.json");
if (!ofs.is_open())
return Error("Failed to open cache file for writing.");
@ -49,7 +49,8 @@ fn WriteCacheToFile(const WeatherOutput& data) -> Result<> {
return Ok();
}
fn WriteCallback(void* contents, size_t size, size_t nmemb, std::string* str) -> size_t {
fn WriteCallback(void* contents, size_t size, size_t nmemb, std::string* str)
-> size_t {
size_t totalSize = size * nmemb;
str->append(static_cast<char*>(contents), totalSize);
return totalSize;
@ -70,10 +71,14 @@ fn MakeApiRequest(const std::string& url) -> Result<WeatherOutput> {
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. Response size: {}", responseBuffer.size());
fmt::println(
"Received response from API. Response size: {}", responseBuffer.size()
);
WeatherOutput output =
rfl::json::read<WeatherOutput>(responseBuffer).value();

116
src/config/weather.h Normal file
View file

@ -0,0 +1,116 @@
#pragma once
#include <fmt/core.h>
#include <rfl.hpp>
#include <rfl/toml.hpp>
#include <string>
#include <variant>
#include "util/macros.h"
#include "util/numtypes.h"
class Weather {
public:
using degrees = rfl::Validator<u16, rfl::Minimum<0>, rfl::Maximum<360>>;
using percentage = rfl::Validator<i8, rfl::Minimum<0>, rfl::Maximum<100>>;
struct Condition {
std::string description;
std::string icon;
std::string main;
usize id;
};
struct Main {
f64 feels_like;
f64 temp;
f64 temp_max;
f64 temp_min;
isize pressure;
percentage humidity;
std::optional<isize> grnd_level;
std::optional<isize> sea_level;
};
struct Wind {
degrees deg;
f64 speed;
std::optional<f64> gust;
};
struct Precipitation {
rfl::Rename<"1h", f64> one_hour;
rfl::Rename<"3h", f64> three_hours;
};
struct Sys {
std::string country;
usize id;
usize sunrise;
usize sunset;
usize type;
};
struct Clouds {
percentage all;
};
struct Coords {
double lat;
double lon;
};
struct WeatherOutput {
Clouds clouds;
isize timezone;
isize visibility;
Main main;
rfl::Rename<"coord", Coords> coords;
std::optional<Precipitation> rain;
std::optional<Precipitation> snow;
std::string base;
std::string name;
std::vector<Condition> weather;
Sys sys;
usize cod;
usize dt;
usize id;
Wind wind;
};
using Location = std::variant<std::string, Coords>;
private:
Location m_Location;
std::string m_ApiKey;
std::string m_Units;
public:
Weather(Location location, std::string api_key, std::string units);
[[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 {
Weather::Location location;
std::string api_key;
std::string units;
static fn from_class(const Weather& weather) noexcept -> WeatherImpl;
[[nodiscard]] fn to_class() const -> Weather;
};
namespace rfl::parsing {
template <class ReaderType, class WriterType, class ProcessorsType>
struct Parser<ReaderType, WriterType, Weather, ProcessorsType>
: public CustomParser<
ReaderType,
WriterType,
ProcessorsType,
Weather,
WeatherImpl> {};
}

View file

@ -9,7 +9,7 @@
+ (NSString*)macOSVersion;
@end
#else
#include "util/numtypes.h"
#include "util/macros.h"
extern "C" {
fn GetCurrentPlayingTitle() -> const char*;

View file

@ -80,8 +80,13 @@ using MRMediaRemoteGetNowPlayingInfoFunction = void (*)(
}
// Dictionary to map macOS versions to their respective names
NSDictionary<NSNumber*, NSString*>* versionNames =
@{@11 : @"Big Sur", @12 : @"Monterey", @13 : @"Ventura", @14 : @"Sonoma"};
NSDictionary<NSNumber*, NSString*>* versionNames = @{
@11 : @"Big Sur",
@12 : @"Monterey",
@13 : @"Ventura",
@14 : @"Sonoma",
@15 : @"Sequoia"
};
NSNumber* majorVersionNumber = @(osVersion.majorVersion);
NSString* versionName = versionNames[majorVersionNumber];
@ -96,7 +101,7 @@ using MRMediaRemoteGetNowPlayingInfoFunction = void (*)(
}
@end
#include "util/numtypes.h"
#include "util/macros.h"
extern "C" {
fn GetCurrentPlayingTitle() -> const char* {

View file

@ -2,8 +2,9 @@
#include <string>
#include "util/macros.h"
#include "util/numtypes.h"
u64 GetMemInfo();
std::string GetNowPlaying();
const char* GetOSVersion();
fn GetMemInfo() -> u64;
fn GetNowPlaying() -> std::string;
fn GetOSVersion() -> const char*;