*/
#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"
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);
}
// 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);
}
}
s32 rows = (s32) m_rows;
if (rows == 0)
return 0;
- else
- return formatted_count - rows;
+
+ return formatted_count - rows;
}
+void ChatBuffer::resize(u32 scrollback)
+{
+ m_scrollback = scrollback;
+ if (m_unformatted.size() > m_scrollback)
+ deleteOldest(m_unformatted.size() - m_scrollback);
+}
ChatPrompt::ChatPrompt(const std::wstring &prompt, u32 history_limit):
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();
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;
{
}
-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);
WStrfnd fnd(text);
while (!fnd.at_end())
{
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";
m_recent_buffer.clear();
}
+
+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)
{
m_recent_buffer.step(dtime);