2
0

chat.cpp 18 KB

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