From 71db0297b9a88cf1ac6b83967433cbffafe8e49b Mon Sep 17 00:00:00 2001 From: Date: Thu, 4 Jan 2018 02:50:36 +0100 Subject: [PATCH] added code for the /commands --- src/application.cpp | 1 + src/main.cpp | 2 + src/singletons/commandmanager.cpp | 186 +++++++++++++++++++++--------- src/singletons/commandmanager.hpp | 39 ++++--- 4 files changed, 153 insertions(+), 75 deletions(-) diff --git a/src/application.cpp b/src/application.cpp index 4a3dde62e..92563e81a 100644 --- a/src/application.cpp +++ b/src/application.cpp @@ -1,6 +1,7 @@ #include "application.hpp" #include "logging/loggingmanager.hpp" #include "singletons/accountmanager.hpp" +#include "singletons/commandmanager.hpp" #include "singletons/emotemanager.hpp" #include "singletons/settingsmanager.hpp" #include "singletons/thememanager.hpp" diff --git a/src/main.cpp b/src/main.cpp index c67aac5f4..0240179be 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -29,6 +29,8 @@ inline bool initSettings(bool portable) } } + qDebug() << settingsPath; + if (!QDir().mkpath(settingsPath)) { printf("Error creating directories for settings: %s\n", qPrintable(settingsPath)); return false; diff --git a/src/singletons/commandmanager.cpp b/src/singletons/commandmanager.cpp index e35ab502e..dfc41b07c 100644 --- a/src/singletons/commandmanager.cpp +++ b/src/singletons/commandmanager.cpp @@ -1,5 +1,7 @@ #include "singletons/commandmanager.hpp" +#include +#include #include namespace chatterino { @@ -10,76 +12,146 @@ CommandManager &CommandManager::getInstance() return instance; } -// QString CommandManager::execCommand(QString text) -//{ -// QStringList words = text.split(' ', QString::SkipEmptyParts); +void CommandManager::loadCommands() +{ + // QFile TextFile(""); + // QStringList SL; -// if (words.length() == 0) { -// return text; -// } + // while (!TextFile.atEnd()) + // SL.append(TextFile.readLine()); +} -// QString commandName = words[0]; -// if (commandName[0] == "/") { -// commandName = commandName.mid(1); -// } +void CommandManager::saveCommands() +{ +} -// Command *command = nullptr; -// for (Command &command : this->commands) { -// if (command.name == commandName) { -// command = &command; -// break; -// } -// } +void CommandManager::setCommands(const QStringList &_commands) +{ + std::lock_guard lock(this->mutex); -// if (command == nullptr) { -// return text; -// } + this->commands.clear(); -// QString result; + for (const QString &commandRef : _commands) { + QString command = commandRef; -// static QRegularExpression parseCommand("[^{]({{)*{(\d+\+?)}"); -// for (QRegularExpressionMatch &match : parseCommand.globalMatch(command->text)) { -// result += text.mid(match.capturedStart(), match.capturedLength()); + if (command.size() == 0) { + continue; + } -// QString wordIndexMatch = match.captured(2); + if (command.at(0) != '/') { + command = QString("/") + command; + } -// 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; -// } + QString commandName = command.mid(0, command.indexOf(' ')); -// if (wordIndexMatch[wordIndexMatch.length() - 1] == '+') { -// for (int i = wordIndex; i < words.length(); i++) { -// result += words[i]; -// } -// } else { -// result += words[wordIndex]; -// } -// } + if (this->commands.find(commandName) == this->commands.end()) { + this->commands.insert(commandName, Command(command)); + } + } -// result += text.mid(match.capturedStart(), match.capturedLength()); + this->commandsStringList = _commands; + this->commandsStringList.detach(); +} -// return result; -//} +QStringList CommandManager::getCommands() +{ + return this->commandsStringList; +} -// CommandManager::Command::Command(QString _text) -//{ -// int index = _text.indexOf(' '); +QString CommandManager::execCommand(QString text) +{ + Command command; + QStringList words = text.split(' ', QString::SkipEmptyParts); -// if (index == -1) { -// this->name == _text; -// return; -// } + { + std::lock_guard lock(this->mutex); -// this->name = _text.mid(0, index); -// this->text = _text.mid(index + 1); -//} + if (words.length() == 0) { + return text; + } + + QString commandName = words[0]; + + auto it = this->commands.find(commandName); + + if (it == this->commands.end()) { + return text; + } + + command = it.value(); + } + + QString result; + + static QRegularExpression parseCommand("(^|[^{])({{)*{(\\d+\\+?)}"); + + int lastCaptureEnd = 0; + + auto globalMatch = parseCommand.globalMatch(command.text); + int matchOffset = 0; + + while (true) { + QRegularExpressionMatch match = parseCommand.match(command.text, matchOffset); + + if (!match.hasMatch()) { + break; + } + + result += command.text.mid(lastCaptureEnd, match.capturedStart() - lastCaptureEnd + 1); + + lastCaptureEnd = match.capturedEnd(); + matchOffset = lastCaptureEnd - 1; + + QString wordIndexMatch = match.captured(3); + + bool plus = wordIndexMatch.at(wordIndexMatch.size() - 1) == '+'; + wordIndexMatch = wordIndexMatch.replace("+", ""); + + bool ok; + int wordIndex = wordIndexMatch.replace("=", "").toInt(&ok); + if (!ok || wordIndex == 0) { + result += "{" + match.captured(3) + "}"; + continue; + } + + if (words.length() <= wordIndex) { + continue; + } + + if (plus) { + bool first = true; + for (int i = wordIndex; i < words.length(); i++) { + if (!first) { + result += " "; + } + result += words[i]; + first = false; + } + } else { + result += words[wordIndex]; + } + } + + result += command.text.mid(lastCaptureEnd); + + if (result.size() > 0 && result.at(0) == '{') { + result = result.mid(1); + } + + return result.replace("{{", "{"); +} + +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/singletons/commandmanager.hpp b/src/singletons/commandmanager.hpp index 75a093720..86e2c3239 100644 --- a/src/singletons/commandmanager.hpp +++ b/src/singletons/commandmanager.hpp @@ -1,11 +1,16 @@ #pragma once +#include #include -#include +#include namespace chatterino { namespace singletons { +// +// this class managed the custom /commands +// + class CommandManager { CommandManager() = default; @@ -13,28 +18,26 @@ class CommandManager public: static CommandManager &getInstance(); - // CommandManager() = delete; + QString execCommand(QString text); - // QString execCommand(QString text); - // void addCommand ? - // void loadCommands(QString) taking all commands as a \n seperated list ? + void loadCommands(); + void saveCommands(); - // static CommandManager *getInstance() - // { - // static CommandManager manager; + void setCommands(const QStringList &commands); + QStringList getCommands(); - // return manager; - // } +private: + struct Command { + QString name; + QString text; - // private: - // struct Command { - // QString name; - // QString text; + Command() = default; + Command(QString text); + }; - // Command(QString text); - // }; - - // std::vector commands; + QMap commands; + std::mutex mutex; + QStringList commandsStringList; }; } }