num_emerge_threads: Initialise value to cope with setting syntax error (#8396)
[oweals/minetest.git] / src / chat.cpp
index 967e159f8a7ac1e6257c0a1d224def386a589197..c3ed59804c0adbbb4f45a796a2aba1785bf0035b 100644 (file)
@@ -18,11 +18,14 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 */
 
 #include "chat.h"
-#include "debug.h"
-#include "config.h"
-#include "util/strfnd.h"
+
+#include <algorithm>
 #include <cctype>
 #include <sstream>
+
+#include "config.h"
+#include "debug.h"
+#include "util/strfnd.h"
 #include "util/string.h"
 #include "util/numeric.h"
 
@@ -34,13 +37,12 @@ ChatBuffer::ChatBuffer(u32 scrollback):
        m_empty_formatted_line.first = true;
 }
 
-void ChatBuffer::addLine(std::wstring name, std::wstring text)
+void ChatBuffer::addLine(const std::wstring &name, const std::wstring &text)
 {
        ChatLine line(name, text);
        m_unformatted.push_back(line);
 
-       if (m_rows > 0)
-       {
+       if (m_rows > 0) {
                // m_formatted is valid and must be kept valid
                bool scrolled_at_bottom = (m_scroll == getBottomScrollPos());
                u32 num_added = formatChatLine(line, m_cols, m_formatted);
@@ -49,8 +51,7 @@ void ChatBuffer::addLine(std::wstring name, std::wstring text)
        }
 
        // Limit number of lines by m_scrollback
-       if (m_unformatted.size() > m_scrollback)
-       {
+       if (m_unformatted.size() > m_scrollback) {
                deleteOldest(m_unformatted.size() - m_scrollback);
        }
 }
@@ -369,12 +370,11 @@ s32 ChatBuffer::getBottomScrollPos() const
        return formatted_count - rows;
 }
 
-void ChatBuffer::resize(u32 scrollback) {
+void ChatBuffer::resize(u32 scrollback)
+{
        m_scrollback = scrollback;
        if (m_unformatted.size() > m_scrollback)
-       {
                deleteOldest(m_unformatted.size() - m_scrollback);
-       }       
 }
 
 
@@ -402,10 +402,16 @@ void ChatPrompt::input(const std::wstring &str)
        m_nick_completion_end = 0;
 }
 
-void ChatPrompt::addToHistory(std::wstring line)
+void ChatPrompt::addToHistory(const std::wstring &line)
 {
-       if (!line.empty())
+       if (!line.empty() &&
+                       (m_history.size() == 0 || m_history.back() != line)) {
+               // Remove all duplicates
+               m_history.erase(std::remove(m_history.begin(), m_history.end(),
+                       line), m_history.end());
+               // Push unique line
                m_history.push_back(line);
+       }
        if (m_history.size() > m_history_limit)
                m_history.erase(m_history.begin());
        m_history_index = m_history.size();
@@ -420,7 +426,7 @@ void ChatPrompt::clear()
        m_nick_completion_end = 0;
 }
 
-std::wstring ChatPrompt::replace(std::wstring line)
+std::wstring ChatPrompt::replace(const std::wstring &line)
 {
        std::wstring old_line = m_line;
        m_line =  line;
@@ -654,7 +660,7 @@ ChatBackend::ChatBackend():
 {
 }
 
-void ChatBackend::addMessage(std::wstring name, std::wstring text)
+void ChatBackend::addMessage(const std::wstring &name, std::wstring text)
 {
        // Note: A message may consist of multiple lines, for example the MOTD.
        text = translate_string(text);
@@ -700,11 +706,10 @@ ChatBuffer& ChatBackend::getRecentBuffer()
        return m_recent_buffer;
 }
 
-EnrichedString ChatBackend::getRecentChat()
+EnrichedString ChatBackend::getRecentChat() const
 {
        EnrichedString result;
-       for (u32 i = 0; i < m_recent_buffer.getLineCount(); ++i)
-       {
+       for (u32 i = 0; i < m_recent_buffer.getLineCount(); ++i) {
                const ChatLine& line = m_recent_buffer.getLine(i);
                if (i != 0)
                        result += L"\n";
@@ -739,8 +744,11 @@ void ChatBackend::clearRecentChat()
 }
 
 
-void ChatBackend::applySettings(Settings* settings) {
-       m_recent_buffer.resize(settings->getU32("recent_chat_size"));
+void ChatBackend::applySettings()
+{
+       u32 recent_lines = g_settings->getU32("recent_chat_messages");
+       recent_lines = rangelim(recent_lines, 2, 20);
+       m_recent_buffer.resize(recent_lines);
 }
 
 void ChatBackend::step(float dtime)