lots of stuff
This commit is contained in:
parent
e8fb8ec19f
commit
791e237470
224 changed files with 19811 additions and 129 deletions
21
src/config/config.cpp
Normal file
21
src/config/config.cpp
Normal file
|
@ -0,0 +1,21 @@
|
|||
#include "config.h"
|
||||
#include <fmt/core.h>
|
||||
#include <toml++/toml.h>
|
||||
#include <unistd.h>
|
||||
|
||||
Weather make_weather(toml::node_view<toml::node> location,
|
||||
const char* api_key,
|
||||
const char* units) {
|
||||
return location.is_string()
|
||||
? Weather(location.value_or(""), api_key, units)
|
||||
: Weather(std::make_tuple(location["lat"].value_or(0.0),
|
||||
location["lon"].value_or(0.0)),
|
||||
api_key, units);
|
||||
}
|
||||
|
||||
Config::Config(toml::table toml)
|
||||
: m_general(toml["general"]["name"].value_or(getlogin())),
|
||||
m_now_playing(toml["now_playing"]["enable"].value_or(false)),
|
||||
m_weather(make_weather(toml["weather"]["location"],
|
||||
toml["weather"]["api_key"].value_or(""),
|
||||
toml["weather"]["units"].value_or("metric"))) {}
|
63
src/config/config.h
Normal file
63
src/config/config.h
Normal file
|
@ -0,0 +1,63 @@
|
|||
#pragma once
|
||||
|
||||
#include <toml++/toml.h>
|
||||
#include <string>
|
||||
|
||||
using std::string;
|
||||
|
||||
typedef std::tuple<double, double> Coords;
|
||||
typedef std::variant<string, Coords> Location;
|
||||
|
||||
class Weather {
|
||||
private:
|
||||
Location m_location;
|
||||
string m_api_key;
|
||||
string m_units;
|
||||
|
||||
public:
|
||||
Weather(string city, string api_key, string units)
|
||||
: m_location(city), m_api_key(api_key), m_units(units) {}
|
||||
|
||||
Weather(Coords coords, string api_key, string units)
|
||||
: m_location(coords), m_api_key(api_key), m_units(units) {}
|
||||
|
||||
inline Location get_location() { return m_location; }
|
||||
inline string get_api_key() { return m_api_key; }
|
||||
inline string get_units() { return m_units; }
|
||||
};
|
||||
|
||||
class General {
|
||||
private:
|
||||
string m_name;
|
||||
|
||||
public:
|
||||
General(string name) { this->m_name = name; }
|
||||
|
||||
inline string get_name() { return m_name; }
|
||||
};
|
||||
|
||||
class NowPlaying {
|
||||
private:
|
||||
bool m_enable;
|
||||
|
||||
public:
|
||||
NowPlaying(bool enable) { this->m_enable = enable; }
|
||||
|
||||
inline bool get_enabled() { return m_enable; }
|
||||
};
|
||||
|
||||
class Config {
|
||||
private:
|
||||
General m_general;
|
||||
NowPlaying m_now_playing;
|
||||
Weather m_weather;
|
||||
|
||||
public:
|
||||
Config(toml::table toml);
|
||||
|
||||
~Config();
|
||||
|
||||
inline Weather get_weather() { return m_weather; }
|
||||
inline General get_general() { return m_general; }
|
||||
inline NowPlaying get_now_playing() { return m_now_playing; }
|
||||
};
|
138
src/main.cpp
138
src/main.cpp
|
@ -6,88 +6,13 @@
|
|||
#include <boost/json/src.hpp>
|
||||
#include <ctime>
|
||||
#include <toml++/toml.hpp>
|
||||
#include <variant>
|
||||
#include "config/config.h"
|
||||
#include "os/os.h"
|
||||
|
||||
using std::string;
|
||||
|
||||
struct General {
|
||||
string name;
|
||||
};
|
||||
|
||||
struct NowPlaying {
|
||||
bool enable;
|
||||
};
|
||||
|
||||
struct LatLon {
|
||||
double lat;
|
||||
double lon;
|
||||
};
|
||||
|
||||
struct Location {
|
||||
enum {
|
||||
coords,
|
||||
city,
|
||||
} type;
|
||||
|
||||
union {
|
||||
const char* city;
|
||||
LatLon coords;
|
||||
} data;
|
||||
};
|
||||
|
||||
struct Weather {
|
||||
Location location;
|
||||
string api_key;
|
||||
string units;
|
||||
|
||||
Weather() = default;
|
||||
|
||||
Weather(const char* _city, string _api_key, string _units) {
|
||||
this->api_key = _api_key;
|
||||
this->units = _units;
|
||||
this->location = Location {
|
||||
Location::city,
|
||||
{.city = _city},
|
||||
};
|
||||
}
|
||||
|
||||
Weather(LatLon _coords, string _api_key, string _units) {
|
||||
this->api_key = _api_key;
|
||||
this->units = _units;
|
||||
this->location = Location {
|
||||
Location::coords,
|
||||
{.coords = _coords},
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
struct Config {
|
||||
General general;
|
||||
NowPlaying now_playing;
|
||||
Weather weather;
|
||||
|
||||
Config(toml::table toml) {
|
||||
general = General {toml["general"]["name"].value_or(getlogin())};
|
||||
|
||||
now_playing = NowPlaying {toml["now_playing"]["enable"].value_or(false)};
|
||||
|
||||
const auto location = toml["weather"]["location"];
|
||||
const string api_key = toml["weather"]["api_key"].value_or("");
|
||||
const string units = toml["weather"]["units"].value_or("metric");
|
||||
|
||||
if (location.is_string())
|
||||
weather = Weather(location.value_or(""), api_key, units);
|
||||
else
|
||||
weather = Weather(
|
||||
LatLon {location["lat"].value_or(0.0), location["lon"].value_or(0.0)},
|
||||
api_key, units);
|
||||
}
|
||||
};
|
||||
|
||||
static const Config& CONFIG() {
|
||||
static const Config& CONFIG = *new Config(toml::parse_file("./config.toml"));
|
||||
return CONFIG;
|
||||
}
|
||||
static Config& CONFIG = *new Config(toml::parse_file("./config.toml"));
|
||||
|
||||
struct BytesToGiB {
|
||||
uint64_t value;
|
||||
|
@ -122,36 +47,38 @@ date_num parse_date(string const& input) {
|
|||
}
|
||||
|
||||
boost::json::object get_weather() {
|
||||
using namespace std;
|
||||
using namespace cpr;
|
||||
using namespace boost::json;
|
||||
using namespace fmt;
|
||||
using namespace boost;
|
||||
|
||||
if (CONFIG().weather.location.type == Location::city) {
|
||||
const char* location = curl_easy_escape(
|
||||
nullptr, CONFIG().weather.location.data.city,
|
||||
static_cast<int>(strlen(CONFIG().weather.location.data.city)));
|
||||
const char* api_key = CONFIG().weather.api_key.c_str();
|
||||
const char* units = CONFIG().weather.units.c_str();
|
||||
Weather weather = CONFIG.get_weather();
|
||||
Location loc = weather.get_location();
|
||||
string api_key = weather.get_api_key();
|
||||
string units = weather.get_units();
|
||||
|
||||
if (holds_alternative<string>(loc)) {
|
||||
const string city = get<string>(loc);
|
||||
|
||||
const char* location = curl_easy_escape(nullptr, city.c_str(),
|
||||
static_cast<int>(city.length()));
|
||||
|
||||
const Response r =
|
||||
Get(Url {format("https://api.openweathermap.org/data/2.5/"
|
||||
"weather?q={}&appid={}&units={}",
|
||||
location, api_key, units)});
|
||||
Get(Url {fmt::format("https://api.openweathermap.org/data/2.5/"
|
||||
"weather?q={}&appid={}&units={}",
|
||||
location, api_key, units)});
|
||||
|
||||
value json = parse(r.text);
|
||||
json::value json = json::parse(r.text);
|
||||
|
||||
return json.as_object();
|
||||
} else {
|
||||
const auto [lat, lon] = CONFIG().weather.location.data.coords;
|
||||
const char* api_key = CONFIG().weather.api_key.c_str();
|
||||
const char* units = CONFIG().weather.units.c_str();
|
||||
const auto [lat, lon] = get<Coords>(loc);
|
||||
|
||||
const Response r =
|
||||
Get(Url {format("https://api.openweathermap.org/data/2.5/"
|
||||
"weather?lat={}&lon={}&appid={}&units={}",
|
||||
lat, lon, api_key, units)});
|
||||
|
||||
value json = parse(r.text);
|
||||
json::value json = json::parse(r.text);
|
||||
|
||||
return json.as_object();
|
||||
}
|
||||
|
@ -159,27 +86,20 @@ boost::json::object get_weather() {
|
|||
|
||||
int main() {
|
||||
using boost::json::object;
|
||||
using fmt::format;
|
||||
using fmt::localtime;
|
||||
using fmt::println;
|
||||
using std::time;
|
||||
using std::time_t;
|
||||
using toml::parse_result;
|
||||
|
||||
const parse_result toml = toml::parse_file("./config.toml");
|
||||
if (CONFIG.get_now_playing().get_enabled())
|
||||
fmt::println("{}", get_nowplaying());
|
||||
|
||||
if (CONFIG().now_playing.enable)
|
||||
println("{}", get_nowplaying());
|
||||
|
||||
println("Hello {}!", CONFIG().general.name);
|
||||
fmt::println("Hello {}!", CONFIG.get_general().get_name());
|
||||
|
||||
const uint64_t meminfo = get_meminfo();
|
||||
|
||||
println("{:.2f}", BytesToGiB {meminfo});
|
||||
fmt::println("{:.2f}", BytesToGiB {meminfo});
|
||||
|
||||
const time_t t = time(nullptr);
|
||||
const std::tm t = fmt::localtime(time(nullptr));
|
||||
|
||||
string date = format("{:%d}", localtime(t));
|
||||
string date = fmt::format("{:%d}", t);
|
||||
|
||||
switch (parse_date(date)) {
|
||||
case Ones:
|
||||
|
@ -199,14 +119,14 @@ int main() {
|
|||
break;
|
||||
}
|
||||
|
||||
println("{:%B} {}, {:%-I:%0M %p}", localtime(t), date, localtime(t));
|
||||
fmt::println("{:%B} {}, {:%-I:%0M %p}", t, date, t);
|
||||
|
||||
object json = get_weather();
|
||||
|
||||
const char* town_name =
|
||||
json["name"].is_string() ? json["name"].as_string().c_str() : "Unknown";
|
||||
|
||||
println("{}", town_name);
|
||||
fmt::println("{}", town_name);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
#ifdef __linux__
|
||||
|
||||
#include <fmt/core.h>
|
||||
#include <playerctl/playerctl.h>
|
||||
#include <fstream>
|
||||
#include "os.h"
|
||||
|
||||
using std::string;
|
||||
|
||||
|
@ -9,13 +12,15 @@ uint64_t parse_line_as_number(const string& input) {
|
|||
string::size_type start = 0;
|
||||
|
||||
// Skip leading non-numbers
|
||||
while (!isdigit(input[++start]));
|
||||
while (!isdigit(input[++start]))
|
||||
;
|
||||
|
||||
// Start searching from the start of the number
|
||||
string::size_type end = start;
|
||||
|
||||
// Increment to the end of the number
|
||||
while (isdigit(input[++end]));
|
||||
while (isdigit(input[++end]))
|
||||
;
|
||||
|
||||
// Return the substring containing the number
|
||||
return std::stoul(input.substr(start, end - start));
|
||||
|
@ -25,7 +30,8 @@ uint64_t meminfo_parse(std::ifstream is) {
|
|||
string line;
|
||||
|
||||
// Skip every line before the one that starts with "MemTotal"
|
||||
while (std::getline(is, line) && !line.starts_with("MemTotal"));
|
||||
while (std::getline(is, line) && !line.starts_with("MemTotal"))
|
||||
;
|
||||
|
||||
// Parse the number from the line
|
||||
const uint64_t num = parse_line_as_number(line);
|
||||
|
@ -78,3 +84,5 @@ string get_nowplaying() {
|
|||
return playerctl_player_get_title(current_player, nullptr);
|
||||
return "Could not get now playing info";
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
#ifdef __APPLE__
|
||||
|
||||
#include <sys/sysctl.h>
|
||||
#include <unistd.h>
|
||||
#include <string>
|
||||
#include "os.h"
|
||||
|
||||
uint64_t get_meminfo() {
|
||||
uint64_t mem = 0;
|
||||
|
@ -11,6 +13,8 @@ uint64_t get_meminfo() {
|
|||
return mem;
|
||||
}
|
||||
|
||||
static std::string get_nowplaying() {
|
||||
std::string get_nowplaying() {
|
||||
return "";
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue