chat.cpp 20 KB

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