From f676450d03598a2d69ea4fc7a4f596e46350f24a Mon Sep 17 00:00:00 2001 From: Date: Wed, 27 Dec 2017 19:50:05 +0100 Subject: [PATCH] added code for command handling --- src/commandmanager.cpp | 93 ++++++++++++++++++++++++++++-------------- src/commandmanager.hpp | 25 ++++++++++-- 2 files changed, 85 insertions(+), 33 deletions(-) diff --git a/src/commandmanager.cpp b/src/commandmanager.cpp index 1d545ad10..ae996ac15 100644 --- a/src/commandmanager.cpp +++ b/src/commandmanager.cpp @@ -1,44 +1,77 @@ #include "commandmanager.hpp" -#include "windowmanager.hpp" + +#include namespace chatterino { -void CommandManager::execCommand(QString command) +void CommandManager::execCommand(QString text) { -// if (command == "selectr") { -// selectSplitRelative(false, 1); -// } -// if (command == "selectl") { -// selectSplitRelative(false, -1); -// } -// if (command == "selectu") { -// selectSplitRelative(true, -1); -// } -// if (command == "selectd") { -// selectSplitRelative(true, 1); -// } + QStringList words = text.split(' ', QString::SkipEmptyParts); - if (command == "mover") { - moveSplitRelative(false, 1); + if (words.length() == 0) { + return text; } - if (command == "movel") { - moveSplitRelative(false, -1); + + QString commandName = words[0]; + if (commandName[0] == "/") { + commandName = commandName.mid(1); } - if (command == "moveu") { - moveSplitRelative(true, -1); + + Command *command = nullptr; + for (Command &command : this->commands) { + if (command.name == commandName) { + command = &command; + break; + } } - if (command == "moved") { - moveSplitRelative(true, 1); + + if (command == nullptr) { + return text; } + + QString result; + + static QRegularExpression parseCommand("[^{]({{)*{(\d+\+?)}"); + for (QRegularExpressionMatch &match : parseCommand.globalMatch(command->text)) { + result += text.mid(match.capturedStart(), match.capturedLength()); + + QString wordIndexMatch = match.captured(2); + + bool ok; + int wordIndex = wordIndexMatch.replace("=", "").toInt(ok); + if (!ok) { + result += match.captured(); + continue; + } + if (words.length() <= wordIndex) { + // alternatively return text because the operation failed + result += ""; + return; + } + + if (wordIndexMatch[wordIndexMatch.length() - 1] == '+') { + for (int i = wordIndex; i < words.length(); i++) { + result += words[i]; + } + } else { + result += words[wordIndex]; + } + } + + result += text.mid(match.capturedStart(), match.capturedLength()); + + return result; } -//void CommandManager::selectSplitRelative(bool vertical, int offset) -//{ -// SplitContainer *container = WindowManager::instance-> -// -// if (vertical) -//} - -void CommandManager::moveSplitRelative(int dx, int dy) +CommandManager::Command::Command(QString _text) { + int index = _text.indexOf(' '); + + if (index == -1) { + this->name == _text; + return; + } + + this->name = _text.mid(0, index); + this->text = _text.mid(index + 1); } } diff --git a/src/commandmanager.hpp b/src/commandmanager.hpp index 92ef7896a..87d915a6b 100644 --- a/src/commandmanager.hpp +++ b/src/commandmanager.hpp @@ -1,15 +1,34 @@ #pragma once #include +#include namespace chatterino { + class CommandManager { public: CommandManager() = delete; - void execCommand(QString command); - // void selectSplitRelative(int dx, int dy); - void moveSplitRelative(int dx, int dy); + QString execCommand(QString text); + // void addCommand ? + // void loadCommands(QString) taking all commands as a \n seperated list ? + + static CommandManager *getInstance() + { + static CommandManager manager; + + return manager; + } + +private: + struct Command { + QString name; + QString text; + + Command(QString text); + }; + + std::vector commands; }; }