chat.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  1. /*
  2. Minetest
  3. Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 3.0 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include "chat.h"
  17. #include "debug.h"
  18. #include "config.h"
  19. #include "util/strfnd.h"
  20. #include <cctype>
  21. #include <sstream>
  22. #include "util/string.h"
  23. #include "util/numeric.h"
  24. ChatBuffer::ChatBuffer(u32 scrollback):
  25. m_scrollback(scrollback),
  26. m_unformatted(),
  27. m_cols(0),
  28. m_rows(0),
  29. m_scroll(0),
  30. m_formatted(),
  31. m_empty_formatted_line()
  32. {
  33. if (m_scrollback == 0)
  34. m_scrollback = 1;
  35. m_empty_formatted_line.first = true;
  36. }
  37. ChatBuffer::~ChatBuffer()
  38. {
  39. }
  40. void ChatBuffer::addLine(std::wstring name, std::wstring text)
  41. {
  42. ChatLine line(name, text);
  43. m_unformatted.push_back(line);
  44. if (m_rows > 0)
  45. {
  46. // m_formatted is valid and must be kept valid
  47. bool scrolled_at_bottom = (m_scroll == getBottomScrollPos());
  48. u32 num_added = formatChatLine(line, m_cols, m_formatted);
  49. if (scrolled_at_bottom)
  50. m_scroll += num_added;
  51. }
  52. // Limit number of lines by m_scrollback
  53. if (m_unformatted.size() > m_scrollback)
  54. {
  55. deleteOldest(m_unformatted.size() - m_scrollback);
  56. }
  57. }
  58. void ChatBuffer::clear()
  59. {
  60. m_unformatted.clear();
  61. m_formatted.clear();
  62. m_scroll = 0;
  63. }
  64. u32 ChatBuffer::getLineCount() const
  65. {
  66. return m_unformatted.size();
  67. }
  68. const ChatLine& ChatBuffer::getLine(u32 index) const
  69. {
  70. assert(index < getLineCount()); // pre-condition
  71. return m_unformatted[index];
  72. }
  73. void ChatBuffer::step(f32 dtime)
  74. {
  75. for (u32 i = 0; i < m_unformatted.size(); ++i)
  76. {
  77. m_unformatted[i].age += dtime;
  78. }
  79. }
  80. void ChatBuffer::deleteOldest(u32 count)
  81. {
  82. bool at_bottom = (m_scroll == getBottomScrollPos());
  83. u32 del_unformatted = 0;
  84. u32 del_formatted = 0;
  85. while (count > 0 && del_unformatted < m_unformatted.size())
  86. {
  87. ++del_unformatted;
  88. // keep m_formatted in sync
  89. if (del_formatted < m_formatted.size())
  90. {
  91. sanity_check(m_formatted[del_formatted].first);
  92. ++del_formatted;
  93. while (del_formatted < m_formatted.size() &&
  94. !m_formatted[del_formatted].first)
  95. ++del_formatted;
  96. }
  97. --count;
  98. }
  99. m_unformatted.erase(m_unformatted.begin(), m_unformatted.begin() + del_unformatted);
  100. m_formatted.erase(m_formatted.begin(), m_formatted.begin() + del_formatted);
  101. if (at_bottom)
  102. m_scroll = getBottomScrollPos();
  103. else
  104. scrollAbsolute(m_scroll - del_formatted);
  105. }
  106. void ChatBuffer::deleteByAge(f32 maxAge)
  107. {
  108. u32 count = 0;
  109. while (count < m_unformatted.size() && m_unformatted[count].age > maxAge)
  110. ++count;
  111. deleteOldest(count);
  112. }
  113. u32 ChatBuffer::getColumns() const
  114. {
  115. return m_cols;
  116. }
  117. u32 ChatBuffer::getRows() const
  118. {
  119. return m_rows;
  120. }
  121. void ChatBuffer::reformat(u32 cols, u32 rows)
  122. {
  123. if (cols == 0 || rows == 0)
  124. {
  125. // Clear formatted buffer
  126. m_cols = 0;
  127. m_rows = 0;
  128. m_scroll = 0;
  129. m_formatted.clear();
  130. }
  131. else if (cols != m_cols || rows != m_rows)
  132. {
  133. // TODO: Avoid reformatting ALL lines (even invisible ones)
  134. // each time the console size changes.
  135. // Find out the scroll position in *unformatted* lines
  136. u32 restore_scroll_unformatted = 0;
  137. u32 restore_scroll_formatted = 0;
  138. bool at_bottom = (m_scroll == getBottomScrollPos());
  139. if (!at_bottom)
  140. {
  141. for (s32 i = 0; i < m_scroll; ++i)
  142. {
  143. if (m_formatted[i].first)
  144. ++restore_scroll_unformatted;
  145. }
  146. }
  147. // If number of columns change, reformat everything
  148. if (cols != m_cols)
  149. {
  150. m_formatted.clear();
  151. for (u32 i = 0; i < m_unformatted.size(); ++i)
  152. {
  153. if (i == restore_scroll_unformatted)
  154. restore_scroll_formatted = m_formatted.size();
  155. formatChatLine(m_unformatted[i], cols, m_formatted);
  156. }
  157. }
  158. // Update the console size
  159. m_cols = cols;
  160. m_rows = rows;
  161. // Restore the scroll position
  162. if (at_bottom)
  163. {
  164. scrollBottom();
  165. }
  166. else
  167. {
  168. scrollAbsolute(restore_scroll_formatted);
  169. }
  170. }
  171. }
  172. const ChatFormattedLine& ChatBuffer::getFormattedLine(u32 row) const
  173. {
  174. s32 index = m_scroll + (s32) row;
  175. if (index >= 0 && index < (s32) m_formatted.size())
  176. return m_formatted[index];
  177. else
  178. return m_empty_formatted_line;
  179. }
  180. void ChatBuffer::scroll(s32 rows)
  181. {
  182. scrollAbsolute(m_scroll + rows);
  183. }
  184. void ChatBuffer::scrollAbsolute(s32 scroll)
  185. {
  186. s32 top = getTopScrollPos();
  187. s32 bottom = getBottomScrollPos();
  188. m_scroll = scroll;
  189. if (m_scroll < top)
  190. m_scroll = top;
  191. if (m_scroll > bottom)
  192. m_scroll = bottom;
  193. }
  194. void ChatBuffer::scrollBottom()
  195. {
  196. m_scroll = getBottomScrollPos();
  197. }
  198. void ChatBuffer::scrollTop()
  199. {
  200. m_scroll = getTopScrollPos();
  201. }
  202. u32 ChatBuffer::formatChatLine(const ChatLine& line, u32 cols,
  203. std::vector<ChatFormattedLine>& destination) const
  204. {
  205. u32 num_added = 0;
  206. std::vector<ChatFormattedFragment> next_frags;
  207. ChatFormattedLine next_line;
  208. ChatFormattedFragment temp_frag;
  209. u32 out_column = 0;
  210. u32 in_pos = 0;
  211. u32 hanging_indentation = 0;
  212. // Format the sender name and produce fragments
  213. if (!line.name.empty()) {
  214. temp_frag.text = L"<";
  215. temp_frag.column = 0;
  216. //temp_frag.bold = 0;
  217. next_frags.push_back(temp_frag);
  218. temp_frag.text = line.name;
  219. temp_frag.column = 0;
  220. //temp_frag.bold = 1;
  221. next_frags.push_back(temp_frag);
  222. temp_frag.text = L"> ";
  223. temp_frag.column = 0;
  224. //temp_frag.bold = 0;
  225. next_frags.push_back(temp_frag);
  226. }
  227. std::wstring name_sanitized = line.name.c_str();
  228. // Choose an indentation level
  229. if (line.name.empty()) {
  230. // Server messages
  231. hanging_indentation = 0;
  232. } else if (name_sanitized.size() + 3 <= cols/2) {
  233. // Names shorter than about half the console width
  234. hanging_indentation = line.name.size() + 3;
  235. } else {
  236. // Very long names
  237. hanging_indentation = 2;
  238. }
  239. //EnrichedString line_text(line.text);
  240. next_line.first = true;
  241. bool text_processing = false;
  242. // Produce fragments and layout them into lines
  243. while (!next_frags.empty() || in_pos < line.text.size())
  244. {
  245. // Layout fragments into lines
  246. while (!next_frags.empty())
  247. {
  248. ChatFormattedFragment& frag = next_frags[0];
  249. if (frag.text.size() <= cols - out_column)
  250. {
  251. // Fragment fits into current line
  252. frag.column = out_column;
  253. next_line.fragments.push_back(frag);
  254. out_column += frag.text.size();
  255. next_frags.erase(next_frags.begin());
  256. }
  257. else
  258. {
  259. // Fragment does not fit into current line
  260. // So split it up
  261. temp_frag.text = frag.text.substr(0, cols - out_column);
  262. temp_frag.column = out_column;
  263. //temp_frag.bold = frag.bold;
  264. next_line.fragments.push_back(temp_frag);
  265. frag.text = frag.text.substr(cols - out_column);
  266. out_column = cols;
  267. }
  268. if (out_column == cols || text_processing)
  269. {
  270. // End the current line
  271. destination.push_back(next_line);
  272. num_added++;
  273. next_line.fragments.clear();
  274. next_line.first = false;
  275. out_column = text_processing ? hanging_indentation : 0;
  276. }
  277. }
  278. // Produce fragment
  279. if (in_pos < line.text.size())
  280. {
  281. u32 remaining_in_input = line.text.size() - in_pos;
  282. u32 remaining_in_output = cols - out_column;
  283. // Determine a fragment length <= the minimum of
  284. // remaining_in_{in,out}put. Try to end the fragment
  285. // on a word boundary.
  286. u32 frag_length = 1, space_pos = 0;
  287. while (frag_length < remaining_in_input &&
  288. frag_length < remaining_in_output)
  289. {
  290. if (iswspace(line.text.getString()[in_pos + frag_length]))
  291. space_pos = frag_length;
  292. ++frag_length;
  293. }
  294. if (space_pos != 0 && frag_length < remaining_in_input)
  295. frag_length = space_pos + 1;
  296. temp_frag.text = line.text.substr(in_pos, frag_length);
  297. temp_frag.column = 0;
  298. //temp_frag.bold = 0;
  299. next_frags.push_back(temp_frag);
  300. in_pos += frag_length;
  301. text_processing = true;
  302. }
  303. }
  304. // End the last line
  305. if (num_added == 0 || !next_line.fragments.empty())
  306. {
  307. destination.push_back(next_line);
  308. num_added++;
  309. }
  310. return num_added;
  311. }
  312. s32 ChatBuffer::getTopScrollPos() const
  313. {
  314. s32 formatted_count = (s32) m_formatted.size();
  315. s32 rows = (s32) m_rows;
  316. if (rows == 0)
  317. return 0;
  318. else if (formatted_count <= rows)
  319. return formatted_count - rows;
  320. else
  321. return 0;
  322. }
  323. s32 ChatBuffer::getBottomScrollPos() const
  324. {
  325. s32 formatted_count = (s32) m_formatted.size();
  326. s32 rows = (s32) m_rows;
  327. if (rows == 0)
  328. return 0;
  329. else
  330. return formatted_count - rows;
  331. }
  332. ChatPrompt::ChatPrompt(const std::wstring &prompt, u32 history_limit):
  333. m_prompt(prompt),
  334. m_line(L""),
  335. m_history(),
  336. m_history_index(0),
  337. m_history_limit(history_limit),
  338. m_cols(0),
  339. m_view(0),
  340. m_cursor(0),
  341. m_cursor_len(0),
  342. m_nick_completion_start(0),
  343. m_nick_completion_end(0)
  344. {
  345. }
  346. ChatPrompt::~ChatPrompt()
  347. {
  348. }
  349. void ChatPrompt::input(wchar_t ch)
  350. {
  351. m_line.insert(m_cursor, 1, ch);
  352. m_cursor++;
  353. clampView();
  354. m_nick_completion_start = 0;
  355. m_nick_completion_end = 0;
  356. }
  357. void ChatPrompt::input(const std::wstring &str)
  358. {
  359. m_line.insert(m_cursor, str);
  360. m_cursor += str.size();
  361. clampView();
  362. m_nick_completion_start = 0;
  363. m_nick_completion_end = 0;
  364. }
  365. void ChatPrompt::addToHistory(std::wstring line)
  366. {
  367. if (!line.empty())
  368. m_history.push_back(line);
  369. if (m_history.size() > m_history_limit)
  370. m_history.erase(m_history.begin());
  371. m_history_index = m_history.size();
  372. }
  373. void ChatPrompt::clear()
  374. {
  375. m_line.clear();
  376. m_view = 0;
  377. m_cursor = 0;
  378. m_nick_completion_start = 0;
  379. m_nick_completion_end = 0;
  380. }
  381. std::wstring ChatPrompt::replace(std::wstring line)
  382. {
  383. std::wstring old_line = m_line;
  384. m_line = line;
  385. m_view = m_cursor = line.size();
  386. clampView();
  387. m_nick_completion_start = 0;
  388. m_nick_completion_end = 0;
  389. return old_line;
  390. }
  391. void ChatPrompt::historyPrev()
  392. {
  393. if (m_history_index != 0)
  394. {
  395. --m_history_index;
  396. replace(m_history[m_history_index]);
  397. }
  398. }
  399. void ChatPrompt::historyNext()
  400. {
  401. if (m_history_index + 1 >= m_history.size())
  402. {
  403. m_history_index = m_history.size();
  404. replace(L"");
  405. }
  406. else
  407. {
  408. ++m_history_index;
  409. replace(m_history[m_history_index]);
  410. }
  411. }
  412. void ChatPrompt::nickCompletion(const std::list<std::string>& names, bool backwards)
  413. {
  414. // Two cases:
  415. // (a) m_nick_completion_start == m_nick_completion_end == 0
  416. // Then no previous nick completion is active.
  417. // Get the word around the cursor and replace with any nick
  418. // that has that word as a prefix.
  419. // (b) else, continue a previous nick completion.
  420. // m_nick_completion_start..m_nick_completion_end are the
  421. // interval where the originally used prefix was. Cycle
  422. // through the list of completions of that prefix.
  423. u32 prefix_start = m_nick_completion_start;
  424. u32 prefix_end = m_nick_completion_end;
  425. bool initial = (prefix_end == 0);
  426. if (initial)
  427. {
  428. // no previous nick completion is active
  429. prefix_start = prefix_end = m_cursor;
  430. while (prefix_start > 0 && !iswspace(m_line[prefix_start-1]))
  431. --prefix_start;
  432. while (prefix_end < m_line.size() && !iswspace(m_line[prefix_end]))
  433. ++prefix_end;
  434. if (prefix_start == prefix_end)
  435. return;
  436. }
  437. std::wstring prefix = m_line.substr(prefix_start, prefix_end - prefix_start);
  438. // find all names that start with the selected prefix
  439. std::vector<std::wstring> completions;
  440. for (std::list<std::string>::const_iterator
  441. i = names.begin();
  442. i != names.end(); ++i)
  443. {
  444. if (str_starts_with(narrow_to_wide(*i), prefix, true))
  445. {
  446. std::wstring completion = narrow_to_wide(*i);
  447. if (prefix_start == 0)
  448. completion += L": ";
  449. completions.push_back(completion);
  450. }
  451. }
  452. if (completions.empty())
  453. return;
  454. // find a replacement string and the word that will be replaced
  455. u32 word_end = prefix_end;
  456. u32 replacement_index = 0;
  457. if (!initial)
  458. {
  459. while (word_end < m_line.size() && !iswspace(m_line[word_end]))
  460. ++word_end;
  461. std::wstring word = m_line.substr(prefix_start, word_end - prefix_start);
  462. // cycle through completions
  463. for (u32 i = 0; i < completions.size(); ++i)
  464. {
  465. if (str_equal(word, completions[i], true))
  466. {
  467. if (backwards)
  468. replacement_index = i + completions.size() - 1;
  469. else
  470. replacement_index = i + 1;
  471. replacement_index %= completions.size();
  472. break;
  473. }
  474. }
  475. }
  476. std::wstring replacement = completions[replacement_index];
  477. if (word_end < m_line.size() && iswspace(m_line[word_end]))
  478. ++word_end;
  479. // replace existing word with replacement word,
  480. // place the cursor at the end and record the completion prefix
  481. m_line.replace(prefix_start, word_end - prefix_start, replacement);
  482. m_cursor = prefix_start + replacement.size();
  483. clampView();
  484. m_nick_completion_start = prefix_start;
  485. m_nick_completion_end = prefix_end;
  486. }
  487. void ChatPrompt::reformat(u32 cols)
  488. {
  489. if (cols <= m_prompt.size())
  490. {
  491. m_cols = 0;
  492. m_view = m_cursor;
  493. }
  494. else
  495. {
  496. s32 length = m_line.size();
  497. bool was_at_end = (m_view + m_cols >= length + 1);
  498. m_cols = cols - m_prompt.size();
  499. if (was_at_end)
  500. m_view = length;
  501. clampView();
  502. }
  503. }
  504. std::wstring ChatPrompt::getVisiblePortion() const
  505. {
  506. return m_prompt + m_line.substr(m_view, m_cols);
  507. }
  508. s32 ChatPrompt::getVisibleCursorPosition() const
  509. {
  510. return m_cursor - m_view + m_prompt.size();
  511. }
  512. void ChatPrompt::cursorOperation(CursorOp op, CursorOpDir dir, CursorOpScope scope)
  513. {
  514. s32 old_cursor = m_cursor;
  515. s32 new_cursor = m_cursor;
  516. s32 length = m_line.size();
  517. s32 increment = (dir == CURSOROP_DIR_RIGHT) ? 1 : -1;
  518. switch (scope) {
  519. case CURSOROP_SCOPE_CHARACTER:
  520. new_cursor += increment;
  521. break;
  522. case CURSOROP_SCOPE_WORD:
  523. if (dir == CURSOROP_DIR_RIGHT) {
  524. // skip one word to the right
  525. while (new_cursor < length && iswspace(m_line[new_cursor]))
  526. new_cursor++;
  527. while (new_cursor < length && !iswspace(m_line[new_cursor]))
  528. new_cursor++;
  529. while (new_cursor < length && iswspace(m_line[new_cursor]))
  530. new_cursor++;
  531. } else {
  532. // skip one word to the left
  533. while (new_cursor >= 1 && iswspace(m_line[new_cursor - 1]))
  534. new_cursor--;
  535. while (new_cursor >= 1 && !iswspace(m_line[new_cursor - 1]))
  536. new_cursor--;
  537. }
  538. break;
  539. case CURSOROP_SCOPE_LINE:
  540. new_cursor += increment * length;
  541. break;
  542. case CURSOROP_SCOPE_SELECTION:
  543. break;
  544. }
  545. new_cursor = MYMAX(MYMIN(new_cursor, length), 0);
  546. switch (op) {
  547. case CURSOROP_MOVE:
  548. m_cursor = new_cursor;
  549. m_cursor_len = 0;
  550. break;
  551. case CURSOROP_DELETE:
  552. if (m_cursor_len > 0) { // Delete selected text first
  553. m_line.erase(m_cursor, m_cursor_len);
  554. } else {
  555. m_cursor = MYMIN(new_cursor, old_cursor);
  556. m_line.erase(m_cursor, abs(new_cursor - old_cursor));
  557. }
  558. m_cursor_len = 0;
  559. break;
  560. case CURSOROP_SELECT:
  561. if (scope == CURSOROP_SCOPE_LINE) {
  562. m_cursor = 0;
  563. m_cursor_len = length;
  564. } else {
  565. m_cursor = MYMIN(new_cursor, old_cursor);
  566. m_cursor_len += abs(new_cursor - old_cursor);
  567. m_cursor_len = MYMIN(m_cursor_len, length - m_cursor);
  568. }
  569. break;
  570. }
  571. clampView();
  572. m_nick_completion_start = 0;
  573. m_nick_completion_end = 0;
  574. }
  575. void ChatPrompt::clampView()
  576. {
  577. s32 length = m_line.size();
  578. if (length + 1 <= m_cols)
  579. {
  580. m_view = 0;
  581. }
  582. else
  583. {
  584. m_view = MYMIN(m_view, length + 1 - m_cols);
  585. m_view = MYMIN(m_view, m_cursor);
  586. m_view = MYMAX(m_view, m_cursor - m_cols + 1);
  587. m_view = MYMAX(m_view, 0);
  588. }
  589. }
  590. ChatBackend::ChatBackend():
  591. m_console_buffer(500),
  592. m_recent_buffer(6),
  593. m_prompt(L"]", 500)
  594. {
  595. }
  596. ChatBackend::~ChatBackend()
  597. {
  598. }
  599. void ChatBackend::addMessage(std::wstring name, std::wstring text)
  600. {
  601. // Note: A message may consist of multiple lines, for example the MOTD.
  602. WStrfnd fnd(text);
  603. while (!fnd.at_end())
  604. {
  605. std::wstring line = fnd.next(L"\n");
  606. m_console_buffer.addLine(name, line);
  607. m_recent_buffer.addLine(name, line);
  608. }
  609. }
  610. void ChatBackend::addUnparsedMessage(std::wstring message)
  611. {
  612. // TODO: Remove the need to parse chat messages client-side, by sending
  613. // separate name and text fields in TOCLIENT_CHAT_MESSAGE.
  614. if (message.size() >= 2 && message[0] == L'<')
  615. {
  616. std::size_t closing = message.find_first_of(L'>', 1);
  617. if (closing != std::wstring::npos &&
  618. closing + 2 <= message.size() &&
  619. message[closing+1] == L' ')
  620. {
  621. std::wstring name = message.substr(1, closing - 1);
  622. std::wstring text = message.substr(closing + 2);
  623. addMessage(name, text);
  624. return;
  625. }
  626. }
  627. // Unable to parse, probably a server message.
  628. addMessage(L"", message);
  629. }
  630. ChatBuffer& ChatBackend::getConsoleBuffer()
  631. {
  632. return m_console_buffer;
  633. }
  634. ChatBuffer& ChatBackend::getRecentBuffer()
  635. {
  636. return m_recent_buffer;
  637. }
  638. EnrichedString ChatBackend::getRecentChat()
  639. {
  640. EnrichedString result;
  641. for (u32 i = 0; i < m_recent_buffer.getLineCount(); ++i)
  642. {
  643. const ChatLine& line = m_recent_buffer.getLine(i);
  644. if (i != 0)
  645. result += L"\n";
  646. if (!line.name.empty()) {
  647. result += L"<";
  648. result += line.name;
  649. result += L"> ";
  650. }
  651. result += line.text;
  652. }
  653. return result;
  654. }
  655. ChatPrompt& ChatBackend::getPrompt()
  656. {
  657. return m_prompt;
  658. }
  659. void ChatBackend::reformat(u32 cols, u32 rows)
  660. {
  661. m_console_buffer.reformat(cols, rows);
  662. // no need to reformat m_recent_buffer, its formatted lines
  663. // are not used
  664. m_prompt.reformat(cols);
  665. }
  666. void ChatBackend::clearRecentChat()
  667. {
  668. m_recent_buffer.clear();
  669. }
  670. void ChatBackend::step(float dtime)
  671. {
  672. m_recent_buffer.step(dtime);
  673. m_recent_buffer.deleteByAge(60.0);
  674. // no need to age messages in anything but m_recent_buffer
  675. }
  676. void ChatBackend::scroll(s32 rows)
  677. {
  678. m_console_buffer.scroll(rows);
  679. }
  680. void ChatBackend::scrollPageDown()
  681. {
  682. m_console_buffer.scroll(m_console_buffer.getRows());
  683. }
  684. void ChatBackend::scrollPageUp()
  685. {
  686. m_console_buffer.scroll(-(s32)m_console_buffer.getRows());
  687. }