Skip to content

Commit

Permalink
misc: improve some query functions and config modify
Browse files Browse the repository at this point in the history
  • Loading branch information
Toni500github committed Jun 20, 2024
1 parent 40b8153 commit 774757d
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 93 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ VENDOR_TEST ?= 0
# WAY easier way to build debug and release builds
ifeq ($(DEBUG), 1)
BUILDDIR = build/debug
CXXFLAGS := -ggdb -Wall $(DEBUG_CXXFLAGS) $(CXXFLAGS)
CXXFLAGS := -ggdb -Wall -DDEBUG=1 $(DEBUG_CXXFLAGS) $(CXXFLAGS)
else
BUILDDIR = build/release
CXXFLAGS := -O2 $(CXXFLAGS)
Expand Down
4 changes: 2 additions & 2 deletions include/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,10 @@ layout = [
]
# display ascii-art or image/gif (GUI only) near layout
# put "ascii" for displaying the OS ascii-art
# put "os" for displaying the OS ascii-art
# or the "/path/to/file" for displaying custom files
# or "off" for disabling ascii-art or image displaying
source-path = "ascii"
source-path = "os"
# offset between the ascii art and the system infos
offset = 5
Expand Down
1 change: 1 addition & 0 deletions include/query.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class System {
long uptime();

private:
std::array<std::string, 8> m_os_release_vars;
struct utsname m_uname_infos;
struct sysinfo m_sysInfos;
struct passwd *m_pPwd;
Expand Down
22 changes: 11 additions & 11 deletions src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,27 +59,27 @@ void Config::loadConfigFile(std::string_view filename) {
warn("An element of the includes variable in {} is not a string", filename);
});

this->source_path = getConfigValue<std::string>("config.source-path", "ascii");
this->source_path = getConfigValue<std::string>("config.source-path", "os");
this->offset = getConfigValue<u_short>("config.offset", 5);
this->gui = getConfigValue<bool>("gui.enable", false);

color.black = this->getThemeValue("config.black", "\033[1;90m");
color.black = this->getThemeValue("config.black", "\033[1;90m");
color.red = this->getThemeValue("config.red", "\033[1;91m");
color.green = this->getThemeValue("config.green", "\033[1;92m");
color.yellow = this->getThemeValue("config.yellow", "\033[1;93m");
color.blue = this->getThemeValue("config.blue", "\033[1;94m");
color.magenta = this->getThemeValue("config.magenta", "\033[1;95m");
color.cyan = this->getThemeValue("config.cyan", "\033[1;96m");
color.white = this->getThemeValue("config.white", "\033[1;97m");
color.white = this->getThemeValue("config.white", "\033[1;97m");

color.gui_black = this->getThemeValue("gui.black", "#000005");
color.gui_red = this->getThemeValue("gui.red", "#ff2000");
color.gui_green = this->getThemeValue("gui.green", "#00ff00");
color.gui_blue = this->getThemeValue("gui.blue", "#00aaff");
color.gui_cyan = this->getThemeValue("gui.cyan", "#00ffff");
color.gui_yellow = this->getThemeValue("gui.yellow", "#ffff00");
color.gui_magenta = this->getThemeValue("gui.magenta", "#ff11cc");
color.gui_white = this->getThemeValue("gui.white", "#ffffff");
color.gui_black = this->getThemeValue("gui.black", "!#000005");
color.gui_red = this->getThemeValue("gui.red", "!#ff2000");
color.gui_green = this->getThemeValue("gui.green", "!#00ff00");
color.gui_blue = this->getThemeValue("gui.blue", "!#00aaff");
color.gui_cyan = this->getThemeValue("gui.cyan", "!#00ffff");
color.gui_yellow = this->getThemeValue("gui.yellow", "!#ffff00");
color.gui_magenta = this->getThemeValue("gui.magenta", "!#ff11cc");
color.gui_white = this->getThemeValue("gui.white", "!#ffffff");
}

std::string Config::getThemeValue(const std::string& value, const std::string& fallback) {
Expand Down
4 changes: 2 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ A command-line system information tool (or neofetch like program), which its foc
-s, --source-path <path> Path to the ascii art file to display
-C, --config <path> Path to the config file to use
-d, --distro <name> Print a custom distro logo (must be the same name, uppercase or lowercase)
-g, --gui Use GUI mode instead of priting in the terminal (customfetch needs GUI_SUPPORT to be enabled at compile time)
-g, --gui Use GUI mode instead of priting in the terminal (use -V to check if it's enabled)
-l. --list-components Print the list of the components and its members
-h, --help Print this help menu
-V, --version Print the version along with the git branch it was built
Expand Down Expand Up @@ -180,7 +180,7 @@ int main (int argc, char *argv[]) {
if ( config.source_path.empty() || config.source_path == "off" )
config.m_disable_source = true;

if (config.source_path == "ascii")
if (config.source_path == "os")
config.m_display_distro = true;
else
config.m_display_distro = false;
Expand Down
5 changes: 0 additions & 5 deletions src/query.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ std::string parse( std::string& input, systemInfo_t& systemInfo, std::unique_ptr
size_t dollarSignIndex = 0;
size_t pureOutputOffset = 0;
bool start = false;
bool resetclr = false;

while ( true )
{
Expand Down Expand Up @@ -201,7 +200,6 @@ std::string parse( std::string& input, systemInfo_t& systemInfo, std::unique_ptr
case '}': // please pay very attention when reading this unreadable code
if ( command == "0" )
{
resetclr = true;
output = output.replace( dollarSignIndex, ( endBracketIndex + 1 ) - dollarSignIndex, config.gui ? "</span><span>" : NOCOLOR );
if ( pureOutput )
*pureOutput = pureOutput->replace( pureOutput->size() /*dollarSignIndex-pureOutputOffset*/,
Expand Down Expand Up @@ -321,9 +319,6 @@ std::string parse( std::string& input, systemInfo_t& systemInfo, std::unique_ptr
}
break;
}
// close the span tag of the reseted color
//output += (config.gui && resetclr) ? "</span>" : "";
resetclr = false;
}

return output;
Expand Down
43 changes: 21 additions & 22 deletions src/query/ram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

using namespace Query;

static std::array<size_t, 3> get_amount();

enum {
USED = 0,
AVAILABLE,
Expand All @@ -24,25 +22,8 @@ std::string_view meminfo_path = "/proc/meminfo";

// minimaze the while loop iteration once we have all the values we needed
// less cpu cicles and saving ms of time
u_short iter_index = 0;

RAM::RAM() {
m_memory_infos = get_amount();
}

size_t RAM::free_amount() {
return m_memory_infos.at(AVAILABLE) / 1024;
}

size_t RAM::used_amount() {
return m_memory_infos.at(USED) / 1024;
}

size_t RAM::total_amount() {
return m_memory_infos.at(TOTAL) / 1024;
}

static size_t get_from_text(std::string& line) {
static size_t get_from_text(std::string& line, u_short& iter_index) {
std::vector<std::string> amount = split(line, ':');
strip(amount.at(1));
++iter_index;
Expand All @@ -59,12 +40,13 @@ static std::array<size_t, 3> get_amount() {
}

std::string line;
u_short iter_index = 0;
while (std::getline(file, line) && iter_index < 2) {
if (line.find("MemAvailable:") != std::string::npos)
memory_infos.at(AVAILABLE) = get_from_text(line);
memory_infos.at(AVAILABLE) = get_from_text(line, iter_index);

if (line.find("MemTotal:") != std::string::npos)
memory_infos.at(TOTAL) = get_from_text(line);
memory_infos.at(TOTAL) = get_from_text(line, iter_index);

/*if (line.find("Shmem:") != std::string::npos)
extra_mem_info.at(SHMEM) = get_from_text(line);
Expand All @@ -87,3 +69,20 @@ static std::array<size_t, 3> get_amount() {

return memory_infos;
}

RAM::RAM() {
m_memory_infos = get_amount();
}

size_t RAM::free_amount() {
return m_memory_infos.at(AVAILABLE) / 1024;
}

size_t RAM::used_amount() {
return m_memory_infos.at(USED) / 1024;
}

size_t RAM::total_amount() {
return m_memory_infos.at(TOTAL) / 1024;
}

98 changes: 49 additions & 49 deletions src/query/system.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "query.hpp"
#include "util.hpp"

#include <array>
#include <cerrno>
#include <fstream>
#include <algorithm>
Expand All @@ -9,6 +10,50 @@

using namespace Query;

enum {
PRETTY_NAME = 0,
NAME,
ID_LIKE,
ID,
BUILD_ID,
VERSION_ID,
_VERSION, // conflicts with the macro VERSION so had to put _
VERSION_CODENAME
};

static std::string get_var(std::string& line, u_short& iter_index) {
std::vector<std::string> var = split(line, '=');
std::string ret = var.at(1);
ret.erase(std::remove(ret.begin(), ret.end(), '\"'), ret.end());
++iter_index;
return ret;
}

static std::array<std::string, 8> get_os_release_vars() {
std::array<std::string, 8> ret;
for (int i = 0; i < 8; i++)
ret.at(i) = UNKNOWN;

std::string_view os_release_path = "/etc/os-release";
std::ifstream os_release_file(os_release_path.data());
if (!os_release_file.is_open()) {
error("Could not open {}", os_release_path);
return ret;
}

u_short iter_index = 0;
std::string line;
while (std::getline(os_release_file, line) && iter_index < 2) {
if(hasStart(line, "PRETTY_NAME="))
ret.at(PRETTY_NAME) = get_var(line, iter_index);

if(hasStart(line, "NAME="))
ret.at(NAME) = get_var(line, iter_index);
}

return ret;
}

System::System() {
uid_t uid = geteuid();

Expand All @@ -20,6 +65,8 @@ System::System() {

if (m_pPwd = getpwuid(uid), !m_pPwd)
die("getpwent failed: {}\nCould not get user infos", errno);

m_os_release_vars = get_os_release_vars();
}

std::string System::kernel_name() {
Expand All @@ -46,57 +93,10 @@ long System::uptime() {
return m_sysInfos.uptime;
}

// TODO: this just temp, i'll then fix it
std::string System::os_pretty_name() {
std::string sysName = this->kernel_name();

if (sysName == "Linux") {
std::string os_pretty_name;
std::string_view os_release_path = "/etc/os-release";
std::ifstream os_release_file(os_release_path.data());
if (!os_release_file.is_open()) {
error("Could not open {}", os_release_path.data());
return UNKNOWN;
}

std::string line;
while (std::getline(os_release_file, line)) {
if(line.rfind("PRETTY_NAME=", 0) == 0) {
os_pretty_name = line.substr(12);
os_pretty_name.erase(std::remove(os_pretty_name.begin(), os_pretty_name.end(), '\"'), os_pretty_name.end());

return os_pretty_name;
}
}

}

return UNKNOWN;
return m_os_release_vars.at(PRETTY_NAME);
}

std::string System::os_name() {
std::string sysName = this->kernel_name();

if (sysName == "Linux") {
std::string os_pretty_name;
std::string_view os_release_path = "/etc/os-release";
std::ifstream os_release_file(os_release_path.data());
if (!os_release_file.is_open()) {
error("Could not open {}", os_release_path.data());
return UNKNOWN;
}

std::string line;
while (std::getline(os_release_file, line)) {
if(line.rfind("NAME=", 0) == 0) {
os_pretty_name = line.substr(5);
os_pretty_name.erase(std::remove(os_pretty_name.begin(), os_pretty_name.end(), '\"'), os_pretty_name.end());

return os_pretty_name;
}
}

}

return UNKNOWN;
return m_os_release_vars.at(NAME);
}
2 changes: 1 addition & 1 deletion src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ bool hasEnding(std::string_view fullString, std::string_view ending) {
bool hasStart(std::string_view fullString, std::string_view start) {
if (start.length() > fullString.length())
return false;
return (0 == fullString.compare(0, start.length(), start));
return (fullString.substr(0, start.size()) == start);
}

std::vector<std::string> split(std::string_view text, char delim) {
Expand Down

0 comments on commit 774757d

Please sign in to comment.