pushing this cuz i need to

This commit is contained in:
Mars 2024-05-29 00:28:54 -04:00
parent 37a93c57ea
commit a24f19d16b
Signed by: pupbrained
GPG key ID: 874E22DF2F9DFCB5
6 changed files with 189 additions and 112 deletions

View file

@ -4,12 +4,30 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS 1)
project(draconis++ C CXX) project(draconis++ C CXX)
add_executable(${PROJECT_NAME} src/main.cpp) set(sources )
list(APPEND sources src/main.cpp)
if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
set(MACOSX TRUE)
endif ()
if (MACOSX)
list(APPEND sources src/os/macos.cpp)
elseif (LINUX)
list(APPEND sources src/os/linux.cpp)
endif ()
add_executable(${PROJECT_NAME} ${sources})
find_package(PkgConfig REQUIRED) find_package(PkgConfig REQUIRED)
find_package(cpr REQUIRED) find_package(cpr REQUIRED)
find_package(Boost REQUIRED) find_package(Boost REQUIRED)
pkg_check_modules(${PROJECT_NAME} REQUIRED IMPORTED_TARGET fmt playerctl tomlplusplus) if (MACOSX)
pkg_check_modules(${PROJECT_NAME} REQUIRED IMPORTED_TARGET fmt tomlplusplus)
else ()
pkg_check_modules(${PROJECT_NAME} REQUIRED IMPORTED_TARGET fmt playerctl tomlplusplus)
endif ()
target_link_libraries(${PROJECT_NAME} PRIVATE PkgConfig::${PROJECT_NAME} cpr::cpr Boost::boost) target_link_libraries(${PROJECT_NAME} PRIVATE PkgConfig::${PROJECT_NAME} cpr::cpr Boost::boost)

106
flake.nix
View file

@ -21,7 +21,7 @@
ccacheWrapper = super.ccacheWrapper.override { ccacheWrapper = super.ccacheWrapper.override {
extraConfig = '' extraConfig = ''
export CCACHE_COMPRESS=1 export CCACHE_COMPRESS=1
export CCACHE_DIR="/var/cache/ccache" export CCACHE_DIR="/nix/var/cache/ccache"
export CCACHE_UMASK=007 export CCACHE_UMASK=007
if [ ! -d "$CCACHE_DIR" ]; then if [ ! -d "$CCACHE_DIR" ]; then
echo "=====" echo "====="
@ -44,8 +44,26 @@
}) })
]; ];
}; };
llvm = pkgs.llvmPackages_latest; llvm = pkgs.llvmPackages_latest;
stdenv = pkgs.ccacheStdenv.override {stdenv = pkgs.stdenvAdapters.useMoldLinker pkgs.clangStdenv;};
stdenv = pkgs.ccacheStdenv.override {
stdenv =
if pkgs.hostPlatform.isDarwin
then llvm.libcxxStdenv
else pkgs.stdenvAdapters.useMoldLinker pkgs.clangStdenv;
};
darwinPkgs = nixpkgs.lib.optionals pkgs.stdenv.isDarwin (with pkgs.darwin; [
apple_sdk.frameworks.AppKit
apple_sdk.frameworks.Carbon
apple_sdk.frameworks.Cocoa
apple_sdk.frameworks.CoreFoundation
apple_sdk.frameworks.IOKit
apple_sdk.frameworks.WebKit
apple_sdk.frameworks.Security
apple_sdk.frameworks.DisplayServices
]);
in { in {
packages = rec { packages = rec {
draconis-cpp = with pkgs; draconis-cpp = with pkgs;
@ -58,17 +76,24 @@
pkg-config pkg-config
]; ];
propagatedBuildInputs = [ propagatedBuildInputs =
boost [
glib boost185
playerctl glib
]; ]
++ (
if pkgs.hostPlatform.isLinux
then [playerctl]
else []
);
buildInputs = [ buildInputs =
fmt [
libcpr fmt
tomlplusplus libcpr
]; tomlplusplus
]
++ darwinPkgs;
buildPhase = '' buildPhase = ''
cmake . cmake .
@ -85,39 +110,42 @@
devShell = with pkgs; devShell = with pkgs;
mkShell.override {inherit stdenv;} { mkShell.override {inherit stdenv;} {
CXX = "${clang}/bin/clang++"; packages = with pkgs;
CC = "${clang}/bin/clang++"; [
# builder
gnumake
cmake
pkg-config
packages = with pkgs; [ # debugger
# builder lldb
gnumake
cmake
pkg-config
# debugger # fix headers not found
lldb clang-tools
# fix headers not found # LSP and compiler
clang-tools llvm.libstdcxxClang
# LSP and compiler # other tools
llvm.libstdcxxClang cppcheck
llvm.libllvm
# other tools # stdlib for cpp
cppcheck llvm.libcxx
llvm.libllvm
# stdlib for cpp # libraries
llvm.libcxx boost185
fmt
# libraries glib
boost libcpr
fmt tomlplusplus
glib ]
libcpr ++ (
playerctl if stdenv.isDarwin
tomlplusplus then []
]; else [playerctl]
)
++ darwinPkgs;
name = "C++"; name = "C++";
}; };

View file

@ -1,11 +1,13 @@
#include "os/os.h"
#include <boost/json/src.hpp> #include <boost/json/src.hpp>
#include <cpr/cpr.h> #include <cpr/cpr.h>
#include <fmt/chrono.h> #include <fmt/chrono.h>
#include <fmt/core.h> #include <fmt/core.h>
#include <fmt/format.h> #include <fmt/format.h>
#include <fstream> #include <fstream>
#include <playerctl/playerctl.h> #include <sys/sysctl.h>
#include <toml++/toml.hpp> #include <toml++/toml.hpp>
#include <unistd.h>
using fmt::format; using fmt::format;
using fmt::formatter; using fmt::formatter;
@ -27,74 +29,6 @@ template <> struct formatter<b_to_gib> : formatter<double> {
} }
}; };
uint64_t parse_line_as_number(const string &input) {
// Find the first number
string::size_type start = 0;
// Skip leading non-numbers
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]))
;
// Return the substring containing the number
return std::stoul(input.substr(start, end - start));
}
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"))
;
// Parse the number from the line
const auto num = parse_line_as_number(line);
return num;
}
PlayerctlPlayer *init_playerctl() {
// Create a player manager
PlayerctlPlayerManager *const player_manager =
playerctl_player_manager_new(nullptr);
// Create an empty player list
GList *available_players = nullptr;
// Get the list of available players and put it in the player list
g_object_get(player_manager, "player-names", &available_players, nullptr);
// If no players are available, return nullptr
if (!available_players)
return nullptr;
// Get the first player
const auto player_name =
static_cast<PlayerctlPlayerName *>(available_players->data);
// Create the player
PlayerctlPlayer *const current_player =
playerctl_player_new_from_name(player_name, nullptr);
// If no player is available, return nullptr
if (!current_player)
return nullptr;
// Manage the player
playerctl_player_manager_manage_player(player_manager, current_player);
// Unref the player
g_object_unref(current_player);
return current_player;
}
enum date_num { Ones, Twos, Threes, Default }; enum date_num { Ones, Twos, Threes, Default };
date_num parse_date(string const &inString) { date_num parse_date(string const &inString) {
@ -117,7 +51,7 @@ boost::json::object get_weather() {
Response r = Get(Url{format("https://api.openweathermap.org/data/2.5/" Response r = Get(Url{format("https://api.openweathermap.org/data/2.5/"
"weather?lat={}&lon={}&appid={}&units={}", "weather?lat={}&lon={}&appid={}&units={}",
"39.9537", "-74.1979", "39.9537", "-74.1979",
"00000000000000000000000000000000", "imperial")}); "be387ee0e0d79c03368c52e0e3fe4f5b", "imperial")});
value json = parse(r.text); value json = parse(r.text);
@ -129,16 +63,18 @@ int main() {
const char *name = config["general"]["name"].value_or(getlogin()); const char *name = config["general"]["name"].value_or(getlogin());
#ifdef __linux__
if (config["playerctl"]["enable"].value_or(false)) { if (config["playerctl"]["enable"].value_or(false)) {
if (PlayerctlPlayer *current_player = init_playerctl()) { if (PlayerctlPlayer *current_player = init_playerctl()) {
gchar *song_title = playerctl_player_get_title(current_player, nullptr); gchar *song_title = playerctl_player_get_title(current_player, nullptr);
fmt::println("Now playing: {}", song_title); fmt::println("Now playing: {}", song_title);
} }
} }
#endif
fmt::println("Hello {}!", name); fmt::println("Hello {}!", name);
const uint64_t meminfo = meminfo_parse(std::ifstream("/proc/meminfo")) * 1024; const uint64_t meminfo = get_meminfo();
fmt::println("{:.2f}", b_to_gib{meminfo}); fmt::println("{:.2f}", b_to_gib{meminfo});

77
src/os/linux.cpp Normal file
View file

@ -0,0 +1,77 @@
#include "os.h"
#include <fstream>
#include <playerctl/playerctl.h>
using std::string;
uint64_t parse_line_as_number(const string &input) {
// Find the first number
string::size_type start = 0;
// Skip leading non-numbers
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]))
;
// Return the substring containing the number
return std::stoul(input.substr(start, end - start));
}
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"))
;
// Parse the number from the line
const auto num = parse_line_as_number(line);
return num;
}
uint64_t get_meminfo() {
return meminfo_parse(std::ifstream("/proc/meminfo")) * 1024;
}
PlayerctlPlayer *init_playerctl() {
// Create a player manager
PlayerctlPlayerManager *const player_manager =
playerctl_player_manager_new(nullptr);
// Create an empty player list
GList *available_players = nullptr;
// Get the list of available players and put it in the player list
g_object_get(player_manager, "player-names", &available_players, nullptr);
// If no players are available, return nullptr
if (!available_players)
return nullptr;
// Get the first player
const auto player_name =
static_cast<PlayerctlPlayerName *>(available_players->data);
// Create the player
PlayerctlPlayer *const current_player =
playerctl_player_new_from_name(player_name, nullptr);
// If no player is available, return nullptr
if (!current_player)
return nullptr;
// Manage the player
playerctl_player_manager_manage_player(player_manager, current_player);
// Unref the player
g_object_unref(current_player);
return current_player;
}

11
src/os/macos.cpp Normal file
View file

@ -0,0 +1,11 @@
#include "os.h"
#include <sys/sysctl.h>
uint64_t get_meminfo() {
uint64_t mem = 0;
size_t size = sizeof(mem);
sysctlbyname("hw.memsize", &mem, &size, nullptr, 0);
return mem;
}

7
src/os/os.h Normal file
View file

@ -0,0 +1,7 @@
#include <inttypes.h>
#include <string>
using std::string;
uint64_t get_meminfo();
string get_nowplaying();