Skip to content

Commit f0e98b5

Browse files
committed
Address tidy checks
1 parent b89d747 commit f0e98b5

2 files changed

Lines changed: 30 additions & 30 deletions

File tree

library/include/modules/Hotkey.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ namespace DFHack {
3939
~HotkeyManager();
4040

4141

42-
bool addKeybind(std::string keyspec, std::string cmd);
43-
bool addKeybind(Hotkey::KeySpec spec, std::string cmd);
42+
bool addKeybind(std::string keyspec, std::string_view cmd);
43+
bool addKeybind(Hotkey::KeySpec spec, std::string_view cmd);
4444
// Clear a keybind with the given keyspec, optionally for any focus, or with a specific command
4545
bool removeKeybind(std::string keyspec, bool match_focus=true, std::string_view cmdline="");
4646
bool removeKeybind(const Hotkey::KeySpec& spec, bool match_focus=true, std::string_view cmdline="");
@@ -62,13 +62,13 @@ namespace DFHack {
6262

6363
private:
6464
std::thread hotkey_thread;
65-
std::mutex lock {};
66-
std::condition_variable cond {};
65+
std::mutex lock;
66+
std::condition_variable cond;
6767

6868
bool keybind_save_requested = false;
6969
std::string requested_keybind;
7070

71-
int hotkey_sig = 0;
71+
uint8_t hotkey_sig = 0;
7272
std::string queued_command;
7373

7474
std::map<int, std::vector<Hotkey::KeyBinding>> bindings;

library/modules/Hotkey.cpp

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,20 @@ using namespace DFHack;
2020
using Hotkey::KeySpec;
2121
using Hotkey::KeyBinding;
2222

23-
enum HotkeySignal {
23+
enum HotkeySignal : uint8_t {
2424
None = 0,
2525
CmdReady,
2626
Shutdown,
2727
};
2828

29-
bool operator==(const KeySpec& a, const KeySpec& b) {
29+
static bool operator==(const KeySpec& a, const KeySpec& b) {
3030
return a.modifiers == b.modifiers && a.sym == b.sym &&
3131
a.focus.size() == b.focus.size() &&
3232
std::equal(a.focus.begin(), a.focus.end(), b.focus.begin());
3333
}
3434

3535
// Equality operator for key bindings
36-
bool operator==(const KeyBinding& a, const KeyBinding& b) {
36+
static bool operator==(const KeyBinding& a, const KeyBinding& b) {
3737
return a.spec == b.spec &&
3838
a.command == b.command &&
3939
a.cmdline == b.cmdline;
@@ -134,7 +134,7 @@ bool KeySpec::isDisruptive() const {
134134
// Letters A-Z, 0-9, and other special keys such as return/escape, and other general typing keys
135135
bool is_essential_key = (this->sym >= SDLK_a && this->sym <= SDLK_z) // A-Z
136136
|| (this->sym >= SDLK_0 && this->sym <= SDLK_9) // 0-9
137-
|| essential_key_set.find(this->sym) != std::string::npos
137+
|| (this->sym < CHAR_MAX && essential_key_set.find((char)this->sym) != std::string::npos)
138138
|| (this->sym >= SDLK_LEFT && this->sym <= SDLK_UP); // Arrow keys
139139

140140
// Essential keys are safe, so long as they have a modifier that isn't Shift
@@ -173,13 +173,13 @@ void HotkeyManager::hotkey_thread_fn() {
173173
}
174174

175175

176-
bool HotkeyManager::addKeybind(KeySpec spec, std::string cmd) {
176+
bool HotkeyManager::addKeybind(KeySpec spec, std::string_view cmd) {
177177
// No point in a hotkey with no action
178178
if (cmd.empty())
179179
return false;
180180

181181
KeyBinding binding;
182-
binding.spec = spec;
182+
binding.spec = std::move(spec);
183183
binding.cmdline = cmd;
184184
size_t space_idx = cmd.find(' ');
185185
binding.command = space_idx == std::string::npos ? cmd : cmd.substr(0, space_idx);
@@ -196,8 +196,8 @@ bool HotkeyManager::addKeybind(KeySpec spec, std::string cmd) {
196196
return true;
197197
}
198198

199-
bool HotkeyManager::addKeybind(std::string keyspec, std::string cmd) {
200-
std::optional<KeySpec> spec_opt = KeySpec::parse(keyspec);
199+
bool HotkeyManager::addKeybind(std::string keyspec, std::string_view cmd) {
200+
std::optional<KeySpec> spec_opt = KeySpec::parse(std::move(keyspec));
201201
if (!spec_opt.has_value())
202202
return false;
203203
return this->addKeybind(spec_opt.value(), cmd);
@@ -223,7 +223,7 @@ bool HotkeyManager::removeKeybind(const KeySpec& spec, bool match_focus, std::st
223223
}
224224

225225
bool HotkeyManager::removeKeybind(std::string keyspec, bool match_focus, std::string_view cmdline) {
226-
std::optional<KeySpec> spec_opt = KeySpec::parse(keyspec);
226+
std::optional<KeySpec> spec_opt = KeySpec::parse(std::move(keyspec));
227227
if (!spec_opt.has_value())
228228
return false;
229229
return this->removeKeybind(spec_opt.value(), match_focus, cmdline);
@@ -261,7 +261,7 @@ std::vector<std::string> HotkeyManager::listKeybinds(const KeySpec& spec) {
261261

262262
std::vector<std::string> HotkeyManager::listKeybinds(std::string keyspec) {
263263
std::lock_guard<std::mutex> l(lock);
264-
std::optional<KeySpec> spec_opt = KeySpec::parse(keyspec);
264+
std::optional<KeySpec> spec_opt = KeySpec::parse(std::move(keyspec));
265265
if (!spec_opt.has_value())
266266
return {};
267267
return this->listKeybinds(spec_opt.value());
@@ -371,7 +371,7 @@ bool HotkeyManager::handleKeybind(int sym, int modifiers) {
371371

372372
void HotkeyManager::setHotkeyCommand(std::string cmd) {
373373
std::unique_lock<std::mutex> l(lock);
374-
queued_command = cmd;
374+
queued_command = std::move(cmd);
375375
hotkey_sig = HotkeySignal::CmdReady;
376376
l.unlock();
377377
cond.notify_all();
@@ -391,7 +391,7 @@ std::string HotkeyManager::getKeybindingInput() {
391391
void HotkeyManager::handleKeybindingCommand(color_ostream &con, const std::vector<std::string>& parts) {
392392
std::string parse_error;
393393
if (parts.size() >= 3 && (parts[0] == "set" || parts[0] == "add")) {
394-
std::string keystr = parts[1];
394+
const std::string& keystr = parts[1];
395395
if (parts[0] == "set")
396396
removeKeybind(keystr);
397397
for (const auto& part : parts | std::views::drop(2) | std::views::reverse) {
@@ -426,22 +426,22 @@ void HotkeyManager::handleKeybindingCommand(color_ostream &con, const std::vecto
426426
}
427427
std::vector<std::string> list = listKeybinds(spec.value());
428428
if (list.empty())
429-
con << "No bindings." << std::endl;
429+
con << "No bindings.\n";
430430
for (const auto& kb : list)
431-
con << " " << kb << std::endl;
431+
con << " " << kb << "\n";
432432
}
433433
else {
434-
con << "Usage:" << std::endl
435-
<< " keybinding list <key>" << std::endl
436-
<< " keybinding clear <key>[@context]..." << std::endl
437-
<< " keybinding set <key>[@context] \"cmdline\" \"cmdline\"..." << std::endl
438-
<< " keybinding add <key>[@context] \"cmdline\" \"cmdline\"..." << std::endl
439-
<< "Later adds, and earlier items within one command have priority." << std::endl
440-
<< "Key format: [Ctrl-][Alt-][Super-][Shift-](A-Z, 0-9, F1-F12, `, etc.)." << std::endl
441-
<< "Context may be used to limit the scope of the binding, by" << std::endl
442-
<< "requiring the current context to have a certain prefix." << std::endl
443-
<< "Current UI context is: " << std::endl
444-
<< join_strings("\n", Gui::getCurFocus(true)) << std::endl;
434+
con << "Usage:\n"
435+
<< " keybinding list <key>\n"
436+
<< " keybinding clear <key>[@context]...\n"
437+
<< " keybinding set <key>[@context] \"cmdline\" \"cmdline\"...\n"
438+
<< " keybinding add <key>[@context] \"cmdline\" \"cmdline\"...\n"
439+
<< "Later adds, and earlier items within one command have priority.\n"
440+
<< "Key format: [Ctrl-][Alt-][Super-][Shift-](A-Z, 0-9, F1-F12, `, etc.).\n"
441+
<< "Context may be used to limit the scope of the binding, by\n"
442+
<< "requiring the current context to have a certain prefix.\n"
443+
<< "Current UI context is: \n"
444+
<< join_strings("\n", Gui::getCurFocus(true)) << "\n";
445445
}
446446
}
447447

0 commit comments

Comments
 (0)