From b3a823848c98800653c1bf49dd0e4858d9114759 Mon Sep 17 00:00:00 2001 From: Toni500git Date: Sat, 14 Sep 2024 18:43:17 +0200 Subject: [PATCH] query: user: fix/improve terminal version detection --- src/query/unix/user.cpp | 26 ++++++-------------------- src/query/unix/utils/term.cpp | 31 +++++++++++++++++++++++-------- src/query/unix/utils/term.hpp | 6 ++++-- src/util.cpp | 1 + 4 files changed, 34 insertions(+), 30 deletions(-) diff --git a/src/query/unix/user.cpp b/src/query/unix/user.cpp index 87c1fad..d7a1e17 100644 --- a/src/query/unix/user.cpp +++ b/src/query/unix/user.cpp @@ -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()) @@ -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); } diff --git a/src/query/unix/utils/term.cpp b/src/query/unix/utils/term.cpp index 94d5a9d..65d9525 100644 --- a/src/query/unix/utils/term.cpp +++ b/src/query/unix/utils/term.cpp @@ -3,23 +3,34 @@ #include "util.hpp" #include -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) @@ -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; } diff --git a/src/query/unix/utils/term.hpp b/src/query/unix/utils/term.hpp index 4c77c18..94adfde 100644 --- a/src/query/unix/utils/term.hpp +++ b/src/query/unix/utils/term.hpp @@ -3,7 +3,9 @@ #include -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 diff --git a/src/util.cpp b/src/util.cpp index 6408aca..d0a72db 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -299,6 +299,7 @@ void replace_str(std::string& str, const std::string_view from, const std::strin bool read_exec(std::vector cmd, std::string& output, bool useStdErr, bool noerror_print) { + debug("{} cmd = {}", __func__, cmd); int pipeout[2]; if (pipe(pipeout) < 0)