From 1df718ca26c19f6d440a4483f75d7a28003bf976 Mon Sep 17 00:00:00 2001 From: Toni500git Date: Thu, 27 Jun 2024 20:18:09 +0200 Subject: [PATCH] query: user: add user component --- include/config.hpp | 5 +++-- include/query.hpp | 12 ++++++++++- include/util.hpp | 1 + src/main.cpp | 6 ++++++ src/parse.cpp | 47 ++++++++++++++++++++++++++++++++++++++---- src/query/system.cpp | 10 --------- src/user.cpp | 49 ++++++++++++++++++++++++++++++++++++++++++++ src/util.cpp | 7 +++++++ 8 files changed, 120 insertions(+), 17 deletions(-) create mode 100644 src/user.cpp diff --git a/include/config.hpp b/include/config.hpp index 97fc784b..bde05fd0 100644 --- a/include/config.hpp +++ b/include/config.hpp @@ -93,14 +93,15 @@ inline const constexpr std::string_view AUTOCONFIG = R"#([config] # includes directive, include the top name of each module you use. # e.g. if you want to use $, then `includes = ["os"]`. # you can also put specific includes, for example if you only want os.name, then `includes = ["os.name"]` -includes = ["os", "system", "cpu", "gpu", "ram"] +includes = ["os", "system", "user", "cpu", "gpu", "ram"] layout = [ - "${red}$${0}@${cyan}$", + "${red}$${0}@${cyan}$", "───────────────────────────", "${red}OS${0}: $ $", "${yellow}Host${0}: $ $", "${cyan}Uptime${0}: $ hours, $ minutes", + "${cyan}Shell${0}: $ $", "${green}Kernel${0}: $ $", "${magenta}CPU${0}: $ ($) @ $GHz", "${blue}GPU${0}: $", diff --git a/include/query.hpp b/include/query.hpp index a7eb305e..9b01f3f8 100644 --- a/include/query.hpp +++ b/include/query.hpp @@ -34,7 +34,6 @@ class System { std::string kernel_version(); std::string hostname(); std::string arch(); - std::string username(); std::string os_pretty_name(); std::string os_name(); std::string os_id(); @@ -49,6 +48,17 @@ class System { std::array m_os_release_vars; struct utsname m_uname_infos; struct sysinfo m_sysInfos; +}; + +class User { +public: + User(); + std::string name(); + std::string shell(); + std::string shell_path(); + std::string shell_version(); + +private: struct passwd *m_pPwd; }; diff --git a/include/util.hpp b/include/util.hpp index 398b29a6..1c8a90b6 100644 --- a/include/util.hpp +++ b/include/util.hpp @@ -27,6 +27,7 @@ std::string expandVar(std::string& str); // Replace string inplace void replace_str(std::string &str, const std::string& from, const std::string& to); std::string str_tolower(std::string str); +std::string str_toupper(std::string str); void strip(std::string& input); std::string read_by_syspath(const std::string_view path); fmt::rgb hexStringToColor(std::string_view hexstr); diff --git a/src/main.cpp b/src/main.cpp index 86236318..cb4d49c2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -57,6 +57,12 @@ os hostname : hostname of the OS [mymainPC] arch : the architecture of the machine [x86_64, aarch64] +user + name : name you are currently logged in (not real name) [toni69] + shell : login shell [zsh] + shell_path : login shell (with path) [/bin/zsh] + shell_version : login shell version (may be not correct) [5.9] + system host_name : Host (aka. Motherboard) model name [PRO B550M-P GEN3 (MS-7D95)] host_version : Host (aka. Motherboard) model version [1.0] diff --git a/src/parse.cpp b/src/parse.cpp index e68f887e..4adf5c38 100644 --- a/src/parse.cpp +++ b/src/parse.cpp @@ -344,7 +344,6 @@ void addModuleValues(systemInfo_t& sysInfo, const std::string_view moduleName) { sysInfo.insert( {"os", { {"name", variant(query_system.os_pretty_name())}, - {"username", variant(query_system.username())}, {"uptime_secs", variant((size_t)uptime_secs.count()%60)}, {"uptime_mins", variant((size_t)uptime_mins.count()%60)}, {"uptime_hours", variant((size_t)uptime_hours.count())}, @@ -370,6 +369,20 @@ void addModuleValues(systemInfo_t& sysInfo, const std::string_view moduleName) { return; } + if (moduleName == "user") { + Query::User query_user; + + sysInfo.insert( + {"user", { + {"name", variant(query_user.name())}, + {"shell", variant(query_user.shell())}, + {"shell_path", variant(query_user.shell_path())}, + {"shell_version", variant(query_user.shell_version())} + }} + ); + + return; + } if (moduleName == "cpu") { Query::CPU query_cpu; @@ -439,9 +452,6 @@ void addValueFromModule(systemInfo_t& sysInfo, const std::string& moduleName, co case "name"_fnv1a32: sysInfo[moduleName].insert({moduleValueName, variant(query_system.os_pretty_name())}); break; - case "username"_fnv1a32: - sysInfo[moduleName].insert({moduleValueName, variant(query_system.username())}); break; - case "uptime_secs"_fnv1a32: sysInfo[moduleName].insert({moduleValueName, variant((size_t)uptime_secs.count()%60)}); break; @@ -493,6 +503,35 @@ void addValueFromModule(systemInfo_t& sysInfo, const std::string& moduleName, co return; } + if (moduleName == "user") { + Query::User query_user; + + if (sysInfo.find(moduleName) == sysInfo.end()) + sysInfo.insert( + {moduleName, { }} + ); + + if (sysInfo[moduleName].find(moduleValueName) == sysInfo[moduleName].end()) + { + switch (module_hash) { + case "name"_fnv1a32: + sysInfo[moduleName].insert({moduleValueName, variant(query_user.name())}); break; + + case "shell"_fnv1a32: + sysInfo[moduleName].insert({moduleValueName, variant(query_user.shell())}); break; + + case "shell_path"_fnv1a32: + sysInfo[moduleName].insert({moduleValueName, variant(query_user.shell_path())}); break; + + case "shell_version"_fnv1a32: + sysInfo[moduleName].insert({moduleValueName, variant(query_user.shell_version())}); break; + } + } + + return; + + } + if (moduleName == "cpu") { Query::CPU query_cpu; diff --git a/src/query/system.cpp b/src/query/system.cpp index 48c1c79c..6980f204 100644 --- a/src/query/system.cpp +++ b/src/query/system.cpp @@ -3,9 +3,7 @@ #include #include -#include #include -#include #include using namespace Query; @@ -57,7 +55,6 @@ static std::array get_os_release_vars() { } System::System() { - uid_t uid = geteuid(); if (uname(&m_uname_infos) != 0) die("uname() failed: {}\nCould not get system infos", errno); @@ -65,9 +62,6 @@ System::System() { if (sysinfo(&m_sysInfos) != 0) die("uname() failed: {}\nCould not get system infos", errno); - if (m_pPwd = getpwuid(uid), !m_pPwd) - die("getpwent failed: {}\nCould not get user infos", errno); - m_os_release_vars = get_os_release_vars(); } @@ -87,10 +81,6 @@ std::string System::arch() { return m_uname_infos.machine; } -std::string System::username() { - return m_pPwd->pw_name; -} - long System::uptime() { return m_sysInfos.uptime; } diff --git a/src/user.cpp b/src/user.cpp new file mode 100644 index 00000000..80ab4ec7 --- /dev/null +++ b/src/user.cpp @@ -0,0 +1,49 @@ +#include "query.hpp" +#include "util.hpp" +#include "switch_fnv1a.hpp" + +using namespace Query; + +User::User() { + uid_t uid = geteuid(); + + if (m_pPwd = getpwuid(uid), !m_pPwd) + die("getpwent failed: {}\nCould not get user infos", errno); +} + +std::string User::name() { + return m_pPwd->pw_name; +} + +std::string User::shell() { + std::string shell = this->shell_path(); + shell.erase(0, shell.find_last_of('/')); + + return shell; +} + +std::string User::shell_path() { + return m_pPwd->pw_shell; +} + +std::string User::shell_version() { + std::string shell = this->shell(); + std::string ret = UNKNOWN; + + switch (fnv1a32::hash(shell)) { + case "bash"_fnv1a32: + case "osh"_fnv1a32: + case "zsh"_fnv1a32: + ret = shell_exec(fmt::format("{} -c \"printf %s \"${}_VERSION\"\"", shell, str_toupper(shell))); break; + + case "nu"_fnv1a32: + ret = shell_exec("nu -c \"version | get version\""); break; + + default: + ret = shell_exec(fmt::format("{} --version", shell)); + ret.erase(0, ret.find(shell)); + } + + strip(ret); + return ret; +} diff --git a/src/util.cpp b/src/util.cpp index b47c92f5..86abda5f 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -125,6 +125,13 @@ std::string str_tolower(std::string str) { return str; } +std::string str_toupper(std::string str) { + for (auto& x : str) { + x = std::toupper(x); + } + return str; +} + // Function to perform binary search on the pci vendors array to find a device from a vendor. std::string binarySearchPCIArray(std::string_view vendor_id_s, std::string_view pci_id_s) { std::string_view vendor_id = hasStart(vendor_id_s, "0x") ? vendor_id_s.substr(2) : vendor_id_s;