added code for the /commands

This commit is contained in:
2018-01-04 02:50:36 +01:00
parent 02845b32d7
commit 71db0297b9
4 changed files with 153 additions and 75 deletions

View file

@ -1,6 +1,7 @@
#include "application.hpp" #include "application.hpp"
#include "logging/loggingmanager.hpp" #include "logging/loggingmanager.hpp"
#include "singletons/accountmanager.hpp" #include "singletons/accountmanager.hpp"
#include "singletons/commandmanager.hpp"
#include "singletons/emotemanager.hpp" #include "singletons/emotemanager.hpp"
#include "singletons/settingsmanager.hpp" #include "singletons/settingsmanager.hpp"
#include "singletons/thememanager.hpp" #include "singletons/thememanager.hpp"

View file

@ -29,6 +29,8 @@ inline bool initSettings(bool portable)
} }
} }
qDebug() << settingsPath;
if (!QDir().mkpath(settingsPath)) { if (!QDir().mkpath(settingsPath)) {
printf("Error creating directories for settings: %s\n", qPrintable(settingsPath)); printf("Error creating directories for settings: %s\n", qPrintable(settingsPath));
return false; return false;

View file

@ -1,5 +1,7 @@
#include "singletons/commandmanager.hpp" #include "singletons/commandmanager.hpp"
#include <QDebug>
#include <QFile>
#include <QRegularExpression> #include <QRegularExpression>
namespace chatterino { namespace chatterino {
@ -10,76 +12,146 @@ CommandManager &CommandManager::getInstance()
return instance; return instance;
} }
// QString CommandManager::execCommand(QString text) void CommandManager::loadCommands()
//{ {
// QStringList words = text.split(' ', QString::SkipEmptyParts); // QFile TextFile("");
// QStringList SL;
// if (words.length() == 0) { // while (!TextFile.atEnd())
// return text; // SL.append(TextFile.readLine());
// } }
// QString commandName = words[0]; void CommandManager::saveCommands()
// if (commandName[0] == "/") { {
// commandName = commandName.mid(1); }
// }
// Command *command = nullptr; void CommandManager::setCommands(const QStringList &_commands)
// for (Command &command : this->commands) { {
// if (command.name == commandName) { std::lock_guard<std::mutex> lock(this->mutex);
// command = &command;
// break;
// }
// }
// if (command == nullptr) { this->commands.clear();
// return text;
// }
// QString result; for (const QString &commandRef : _commands) {
QString command = commandRef;
// static QRegularExpression parseCommand("[^{]({{)*{(\d+\+?)}"); if (command.size() == 0) {
// for (QRegularExpressionMatch &match : parseCommand.globalMatch(command->text)) { continue;
// result += text.mid(match.capturedStart(), match.capturedLength()); }
// QString wordIndexMatch = match.captured(2); if (command.at(0) != '/') {
command = QString("/") + command;
}
// bool ok; QString commandName = command.mid(0, command.indexOf(' '));
// 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] == '+') { if (this->commands.find(commandName) == this->commands.end()) {
// for (int i = wordIndex; i < words.length(); i++) { this->commands.insert(commandName, Command(command));
// result += words[i]; }
// } }
// } else {
// result += words[wordIndex];
// }
// }
// 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) QString CommandManager::execCommand(QString text)
//{ {
// int index = _text.indexOf(' '); Command command;
QStringList words = text.split(' ', QString::SkipEmptyParts);
// if (index == -1) { {
// this->name == _text; std::lock_guard<std::mutex> lock(this->mutex);
// return;
// }
// this->name = _text.mid(0, index); if (words.length() == 0) {
// this->text = _text.mid(index + 1); 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);
}
} }
} }

View file

@ -1,11 +1,16 @@
#pragma once #pragma once
#include <QMap>
#include <QString> #include <QString>
#include <vector> #include <mutex>
namespace chatterino { namespace chatterino {
namespace singletons { namespace singletons {
//
// this class managed the custom /commands
//
class CommandManager class CommandManager
{ {
CommandManager() = default; CommandManager() = default;
@ -13,28 +18,26 @@ class CommandManager
public: public:
static CommandManager &getInstance(); static CommandManager &getInstance();
// CommandManager() = delete; QString execCommand(QString text);
// QString execCommand(QString text); void loadCommands();
// void addCommand ? void saveCommands();
// void loadCommands(QString) taking all commands as a \n seperated list ?
// static CommandManager *getInstance() void setCommands(const QStringList &commands);
// { QStringList getCommands();
// static CommandManager manager;
// return manager; private:
// } struct Command {
QString name;
QString text;
// private: Command() = default;
// struct Command { Command(QString text);
// QString name; };
// QString text;
// Command(QString text); QMap<QString, Command> commands;
// }; std::mutex mutex;
QStringList commandsStringList;
// std::vector<Command> commands;
}; };
} }
} }