Skip to content

Commit 0e341b7

Browse files
committed
Relocate keybinding command handling
1 parent 32bd846 commit 0e341b7

3 files changed

Lines changed: 43 additions & 95 deletions

File tree

library/Commands.cpp

Lines changed: 43 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "RemoteTools.h"
1010

1111
#include "modules/Gui.h"
12+
#include "modules/Hotkey.h"
1213
#include "modules/World.h"
1314

1415
#include "df/viewscreen_new_regionst.h"
@@ -282,56 +283,62 @@ namespace DFHack
282283

283284
command_result Commands::keybinding(color_ostream& con, Core& core, const std::string& first, const std::vector<std::string>& parts)
284285
{
285-
if (parts.size() >= 3 && (parts[0] == "set" || parts[0] == "add"))
286-
{
287-
std::string keystr = parts[1];
286+
using Hotkey::KeySpec;
287+
auto hotkey_mgr = core.getHotkeyManager();
288+
std::string parse_error;
289+
if (parts.size() >= 3 && (parts[0] == "set" || parts[0] == "add")) {
290+
const std::string& keystr = parts[1];
288291
if (parts[0] == "set")
289-
core.ClearKeyBindings(keystr);
290-
// for (int i = parts.size()-1; i >= 2; i--)
291-
for (const auto& part : parts | std::views::drop(2) | std::views::reverse)
292-
{
293-
if (!core.AddKeyBinding(keystr, part))
294-
{
295-
con.printerr("Invalid key spec: %s\n", keystr.c_str());
296-
return CR_FAILURE;
292+
hotkey_mgr->removeKeybind(keystr);
293+
for (const auto& part : parts | std::views::drop(2) | std::views::reverse) {
294+
auto spec = KeySpec::parse(keystr, &parse_error);
295+
if (!spec.has_value()) {
296+
con.printerr("%s\n", parse_error.c_str());
297+
break;
298+
}
299+
if (!hotkey_mgr->addKeybind(spec.value(), part)) {
300+
con.printerr("Invalid command: '%s'\n", part.c_str());
301+
break;
297302
}
298303
}
299304
}
300-
else if (parts.size() >= 2 && parts[0] == "clear")
301-
{
302-
// for (size_t i = 1; i < parts.size(); i++)
303-
for (const auto& part : parts | std::views::drop(1))
304-
{
305-
if (!core.ClearKeyBindings(part))
306-
{
307-
con.printerr("Invalid key spec: %s\n", part.c_str());
308-
return CR_FAILURE;
305+
else if (parts.size() >= 2 && parts[0] == "clear") {
306+
for (const auto& part : parts | std::views::drop(1)) {
307+
auto spec = KeySpec::parse(part, &parse_error);
308+
if (!spec.has_value()) {
309+
con.printerr("%s\n", parse_error.c_str());
310+
}
311+
if (!hotkey_mgr->removeKeybind(spec.value())) {
312+
con.printerr("No matching keybinds to remove\n");
313+
break;
309314
}
310315
}
311316
}
312-
else if (parts.size() == 2 && parts[0] == "list")
313-
{
314-
std::vector<std::string> list = core.ListKeyBindings(parts[1]);
317+
else if (parts.size() == 2 && parts[0] == "list") {
318+
auto spec = KeySpec::parse(parts[1], &parse_error);
319+
if (!spec.has_value()) {
320+
con.printerr("%s\n", parse_error.c_str());
321+
return CR_FAILURE;
322+
}
323+
std::vector<std::string> list = hotkey_mgr->listKeybinds(spec.value());
315324
if (list.empty())
316325
con << "No bindings." << std::endl;
317326
for (const auto& kb : list)
318327
con << " " << kb << std::endl;
319328
}
320-
else
321-
{
322-
con << "Usage:" << std::endl
323-
<< " keybinding list <key>" << std::endl
324-
<< " keybinding clear <key>[@context]..." << std::endl
325-
<< " keybinding set <key>[@context] \"cmdline\" \"cmdline\"..." << std::endl
326-
<< " keybinding add <key>[@context] \"cmdline\" \"cmdline\"..." << std::endl
327-
<< "Later adds, and earlier items within one command have priority." << std::endl
328-
<< "Supported keys: [Ctrl-][Alt-][Shift-](A-Z, 0-9, F1-F12, `, or Enter)." << std::endl
329-
<< "Context may be used to limit the scope of the binding, by" << std::endl
330-
<< "requiring the current context to have a certain prefix." << std::endl
331-
<< "Current UI context is: " << std::endl
329+
else {
330+
con << "Usage:\n"
331+
<< " keybinding list <key>\n"
332+
<< " keybinding clear <key>[@context]...\n"
333+
<< " keybinding set <key>[@context] \"cmdline\" \"cmdline\"...\n"
334+
<< " keybinding add <key>[@context] \"cmdline\" \"cmdline\"...\n"
335+
<< "Later adds, and earlier items within one command have priority.\n"
336+
<< "Key format: [Ctrl-][Alt-][Super-][Shift-](A-Z, 0-9, F1-F12, `, etc.).\n"
337+
<< "Context may be used to limit the scope of the binding, by\n"
338+
<< "requiring the current context to have a certain prefix.\n"
339+
<< "Current UI context is: \n"
332340
<< join_strings("\n", Gui::getCurFocus(true)) << std::endl;
333341
}
334-
335342
return CR_OK;
336343
}
337344

library/include/modules/Hotkey.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ namespace DFHack {
3333
};
3434
}
3535
class DFHACK_EXPORT HotkeyManager {
36-
friend class Core;
3736
public:
3837
HotkeyManager();
3938
~HotkeyManager();
@@ -74,6 +73,5 @@ namespace DFHack {
7473
std::map<int, std::vector<Hotkey::KeyBinding>> bindings;
7574

7675
void hotkey_thread_fn();
77-
void handleKeybindingCommand(color_ostream& out, const std::vector<std::string>& parts);
7876
};
7977
}

library/modules/Hotkey.cpp

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -387,63 +387,6 @@ std::string HotkeyManager::getKeybindingInput() {
387387
return requested_keybind;
388388
}
389389

390-
void HotkeyManager::handleKeybindingCommand(color_ostream &con, const std::vector<std::string>& parts) {
391-
std::string parse_error;
392-
if (parts.size() >= 3 && (parts[0] == "set" || parts[0] == "add")) {
393-
const std::string& keystr = parts[1];
394-
if (parts[0] == "set")
395-
removeKeybind(keystr);
396-
for (const auto& part : parts | std::views::drop(2) | std::views::reverse) {
397-
auto spec = KeySpec::parse(keystr, &parse_error);
398-
if (!spec.has_value()) {
399-
con.printerr("%s\n", parse_error.c_str());
400-
break;
401-
}
402-
if (!addKeybind(spec.value(), part)) {
403-
con.printerr("Invalid command: '%s'\n", part.c_str());
404-
break;
405-
}
406-
}
407-
}
408-
else if (parts.size() >= 2 && parts[0] == "clear") {
409-
for (const auto& part : parts | std::views::drop(1)) {
410-
auto spec = KeySpec::parse(part, &parse_error);
411-
if (!spec.has_value()) {
412-
con.printerr("%s\n", parse_error.c_str());
413-
}
414-
if (!removeKeybind(spec.value())) {
415-
con.printerr("No matching keybinds to remove\n");
416-
break;
417-
}
418-
}
419-
}
420-
else if (parts.size() == 2 && parts[0] == "list") {
421-
auto spec = KeySpec::parse(parts[1], &parse_error);
422-
if (!spec.has_value()) {
423-
con.printerr("%s\n", parse_error.c_str());
424-
return;
425-
}
426-
std::vector<std::string> list = listKeybinds(spec.value());
427-
if (list.empty())
428-
con << "No bindings.\n";
429-
for (const auto& kb : list)
430-
con << " " << kb << "\n";
431-
}
432-
else {
433-
con << "Usage:\n"
434-
<< " keybinding list <key>\n"
435-
<< " keybinding clear <key>[@context]...\n"
436-
<< " keybinding set <key>[@context] \"cmdline\" \"cmdline\"...\n"
437-
<< " keybinding add <key>[@context] \"cmdline\" \"cmdline\"...\n"
438-
<< "Later adds, and earlier items within one command have priority.\n"
439-
<< "Key format: [Ctrl-][Alt-][Super-][Shift-](A-Z, 0-9, F1-F12, `, etc.).\n"
440-
<< "Context may be used to limit the scope of the binding, by\n"
441-
<< "requiring the current context to have a certain prefix.\n"
442-
<< "Current UI context is: \n"
443-
<< join_strings("\n", Gui::getCurFocus(true)) << "\n";
444-
}
445-
}
446-
447390
HotkeyManager::HotkeyManager() {
448391
this->hotkey_thread = std::thread(&HotkeyManager::hotkey_thread_fn, this);
449392
}

0 commit comments

Comments
 (0)