Skip to content

Commit

Permalink
query: user: fast detect konsole version
Browse files Browse the repository at this point in the history
  • Loading branch information
Toni500github committed Sep 14, 2024
1 parent 754fc6e commit d287eeb
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ disk(/path/to/fs)
# Infos are gotten from `/sys/class/drm/` and on each cardN directory
gpu
name : GPU model name [NVIDIA GeForce GTX 1650]
vendor : GPU vendor (UNSTABLE IDK WHY) [NVIDIA Corporation]
vendor : GPU vendor [NVIDIA Corporation]
cpu
cpu : CPU model name with number of virtual proccessors and max freq [AMD Ryzen 5 5500 (12) @ 4.90 GHz]
Expand Down
4 changes: 4 additions & 0 deletions src/parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,10 @@ void addValueFromModule(systemInfo_t& sysInfo, const std::string& moduleName, co
case "initsys_version"_fnv1a16: SYSINFO_INSERT(query_system.os_initsys_version()); break;

case "hostname"_fnv1a16: SYSINFO_INSERT(query_system.hostname()); break;

case "version_codename"_fnv1a16: SYSINFO_INSERT(query_system.os_version_codename()); break;

case "version_id"_fnv1a16: SYSINFO_INSERT(query_system.os_versionid()); break;
}
}
}
Expand Down
43 changes: 30 additions & 13 deletions src/query/unix/user.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,12 @@ static std::string get_term_name(std::string& term_ver)
return term_name;
}

static void get_term_version_exec(std::string& term, short v = false)
{
term.clear();
read_exec({ term.data(), v ? "-v" : "--version" }, term, true);
}

static std::string get_term_version(std::string_view term_name)
{
if (term_name.empty())
Expand All @@ -259,22 +265,33 @@ static std::string get_term_version(std::string_view term_name)
if (hasStart(term_name, "kitty"))
term_name = "kitten";

if (term_name == "st")
switch (fnv1a16::hash(str_tolower(term_name.data())))
{
ret = detect_st_ver();
if (ret == UNKNOWN)
case "st"_fnv1a16:
{
ret.clear();
read_exec({ term_name.data(), "-v" }, ret, true);
}
else
remove_term_name = false;
ret = detect_st_ver();
if (ret == UNKNOWN)
get_term_version_exec(ret, true);
else
remove_term_name = false;
} break;

case "konsole"_fnv1a16:
{
ret = detect_konsole_ver();
if (ret == UNKNOWN)
get_term_version_exec(ret);
else
remove_term_name = false;
} break;

case "xterm"_fnv1a16:
get_term_version_exec(ret, true); break;

default:
get_term_version_exec(ret);
}
// 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);
else
read_exec({ term_name.data(), "--version" }, ret);


debug("get_term_version ret = {}", ret);

Expand Down
22 changes: 21 additions & 1 deletion src/query/unix/utils/term.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "term.hpp"
#include "fmt/format.h"
#include "util.hpp"
#include <fstream>

Expand All @@ -14,6 +15,25 @@ std::string detect_st_ver()

ret = line;
}
debug("failed to detect st version");
debug("failed to fast detect st version");
return UNKNOWN;
}

std::string detect_konsole_ver()
{
const char* env = std::getenv("KONSOLE_VERSION");
if (env)
{
long major = strtol(env, NULL, 10);
if (major >= 0)
{
long patch = major % 100;
major /= 100;
long minor = major % 100;
major /= 100;
return fmt::format("{}.{}.{}", major, minor, patch);
}
}
debug("failed to fast detect konsole version");
return UNKNOWN;
}
1 change: 1 addition & 0 deletions src/query/unix/utils/term.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
#include <string>

std::string detect_st_ver();
std::string detect_konsole_ver();

#endif // _TERM_HPP

0 comments on commit d287eeb

Please sign in to comment.