Skip to content

Commit

Permalink
query: user: add user component
Browse files Browse the repository at this point in the history
  • Loading branch information
Toni500github committed Jun 27, 2024
1 parent 5d2ce9b commit 1df718c
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 17 deletions.
5 changes: 3 additions & 2 deletions include/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 $<os.name>, 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}$<os.username>${0}@${cyan}$<os.hostname>",
"${red}$<user.name>${0}@${cyan}$<os.hostname>",
"───────────────────────────",
"${red}OS${0}: $<os.name> $<os.arch>",
"${yellow}Host${0}: $<system.host_vendor> $<system.host_name>",
"${cyan}Uptime${0}: $<os.uptime_hours> hours, $<os.uptime_mins> minutes",
"${cyan}Shell${0}: $<user.shell> $<user.shell_version>",
"${green}Kernel${0}: $<os.kernel_name> $<os.kernel_version>",
"${magenta}CPU${0}: $<cpu.name> ($<cpu.nproc>) @ $<cpu.freq_max>GHz",
"${blue}GPU${0}: $<gpu.name>",
Expand Down
12 changes: 11 additions & 1 deletion include/query.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -49,6 +48,17 @@ class System {
std::array<std::string, 4> 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;
};

Expand Down
1 change: 1 addition & 0 deletions include/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
47 changes: 43 additions & 4 deletions src/parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())},
Expand All @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down
10 changes: 0 additions & 10 deletions src/query/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@

#include <array>
#include <cerrno>
#include <fstream>
#include <algorithm>
#include <pwd.h>
#include <unistd.h>

using namespace Query;
Expand Down Expand Up @@ -57,17 +55,13 @@ static std::array<std::string, 4> 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);

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

Expand All @@ -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;
}
Expand Down
49 changes: 49 additions & 0 deletions src/user.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
7 changes: 7 additions & 0 deletions src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 1df718c

Please sign in to comment.