aboutsummaryrefslogtreecommitdiff
path: root/src/chat.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/chat.cpp')
-rw-r--r--src/chat.cpp185
1 files changed, 116 insertions, 69 deletions
diff --git a/src/chat.cpp b/src/chat.cpp
index d3eadbbf4..c3ed59804 100644
--- a/src/chat.cpp
+++ b/src/chat.cpp
@@ -29,7 +29,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "util/string.h"
#include "util/numeric.h"
-ChatBuffer::ChatBuffer(u32 scrollback) : m_scrollback(scrollback)
+ChatBuffer::ChatBuffer(u32 scrollback):
+ m_scrollback(scrollback)
{
if (m_scrollback == 0)
m_scrollback = 1;
@@ -67,9 +68,9 @@ u32 ChatBuffer::getLineCount() const
return m_unformatted.size();
}
-const ChatLine &ChatBuffer::getLine(u32 index) const
+const ChatLine& ChatBuffer::getLine(u32 index) const
{
- assert(index < getLineCount()); // pre-condition
+ assert(index < getLineCount()); // pre-condition
return m_unformatted[index];
}
@@ -87,11 +88,13 @@ void ChatBuffer::deleteOldest(u32 count)
u32 del_unformatted = 0;
u32 del_formatted = 0;
- while (count > 0 && del_unformatted < m_unformatted.size()) {
+ while (count > 0 && del_unformatted < m_unformatted.size())
+ {
++del_unformatted;
// keep m_formatted in sync
- if (del_formatted < m_formatted.size()) {
+ if (del_formatted < m_formatted.size())
+ {
sanity_check(m_formatted[del_formatted].first);
++del_formatted;
@@ -103,8 +106,7 @@ void ChatBuffer::deleteOldest(u32 count)
--count;
}
- m_unformatted.erase(
- m_unformatted.begin(), m_unformatted.begin() + del_unformatted);
+ m_unformatted.erase(m_unformatted.begin(), m_unformatted.begin() + del_unformatted);
m_formatted.erase(m_formatted.begin(), m_formatted.begin() + del_formatted);
if (at_bottom)
@@ -133,13 +135,16 @@ u32 ChatBuffer::getRows() const
void ChatBuffer::reformat(u32 cols, u32 rows)
{
- if (cols == 0 || rows == 0) {
+ if (cols == 0 || rows == 0)
+ {
// Clear formatted buffer
m_cols = 0;
m_rows = 0;
m_scroll = 0;
m_formatted.clear();
- } else if (cols != m_cols || rows != m_rows) {
+ }
+ else if (cols != m_cols || rows != m_rows)
+ {
// TODO: Avoid reformatting ALL lines (even invisible ones)
// each time the console size changes.
@@ -147,17 +152,21 @@ void ChatBuffer::reformat(u32 cols, u32 rows)
u32 restore_scroll_unformatted = 0;
u32 restore_scroll_formatted = 0;
bool at_bottom = (m_scroll == getBottomScrollPos());
- if (!at_bottom) {
- for (s32 i = 0; i < m_scroll; ++i) {
+ if (!at_bottom)
+ {
+ for (s32 i = 0; i < m_scroll; ++i)
+ {
if (m_formatted[i].first)
++restore_scroll_unformatted;
}
}
// If number of columns change, reformat everything
- if (cols != m_cols) {
+ if (cols != m_cols)
+ {
m_formatted.clear();
- for (u32 i = 0; i < m_unformatted.size(); ++i) {
+ for (u32 i = 0; i < m_unformatted.size(); ++i)
+ {
if (i == restore_scroll_unformatted)
restore_scroll_formatted = m_formatted.size();
formatChatLine(m_unformatted[i], cols, m_formatted);
@@ -169,18 +178,21 @@ void ChatBuffer::reformat(u32 cols, u32 rows)
m_rows = rows;
// Restore the scroll position
- if (at_bottom) {
+ if (at_bottom)
+ {
scrollBottom();
- } else {
+ }
+ else
+ {
scrollAbsolute(restore_scroll_formatted);
}
}
}
-const ChatFormattedLine &ChatBuffer::getFormattedLine(u32 row) const
+const ChatFormattedLine& ChatBuffer::getFormattedLine(u32 row) const
{
- s32 index = m_scroll + (s32)row;
- if (index >= 0 && index < (s32)m_formatted.size())
+ s32 index = m_scroll + (s32) row;
+ if (index >= 0 && index < (s32) m_formatted.size())
return m_formatted[index];
return m_empty_formatted_line;
@@ -213,8 +225,8 @@ void ChatBuffer::scrollTop()
m_scroll = getTopScrollPos();
}
-u32 ChatBuffer::formatChatLine(const ChatLine &line, u32 cols,
- std::vector<ChatFormattedLine> &destination) const
+u32 ChatBuffer::formatChatLine(const ChatLine& line, u32 cols,
+ std::vector<ChatFormattedLine>& destination) const
{
u32 num_added = 0;
std::vector<ChatFormattedFragment> next_frags;
@@ -228,15 +240,15 @@ u32 ChatBuffer::formatChatLine(const ChatLine &line, u32 cols,
if (!line.name.empty()) {
temp_frag.text = L"<";
temp_frag.column = 0;
- // temp_frag.bold = 0;
+ //temp_frag.bold = 0;
next_frags.push_back(temp_frag);
temp_frag.text = line.name;
temp_frag.column = 0;
- // temp_frag.bold = 1;
+ //temp_frag.bold = 1;
next_frags.push_back(temp_frag);
temp_frag.text = L"> ";
temp_frag.column = 0;
- // temp_frag.bold = 0;
+ //temp_frag.bold = 0;
next_frags.push_back(temp_frag);
}
@@ -246,40 +258,46 @@ u32 ChatBuffer::formatChatLine(const ChatLine &line, u32 cols,
if (line.name.empty()) {
// Server messages
hanging_indentation = 0;
- } else if (name_sanitized.size() + 3 <= cols / 2) {
+ } else if (name_sanitized.size() + 3 <= cols/2) {
// Names shorter than about half the console width
hanging_indentation = line.name.size() + 3;
} else {
// Very long names
hanging_indentation = 2;
}
- // EnrichedString line_text(line.text);
+ //EnrichedString line_text(line.text);
next_line.first = true;
bool text_processing = false;
// Produce fragments and layout them into lines
- while (!next_frags.empty() || in_pos < line.text.size()) {
+ while (!next_frags.empty() || in_pos < line.text.size())
+ {
// Layout fragments into lines
- while (!next_frags.empty()) {
- ChatFormattedFragment &frag = next_frags[0];
- if (frag.text.size() <= cols - out_column) {
+ while (!next_frags.empty())
+ {
+ ChatFormattedFragment& frag = next_frags[0];
+ if (frag.text.size() <= cols - out_column)
+ {
// Fragment fits into current line
frag.column = out_column;
next_line.fragments.push_back(frag);
out_column += frag.text.size();
next_frags.erase(next_frags.begin());
- } else {
+ }
+ else
+ {
// Fragment does not fit into current line
// So split it up
temp_frag.text = frag.text.substr(0, cols - out_column);
temp_frag.column = out_column;
- // temp_frag.bold = frag.bold;
+ //temp_frag.bold = frag.bold;
next_line.fragments.push_back(temp_frag);
frag.text = frag.text.substr(cols - out_column);
out_column = cols;
}
- if (out_column == cols || text_processing) {
+ if (out_column == cols || text_processing)
+ {
// End the current line
destination.push_back(next_line);
num_added++;
@@ -291,7 +309,8 @@ u32 ChatBuffer::formatChatLine(const ChatLine &line, u32 cols,
}
// Produce fragment
- if (in_pos < line.text.size()) {
+ if (in_pos < line.text.size())
+ {
u32 remaining_in_input = line.text.size() - in_pos;
u32 remaining_in_output = cols - out_column;
@@ -300,7 +319,8 @@ u32 ChatBuffer::formatChatLine(const ChatLine &line, u32 cols,
// on a word boundary.
u32 frag_length = 1, space_pos = 0;
while (frag_length < remaining_in_input &&
- frag_length < remaining_in_output) {
+ frag_length < remaining_in_output)
+ {
if (iswspace(line.text.getString()[in_pos + frag_length]))
space_pos = frag_length;
++frag_length;
@@ -310,7 +330,7 @@ u32 ChatBuffer::formatChatLine(const ChatLine &line, u32 cols,
temp_frag.text = line.text.substr(in_pos, frag_length);
temp_frag.column = 0;
- // temp_frag.bold = 0;
+ //temp_frag.bold = 0;
next_frags.push_back(temp_frag);
in_pos += frag_length;
text_processing = true;
@@ -318,7 +338,8 @@ u32 ChatBuffer::formatChatLine(const ChatLine &line, u32 cols,
}
// End the last line
- if (num_added == 0 || !next_line.fragments.empty()) {
+ if (num_added == 0 || !next_line.fragments.empty())
+ {
destination.push_back(next_line);
num_added++;
}
@@ -328,8 +349,8 @@ u32 ChatBuffer::formatChatLine(const ChatLine &line, u32 cols,
s32 ChatBuffer::getTopScrollPos() const
{
- s32 formatted_count = (s32)m_formatted.size();
- s32 rows = (s32)m_rows;
+ s32 formatted_count = (s32) m_formatted.size();
+ s32 rows = (s32) m_rows;
if (rows == 0)
return 0;
@@ -341,8 +362,8 @@ s32 ChatBuffer::getTopScrollPos() const
s32 ChatBuffer::getBottomScrollPos() const
{
- s32 formatted_count = (s32)m_formatted.size();
- s32 rows = (s32)m_rows;
+ s32 formatted_count = (s32) m_formatted.size();
+ s32 rows = (s32) m_rows;
if (rows == 0)
return 0;
@@ -356,8 +377,10 @@ void ChatBuffer::resize(u32 scrollback)
deleteOldest(m_unformatted.size() - m_scrollback);
}
-ChatPrompt::ChatPrompt(const std::wstring &prompt, u32 history_limit) :
- m_prompt(prompt), m_history_limit(history_limit)
+
+ChatPrompt::ChatPrompt(const std::wstring &prompt, u32 history_limit):
+ m_prompt(prompt),
+ m_history_limit(history_limit)
{
}
@@ -381,10 +404,11 @@ void ChatPrompt::input(const std::wstring &str)
void ChatPrompt::addToHistory(const std::wstring &line)
{
- if (!line.empty() && (m_history.size() == 0 || m_history.back() != line)) {
+ 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());
+ m_history.erase(std::remove(m_history.begin(), m_history.end(),
+ line), m_history.end());
// Push unique line
m_history.push_back(line);
}
@@ -405,7 +429,7 @@ void ChatPrompt::clear()
std::wstring ChatPrompt::replace(const std::wstring &line)
{
std::wstring old_line = m_line;
- m_line = line;
+ m_line = line;
m_view = m_cursor = line.size();
clampView();
m_nick_completion_start = 0;
@@ -415,7 +439,8 @@ std::wstring ChatPrompt::replace(const std::wstring &line)
void ChatPrompt::historyPrev()
{
- if (m_history_index != 0) {
+ if (m_history_index != 0)
+ {
--m_history_index;
replace(m_history[m_history_index]);
}
@@ -423,16 +448,19 @@ void ChatPrompt::historyPrev()
void ChatPrompt::historyNext()
{
- if (m_history_index + 1 >= m_history.size()) {
+ if (m_history_index + 1 >= m_history.size())
+ {
m_history_index = m_history.size();
replace(L"");
- } else {
+ }
+ else
+ {
++m_history_index;
replace(m_history[m_history_index]);
}
}
-void ChatPrompt::nickCompletion(const std::list<std::string> &names, bool backwards)
+void ChatPrompt::nickCompletion(const std::list<std::string>& names, bool backwards)
{
// Two cases:
// (a) m_nick_completion_start == m_nick_completion_end == 0
@@ -446,10 +474,11 @@ void ChatPrompt::nickCompletion(const std::list<std::string> &names, bool backwa
u32 prefix_start = m_nick_completion_start;
u32 prefix_end = m_nick_completion_end;
bool initial = (prefix_end == 0);
- if (initial) {
+ if (initial)
+ {
// no previous nick completion is active
prefix_start = prefix_end = m_cursor;
- while (prefix_start > 0 && !iswspace(m_line[prefix_start - 1]))
+ while (prefix_start > 0 && !iswspace(m_line[prefix_start-1]))
--prefix_start;
while (prefix_end < m_line.size() && !iswspace(m_line[prefix_end]))
++prefix_end;
@@ -475,14 +504,17 @@ void ChatPrompt::nickCompletion(const std::list<std::string> &names, bool backwa
// find a replacement string and the word that will be replaced
u32 word_end = prefix_end;
u32 replacement_index = 0;
- if (!initial) {
+ if (!initial)
+ {
while (word_end < m_line.size() && !iswspace(m_line[word_end]))
++word_end;
std::wstring word = m_line.substr(prefix_start, word_end - prefix_start);
// cycle through completions
- for (u32 i = 0; i < completions.size(); ++i) {
- if (str_equal(word, completions[i], true)) {
+ for (u32 i = 0; i < completions.size(); ++i)
+ {
+ if (str_equal(word, completions[i], true))
+ {
if (backwards)
replacement_index = i + completions.size() - 1;
else
@@ -507,10 +539,13 @@ void ChatPrompt::nickCompletion(const std::list<std::string> &names, bool backwa
void ChatPrompt::reformat(u32 cols)
{
- if (cols <= m_prompt.size()) {
+ if (cols <= m_prompt.size())
+ {
m_cols = 0;
m_view = m_cursor;
- } else {
+ }
+ else
+ {
s32 length = m_line.size();
bool was_at_end = (m_view + m_cols >= length + 1);
m_cols = cols - m_prompt.size();
@@ -603,9 +638,12 @@ void ChatPrompt::cursorOperation(CursorOp op, CursorOpDir dir, CursorOpScope sco
void ChatPrompt::clampView()
{
s32 length = m_line.size();
- if (length + 1 <= m_cols) {
+ if (length + 1 <= m_cols)
+ {
m_view = 0;
- } else {
+ }
+ else
+ {
m_view = MYMIN(m_view, length + 1 - m_cols);
m_view = MYMIN(m_view, m_cursor);
m_view = MYMAX(m_view, m_cursor - m_cols + 1);
@@ -613,8 +651,12 @@ void ChatPrompt::clampView()
}
}
-ChatBackend::ChatBackend() :
- m_console_buffer(500), m_recent_buffer(6), m_prompt(L"]", 500)
+
+
+ChatBackend::ChatBackend():
+ m_console_buffer(500),
+ m_recent_buffer(6),
+ m_prompt(L"]", 500)
{
}
@@ -623,7 +665,8 @@ 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()) {
+ while (!fnd.at_end())
+ {
std::wstring line = fnd.next(L"\n");
m_console_buffer.addLine(name, line);
m_recent_buffer.addLine(name, line);
@@ -635,10 +678,13 @@ void ChatBackend::addUnparsedMessage(std::wstring message)
// TODO: Remove the need to parse chat messages client-side, by sending
// separate name and text fields in TOCLIENT_CHAT_MESSAGE.
- if (message.size() >= 2 && message[0] == L'<') {
+ if (message.size() >= 2 && message[0] == L'<')
+ {
std::size_t closing = message.find_first_of(L'>', 1);
- if (closing != std::wstring::npos && closing + 2 <= message.size() &&
- message[closing + 1] == L' ') {
+ if (closing != std::wstring::npos &&
+ closing + 2 <= message.size() &&
+ message[closing+1] == L' ')
+ {
std::wstring name = message.substr(1, closing - 1);
std::wstring text = message.substr(closing + 2);
addMessage(name, text);
@@ -650,12 +696,12 @@ void ChatBackend::addUnparsedMessage(std::wstring message)
addMessage(L"", message);
}
-ChatBuffer &ChatBackend::getConsoleBuffer()
+ChatBuffer& ChatBackend::getConsoleBuffer()
{
return m_console_buffer;
}
-ChatBuffer &ChatBackend::getRecentBuffer()
+ChatBuffer& ChatBackend::getRecentBuffer()
{
return m_recent_buffer;
}
@@ -664,7 +710,7 @@ EnrichedString ChatBackend::getRecentChat() const
{
EnrichedString result;
for (u32 i = 0; i < m_recent_buffer.getLineCount(); ++i) {
- const ChatLine &line = m_recent_buffer.getLine(i);
+ const ChatLine& line = m_recent_buffer.getLine(i);
if (i != 0)
result += L"\n";
if (!line.name.empty()) {
@@ -677,7 +723,7 @@ EnrichedString ChatBackend::getRecentChat() const
return result;
}
-ChatPrompt &ChatBackend::getPrompt()
+ChatPrompt& ChatBackend::getPrompt()
{
return m_prompt;
}
@@ -697,6 +743,7 @@ void ChatBackend::clearRecentChat()
m_recent_buffer.clear();
}
+
void ChatBackend::applySettings()
{
u32 recent_lines = g_settings->getU32("recent_chat_messages");