1
0
mirror of synced 2024-11-25 00:00:27 +01:00

tools: Added wikipedia term definition search

This commit is contained in:
WerWolv 2021-08-28 16:02:53 +02:00
parent e9c349fc55
commit c1751f39ae
14 changed files with 121 additions and 12 deletions

View File

@ -18,6 +18,8 @@ enum ImGuiCustomCol {
ImGuiCustomCol_ToolbarPurple,
ImGuiCustomCol_ToolbarBrown,
ImGuiCustomCol_Highlight,
ImGuiCustomCol_COUNT
};
@ -54,6 +56,7 @@ namespace ImGui {
void TextSpinner(const char* label);
void Header(const char *label, bool firstEntry = false);
void HeaderColored(const char *label, ImColor color, bool firstEntry);
void InfoTooltip(const char *text);

View File

@ -203,6 +203,13 @@ namespace ImGui {
ImGui::Separator();
}
void HeaderColored(const char *label, ImColor color, bool firstEntry) {
if (!firstEntry)
ImGui::NewLine();
ImGui::TextColored(color, "%s", label);
ImGui::Separator();
}
void InfoTooltip(const char *text) {
static double lastMoveTime;
static ImGuiID lastHoveredID;
@ -250,6 +257,8 @@ namespace ImGui {
colors[ImGuiCustomCol_ToolbarBlue] = ImColor(6, 83, 155);
colors[ImGuiCustomCol_ToolbarPurple] = ImColor(103, 42, 120);
colors[ImGuiCustomCol_ToolbarBrown] = ImColor(219, 179, 119);
colors[ImGuiCustomCol_Highlight] = ImColor(77, 198, 155);
}
void StyleCustomColorsLight() {
@ -266,6 +275,8 @@ namespace ImGui {
colors[ImGuiCustomCol_ToolbarBlue] = ImColor(6, 83, 155);
colors[ImGuiCustomCol_ToolbarPurple] = ImColor(103, 42, 120);
colors[ImGuiCustomCol_ToolbarBrown] = ImColor(219, 179, 119);
colors[ImGuiCustomCol_Highlight] = ImColor(41, 151, 112);
}
void StyleCustomColorsClassic() {
@ -282,6 +293,8 @@ namespace ImGui {
colors[ImGuiCustomCol_ToolbarBlue] = ImColor(6, 83, 155);
colors[ImGuiCustomCol_ToolbarPurple] = ImColor(103, 42, 120);
colors[ImGuiCustomCol_ToolbarBrown] = ImColor(219, 179, 119);
colors[ImGuiCustomCol_Highlight] = ImColor(77, 198, 155);
}
Texture LoadImageFromPath(const char *path) {

View File

@ -12,6 +12,7 @@ namespace hex::plugin::builtin {
namespace {
using namespace std::literals::string_literals;
using namespace std::literals::chrono_literals;
void drawDemangler() {
@ -496,6 +497,70 @@ namespace hex::plugin::builtin {
}
}
void drawWikiExplainer() {
static hex::Net net;
static std::string searchString(0xFFF, 0x00);
static std::string resultTitle, resultExtract;
static std::future<Response<std::string>> searchProcess;
static bool extendedSearch = false;
constexpr static auto WikipediaApiUrl = "https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&explaintext&redirects=10&formatversion=2";
ImGui::Header("hex.builtin.tools.file_uploader.control"_lang, true);
ImGui::BeginDisabled(searchProcess.valid() && searchProcess.wait_for(0s) != std::future_status::ready);
bool startSearch = ImGui::InputText("##search", searchString.data(), searchString.capacity(), ImGuiInputTextFlags_EnterReturnsTrue);
ImGui::SameLine();
startSearch = ImGui::Button("hex.builtin.tools.file_uploader.search"_lang) || startSearch;
ImGui::EndDisabled();
if (startSearch) {
searchProcess = net.getString(WikipediaApiUrl + "&exintro"s + "&titles="s + net.encode(searchString));
}
ImGui::Header("hex.builtin.tools.file_uploader.results"_lang);
if (ImGui::BeginChild("##summary", ImVec2(0, 300), true)) {
if (!resultTitle.empty() && !resultExtract.empty()) {
ImGui::HeaderColored(resultTitle.c_str(), ImGui::GetCustomColorVec4(ImGuiCustomCol_Highlight),true);
ImGui::TextWrapped("%s", resultExtract.c_str());
}
}
ImGui::EndChild();
if (searchProcess.valid() && searchProcess.wait_for(0s) == std::future_status::ready) {
try {
auto response = searchProcess.get();
if (response.code != 200) throw std::runtime_error("Invalid response");
auto json = nlohmann::json::parse(response.body);
resultTitle = json["query"]["pages"][0]["title"];
resultExtract = json["query"]["pages"][0]["extract"];
if (!extendedSearch && resultExtract.ends_with(':')) {
extendedSearch = true;
searchProcess = net.getString(WikipediaApiUrl + "&titles="s + net.encode(searchString));
resultTitle.clear();
resultExtract.clear();
} else {
extendedSearch = false;
searchString.clear();
}
} catch (...) {
resultTitle.clear();
resultExtract.clear();
extendedSearch = false;
searchProcess = { };
resultTitle = "???";
resultExtract = "hex.builtin.tools.file_uploader.invalid_response"_lang.get();
}
}
}
void registerToolEntries() {
ContentRegistry::Tools::add("hex.builtin.tools.demangler", drawDemangler);
ContentRegistry::Tools::add("hex.builtin.tools.ascii_table", drawASCIITable);
@ -505,6 +570,7 @@ namespace hex::plugin::builtin {
ContentRegistry::Tools::add("hex.builtin.tools.base_converter", drawBaseConverter);
ContentRegistry::Tools::add("hex.builtin.tools.permissions", drawPermissionsCalculator);
ContentRegistry::Tools::add("hex.builtin.tools.file_uploader", drawFileUploader);
ContentRegistry::Tools::add("hex.builtin.tools.wiki_explain", drawWikiExplainer);
}
}

View File

@ -546,8 +546,13 @@ namespace hex::plugin::builtin {
{ "hex.builtin.tools.file_uploader.done", "Fertig!" },
{ "hex.builtin.tools.file_uploader.recent", "Letzte Uploads" },
{ "hex.builtin.tools.file_uploader.tooltip", "Klicken zum Kopieren\nCTRL + Klicken zum öffnen" },
{ "hex.builtin.tools.file_uploader.invalid_response", "Ungültige Antwort von Anonfiles!"_lang },
{ "hex.builtin.tools.file_uploader.error", "Dateiupload fehlgeschlagen\n\nError Code: {0}"_lang },
{ "hex.builtin.tools.file_uploader.invalid_response", "Ungültige Antwort von Anonfiles!" },
{ "hex.builtin.tools.file_uploader.error", "Dateiupload fehlgeschlagen\n\nError Code: {0}" },
{ "hex.builtin.tools.wiki_explain", "Wikipedia Definition" },
{ "hex.builtin.tools.file_uploader.control", "Einstellungen" },
{ "hex.builtin.tools.file_uploader.search", "Suchen" },
{ "hex.builtin.tools.file_uploader.results", "Resultate" },
{ "hex.builtin.tools.file_uploader.invalid_response", "Ungültige Antwort von Wikipedia!" },
{ "hex.builtin.setting.imhex", "ImHex" },
{ "hex.builtin.setting.imhex.recent_files", "Kürzlich geöffnete Dateien" },

View File

@ -548,7 +548,11 @@ namespace hex::plugin::builtin {
{ "hex.builtin.tools.file_uploader.tooltip", "Click to copy\nCTRL + Click to open" },
{ "hex.builtin.tools.file_uploader.invalid_response", "Invalid response from Anonfiles!" },
{ "hex.builtin.tools.file_uploader.error", "Failed to upload file!\n\nError Code: {0}" },
{ "hex.builtin.tools.wiki_explain", "Wikipedia term definitions" },
{ "hex.builtin.tools.file_uploader.control", "Control" },
{ "hex.builtin.tools.file_uploader.search", "Search" },
{ "hex.builtin.tools.file_uploader.results", "Results" },
{ "hex.builtin.tools.file_uploader.invalid_response", "Invalid response from Wikipedia!" },
{ "hex.builtin.setting.imhex", "ImHex" },
{ "hex.builtin.setting.imhex.recent_files", "Recent Files" },

View File

@ -547,6 +547,11 @@ namespace hex::plugin::builtin {
//{ "hex.builtin.tools.file_uploader.tooltip", "Click to copy\nCTRL + Click to open" },
//{ "hex.builtin.tools.file_uploader.invalid_response", "Invalid response from Anonfiles!" },
//{ "hex.builtin.tools.file_uploader.error", "Failed to upload file!\n\nError Code: {0}" },
//{ "hex.builtin.tools.wiki_explain", "Wikipedia term definitions" },
//{ "hex.builtin.tools.file_uploader.control", "Control" },
//{ "hex.builtin.tools.file_uploader.search", "Search" },
//{ "hex.builtin.tools.file_uploader.results", "Results" },
//{ "hex.builtin.tools.file_uploader.invalid_response", "Invalid response from Wikipedia!" },
{ "hex.builtin.setting.imhex", "ImHex" },
{ "hex.builtin.setting.imhex.recent_files", "File recenti" },

View File

@ -28,6 +28,20 @@ namespace hex {
std::future<Response<std::string>> uploadFile(const std::string &url, const std::filesystem::path &filePath);
[[nodiscard]]
std::string encode(const std::string &input) {
auto escapedString = curl_easy_escape(this->m_ctx, input.c_str(), std::strlen(input.c_str()));
if (escapedString != nullptr) {
std::string output = escapedString;
curl_free(escapedString);
return output;
}
return { };
}
[[nodiscard]]
float getProgress() const { return this->m_progress; }

View File

@ -115,9 +115,8 @@ namespace hex {
ImGui::NewLine();
}
}
ImGui::EndChild();
}
ImGui::EndChild();
if (ImGui::Button("hex.view.bookmarks.button.jump"_lang))
EventManager::post<RequestSelectionChange>(region);
@ -168,8 +167,8 @@ namespace hex {
ProjectFile::markDirty();
}
ImGui::EndChild();
}
ImGui::EndChild();
}
ImGui::End();
}

View File

@ -244,8 +244,8 @@ namespace hex {
break;
}
ImGui::EndChild();
}
ImGui::EndChild();
ImGui::Disabled([this] {
if (ImGui::Button("hex.view.disassembler.disassemble"_lang))

View File

@ -223,8 +223,8 @@ namespace hex {
this->m_shouldInvalidate = false;
}
ImGui::EndChild();
}
ImGui::EndChild();
}
ImGui::End();
}

View File

@ -40,8 +40,8 @@ namespace hex {
ImGui::PushStyleColor(ImGuiCol_ChildBg, ImVec4(0.2F, 0.2F, 0.2F, 0.3F));
if (ImGui::BeginChild(id.c_str(), ImVec2(-1, ImGui::CalcTextSize(code.c_str()).y))) {
ImGui::Text("%s", code.c_str());
ImGui::EndChild();
}
ImGui::EndChild();
ImGui::NewLine();
ImGui::PopStyleColor();

View File

@ -241,8 +241,8 @@ namespace hex {
}
}
ImGui::EndChild();
}
ImGui::EndChild();
}
ImGui::End();
}

View File

@ -258,8 +258,8 @@ namespace hex {
ImGui::PopStyleColor();
}
ImGui::EndChild();
}
ImGui::EndChild();
ImGui::PopStyleColor(1);
ImGui::Disabled([this]{

View File

@ -190,8 +190,8 @@ namespace hex {
if (ImGui::Button("hex.view.strings.demangle.copy"_lang))
ImGui::SetClipboardText(this->m_demangledName.c_str());
ImGui::EndChild();
}
ImGui::EndChild();
ImGui::EndPopup();
}
}