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)
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(cpr 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)

106
flake.nix
View file

@ -21,7 +21,7 @@
ccacheWrapper = super.ccacheWrapper.override {
extraConfig = ''
export CCACHE_COMPRESS=1
export CCACHE_DIR="/var/cache/ccache"
export CCACHE_DIR="/nix/var/cache/ccache"
export CCACHE_UMASK=007
if [ ! -d "$CCACHE_DIR" ]; then
echo "====="
@ -44,8 +44,26 @@
})
];
};
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 {
packages = rec {
draconis-cpp = with pkgs;
@ -58,17 +76,24 @@
pkg-config
];
propagatedBuildInputs = [
boost
glib
playerctl
];
propagatedBuildInputs =
[
boost185
glib
]
++ (
if pkgs.hostPlatform.isLinux
then [playerctl]
else []
);
buildInputs = [
fmt
libcpr
tomlplusplus
];
buildInputs =
[
fmt
libcpr
tomlplusplus
]
++ darwinPkgs;
buildPhase = ''
cmake .
@ -85,39 +110,42 @@
devShell = with pkgs;
mkShell.override {inherit stdenv;} {
CXX = "${clang}/bin/clang++";
CC = "${clang}/bin/clang++";
packages = with pkgs;
[
# builder
gnumake
cmake
pkg-config
packages = with pkgs; [
# builder
gnumake
cmake
pkg-config
# debugger
lldb
# debugger
lldb
# fix headers not found
clang-tools
# fix headers not found
clang-tools
# LSP and compiler
llvm.libstdcxxClang
# LSP and compiler
llvm.libstdcxxClang
# other tools
cppcheck
llvm.libllvm
# other tools
cppcheck
llvm.libllvm
# stdlib for cpp
llvm.libcxx
# stdlib for cpp
llvm.libcxx
# libraries
boost
fmt
glib
libcpr
playerctl
tomlplusplus
];
# libraries
boost185
fmt
glib
libcpr
tomlplusplus
]
++ (
if stdenv.isDarwin
then []
else [playerctl]
)
++ darwinPkgs;
name = "C++";
};

View file

@ -1,11 +1,13 @@
#include "os/os.h"
#include <boost/json/src.hpp>
#include <cpr/cpr.h>
#include <fmt/chrono.h>
#include <fmt/core.h>
#include <fmt/format.h>
#include <fstream>
#include <playerctl/playerctl.h>
#include <sys/sysctl.h>
#include <toml++/toml.hpp>
#include <unistd.h>
using fmt::format;
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 };
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/"
"weather?lat={}&lon={}&appid={}&units={}",
"39.9537", "-74.1979",
"00000000000000000000000000000000", "imperial")});
"be387ee0e0d79c03368c52e0e3fe4f5b", "imperial")});
value json = parse(r.text);
@ -129,16 +63,18 @@ int main() {
const char *name = config["general"]["name"].value_or(getlogin());
#ifdef __linux__
if (config["playerctl"]["enable"].value_or(false)) {
if (PlayerctlPlayer *current_player = init_playerctl()) {
gchar *song_title = playerctl_player_get_title(current_player, nullptr);
fmt::println("Now playing: {}", song_title);
}
}
#endif
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});

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();