From 9bb631f1e1861c63f38125fffb91081c98a1cfcc Mon Sep 17 00:00:00 2001 From: dec05eba Date: Thu, 3 May 2018 08:29:45 +0200 Subject: Add/remove/list binds, saving to file --- src/Chatbar.cpp | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) (limited to 'src/Chatbar.cpp') 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 #include #include @@ -25,6 +26,8 @@ namespace dchat const float LINE_PADDING_SIDE = 20.0f; const float LINE_HEIGHT = 1.0f; + unordered_map 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& Chatbar::getBinds() + { + return binds; + } } -- cgit v1.2.3