From 269122d30c3c6a9f7e8943e89921e384299b655b Mon Sep 17 00:00:00 2001 From: pupbrained Date: Fri, 21 Jun 2024 03:34:33 -0400 Subject: [PATCH] wawa --- .clang-format | 5 ++--- src/config/config.h | 14 ++++++-------- src/config/weather.cpp | 16 ++++++++-------- src/config/weather.h | 25 ++++++++++++------------- src/main.cpp | 23 +++++++++++------------ src/os/linux.cpp | 28 +++++++++++++--------------- src/os/macos.cpp | 4 ++-- src/os/os.h | 6 ++---- src/util/result.h | 8 ++++---- src/util/{numtypes.h => types.h} | 8 ++++++++ 10 files changed, 68 insertions(+), 69 deletions(-) rename src/util/{numtypes.h => types.h} (96%) diff --git a/.clang-format b/.clang-format index dcb75a6..93a31a9 100644 --- a/.clang-format +++ b/.clang-format @@ -3,7 +3,6 @@ AlignArrayOfStructures: Right AlignConsecutiveAssignments: true AlignConsecutiveDeclarations: true AllowShortBlocksOnASingleLine: Always -AllowShortCompoundRequirementOnASingleLine: true AllowShortEnumsOnASingleLine: true AllowShortFunctionsOnASingleLine: All AllowShortLoopsOnASingleLine: true @@ -24,7 +23,7 @@ SpacesBeforeTrailingComments: 1 IncludeBlocks: Regroup IncludeCategories: - - Regex: '".*"' + - Regex: '".*"' Priority: 1 - - Regex: '<.*>' + - Regex: '<.*>' Priority: -1 diff --git a/src/config/config.h b/src/config/config.h index d3c002a..a131345 100644 --- a/src/config/config.h +++ b/src/config/config.h @@ -1,18 +1,16 @@ #pragma once -#include #include #include -#include -#include #include "../util/macros.h" +#include "../util/types.h" #include "weather.h" -using Location = std::variant; +using Location = std::variant; struct General { - rfl::Field<"name", std::string> name = "user"; + rfl::Field<"name", string> name = "user"; }; struct NowPlaying { @@ -20,9 +18,9 @@ struct NowPlaying { }; struct Weather { - Location location; - std::string api_key; - std::string units; + Location location; + string api_key; + string units; [[nodiscard]] fn getWeatherInfo() const -> WeatherOutput; }; diff --git a/src/config/weather.cpp b/src/config/weather.cpp index 377236c..e4da179 100644 --- a/src/config/weather.cpp +++ b/src/config/weather.cpp @@ -46,18 +46,18 @@ 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, string* str) -> size_t { size_t totalSize = size * nmemb; str->append(static_cast(contents), totalSize); return totalSize; } // Function to make API request -fn MakeApiRequest(const std::string& url) -> Result { +fn MakeApiRequest(const string& url) -> Result { fmt::println("Making API request to URL: {}", url); - CURL* curl = curl_easy_init(); - std::string responseBuffer; + CURL* curl = curl_easy_init(); + string responseBuffer; if (curl) { curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); @@ -103,14 +103,14 @@ fn Weather::getWeatherInfo() const -> WeatherOutput { WeatherOutput result; - if (holds_alternative(location)) { - const std::string city = get(location); + if (holds_alternative(location)) { + const string city = get(location); const char* loc = curl_easy_escape(nullptr, city.c_str(), static_cast(city.length())); fmt::println("City: {}", loc); - const std::string apiUrl = fmt::format( + const string apiUrl = fmt::format( "https://api.openweathermap.org/data/2.5/" "weather?q={}&appid={}&units={}", loc, @@ -124,7 +124,7 @@ fn Weather::getWeatherInfo() const -> WeatherOutput { fmt::println("Coordinates: lat = {:.3f}, lon = {:.3f}", lat, lon); - const std::string apiUrl = fmt::format( + const string apiUrl = fmt::format( "https://api.openweathermap.org/data/2.5/" "weather?lat={:.3f}&lon={:.3f}&appid={}&units={}", lat, diff --git a/src/config/weather.h b/src/config/weather.h index 85696e6..58e2edb 100644 --- a/src/config/weather.h +++ b/src/config/weather.h @@ -1,20 +1,19 @@ #pragma once -#include #include #include #include -#include "../util/numtypes.h" +#include "../util/types.h" using degrees = rfl::Validator, rfl::Maximum<360>>; using percentage = rfl::Validator, rfl::Maximum<100>>; struct Condition { - std::string description; - std::string icon; - std::string main; - usize id; + string description; + string icon; + string main; + usize id; }; struct Main { @@ -40,11 +39,11 @@ struct Precipitation { }; struct Sys { - std::string country; - usize id; - usize sunrise; - usize sunset; - usize type; + string country; + usize id; + usize sunrise; + usize sunset; + usize type; }; struct Clouds { @@ -64,8 +63,8 @@ struct WeatherOutput { rfl::Rename<"coord", Coords> coords; std::optional rain; std::optional snow; - std::string base; - std::string name; + string base; + string name; std::vector weather; Sys sys; usize cod; diff --git a/src/main.cpp b/src/main.cpp index dae2ea2..72b1c49 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,9 +6,6 @@ #include "config/config.h" #include "os/os.h" -using std::future; -using std::string; - struct BytesToGiB { u64 value; }; @@ -28,10 +25,10 @@ struct fmt::formatter : formatter { } }; -fn GetDate() -> std::string { +fn GetDate() -> string { const std::tm localTime = fmt::localtime(time(nullptr)); - std::string date = fmt::format("{:%e}", localTime); + string date = fmt::format("{:%e}", localTime); if (!date.empty() && std::isspace(date.front())) date.erase(date.begin()); @@ -48,7 +45,9 @@ fn GetDate() -> std::string { return fmt::format("{:%B} {}, {:%-I:%0M %p}", localTime, date, localTime); } -fn main() -> int { +fn main() -> i32 { + using std::future; + const Config& config = Config::getInstance(); auto weatherFuture = @@ -65,16 +64,16 @@ fn main() -> int { const i64 temp = std::lround(json.main.temp); const string townName = json.name; - const bool nowPlayingEnabled = nowPlayingEnabledFuture.get(); - const char* version = osVersionFuture.get(); - const string date = dateFuture.get(); - const std::string name = config.general.get().name.get(); - const u64 mem = memInfoFuture.get(); + const bool nowPlayingEnabled = nowPlayingEnabledFuture.get(); + const char* version = osVersionFuture.get(); + const string date = dateFuture.get(); + const string name = config.general.get().name.get(); + const u64 mem = memInfoFuture.get(); fmt::println("Hello {}!", name); fmt::println("Today is: {}", date); fmt::println("It is {}°F in {}", temp, townName); - fmt::println("Installed RAM: {:.2f} GiB", BytesToGiB(mem)); + fmt::println("Installed RAM: {:.2f}", BytesToGiB(mem)); fmt::println("{}", version); if (nowPlayingEnabled) diff --git a/src/os/linux.cpp b/src/os/linux.cpp index 96d1f6e..48e90d2 100644 --- a/src/os/linux.cpp +++ b/src/os/linux.cpp @@ -8,8 +8,6 @@ #include "os.h" -using std::string; - fn ParseLineAsNumber(const string& input) -> u64 { // Find the first number string::size_type start = 0; @@ -49,12 +47,12 @@ fn GetOSVersion() -> const char* { return nullptr; } - std::string line; - const std::string prefix = "PRETTY_NAME="; + string line; + const string prefix = "PRETTY_NAME="; while (std::getline(file, line)) { if (line.find(prefix) == 0) { - std::string prettyName = line.substr(prefix.size()); + string prettyName = line.substr(prefix.size()); // Remove surrounding quotes if present if (!prettyName.empty() && prettyName.front() == '"' && prettyName.back() == '"') @@ -71,18 +69,18 @@ fn GetOSVersion() -> const char* { return nullptr; } -fn GetMprisPlayers(sdbus::IConnection& connection) -> std::vector { +fn GetMprisPlayers(sdbus::IConnection& connection) -> std::vector { const char *dbusInterface = "org.freedesktop.DBus", *dbusObjectPath = "/org/freedesktop/DBus", *dbusMethodListNames = "ListNames"; const std::unique_ptr dbusProxy = createProxy(connection, dbusInterface, dbusObjectPath); - std::vector names; + std::vector names; dbusProxy->callMethod(dbusMethodListNames).onInterface(dbusInterface).storeResultsTo(names); - std::vector mprisPlayers; + std::vector mprisPlayers; for (const std::basic_string& name : names) if (const char* mprisInterfaceName = "org.mpris.MediaPlayer2"; @@ -92,26 +90,26 @@ fn GetMprisPlayers(sdbus::IConnection& connection) -> std::vector { return mprisPlayers; } -fn GetActivePlayer(const std::vector& mprisPlayers) -> std::string { +fn GetActivePlayer(const std::vector& mprisPlayers) -> string { if (!mprisPlayers.empty()) return mprisPlayers.front(); return ""; } -fn GetNowPlaying() -> std::string { +fn GetNowPlaying() -> string { try { const char *playerObjectPath = "/org/mpris/MediaPlayer2", *playerInterfaceName = "org.mpris.MediaPlayer2.Player"; std::unique_ptr connection = sdbus::createSessionBusConnection(); - std::vector mprisPlayers = GetMprisPlayers(*connection); + std::vector mprisPlayers = GetMprisPlayers(*connection); if (mprisPlayers.empty()) return ""; - std::string activePlayer = GetActivePlayer(mprisPlayers); + string activePlayer = GetActivePlayer(mprisPlayers); if (activePlayer.empty()) return ""; @@ -119,13 +117,13 @@ fn GetNowPlaying() -> std::string { std::unique_ptr playerProxy = sdbus::createProxy(*connection, activePlayer, playerObjectPath); - std::map metadata = + std::map metadata = playerProxy->getProperty("Metadata").onInterface(playerInterfaceName); if (const auto iter = metadata.find("xesam:title"); - iter != metadata.end() && iter->second.containsValueOfType()) - return iter->second.get(); + iter != metadata.end() && iter->second.containsValueOfType()) + return iter->second.get(); } catch (const sdbus::Error& e) { std::cerr << "Error: " << e.what() << '\n'; } return ""; diff --git a/src/os/macos.cpp b/src/os/macos.cpp index 1cc8de5..f9c8e3a 100644 --- a/src/os/macos.cpp +++ b/src/os/macos.cpp @@ -14,9 +14,9 @@ fn GetMemInfo() -> u64 { return mem; } -fn GetNowPlaying() -> std::string { +fn GetNowPlaying() -> string { if (const char* title = GetCurrentPlayingTitle(); const char* artist = GetCurrentPlayingArtist()) - return "Now Playing: " + std::string(artist) + " - " + std::string(title); + return "Now Playing: " + string(artist) + " - " + string(title); return "No song playing"; } diff --git a/src/os/os.h b/src/os/os.h index 1da4157..2d96984 100644 --- a/src/os/os.h +++ b/src/os/os.h @@ -1,9 +1,7 @@ #pragma once -#include - #include "../util/macros.h" -#include "../util/numtypes.h" +#include "../util/types.h" /** * @brief Get the amount of installed RAM in bytes. @@ -13,7 +11,7 @@ fn GetMemInfo() -> u64; /** * @brief Get the currently playing song metadata. */ -fn GetNowPlaying() -> std::string; +fn GetNowPlaying() -> string; /** * @brief Get the OS version. diff --git a/src/util/result.h b/src/util/result.h index 8c96ed2..bf9dcc0 100644 --- a/src/util/result.h +++ b/src/util/result.h @@ -1,11 +1,11 @@ #pragma once #include -#include #include #include #include "macros.h" +#include "types.h" /** * @class Error @@ -19,16 +19,16 @@ class Error { * @brief Constructs an Error with a message. * @param message The error message. */ - explicit Error(std::string message) : m_Message(std::move(message)) {} + explicit Error(string message) : m_Message(std::move(message)) {} /** * @brief Retrieves the error message. * @return A constant reference to the error message string. */ - [[nodiscard]] fn message() const -> const std::string& { return m_Message; } + [[nodiscard]] fn message() const -> const string& { return m_Message; } private: - std::string m_Message; ///< The error message. + string m_Message; ///< The error message. }; // Primary template for Result with a default type of void diff --git a/src/util/numtypes.h b/src/util/types.h similarity index 96% rename from src/util/numtypes.h rename to src/util/types.h index 24a86c3..e03d326 100644 --- a/src/util/numtypes.h +++ b/src/util/types.h @@ -2,6 +2,7 @@ #include #include +#include /** * @typedef u8 @@ -117,3 +118,10 @@ using usize = std::size_t; * subtracting two pointers. */ using isize = std::ptrdiff_t; + +/** + * @typedef string + * @brief Represents a string. + */ +using string = std::string; +