Skip to content

Commit

Permalink
query: user: fix/improve terminal version detection
Browse files Browse the repository at this point in the history
  • Loading branch information
Toni500github committed Sep 14, 2024
1 parent d287eeb commit b3a8238
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 30 deletions.
26 changes: 6 additions & 20 deletions src/query/unix/user.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,12 +248,6 @@ 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 @@ -268,28 +262,20 @@ static std::string get_term_version(std::string_view term_name)
switch (fnv1a16::hash(str_tolower(term_name.data())))
{
case "st"_fnv1a16:
{
ret = detect_st_ver();
if (ret == UNKNOWN)
get_term_version_exec(ret, true);
else
if (detect_st_ver(ret))
remove_term_name = false;
} break;
break;

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

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

default:
get_term_version_exec(ret);
get_term_version_exec(term_name, ret);
}


Expand Down
31 changes: 23 additions & 8 deletions src/query/unix/utils/term.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,34 @@
#include "util.hpp"
#include <fstream>

std::string detect_st_ver()
void get_term_version_exec(const std::string_view term, std::string& ret, bool _short, bool _stderr)
{
std::string ret, line;
ret.clear();
read_exec({ term.data(), _short ? "-v" : "--version" }, ret, _stderr);
}

bool detect_st_ver(std::string& ret)
{
std::string 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.erase(0, 3);
return true;
}

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

get_term_version_exec("st", ret, true, true);
return false;
}

std::string detect_konsole_ver()
bool detect_konsole_ver(std::string& ret)
{
const char* env = std::getenv("KONSOLE_VERSION");
if (env)
Expand All @@ -31,9 +42,13 @@ std::string detect_konsole_ver()
major /= 100;
long minor = major % 100;
major /= 100;
return fmt::format("{}.{}.{}", major, minor, patch);
ret = fmt::format("{}.{}.{}", major, minor, patch);
return true;
}
}
}

debug("failed to fast detect konsole version");
return UNKNOWN;
get_term_version_exec("konsole", ret);

return false;
}
6 changes: 4 additions & 2 deletions src/query/unix/utils/term.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

#include <string>

std::string detect_st_ver();
std::string detect_konsole_ver();
void get_term_version_exec(const std::string_view term, std::string& ret, bool _short = false, bool _stderr = false);

bool detect_konsole_ver(std::string& ret);
bool detect_st_ver(std::string& ret);

#endif // _TERM_HPP
1 change: 1 addition & 0 deletions src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ void replace_str(std::string& str, const std::string_view from, const std::strin

bool read_exec(std::vector<const char*> cmd, std::string& output, bool useStdErr, bool noerror_print)
{
debug("{} cmd = {}", __func__, cmd);
int pipeout[2];

if (pipe(pipeout) < 0)
Expand Down

0 comments on commit b3a8238

Please sign in to comment.