Skip to content

Commit

Permalink
misc: detect st version from binary + some coding changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Toni500github committed Sep 6, 2024
1 parent 114d207 commit be28ba4
Show file tree
Hide file tree
Showing 11 changed files with 145 additions and 93 deletions.
9 changes: 5 additions & 4 deletions include/parse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,19 @@ std::string parse(const std::string_view input, systemInfo_t& systemInfo, std::s
* else, error out.
* @param sysInfo The systemInfo_t map
* @param moduleName The module name
* @param moduleValueName The module member value name
* @param moduleMemberName The module member name
* @param config The config
* @param colors The colors
* @param parsingLayout If we are parsing the layout or not (default true)
*/
void addValueFromModule(systemInfo_t& sysInfo, const std::string& moduleName, const std::string& moduleValueName,
const Config& config, const colors_t& colors);
void addValueFromModule(systemInfo_t& sysInfo, const std::string& moduleName, const std::string& moduleMemberName,
const Config& config, const colors_t& colors, bool parsingLayout = true);

/*
* Return a module member value
*/
std::string getInfoFromName(const systemInfo_t& systemInfo, const std::string_view moduleName,
const std::string_view moduleValueName);
const std::string_view moduleMemberName);

// Function to combine multiple fmt::text_style arguments
template <typename... Styles>
Expand Down
165 changes: 94 additions & 71 deletions src/parse.cpp

Large diffs are not rendered by default.

6 changes: 2 additions & 4 deletions src/query/unix/cpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ static CPU::CPU_t get_cpu_infos()
// sometimes /proc/cpuinfo at model name
// the name will contain the min freq
// happens on intel cpus especially
size_t pos = 0;
if ((pos = ret.name.find('@')) != std::string::npos)
const size_t& pos = ret.name.rfind('@');
if (pos != std::string::npos)
ret.name.erase(pos - 1);

cpu_mhz /= 1000;
Expand Down Expand Up @@ -86,8 +86,6 @@ static CPU::CPU_t get_cpu_infos()

CPU::CPU()
{
debug("Constructing {}", __func__);

if (!m_bInit)
{
m_cpu_infos = get_cpu_infos();
Expand Down
2 changes: 0 additions & 2 deletions src/query/unix/disk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ using namespace Query;

Disk::Disk(const std::string_view path, std::vector<std::string_view>& paths)
{
debug("Constructing {}", __func__);

if (std::find(paths.begin(), paths.end(), path) == paths.end())
{ debug("DISK: adding {} to disk paths", path); paths.push_back(path); }
else
Expand Down
1 change: 0 additions & 1 deletion src/query/unix/gpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ static GPU::GPU_t get_gpu_infos(const std::string_view m_vendor_id_s, const std:

GPU::GPU(std::uint16_t& id, std::vector<std::uint16_t>& queried_gpus)
{
debug("Constructing {}", __func__);
if (std::find(queried_gpus.begin(), queried_gpus.end(), id) == queried_gpus.end())
queried_gpus.push_back(id);
else
Expand Down
1 change: 0 additions & 1 deletion src/query/unix/ram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ static RAM::RAM_t get_amount()

RAM::RAM()
{
debug("Constructing {}", __func__);
if (!m_bInit)
{
m_memory_infos = get_amount();
Expand Down
3 changes: 1 addition & 2 deletions src/query/unix/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ static System::System_t get_system_infos()
}

// get OS /etc/os-release infos
static u_short iter_index = 0;
u_short iter_index = 0;
std::string line;
while (std::getline(os_release_file, line) && iter_index < 5)
{
Expand All @@ -95,7 +95,6 @@ static System::System_t get_system_infos()

System::System()
{
debug("Constructing {}", __func__);
if (!m_bInit)
{
if (uname(&m_uname_infos) != 0)
Expand Down
2 changes: 0 additions & 2 deletions src/query/unix/theme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,6 @@ Theme::Theme(const std::uint8_t ver, systemInfo_t& queried_themes, std::vector<s
: m_queried_themes(queried_themes),
m_theme_name_version(theme_name_version)
{
debug("Constructing {}", __func__);

if (std::find(queried_themes_names.begin(), queried_themes_names.end(), m_theme_name_version)
== queried_themes_names.end())
queried_themes_names.push_back(m_theme_name_version);
Expand Down
22 changes: 16 additions & 6 deletions src/query/unix/user.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "switch_fnv1a.hpp"
#include "util.hpp"
#include "utils/dewm.hpp"
#include "utils/term.hpp"

using namespace Query;

Expand Down Expand Up @@ -252,12 +253,23 @@ static std::string get_term_version(std::string_view term_name)
if (term_name.empty())
return UNKNOWN;

bool remove_term_name = true;
std::string ret;

if (hasStart(term_name, "kitty"))
term_name = "kitten";

if (term_name == "st")
read_exec({ term_name.data(), "-v" }, ret, true);
{
ret = detect_st_ver();
if (ret == UNKNOWN)
{
ret.clear();
read_exec({ term_name.data(), "-v" }, ret, true);
}
else
remove_term_name = false;
}
// tell your terminal to NOT RETURN ERROR WHEN ASKING FOR ITS VERSION (looking at you st)
else if (term_name == "xterm")
read_exec({ term_name.data(), "-v" }, ret);
Expand All @@ -269,7 +281,6 @@ static std::string get_term_version(std::string_view term_name)
if (ret.empty())
return UNKNOWN;

bool remove_term_name = true;
if (hasStart(ret, "# GNOME"))
{
ret.erase(0, "# GNOME Terminal "_len);
Expand All @@ -284,11 +295,11 @@ static std::string get_term_version(std::string_view term_name)
return ret;
}

size_t pos = 0;
if (remove_term_name)
ret.erase(0, term_name.length() + 1);

if ((pos = ret.find(' ')) != std::string::npos)

const size_t pos = ret.find(' ');
if (pos != std::string::npos)
ret.erase(pos);

debug("get_term_version ret after = {}", ret);
Expand All @@ -297,7 +308,6 @@ static std::string get_term_version(std::string_view term_name)

User::User()
{
debug("Constructing {}", __func__);
if (!m_bInit)
{
uid_t uid = geteuid();
Expand Down
19 changes: 19 additions & 0 deletions src/query/unix/utils/term.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "term.hpp"
#include "util.hpp"
#include <fstream>

std::string detect_st_ver()
{
std::string ret, line;
std::ifstream f(which("st"), std::ios::binary);

while (read_binary_file(f, line))
{
if (line == "WINDOWID" && hasStart(ret, "%s "))
return ret.substr(3);

ret = line;
}
debug("failed to detect st version");
return UNKNOWN;
}
8 changes: 8 additions & 0 deletions src/query/unix/utils/term.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef _TERM_HPP
#define _TERM_HPP

#include <string>

std::string detect_st_ver();

#endif // _TERM_HPP

0 comments on commit be28ba4

Please sign in to comment.