From 774757d786995ee1361865f96fda5628b081c90e Mon Sep 17 00:00:00 2001 From: Toni500git Date: Thu, 20 Jun 2024 23:33:59 +0200 Subject: [PATCH] misc: improve some query functions and config modify --- Makefile | 2 +- include/config.hpp | 4 +- include/query.hpp | 1 + src/config.cpp | 22 +++++----- src/main.cpp | 4 +- src/query.cpp | 5 --- src/query/ram.cpp | 43 ++++++++++--------- src/query/system.cpp | 98 ++++++++++++++++++++++---------------------- src/util.cpp | 2 +- 9 files changed, 88 insertions(+), 93 deletions(-) diff --git a/Makefile b/Makefile index 4499c80..e0ed41d 100644 --- a/Makefile +++ b/Makefile @@ -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) diff --git a/include/config.hpp b/include/config.hpp index 64357d2..80c1905 100644 --- a/include/config.hpp +++ b/include/config.hpp @@ -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 diff --git a/include/query.hpp b/include/query.hpp index fdfaf2b..dd0b635 100644 --- a/include/query.hpp +++ b/include/query.hpp @@ -38,6 +38,7 @@ class System { long uptime(); private: + std::array m_os_release_vars; struct utsname m_uname_infos; struct sysinfo m_sysInfos; struct passwd *m_pPwd; diff --git a/src/config.cpp b/src/config.cpp index adb51d9..4c72db2 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -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("config.source-path", "ascii"); + this->source_path = getConfigValue("config.source-path", "os"); this->offset = getConfigValue("config.offset", 5); this->gui = getConfigValue("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) { diff --git a/src/main.cpp b/src/main.cpp index d798aec..df7620f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -27,7 +27,7 @@ A command-line system information tool (or neofetch like program), which its foc -s, --source-path Path to the ascii art file to display -C, --config Path to the config file to use -d, --distro 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 @@ -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; diff --git a/src/query.cpp b/src/query.cpp index 86e0c66..e0ca4f4 100644 --- a/src/query.cpp +++ b/src/query.cpp @@ -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 ) { @@ -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 ? "" : NOCOLOR ); if ( pureOutput ) *pureOutput = pureOutput->replace( pureOutput->size() /*dollarSignIndex-pureOutputOffset*/, @@ -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) ? "" : ""; - resetclr = false; } return output; diff --git a/src/query/ram.cpp b/src/query/ram.cpp index 72e5a5c..ebb293c 100644 --- a/src/query/ram.cpp +++ b/src/query/ram.cpp @@ -6,8 +6,6 @@ using namespace Query; -static std::array get_amount(); - enum { USED = 0, AVAILABLE, @@ -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 amount = split(line, ':'); strip(amount.at(1)); ++iter_index; @@ -59,12 +40,13 @@ static std::array 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); @@ -87,3 +69,20 @@ static std::array 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; +} + diff --git a/src/query/system.cpp b/src/query/system.cpp index 5f8a721..98af5df 100644 --- a/src/query/system.cpp +++ b/src/query/system.cpp @@ -1,6 +1,7 @@ #include "query.hpp" #include "util.hpp" +#include #include #include #include @@ -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 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 get_os_release_vars() { + std::array 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(); @@ -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() { @@ -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); } diff --git a/src/util.cpp b/src/util.cpp index 1ea4be0..dbce3f6 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -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 split(std::string_view text, char delim) {