added code for command handling

This commit is contained in:
2017-12-27 19:50:05 +01:00
parent 223f4af55a
commit f676450d03
2 changed files with 85 additions and 33 deletions

View file

@ -1,44 +1,77 @@
#include "commandmanager.hpp" #include "commandmanager.hpp"
#include "windowmanager.hpp"
#include <QRegularExpression>
namespace chatterino { namespace chatterino {
void CommandManager::execCommand(QString command) void CommandManager::execCommand(QString text)
{ {
// if (command == "selectr") { QStringList words = text.split(' ', QString::SkipEmptyParts);
// selectSplitRelative(false, 1);
// }
// if (command == "selectl") {
// selectSplitRelative(false, -1);
// }
// if (command == "selectu") {
// selectSplitRelative(true, -1);
// }
// if (command == "selectd") {
// selectSplitRelative(true, 1);
// }
if (command == "mover") { if (words.length() == 0) {
moveSplitRelative(false, 1); 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 == "moved") { if (command.name == commandName) {
moveSplitRelative(true, 1); command = &command;
break;
} }
} }
//void CommandManager::selectSplitRelative(bool vertical, int offset) if (command == nullptr) {
//{ return text;
// SplitContainer *container = WindowManager::instance-> }
//
// if (vertical)
//}
void CommandManager::moveSplitRelative(int dx, int dy) 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;
}
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);
} }
} }

View file

@ -1,15 +1,34 @@
#pragma once #pragma once
#include <QString> #include <QString>
#include <vector>
namespace chatterino { namespace chatterino {
class CommandManager class CommandManager
{ {
public: public:
CommandManager() = delete; CommandManager() = delete;
void execCommand(QString command); QString execCommand(QString text);
// void selectSplitRelative(int dx, int dy); // void addCommand ?
void moveSplitRelative(int dx, int dy); // 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<Command> commands;
}; };
} }