| 282 | } |
| 283 | |
| 284 | command_result Commands::keybinding(color_ostream& con, Core& core, const std::string& first, const std::vector<std::string>& parts) |
| 285 | { |
| 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]; |
| 291 | if (parts[0] == "set") |
| 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("{}\n", parse_error); |
| 297 | break; |
| 298 | } |
| 299 | if (!hotkey_mgr->addKeybind(spec.value(), part)) { |
| 300 | con.printerr("Invalid command: '{}'\n", part); |
| 301 | break; |
| 302 | } |
| 303 | } |
| 304 | } |
| 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("{}\n", parse_error); |
| 310 | } |
| 311 | if (!hotkey_mgr->removeKeybind(spec.value())) { |
| 312 | con.printerr("No matching keybinds to remove\n"); |
| 313 | break; |
| 314 | } |
| 315 | } |
| 316 | } |
| 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("{}\n", parse_error); |
| 321 | return CR_FAILURE; |
| 322 | } |
| 323 | std::vector<std::string> list = hotkey_mgr->listKeybinds(spec.value()); |
| 324 | if (list.empty()) |
| 325 | con << "No bindings." << std::endl; |
| 326 | for (const auto& kb : list) |
| 327 | con << " " << kb << std::endl; |
| 328 | } |
| 329 | else |
| 330 | { |
| 331 | con << "Usage:\n" |
| 332 | << " keybinding list <key>\n" |
| 333 | << " keybinding clear <key>[@context]...\n" |
| 334 | << " keybinding set <key>[@context] \"cmdline\" \"cmdline\"...\n" |
| 335 | << " keybinding add <key>[@context] \"cmdline\" \"cmdline\"...\n" |
| 336 | << "Later adds, and earlier items within one command have priority.\n" |
| 337 | << "Key format: [Ctrl-][Alt-][Super-][Shift-](A-Z, 0-9, F1-F12, `, etc.).\n" |
| 338 | << "Context may be used to limit the scope of the binding, by\n" |
| 339 | << "requiring the current context to have a certain prefix.\n" |
| 340 | << "Current UI context is: \n" |
| 341 | << join_strings("\n", Gui::getCurFocus(true)) << std::endl; |
nothing calls this directly
no test coverage detected