From c9c99cf37c219d39255637e1de8b9cb314d75ea5 Mon Sep 17 00:00:00 2001 From: tastytea Date: Wed, 16 Aug 2023 21:43:32 +0200 Subject: [PATCH] statusweather: reformat --- statusweather.cpp | 102 ++++++++++++++++++---------------------------- 1 file changed, 40 insertions(+), 62 deletions(-) diff --git a/statusweather.cpp b/statusweather.cpp index f28a1b1..f5a03e3 100644 --- a/statusweather.cpp +++ b/statusweather.cpp @@ -38,32 +38,31 @@ #include #include -struct weather -{ +struct weather { float temperature{-273.15}; std::string icon{"❗"}; bool old{true}; } __attribute__((aligned(64))) weather; // NOLINT(cert-err58-cpp) + std::mutex mutex_weather; -std::string map_icon(const std::string_view icon_id) -{ +std::string map_icon(const std::string_view icon_id) { // // NOTE: There does not seem to be weather emojis with the moon in unicode. const std::map> icons{ - {{1, {"🌞", "🌝"}}, // clear sky - {2, {"🌤", "🌤"}}, // few clouds - {3, {"⛅", "⛅"}}, // scattered clouds - {4, {"☁", "☁"}}, // broken clouds - {9, {"🌧", "🌧"}}, // shower rain - {10, {"🌧", "🌧"}}, // rain - {10, {"🌩", "🌩"}}, // thunderstorm - {13, {"🌨", "🌨"}}, // snow - {50, {"🌫", "🌫"}}}}; // mist + {{1, {"🌞", "🌝"}}, // clear sky + {2, {"🌤", "🌤"}}, // few clouds + {3, {"⛅", "⛅"}}, // scattered clouds + {4, {"☁", "☁"}}, // broken clouds + {9, {"🌧", "🌧"}}, // shower rain + {10, {"🌧", "🌧"}}, // rain + {10, {"🌩", "🌩"}}, // thunderstorm + {13, {"🌨", "🌨"}}, // snow + {50, {"🌫", "🌫"}}} + }; // mist const auto icon{icons.find(std::stoul(icon_id.data()))}; - if (icon != icons.end()) - { + if (icon != icons.end()) { if (*icon_id.rbegin() == 'n') // Night { return icon->second.second; @@ -73,8 +72,7 @@ std::string map_icon(const std::string_view icon_id) return "❔"; } -std::tuple get_options() -{ +std::tuple get_options() { namespace po = boost::program_options; po::options_description options("Options"); @@ -99,18 +97,14 @@ std::tuple get_options() vm["city"].as()); } -bool fetch_weather() -{ +bool fetch_weather() { using fmt::format; std::string api_key; std::string city; - try - { + try { std::tie(api_key, city) = get_options(); - } - catch (std::runtime_error &e) - { + } catch (std::runtime_error &e) { std::cout << R"()" << e.what() << "" << std::endl; return false; @@ -127,43 +121,35 @@ bool fetch_weather() RestClient::disable(); const std::lock_guard guard(mutex_weather); - if (response.code == 200) - { + if (response.code == 200) { const auto json{nlohmann::json::parse(response.body)}; weather.temperature = json[0]["main"]["temp"].get(); weather.icon = map_icon( json[0]["weather"][0]["icon"].get()); weather.old = false; - } - else - { + } else { weather.old = true; } return true; } -void print_weather() -{ +void print_weather() { using fmt::format; const std::lock_guard guard(mutex_weather); - const std::string color{[] - { - if (weather.temperature > 25.0) - { - return "#ff2200"; - } - if (weather.temperature < 0.0) - { - return "#aaffff"; - } - if (weather.temperature < 10.0) - { - return "#44ddff"; - } - return "#66ff66"; - }()}; + const std::string color{[] { + if (weather.temperature > 25.0) { + return "#ff2200"; + } + if (weather.temperature < 0.0) { + return "#aaffff"; + } + if (weather.temperature < 10.0) { + return "#44ddff"; + } + return "#66ff66"; + }()}; std::cout << format(R"({0:s} )" R"({2:.1Lf}°C{3:s})", @@ -172,25 +158,20 @@ void print_weather() << std::endl; } -void update(std::atomic &cancelled) -{ +void update(std::atomic &cancelled) { using clock = std::chrono::system_clock; using namespace std::chrono_literals; - while (!cancelled) - { - if (fetch_weather()) - { + while (!cancelled) { + if (fetch_weather()) { print_weather(); } std::this_thread::sleep_until(clock::now() + 30min); } } -int main() -{ - try - { +int main() { + try { std::locale::global(std::locale("")); // TODO: Implement clean shutdown. std::atomic cancelled{false}; @@ -202,15 +183,12 @@ int main() { if (line == "1") // Left mouse button. { - if (fetch_weather()) - { + if (fetch_weather()) { print_weather(); } } } - } - catch (const std::exception &e) - { + } catch (const std::exception &e) { std::cout << R"()" << e.what() << "" << std::endl; return 1;