Skip to content

Commit ec2b309

Browse files
committed
Move keybinding command handling into Hotkey module
1 parent 1554b55 commit ec2b309

File tree

3 files changed

+47
-47
lines changed

3 files changed

+47
-47
lines changed

library/Core.cpp

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -957,53 +957,7 @@ command_result Core::runCommand(color_ostream &con, const std::string &first_, s
957957
}
958958
else if (first == "keybinding")
959959
{
960-
if (parts.size() >= 3 && (parts[0] == "set" || parts[0] == "add"))
961-
{
962-
std::string keystr = parts[1];
963-
if (parts[0] == "set")
964-
hotkey_mgr->clearKeybind(keystr);
965-
// for (int i = parts.size()-1; i >= 2; i--)
966-
for (const auto& part : parts | std::views::drop(2) | std::views::reverse)
967-
{
968-
if (!hotkey_mgr->addKeybind(keystr, part)) {
969-
con.printerr("Invalid key spec: %s\n", keystr.c_str());
970-
break;
971-
}
972-
}
973-
}
974-
else if (parts.size() >= 2 && parts[0] == "clear")
975-
{
976-
// for (size_t i = 1; i < parts.size(); i++)
977-
for (const auto& part : parts | std::views::drop(1))
978-
{
979-
if (!hotkey_mgr->clearKeybind(part)) {
980-
con.printerr("Invalid key spec: %s\n", part.c_str());
981-
break;
982-
}
983-
}
984-
}
985-
else if (parts.size() == 2 && parts[0] == "list")
986-
{
987-
std::vector<std::string> list = hotkey_mgr->listKeybinds(parts[1]);
988-
if (list.empty())
989-
con << "No bindings." << std::endl;
990-
for (const auto& kb : list)
991-
con << " " << kb << std::endl;
992-
}
993-
else
994-
{
995-
con << "Usage:" << std::endl
996-
<< " keybinding list <key>" << std::endl
997-
<< " keybinding clear <key>[@context]..." << std::endl
998-
<< " keybinding set <key>[@context] \"cmdline\" \"cmdline\"..." << std::endl
999-
<< " keybinding add <key>[@context] \"cmdline\" \"cmdline\"..." << std::endl
1000-
<< "Later adds, and earlier items within one command have priority." << std::endl
1001-
<< "Supported keys: [Ctrl-][Alt-][Shift-](A-Z, 0-9, F1-F12, `, or Enter)." << std::endl
1002-
<< "Context may be used to limit the scope of the binding, by" << std::endl
1003-
<< "requiring the current context to have a certain prefix." << std::endl
1004-
<< "Current UI context is: " << std::endl
1005-
<< join_strings("\n", Gui::getCurFocus(true)) << std::endl;
1006-
}
960+
this->hotkey_mgr->handleKeybindingCommand(con, parts);
1007961
}
1008962
else if (first == "alias")
1009963
{

library/include/modules/Hotkey.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include "Export.h"
4+
#include "ColorText.h"
45

56
#include <condition_variable>
67
#include <map>
@@ -10,6 +11,7 @@
1011

1112
namespace DFHack {
1213
class DFHACK_EXPORT HotkeyManager {
14+
friend class Core;
1315
public:
1416
HotkeyManager();
1517
~HotkeyManager();
@@ -53,5 +55,6 @@ namespace DFHack {
5355
std::map<int, std::vector<KeyBinding>> bindings;
5456

5557
void hotkey_thread_fn();
58+
void handleKeybindingCommand(color_ostream& out, const std::vector<std::string>& parts);
5659
};
5760
}

library/modules/Hotkey.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,49 @@ void HotkeyManager::setHotkeyCommand(std::string cmd) {
284284
cond.notify_all();
285285
}
286286

287+
288+
void HotkeyManager::handleKeybindingCommand(color_ostream &con, const std::vector<std::string>& parts) {
289+
if (parts.size() >= 3 && (parts[0] == "set" || parts[0] == "add")) {
290+
std::string keystr = parts[1];
291+
if (parts[0] == "set")
292+
clearKeybind(keystr);
293+
for (const auto& part : parts | std::views::drop(2) | std::views::reverse) {
294+
if (!addKeybind(keystr, part)) {
295+
con.printerr("Invalid key spec: %s\n", keystr.c_str());
296+
break;
297+
}
298+
}
299+
}
300+
else if (parts.size() >= 2 && parts[0] == "clear") {
301+
for (const auto& part : parts | std::views::drop(1)) {
302+
if (!clearKeybind(part)) {
303+
con.printerr("Invalid key spec: %s\n", part.c_str());
304+
break;
305+
}
306+
}
307+
}
308+
else if (parts.size() == 2 && parts[0] == "list") {
309+
std::vector<std::string> list = listKeybinds(parts[1]);
310+
if (list.empty())
311+
con << "No bindings." << std::endl;
312+
for (const auto& kb : list)
313+
con << " " << kb << std::endl;
314+
}
315+
else {
316+
con << "Usage:" << std::endl
317+
<< " keybinding list <key>" << std::endl
318+
<< " keybinding clear <key>[@context]..." << std::endl
319+
<< " keybinding set <key>[@context] \"cmdline\" \"cmdline\"..." << std::endl
320+
<< " keybinding add <key>[@context] \"cmdline\" \"cmdline\"..." << std::endl
321+
<< "Later adds, and earlier items within one command have priority." << std::endl
322+
<< "Supported keys: [Ctrl-][Alt-][Shift-](A-Z, 0-9, F1-F12, `, or Enter)." << std::endl
323+
<< "Context may be used to limit the scope of the binding, by" << std::endl
324+
<< "requiring the current context to have a certain prefix." << std::endl
325+
<< "Current UI context is: " << std::endl
326+
<< join_strings("\n", Gui::getCurFocus(true)) << std::endl;
327+
}
328+
}
329+
287330
HotkeyManager::HotkeyManager() {
288331
this->hotkey_thread = std::thread(&HotkeyManager::hotkey_thread_fn, this);
289332
}

0 commit comments

Comments
 (0)