chat.cpp 20 KB

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