aboutsummaryrefslogtreecommitdiff
path: root/src/Chatbar.cpp
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2018-05-03 08:29:45 +0200
committerdec05eba <dec05eba@protonmail.com>2018-05-03 08:29:48 +0200
commit9bb631f1e1861c63f38125fffb91081c98a1cfcc (patch)
treead852904b8ae278c789e7f636ed4107038231c3f /src/Chatbar.cpp
parent9cde35c64c9f569055b101a80419d900f58806a9 (diff)
Add/remove/list binds, saving to file
Diffstat (limited to 'src/Chatbar.cpp')
-rw-r--r--src/Chatbar.cpp58
1 files changed, 57 insertions, 1 deletions
diff --git a/src/Chatbar.cpp b/src/Chatbar.cpp
index cd2daa4..dd5a925 100644
--- a/src/Chatbar.cpp
+++ b/src/Chatbar.cpp
@@ -6,6 +6,7 @@
#include "../include/UsersSidePanel.hpp"
#include "../include/Command.hpp"
#include "../include/ColorScheme.hpp"
+#include "../include/Cache.hpp"
#include <cmath>
#include <cstring>
#include <process.hpp>
@@ -25,6 +26,8 @@ namespace dchat
const float LINE_PADDING_SIDE = 20.0f;
const float LINE_HEIGHT = 1.0f;
+ unordered_map<string, string> binds;
+
Chatbar::Chatbar() :
text("", *ResourceCache::getFont("fonts/Roboto-Regular.ttf"), FONT_SIZE * Settings::getScaling()),
caretIndex(0),
@@ -204,7 +207,7 @@ namespace dchat
}
}
- string getClipboard()
+ static string getClipboard()
{
string result;
TinyProcessLib::Process process("xsel -o -b", "", [&result](const char *bytes, size_t n)
@@ -216,6 +219,28 @@ namespace dchat
return result;
}
+ static void findReplaceAll(string &str, const string &substrToReplace, const string &stringToReplaceWith)
+ {
+ size_t findOffset = 0;
+ while(findOffset < (size_t)str.size())
+ {
+ findOffset = str.find(substrToReplace, findOffset);
+ if(findOffset != string::npos)
+ {
+ string substr = str.replace(findOffset, substrToReplace.size(), stringToReplaceWith);
+ findOffset += substrToReplace.size() + stringToReplaceWith.size();
+ }
+ }
+ }
+
+ static void replaceBinds(string &msg)
+ {
+ for(auto &it : binds)
+ {
+ findReplaceAll(msg, it.first, it.second);
+ }
+ }
+
void Chatbar::processEvent(const sf::Event &event, Channel *channel)
{
if(!focused) return;
@@ -241,7 +266,10 @@ namespace dchat
if(msg[0] == '/')
processChatCommand(StringView(msg.data() + 1, msg.size() - 1));
else
+ {
+ replaceBinds(msg);
channel->addMessage(msg);
+ }
clear();
}
}
@@ -314,4 +342,32 @@ namespace dchat
const float fontHeight = ResourceCache::getFont("fonts/Roboto-Regular.ttf")->getLineSpacing(fontSize);
return PADDING_TOP * Settings::getScaling() + floor(fontHeight * 1.7f + BOX_PADDING_Y * Settings::getScaling() * 2.0f) + PADDING_BOTTOM * Settings::getScaling();
}
+
+ bool Chatbar::addBind(const std::string &key, const std::string &value, bool updateFile)
+ {
+ if(binds.find(key) != binds.end())
+ return false;
+
+ binds[key] = value;
+ if(updateFile)
+ Cache::replaceBindsInFile(binds);
+ return true;
+ }
+
+ bool Chatbar::removeBind(const std::string &key, bool updateFile)
+ {
+ auto it = binds.find(key);
+ if(it == binds.end())
+ return false;
+
+ binds.erase(it);
+ if(updateFile)
+ Cache::replaceBindsInFile(binds);
+ return true;
+ }
+
+ const unordered_map<string, string>& Chatbar::getBinds()
+ {
+ return binds;
+ }
}