From 984438e98d51dd1ee1074c5d3eac04b2f0388021 Mon Sep 17 00:00:00 2001 From: iTrooz Date: Mon, 3 Jun 2024 10:02:29 +0200 Subject: [PATCH] feat: show Linux distribution information on startup (#1729) --- lib/libimhex/include/hex/api/imhex_api.hpp | 10 ++++++++++ lib/libimhex/source/api/imhex_api.cpp | 21 +++++++++++++++++++++ main/gui/source/main.cpp | 5 +++++ 3 files changed, 36 insertions(+) diff --git a/lib/libimhex/include/hex/api/imhex_api.hpp b/lib/libimhex/include/hex/api/imhex_api.hpp index db22dbc16..6b89fd799 100644 --- a/lib/libimhex/include/hex/api/imhex_api.hpp +++ b/lib/libimhex/include/hex/api/imhex_api.hpp @@ -597,6 +597,16 @@ namespace hex { */ std::string getArchitecture(); + + struct LinuxDistro { + std::string name; + std::string version; + }; + /** + * @brief Gets information related to the Linux distribution, if running on Linux + */ + std::optional getLinuxDistro(); + /** * @brief Gets the current ImHex version * @return ImHex version diff --git a/lib/libimhex/source/api/imhex_api.cpp b/lib/libimhex/source/api/imhex_api.cpp index 45a2b3ab4..d12e82b89 100644 --- a/lib/libimhex/source/api/imhex_api.cpp +++ b/lib/libimhex/source/api/imhex_api.cpp @@ -15,6 +15,8 @@ #include #include #include +#include +#include #include #if defined(OS_WINDOWS) @@ -742,6 +744,25 @@ namespace hex { #endif } + std::optional getLinuxDistro() { + std::ifstream file("/etc/os-release"); + std::string name; + std::string version; + + std::string line; + while (std::getline(file, line)) { + if (line.find("PRETTY_NAME=") != std::string::npos) { + name = line.substr(line.find("=") + 1); + name.erase(std::remove(name.begin(), name.end(), '\"'), name.end()); + } else if (line.find("VERSION_ID=") != std::string::npos) { + version = line.substr(line.find("=") + 1); + version.erase(std::remove(version.begin(), version.end(), '\"'), version.end()); + } + } + + return {{name, version}}; + } + std::string getImHexVersion(bool withBuildType) { #if defined IMHEX_VERSION if (withBuildType) { diff --git a/main/gui/source/main.cpp b/main/gui/source/main.cpp index 77bab02bd..2f932fb3e 100644 --- a/main/gui/source/main.cpp +++ b/main/gui/source/main.cpp @@ -43,6 +43,11 @@ int main(int argc, char **argv) { log::info("Welcome to ImHex {}!", ImHexApi::System::getImHexVersion()); log::info("Compiled using commit {}@{}", ImHexApi::System::getCommitBranch(), ImHexApi::System::getCommitHash()); log::info("Running on {} {} ({})", ImHexApi::System::getOSName(), ImHexApi::System::getOSVersion(), ImHexApi::System::getArchitecture()); + #if defined(OS_LINUX) + auto distro = ImHexApi::System::getLinuxDistro().value(); + log::info("Linux distribution: {}. Version: {}", distro.name, distro.version == "" ? "None" : distro.version); + #endif + // Run ImHex return init::runImHex();