3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "util/strfnd.h"
26 #include "util/string.h"
27 #include "util/numeric.h"
29 ChatBuffer::ChatBuffer(u32 scrollback):
30 m_scrollback(scrollback)
32 if (m_scrollback == 0)
34 m_empty_formatted_line.first = true;
37 ChatBuffer::~ChatBuffer()
41 void ChatBuffer::addLine(std::wstring name, std::wstring text)
43 ChatLine line(name, text);
44 m_unformatted.push_back(line);
48 // m_formatted is valid and must be kept valid
49 bool scrolled_at_bottom = (m_scroll == getBottomScrollPos());
50 u32 num_added = formatChatLine(line, m_cols, m_formatted);
51 if (scrolled_at_bottom)
52 m_scroll += num_added;
55 // Limit number of lines by m_scrollback
56 if (m_unformatted.size() > m_scrollback)
58 deleteOldest(m_unformatted.size() - m_scrollback);
62 void ChatBuffer::clear()
64 m_unformatted.clear();
69 u32 ChatBuffer::getLineCount() const
71 return m_unformatted.size();
74 const ChatLine& ChatBuffer::getLine(u32 index) const
76 assert(index < getLineCount()); // pre-condition
77 return m_unformatted[index];
80 void ChatBuffer::step(f32 dtime)
82 for (u32 i = 0; i < m_unformatted.size(); ++i)
84 m_unformatted[i].age += dtime;
88 void ChatBuffer::deleteOldest(u32 count)
90 bool at_bottom = (m_scroll == getBottomScrollPos());
92 u32 del_unformatted = 0;
93 u32 del_formatted = 0;
95 while (count > 0 && del_unformatted < m_unformatted.size())
99 // keep m_formatted in sync
100 if (del_formatted < m_formatted.size())
103 sanity_check(m_formatted[del_formatted].first);
105 while (del_formatted < m_formatted.size() &&
106 !m_formatted[del_formatted].first)
113 m_unformatted.erase(m_unformatted.begin(), m_unformatted.begin() + del_unformatted);
114 m_formatted.erase(m_formatted.begin(), m_formatted.begin() + del_formatted);
117 m_scroll = getBottomScrollPos();
119 scrollAbsolute(m_scroll - del_formatted);
122 void ChatBuffer::deleteByAge(f32 maxAge)
125 while (count < m_unformatted.size() && m_unformatted[count].age > maxAge)
130 u32 ChatBuffer::getColumns() const
135 u32 ChatBuffer::getRows() const
140 void ChatBuffer::reformat(u32 cols, u32 rows)
142 if (cols == 0 || rows == 0)
144 // Clear formatted buffer
150 else if (cols != m_cols || rows != m_rows)
152 // TODO: Avoid reformatting ALL lines (even invisible ones)
153 // each time the console size changes.
155 // Find out the scroll position in *unformatted* lines
156 u32 restore_scroll_unformatted = 0;
157 u32 restore_scroll_formatted = 0;
158 bool at_bottom = (m_scroll == getBottomScrollPos());
161 for (s32 i = 0; i < m_scroll; ++i)
163 if (m_formatted[i].first)
164 ++restore_scroll_unformatted;
168 // If number of columns change, reformat everything
172 for (u32 i = 0; i < m_unformatted.size(); ++i)
174 if (i == restore_scroll_unformatted)
175 restore_scroll_formatted = m_formatted.size();
176 formatChatLine(m_unformatted[i], cols, m_formatted);
180 // Update the console size
184 // Restore the scroll position
191 scrollAbsolute(restore_scroll_formatted);
196 const ChatFormattedLine& ChatBuffer::getFormattedLine(u32 row) const
198 s32 index = m_scroll + (s32) row;
199 if (index >= 0 && index < (s32) m_formatted.size())
200 return m_formatted[index];
202 return m_empty_formatted_line;
205 void ChatBuffer::scroll(s32 rows)
207 scrollAbsolute(m_scroll + rows);
210 void ChatBuffer::scrollAbsolute(s32 scroll)
212 s32 top = getTopScrollPos();
213 s32 bottom = getBottomScrollPos();
218 if (m_scroll > bottom)
222 void ChatBuffer::scrollBottom()
224 m_scroll = getBottomScrollPos();
227 void ChatBuffer::scrollTop()
229 m_scroll = getTopScrollPos();
232 u32 ChatBuffer::formatChatLine(const ChatLine& line, u32 cols,
233 std::vector<ChatFormattedLine>& destination) const
236 std::vector<ChatFormattedFragment> next_frags;
237 ChatFormattedLine next_line;
238 ChatFormattedFragment temp_frag;
241 u32 hanging_indentation = 0;
243 // Format the sender name and produce fragments
244 if (!line.name.empty()) {
245 temp_frag.text = L"<";
246 temp_frag.column = 0;
247 //temp_frag.bold = 0;
248 next_frags.push_back(temp_frag);
249 temp_frag.text = line.name;
250 temp_frag.column = 0;
251 //temp_frag.bold = 1;
252 next_frags.push_back(temp_frag);
253 temp_frag.text = L"> ";
254 temp_frag.column = 0;
255 //temp_frag.bold = 0;
256 next_frags.push_back(temp_frag);
259 std::wstring name_sanitized = line.name.c_str();
261 // Choose an indentation level
262 if (line.name.empty()) {
264 hanging_indentation = 0;
265 } else if (name_sanitized.size() + 3 <= cols/2) {
266 // Names shorter than about half the console width
267 hanging_indentation = line.name.size() + 3;
270 hanging_indentation = 2;
272 //EnrichedString line_text(line.text);
274 next_line.first = true;
275 bool text_processing = false;
277 // Produce fragments and layout them into lines
278 while (!next_frags.empty() || in_pos < line.text.size())
280 // Layout fragments into lines
281 while (!next_frags.empty())
283 ChatFormattedFragment& frag = next_frags[0];
284 if (frag.text.size() <= cols - out_column)
286 // Fragment fits into current line
287 frag.column = out_column;
288 next_line.fragments.push_back(frag);
289 out_column += frag.text.size();
290 next_frags.erase(next_frags.begin());
294 // Fragment does not fit into current line
296 temp_frag.text = frag.text.substr(0, cols - out_column);
297 temp_frag.column = out_column;
298 //temp_frag.bold = frag.bold;
299 next_line.fragments.push_back(temp_frag);
300 frag.text = frag.text.substr(cols - out_column);
303 if (out_column == cols || text_processing)
305 // End the current line
306 destination.push_back(next_line);
308 next_line.fragments.clear();
309 next_line.first = false;
311 out_column = text_processing ? hanging_indentation : 0;
316 if (in_pos < line.text.size())
318 u32 remaining_in_input = line.text.size() - in_pos;
319 u32 remaining_in_output = cols - out_column;
321 // Determine a fragment length <= the minimum of
322 // remaining_in_{in,out}put. Try to end the fragment
323 // on a word boundary.
324 u32 frag_length = 1, space_pos = 0;
325 while (frag_length < remaining_in_input &&
326 frag_length < remaining_in_output)
328 if (iswspace(line.text.getString()[in_pos + frag_length]))
329 space_pos = frag_length;
332 if (space_pos != 0 && frag_length < remaining_in_input)
333 frag_length = space_pos + 1;
335 temp_frag.text = line.text.substr(in_pos, frag_length);
336 temp_frag.column = 0;
337 //temp_frag.bold = 0;
338 next_frags.push_back(temp_frag);
339 in_pos += frag_length;
340 text_processing = true;
345 if (num_added == 0 || !next_line.fragments.empty())
347 destination.push_back(next_line);
354 s32 ChatBuffer::getTopScrollPos() const
356 s32 formatted_count = (s32) m_formatted.size();
357 s32 rows = (s32) m_rows;
360 else if (formatted_count <= rows)
361 return formatted_count - rows;
366 s32 ChatBuffer::getBottomScrollPos() const
368 s32 formatted_count = (s32) m_formatted.size();
369 s32 rows = (s32) m_rows;
373 return formatted_count - rows;
378 ChatPrompt::ChatPrompt(const std::wstring &prompt, u32 history_limit):
380 m_history_limit(history_limit)
384 ChatPrompt::~ChatPrompt()
388 void ChatPrompt::input(wchar_t ch)
390 m_line.insert(m_cursor, 1, ch);
393 m_nick_completion_start = 0;
394 m_nick_completion_end = 0;
397 void ChatPrompt::input(const std::wstring &str)
399 m_line.insert(m_cursor, str);
400 m_cursor += str.size();
402 m_nick_completion_start = 0;
403 m_nick_completion_end = 0;
406 void ChatPrompt::addToHistory(std::wstring line)
409 m_history.push_back(line);
410 if (m_history.size() > m_history_limit)
411 m_history.erase(m_history.begin());
412 m_history_index = m_history.size();
415 void ChatPrompt::clear()
420 m_nick_completion_start = 0;
421 m_nick_completion_end = 0;
424 std::wstring ChatPrompt::replace(std::wstring line)
426 std::wstring old_line = m_line;
428 m_view = m_cursor = line.size();
430 m_nick_completion_start = 0;
431 m_nick_completion_end = 0;
435 void ChatPrompt::historyPrev()
437 if (m_history_index != 0)
440 replace(m_history[m_history_index]);
444 void ChatPrompt::historyNext()
446 if (m_history_index + 1 >= m_history.size())
448 m_history_index = m_history.size();
454 replace(m_history[m_history_index]);
458 void ChatPrompt::nickCompletion(const std::list<std::string>& names, bool backwards)
461 // (a) m_nick_completion_start == m_nick_completion_end == 0
462 // Then no previous nick completion is active.
463 // Get the word around the cursor and replace with any nick
464 // that has that word as a prefix.
465 // (b) else, continue a previous nick completion.
466 // m_nick_completion_start..m_nick_completion_end are the
467 // interval where the originally used prefix was. Cycle
468 // through the list of completions of that prefix.
469 u32 prefix_start = m_nick_completion_start;
470 u32 prefix_end = m_nick_completion_end;
471 bool initial = (prefix_end == 0);
474 // no previous nick completion is active
475 prefix_start = prefix_end = m_cursor;
476 while (prefix_start > 0 && !iswspace(m_line[prefix_start-1]))
478 while (prefix_end < m_line.size() && !iswspace(m_line[prefix_end]))
480 if (prefix_start == prefix_end)
483 std::wstring prefix = m_line.substr(prefix_start, prefix_end - prefix_start);
485 // find all names that start with the selected prefix
486 std::vector<std::wstring> completions;
487 for (std::list<std::string>::const_iterator
489 i != names.end(); ++i)
491 if (str_starts_with(narrow_to_wide(*i), prefix, true))
493 std::wstring completion = narrow_to_wide(*i);
494 if (prefix_start == 0)
496 completions.push_back(completion);
499 if (completions.empty())
502 // find a replacement string and the word that will be replaced
503 u32 word_end = prefix_end;
504 u32 replacement_index = 0;
507 while (word_end < m_line.size() && !iswspace(m_line[word_end]))
509 std::wstring word = m_line.substr(prefix_start, word_end - prefix_start);
511 // cycle through completions
512 for (u32 i = 0; i < completions.size(); ++i)
514 if (str_equal(word, completions[i], true))
517 replacement_index = i + completions.size() - 1;
519 replacement_index = i + 1;
520 replacement_index %= completions.size();
525 std::wstring replacement = completions[replacement_index];
526 if (word_end < m_line.size() && iswspace(m_line[word_end]))
529 // replace existing word with replacement word,
530 // place the cursor at the end and record the completion prefix
531 m_line.replace(prefix_start, word_end - prefix_start, replacement);
532 m_cursor = prefix_start + replacement.size();
534 m_nick_completion_start = prefix_start;
535 m_nick_completion_end = prefix_end;
538 void ChatPrompt::reformat(u32 cols)
540 if (cols <= m_prompt.size())
547 s32 length = m_line.size();
548 bool was_at_end = (m_view + m_cols >= length + 1);
549 m_cols = cols - m_prompt.size();
556 std::wstring ChatPrompt::getVisiblePortion() const
558 return m_prompt + m_line.substr(m_view, m_cols);
561 s32 ChatPrompt::getVisibleCursorPosition() const
563 return m_cursor - m_view + m_prompt.size();
566 void ChatPrompt::cursorOperation(CursorOp op, CursorOpDir dir, CursorOpScope scope)
568 s32 old_cursor = m_cursor;
569 s32 new_cursor = m_cursor;
571 s32 length = m_line.size();
572 s32 increment = (dir == CURSOROP_DIR_RIGHT) ? 1 : -1;
575 case CURSOROP_SCOPE_CHARACTER:
576 new_cursor += increment;
578 case CURSOROP_SCOPE_WORD:
579 if (dir == CURSOROP_DIR_RIGHT) {
580 // skip one word to the right
581 while (new_cursor < length && iswspace(m_line[new_cursor]))
583 while (new_cursor < length && !iswspace(m_line[new_cursor]))
585 while (new_cursor < length && iswspace(m_line[new_cursor]))
588 // skip one word to the left
589 while (new_cursor >= 1 && iswspace(m_line[new_cursor - 1]))
591 while (new_cursor >= 1 && !iswspace(m_line[new_cursor - 1]))
595 case CURSOROP_SCOPE_LINE:
596 new_cursor += increment * length;
598 case CURSOROP_SCOPE_SELECTION:
602 new_cursor = MYMAX(MYMIN(new_cursor, length), 0);
606 m_cursor = new_cursor;
609 case CURSOROP_DELETE:
610 if (m_cursor_len > 0) { // Delete selected text first
611 m_line.erase(m_cursor, m_cursor_len);
613 m_cursor = MYMIN(new_cursor, old_cursor);
614 m_line.erase(m_cursor, abs(new_cursor - old_cursor));
618 case CURSOROP_SELECT:
619 if (scope == CURSOROP_SCOPE_LINE) {
621 m_cursor_len = length;
623 m_cursor = MYMIN(new_cursor, old_cursor);
624 m_cursor_len += abs(new_cursor - old_cursor);
625 m_cursor_len = MYMIN(m_cursor_len, length - m_cursor);
632 m_nick_completion_start = 0;
633 m_nick_completion_end = 0;
636 void ChatPrompt::clampView()
638 s32 length = m_line.size();
639 if (length + 1 <= m_cols)
645 m_view = MYMIN(m_view, length + 1 - m_cols);
646 m_view = MYMIN(m_view, m_cursor);
647 m_view = MYMAX(m_view, m_cursor - m_cols + 1);
648 m_view = MYMAX(m_view, 0);
654 ChatBackend::ChatBackend():
655 m_console_buffer(500),
661 ChatBackend::~ChatBackend()
665 void ChatBackend::addMessage(std::wstring name, std::wstring text)
667 // Note: A message may consist of multiple lines, for example the MOTD.
669 while (!fnd.at_end())
671 std::wstring line = fnd.next(L"\n");
672 m_console_buffer.addLine(name, line);
673 m_recent_buffer.addLine(name, line);
677 void ChatBackend::addUnparsedMessage(std::wstring message)
679 // TODO: Remove the need to parse chat messages client-side, by sending
680 // separate name and text fields in TOCLIENT_CHAT_MESSAGE.
682 if (message.size() >= 2 && message[0] == L'<')
684 std::size_t closing = message.find_first_of(L'>', 1);
685 if (closing != std::wstring::npos &&
686 closing + 2 <= message.size() &&
687 message[closing+1] == L' ')
689 std::wstring name = message.substr(1, closing - 1);
690 std::wstring text = message.substr(closing + 2);
691 addMessage(name, text);
696 // Unable to parse, probably a server message.
697 addMessage(L"", message);
700 ChatBuffer& ChatBackend::getConsoleBuffer()
702 return m_console_buffer;
705 ChatBuffer& ChatBackend::getRecentBuffer()
707 return m_recent_buffer;
710 EnrichedString ChatBackend::getRecentChat()
712 EnrichedString result;
713 for (u32 i = 0; i < m_recent_buffer.getLineCount(); ++i)
715 const ChatLine& line = m_recent_buffer.getLine(i);
718 if (!line.name.empty()) {
728 ChatPrompt& ChatBackend::getPrompt()
733 void ChatBackend::reformat(u32 cols, u32 rows)
735 m_console_buffer.reformat(cols, rows);
737 // no need to reformat m_recent_buffer, its formatted lines
740 m_prompt.reformat(cols);
743 void ChatBackend::clearRecentChat()
745 m_recent_buffer.clear();
748 void ChatBackend::step(float dtime)
750 m_recent_buffer.step(dtime);
751 m_recent_buffer.deleteByAge(60.0);
753 // no need to age messages in anything but m_recent_buffer
756 void ChatBackend::scroll(s32 rows)
758 m_console_buffer.scroll(rows);
761 void ChatBackend::scrollPageDown()
763 m_console_buffer.scroll(m_console_buffer.getRows());
766 void ChatBackend::scrollPageUp()
768 m_console_buffer.scroll(-(s32)m_console_buffer.getRows());