i should sleep

This commit is contained in:
Mars 2024-06-01 06:59:01 -04:00
parent 791e237470
commit b366a7ee63
Signed by: pupbrained
GPG key ID: 874E22DF2F9DFCB5
10 changed files with 341 additions and 109 deletions

View file

@ -1,5 +1,8 @@
---
Language: Cpp
BasedOnStyle: Chromium
SpaceBeforeCpp11BracedList: true
IndentAccessModifiers: false
NamespaceIndentation: All
SpaceBeforeCpp11BracedList: true
SpacesBeforeTrailingComments: 1
AlignConsecutiveAssignments: true

51
.clang-tidy Normal file
View file

@ -0,0 +1,51 @@
Checks: >
bugprone-*,
clang-analyzer-*,
clang-diagnostic-*,
misc-const-correctness,
misc-definitions-in-headers,
modernize-*,
performance-*,
portability-*,
readability-*,
cppcoreguidelines-prefer-member-initializer,
modernize-use-designated-initializers,
-altera-*,
-bugprone-easily-swappable-parameters,
-bugprone-implicit-widening-of-multiplication-result,
-concurrency-mt-unsafe,
-fuchsia-*,
-google-*,
-hicpp-*,
-llvm-include-order,
-llvm-include-order,
-llvmlibc-*,
-modernize-use-trailing-return-type,
-readability-braces-around-statements,
-readability-magic-numbers,
-readability-implicit-bool-conversion
CheckOptions:
readability-identifier-naming.ClassCase: CamelCase
readability-identifier-naming.EnumCase: CamelCase
readability-identifier-naming.LocalConstantCase: camelBack
readability-identifier-naming.LocalVariableCase: camelBack
readability-identifier-naming.GlobalFunctionCase: CamelCase
readability-identifier-naming.MemberCase: lower_case
readability-identifier-naming.MethodCase: camelBack
readability-identifier-naming.MethodIgnoredRegexp: ((to|from)_class)
readability-identifier-naming.ParameterPackCase: lower_case
readability-identifier-naming.PrivateMemberCase: CamelCase
readability-identifier-naming.PrivateMemberPrefix: 'm_'
readability-identifier-naming.PrivateMethodCase: CamelCase
readability-identifier-naming.PrivateMethodPrefix: ''
readability-identifier-naming.ProtectedMemberPrefix: 'm_'
readability-identifier-naming.ProtectedMethodPrefix: ''
readability-identifier-naming.PublicMemberCase: lower_case
readability-identifier-naming.StaticConstantCase: UPPER_CASE
readability-identifier-naming.StaticVariableCase: CamelCase
readability-identifier-naming.StructCase: CamelCase
readability-identifier-naming.TemplateParameterCase: lower_case
readability-identifier-naming.TemplateTemplateParameterCase: CamelCase
readability-identifier-naming.TypeTemplateParameterCase: CamelCase
readability-identifier-naming.TypedefCase: CamelCase
readability-identifier-naming.UnionCase: CamelCase

View file

@ -100,7 +100,7 @@
[
alejandra
bear
clang-tools
clang-tools_18
meson
ninja
pkg-config

View file

@ -3,11 +3,12 @@ project(
default_options: [
'cpp_std=c++26',
'default_library=static',
'buildtype=debugoptimized',
'warning_level=everything',
'warning_level=everything'
]
)
clangtidy = find_program('clang-tidy', required: false)
cc = meson.get_compiler('cpp')
add_project_arguments(
@ -22,7 +23,12 @@ add_project_arguments(
language: 'cpp'
)
source_file_names = ['src/main.cpp', 'src/os/os.h', 'src/config/config.cpp', 'src/config/config.h']
source_file_names = [
'src/main.cpp',
'src/os/os.h',
'src/config/config.cpp',
'src/config/config.h'
]
if host_machine.system() == 'linux'
source_file_names += ['src/os/linux.cpp']
@ -50,7 +56,10 @@ if host_machine.system() == 'linux'
deps += dependency('playerctl')
endif
incdir = include_directories('include')
incdir = include_directories(
'include',
is_system: true # Ignores warnings from include dir
)
executable(
'draconis++',

View file

@ -3,19 +3,93 @@
#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);
#define DEFINE_GETTER(class_name, type, name) \
type class_name::get##name() const { \
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 string, Name)
DEFINE_GETTER(NowPlaying, bool, Enabled)
DEFINE_GETTER(Weather, const Location, Location)
DEFINE_GETTER(Weather, const string, ApiKey)
DEFINE_GETTER(Weather, const string, Units)
const Config& Config::getInstance() {
static const Config& INSTANCE =
*new Config(toml::parse_file("./config.toml"));
return INSTANCE;
}
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"))) {}
: m_General(toml["general"]["name"].value_or(getlogin())),
m_NowPlaying(toml["now_playing"]["enable"].value_or(false)),
m_Weather([location = toml["weather"]["location"],
apiKey = toml["weather"]["api_key"].value_or(""),
units = toml["weather"]["units"].value_or("metric")] {
return location.is_string()
? Weather(location.value_or(""), apiKey, units)
: Weather(
Coords {
.lat = location["lat"].value_or(0.0),
.lon = location["lon"].value_or(0.0),
},
apiKey, units);
}()) {}
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(string name) : m_Name(std::move(name)) {}
NowPlaying::NowPlaying(bool enable) : m_Enabled(enable) {}
Weather::Weather(Location location, string api_key, string units)
: m_Location(std::move(location)),
m_ApiKey(std::move(api_key)),
m_Units(std::move(units)) {}
WeatherImpl WeatherImpl::from_class(const Weather& weather) noexcept {
return {
.location = weather.getLocation(),
.api_key = weather.getApiKey(),
.units = weather.getUnits(),
};
}
Weather WeatherImpl::to_class() const {
return {location, api_key, units};
}
GeneralImpl GeneralImpl::from_class(const General& general) noexcept {
return {general.getName()};
}
General GeneralImpl::to_class() const {
return {name};
}
NowPlayingImpl NowPlayingImpl::from_class(
const NowPlaying& now_playing) noexcept {
return {.enabled = now_playing.getEnabled()};
}
NowPlaying NowPlayingImpl::to_class() const {
return {enabled};
}
ConfigImpl ConfigImpl::from_class(const Config& config) noexcept {
return {
.general = config.getGeneral(),
.now_playing = config.getNowPlaying(),
.weather = config.getWeather(),
};
}
Config ConfigImpl::to_class() const {
return {general, now_playing, weather};
}

View file

@ -1,63 +1,140 @@
#pragma once
#include <fmt/core.h>
#include <toml++/toml.h>
#include <unistd.h>
#include <rfl.hpp>
#include <string>
#include <toml++/impl/parser.hpp>
#include <variant>
using std::string;
typedef std::tuple<double, double> Coords;
typedef std::variant<string, Coords> Location;
struct Coords {
double lat;
double lon;
};
using Location = std::variant<string, Coords>;
class Weather {
private:
Location m_location;
string m_api_key;
string m_units;
Location m_Location;
string m_ApiKey;
string m_Units;
public:
Weather(string city, string api_key, string units)
: m_location(city), m_api_key(api_key), m_units(units) {}
Weather(Location location, string api_key, string units);
Weather(Coords coords, string api_key, string units)
: m_location(coords), m_api_key(api_key), m_units(units) {}
[[nodiscard]] const Location getLocation() const;
[[nodiscard]] const string getApiKey() const;
[[nodiscard]] const string getUnits() const;
};
inline Location get_location() { return m_location; }
inline string get_api_key() { return m_api_key; }
inline string get_units() { return m_units; }
struct WeatherImpl {
Location location;
string api_key;
string units;
static WeatherImpl from_class(const Weather& weather) noexcept;
[[nodiscard]] Weather to_class() const;
};
class General {
private:
string m_name;
string m_Name;
public:
General(string name) { this->m_name = name; }
General(string name);
inline string get_name() { return m_name; }
[[nodiscard]] const string getName() const;
};
struct GeneralImpl {
string name;
static GeneralImpl from_class(const General& general) noexcept;
[[nodiscard]] General to_class() const;
};
class NowPlaying {
private:
bool m_enable;
bool m_Enabled;
public:
NowPlaying(bool enable) { this->m_enable = enable; }
NowPlaying(bool enable);
inline bool get_enabled() { return m_enable; }
[[nodiscard]] bool getEnabled() const;
};
struct NowPlayingImpl {
bool enabled;
static NowPlayingImpl from_class(const NowPlaying& now_playing) noexcept;
[[nodiscard]] NowPlaying to_class() const;
};
class Config {
private:
General m_general;
NowPlaying m_now_playing;
Weather m_weather;
General m_General;
NowPlaying m_NowPlaying;
Weather m_Weather;
public:
Config(toml::table toml);
~Config();
public:
Config(General general, NowPlaying now_playing, Weather weather);
inline Weather get_weather() { return m_weather; }
inline General get_general() { return m_general; }
inline NowPlaying get_now_playing() { return m_now_playing; }
static const Config& getInstance();
[[nodiscard]] const Weather getWeather() const;
[[nodiscard]] const General getGeneral() const;
[[nodiscard]] const NowPlaying getNowPlaying() const;
};
struct ConfigImpl {
General general;
NowPlaying now_playing;
Weather weather;
static ConfigImpl from_class(const Config& config) noexcept;
[[nodiscard]] Config to_class() const;
};
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<ReaderType,
WriterType,
ProcessorsType,
General,
GeneralImpl> {};
template <class ReaderType, class WriterType, class ProcessorsType>
struct Parser<ReaderType, WriterType, NowPlaying, ProcessorsType>
: public CustomParser<ReaderType,
WriterType,
ProcessorsType,
NowPlaying,
NowPlayingImpl> {};
template <class ReaderType, class WriterType, class ProcessorsType>
struct Parser<ReaderType, WriterType, Config, ProcessorsType>
: public CustomParser<ReaderType,
WriterType,
ProcessorsType,
Config,
ConfigImpl> {};
} // namespace rfl::parsing

View file

@ -5,6 +5,10 @@
#include <fmt/format.h>
#include <boost/json/src.hpp>
#include <ctime>
#include <rfl.hpp>
#include <rfl/toml.hpp>
#include <rfl/toml/load.hpp>
#include <rfl/toml/read.hpp>
#include <toml++/toml.hpp>
#include <variant>
#include "config/config.h"
@ -12,8 +16,6 @@
using std::string;
static Config& CONFIG = *new Config(toml::parse_file("./config.toml"));
struct BytesToGiB {
uint64_t value;
};
@ -21,9 +23,9 @@ struct BytesToGiB {
template <>
struct fmt::formatter<BytesToGiB> : formatter<double> {
template <typename FormatContext>
auto format(const BytesToGiB b, FormatContext& ctx) {
auto format(const BytesToGiB BTG, FormatContext& ctx) {
auto out = formatter<double>::format(
static_cast<double>(b.value) / pow(1024, 3), ctx);
static_cast<double>(BTG.value) / pow(1024, 3), ctx);
*out++ = 'G';
*out++ = 'i';
*out++ = 'B';
@ -31,9 +33,9 @@ struct fmt::formatter<BytesToGiB> : formatter<double> {
}
};
enum date_num { Ones, Twos, Threes, Default };
enum DateNum { Ones, Twos, Threes, Default };
date_num parse_date(string const& input) {
DateNum ParseDate(string const& input) {
if (input == "1" || input == "21" || input == "31")
return Ones;
@ -46,15 +48,19 @@ date_num parse_date(string const& input) {
return Default;
}
boost::json::object get_weather() {
boost::json::object GetWeather() {
using namespace std;
using namespace cpr;
using namespace boost;
Weather weather = CONFIG.get_weather();
Location loc = weather.get_location();
string api_key = weather.get_api_key();
string units = weather.get_units();
const Config& config = Config::getInstance();
Weather weather = config.getWeather();
Location loc = weather.getLocation();
string apiKey = weather.getApiKey();
string units = weather.getUnits();
fmt::println("Hello!");
if (holds_alternative<string>(loc)) {
const string city = get<string>(loc);
@ -62,46 +68,58 @@ boost::json::object get_weather() {
const char* location = curl_easy_escape(nullptr, city.c_str(),
static_cast<int>(city.length()));
const Response r =
const Response res =
Get(Url {fmt::format("https://api.openweathermap.org/data/2.5/"
"weather?q={}&appid={}&units={}",
location, api_key, units)});
location, apiKey, units)});
json::value json = json::parse(r.text);
return json.as_object();
} else {
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)});
json::value json = json::parse(r.text);
json::value json = json::parse(res.text);
return json.as_object();
}
const auto [lat, lon] = get<Coords>(loc);
const Response res =
Get(Url {format("https://api.openweathermap.org/data/2.5/"
"weather?lat={:.3f}&lon={:.3f}&appid={}&units={}",
lat, lon, apiKey, units)});
json::value json = json::parse(res.text);
return json.as_object();
}
int main() {
using boost::json::object;
using std::time_t;
if (CONFIG.get_now_playing().get_enabled())
fmt::println("{}", get_nowplaying());
const Config& config = rfl::toml::load<Config>("./config.toml").value();
fmt::println("Hello {}!", CONFIG.get_general().get_name());
if (config.getNowPlaying().getEnabled())
fmt::println("{}", GetNowPlaying());
const uint64_t meminfo = get_meminfo();
fmt::println("Hello {}!", config.getGeneral().getName());
fmt::println("{:.2f}", BytesToGiB {meminfo});
const uint64_t memInfo = GetMemInfo();
const std::tm t = fmt::localtime(time(nullptr));
fmt::println("{:.2f}", BytesToGiB {memInfo});
string date = fmt::format("{:%d}", t);
const std::tm localTime = fmt::localtime(time(nullptr));
switch (parse_date(date)) {
auto trimStart = [](std::string& str) {
auto start = str.begin();
while (start != str.end() && std::isspace(*start)) {
start++;
}
str.erase(str.begin(), start);
};
string date = fmt::format("{:%e}", localTime);
trimStart(date);
switch (ParseDate(date)) {
case Ones:
date += "st";
break;
@ -119,14 +137,14 @@ int main() {
break;
}
fmt::println("{:%B} {}, {:%-I:%0M %p}", t, date, t);
fmt::println("{:%B} {}, {:%-I:%0M %p}", localTime, date, localTime);
object json = get_weather();
object json = GetWeather();
const char* town_name =
const char* townName =
json["name"].is_string() ? json["name"].as_string().c_str() : "Unknown";
fmt::println("{}", town_name);
fmt::println("{}", townName);
return 0;
}

View file

@ -7,7 +7,7 @@
using std::string;
uint64_t parse_line_as_number(const string& input) {
uint64_t ParseLineAsNumber(const string& input) {
// Find the first number
string::size_type start = 0;
@ -26,62 +26,62 @@ uint64_t parse_line_as_number(const string& input) {
return std::stoul(input.substr(start, end - start));
}
uint64_t meminfo_parse(std::ifstream is) {
uint64_t MeminfoParse(std::ifstream input) {
string line;
// Skip every line before the one that starts with "MemTotal"
while (std::getline(is, line) && !line.starts_with("MemTotal"))
while (std::getline(input, line) && !line.starts_with("MemTotal"))
;
// Parse the number from the line
const uint64_t num = parse_line_as_number(line);
const uint64_t num = ParseLineAsNumber(line);
return num;
}
uint64_t get_meminfo() {
return meminfo_parse(std::ifstream("/proc/meminfo")) * 1024;
uint64_t GetMemInfo() {
return MeminfoParse(std::ifstream("/proc/meminfo")) * 1024;
}
PlayerctlPlayer* init_playerctl() {
PlayerctlPlayer* InitPlayerctl() {
// Create a player manager
PlayerctlPlayerManager* const player_manager =
playerctl_player_manager_new(nullptr);
PlayerctlPlayerManager* playerManager = playerctl_player_manager_new(nullptr);
// Create an empty player list
GList* available_players = nullptr;
GList* availablePlayers = nullptr;
// Get the list of available players and put it in the player list
g_object_get(player_manager, "player-names", &available_players, nullptr);
g_object_get(playerManager, "player-names", &availablePlayers, nullptr);
// If no players are available, return nullptr
if (!available_players)
if (!availablePlayers)
return nullptr;
// Get the first player
PlayerctlPlayerName* player_name =
static_cast<PlayerctlPlayerName*>(available_players->data);
PlayerctlPlayerName* playerName =
static_cast<PlayerctlPlayerName*>(availablePlayers->data);
// Create the player
PlayerctlPlayer* const current_player =
playerctl_player_new_from_name(player_name, nullptr);
PlayerctlPlayer* const currentPlayer =
playerctl_player_new_from_name(playerName, nullptr);
// If no player is available, return nullptr
if (!current_player)
if (!currentPlayer)
return nullptr;
// Manage the player
playerctl_player_manager_manage_player(player_manager, current_player);
playerctl_player_manager_manage_player(playerManager, currentPlayer);
// Unref the player
g_object_unref(current_player);
g_object_unref(currentPlayer);
return current_player;
return currentPlayer;
}
string get_nowplaying() {
if (PlayerctlPlayer* current_player = init_playerctl())
return playerctl_player_get_title(current_player, nullptr);
string GetNowPlaying() {
if (PlayerctlPlayer* currentPlayer = InitPlayerctl())
return playerctl_player_get_title(currentPlayer, nullptr);
return "Could not get now playing info";
}

View file

@ -4,7 +4,7 @@
#include <string>
#include "os.h"
uint64_t get_meminfo() {
uint64_t GetMemInfo() {
uint64_t mem = 0;
size_t size = sizeof(mem);
@ -13,7 +13,7 @@ uint64_t get_meminfo() {
return mem;
}
std::string get_nowplaying() {
std::string GetNowPlaying() {
return "";
}

View file

@ -3,5 +3,5 @@
#include <cstdint>
#include <string>
uint64_t get_meminfo();
std::string get_nowplaying();
uint64_t GetMemInfo();
std::string GetNowPlaying();