1
0
mirror of synced 2024-11-15 03:27:40 +01:00
ImHex/plugins/builtin/source/content/command_palette_commands.cpp
2023-12-01 14:07:10 +01:00

85 lines
3.4 KiB
C++

#include <hex/api/content_registry.hpp>
#include <hex/api/localization_manager.hpp>
#include <hex/api/shortcut_manager.hpp>
#include <hex/helpers/utils.hpp>
#include <hex/helpers/fmt.hpp>
#include <wolv/math_eval/math_evaluator.hpp>
#include <wolv/utils/string.hpp>
namespace hex::plugin::builtin {
void registerCommandPaletteCommands() {
ContentRegistry::CommandPaletteCommands::add(
ContentRegistry::CommandPaletteCommands::Type::SymbolCommand,
"#",
"hex.builtin.command.calc.desc",
[](auto input) {
wolv::math_eval::MathEvaluator<long double> evaluator;
evaluator.registerStandardVariables();
evaluator.registerStandardFunctions();
std::optional<long double> result = evaluator.evaluate(input);
if (result.has_value())
return hex::format("#{0} = {1}", input.data(), result.value());
else if (evaluator.hasError())
return hex::format("Error: {}", *evaluator.getLastError());
else
return std::string("???");
});
ContentRegistry::CommandPaletteCommands::add(
ContentRegistry::CommandPaletteCommands::Type::KeywordCommand,
"/web",
"hex.builtin.command.web.desc",
[](auto input) {
return hex::format("hex.builtin.command.web.result"_lang, input.data());
},
[](auto input) {
hex::openWebpage(input);
});
ContentRegistry::CommandPaletteCommands::add(
ContentRegistry::CommandPaletteCommands::Type::SymbolCommand,
"$",
"hex.builtin.command.cmd.desc",
[](auto input) {
return hex::format("hex.builtin.command.cmd.result"_lang, input.data());
},
[](auto input) {
hex::executeCommand(input);
});
ContentRegistry::CommandPaletteCommands::addHandler(
ContentRegistry::CommandPaletteCommands::Type::SymbolCommand,
">",
[](const auto &input) {
std::vector<ContentRegistry::CommandPaletteCommands::impl::QueryResult> result;
for (const auto &[priority, entry] : ContentRegistry::Interface::impl::getMenuItems()) {
if (!entry.enabledCallback())
continue;
std::vector<std::string> names;
std::transform(entry.unlocalizedNames.begin(), entry.unlocalizedNames.end(), std::back_inserter(names), [](auto &name) { return Lang(name); });
if (auto combined = wolv::util::combineStrings(names, " -> "); hex::containsIgnoreCase(combined, input) && !combined.contains(ContentRegistry::Interface::impl::SeparatorValue) && !combined.contains(ContentRegistry::Interface::impl::SubMenuValue)) {
result.emplace_back(ContentRegistry::CommandPaletteCommands::impl::QueryResult {
std::move(combined),
[&entry](const auto&) { entry.callback(); }
});
}
}
return result;
},
[](auto input) {
return hex::format("Menu Item: {}", input.data());
});
}
}