feat: Added command line interface support (#1172)

System design has been discussed on discord

Should fix #948

---------

Co-authored-by: WerWolv <werwolv98@gmail.com>
This commit is contained in:
iTrooz
2023-07-13 14:08:23 +02:00
committed by GitHub
parent 8c0395bc7c
commit 1ed658bcdc
21 changed files with 636 additions and 143 deletions

View File

@@ -0,0 +1,21 @@
ImHex Hex Editor
A Hex Editor for Reverse Engineers, Programmers and people
who value their retinas when working at 3 AM.
                                               
   ((((((((((((((((((((/   (((((((((((((((/  
   ((((((((((((((((((((/   (((((((((((((((/  
   *,,,,,        ,,,,,,*             ,,,,,*   Version: {0}
   ((((//        (((((//             (((((/   Commit: {1}@{2}
   (/////        (//////             ((((//   Build time: {3}, {4}
   /////*        //////*             //////   Type: {5}
   /////*        //////*             /////*  
   /*****        /////**             /////*  
   ******        *******             /*****  
   *****,        ******,             ******  
   **,,,,        *****,,             *****,  
   *,,,,,        *,,,,,,             ****,,  
   ,,,,,,        ,,,,,,,             *,,,,,  
   ////////////////////*             ,,,,,,,  
   *********************             ,.....   
                                               


View File

@@ -1,11 +1,15 @@
#include <hex/plugin.hpp>
#include <hex/subcommands/subcommands.hpp>
#include <hex/api/event.hpp>
#include <hex/api/content_registry.hpp>
#include <hex/helpers/logger.hpp>
#include <romfs/romfs.hpp>
#include <nlohmann/json.hpp>
using namespace hex;
namespace hex::plugin::builtin {
void registerEventHandlers();
@@ -41,8 +45,69 @@ namespace hex::plugin::builtin {
void handleBorderlessWindowMode();
void handleSubCommands();
}
IMHEX_PLUGIN_SUBCOMMANDS() {
{ "version", "Print ImHex version and exit", [](const std::vector<std::string>&) {
hex::print(romfs::get("logo.ans").string(),
ImHexApi::System::getImHexVersion(),
ImHexApi::System::getCommitBranch(), ImHexApi::System::getCommitHash(),
__DATE__, __TIME__,
ImHexApi::System::isPortableVersion() ? "Portable" : "Installed");
exit(EXIT_SUCCESS);
}},
{ "help", "Print help about this command and exit", [](const std::vector<std::string>&) {
hex::print(
"ImHex - A Hex Editor for Reverse Engineers, Programmers and people who value their retinas when working at 3 AM.\n"
"\n"
"usage: imhex [subcommand] [options]\n"
"Available subcommands:\n"
);
size_t longestCommand = 0;
for (const auto &plugin : PluginManager::getPlugins()) {
for (const auto &subCommand : plugin.getSubCommands()) {
longestCommand = std::max(longestCommand, subCommand.commandKey.size());
}
}
for (const auto &plugin : PluginManager::getPlugins()) {
for (const auto &subCommand : plugin.getSubCommands()) {
hex::print(" --{}{: <{}} {}\n", subCommand.commandKey, "", longestCommand - subCommand.commandKey.size(), subCommand.commandDesc);
}
}
exit(EXIT_SUCCESS);
}},
{ "open", "Open files passed as argument. This is the default subcommand is none is entered",
[](const std::vector<std::string> &args) {
hex::subcommands::registerSubCommand("open", [](const std::vector<std::string> &args){
for (auto &arg : args) {
EventManager::post<RequestOpenFile>(arg);
}
});
std::vector<std::string> fullPaths;
bool doubleDashFound = false;
for (auto &arg : args) {
// Skip the first argument named `--`
if (arg == "--" && !doubleDashFound) {
doubleDashFound = true;
} else {
auto path = std::filesystem::weakly_canonical(arg);
fullPaths.push_back(wolv::util::toUTF8String(path));
}
}
hex::subcommands::forwardSubCommand("open", fullPaths);
}}
};
IMHEX_PLUGIN_SETUP("Built-in", "WerWolv", "Default ImHex functionality") {
using namespace hex::plugin::builtin;