statusweather: reformat
This commit is contained in:
parent
fee14c4585
commit
c9c99cf37c
@ -38,32 +38,31 @@
|
||||
#include <tuple>
|
||||
#include <utility>
|
||||
|
||||
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) {
|
||||
// <https://openweathermap.org/weather-conditions>
|
||||
// NOTE: There does not seem to be weather emojis with the moon in unicode.
|
||||
const std::map<std::uint8_t, std::pair<std::string, std::string>> 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<std::string, std::string> get_options()
|
||||
{
|
||||
std::tuple<std::string, std::string> get_options() {
|
||||
namespace po = boost::program_options;
|
||||
|
||||
po::options_description options("Options");
|
||||
@ -99,18 +97,14 @@ std::tuple<std::string, std::string> get_options()
|
||||
vm["city"].as<std::string>());
|
||||
}
|
||||
|
||||
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"(<span color="red">)" << e.what() << "</span>"
|
||||
<< std::endl;
|
||||
return false;
|
||||
@ -127,43 +121,35 @@ bool fetch_weather()
|
||||
RestClient::disable();
|
||||
|
||||
const std::lock_guard<std::mutex> 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<float>();
|
||||
weather.icon = map_icon(
|
||||
json[0]["weather"][0]["icon"].get<std::string_view>());
|
||||
weather.old = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
weather.old = true;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void print_weather()
|
||||
{
|
||||
void print_weather() {
|
||||
using fmt::format;
|
||||
|
||||
const std::lock_guard<std::mutex> 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"(<big>{0:s}</big> )"
|
||||
R"(<span color="{1:s}">{2:.1Lf}°C</span>{3:s})",
|
||||
@ -172,25 +158,20 @@ void print_weather()
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
void update(std::atomic<bool> &cancelled)
|
||||
{
|
||||
void update(std::atomic<bool> &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<bool> 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"(<span color="red">)" << e.what() << "</span>"
|
||||
<< std::endl;
|
||||
return 1;
|
||||
|
Loading…
x
Reference in New Issue
Block a user