]> git.lizzy.rs Git - minetest.git/blob - src/chat.h
Improve chat history (#12975)
[minetest.git] / src / chat.h
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #pragma once
21
22 #include <string>
23 #include <vector>
24 #include <list>
25
26 #include "irrlichttypes.h"
27 #include "util/enriched_string.h"
28 #include "util/Optional.h"
29 #include "settings.h"
30
31 // Chat console related classes
32
33 struct ChatLine
34 {
35         // age in seconds
36         f32 age = 0.0f;
37         // name of sending player, or empty if sent by server
38         EnrichedString name;
39         // message text
40         EnrichedString text;
41
42         ChatLine(const std::wstring &a_name, const std::wstring &a_text):
43                 name(a_name),
44                 text(a_text)
45         {
46         }
47
48         ChatLine(const EnrichedString &a_name, const EnrichedString &a_text):
49                 name(a_name),
50                 text(a_text)
51         {
52         }
53 };
54
55 struct ChatFormattedFragment
56 {
57         // text string
58         EnrichedString text;
59         // starting column
60         u32 column;
61         // web link is empty for most frags
62         std::string weblink;
63         // formatting
64         //u8 bold:1;
65 };
66
67 struct ChatFormattedLine
68 {
69         // Array of text fragments
70         std::vector<ChatFormattedFragment> fragments;
71         // true if first line of one formatted ChatLine
72         bool first;
73 };
74
75 class ChatBuffer
76 {
77 public:
78         ChatBuffer(u32 scrollback);
79         ~ChatBuffer() = default;
80
81         // Append chat line
82         // Removes oldest chat line if scrollback size is reached
83         void addLine(const std::wstring &name, const std::wstring &text);
84
85         // Remove all chat lines
86         void clear();
87
88         // Get number of lines currently in buffer.
89         u32 getLineCount() const;
90         // Get reference to i-th chat line.
91         const ChatLine& getLine(u32 index) const;
92
93         // Increase each chat line's age by dtime.
94         void step(f32 dtime);
95         // Delete oldest N chat lines.
96         void deleteOldest(u32 count);
97         // Delete lines older than maxAge.
98         void deleteByAge(f32 maxAge);
99
100         // Get number of rows, 0 if reformat has not been called yet.
101         u32 getRows() const;
102         // Update console size and reformat all formatted lines.
103         void reformat(u32 cols, u32 rows);
104         // Get formatted line for a given row (0 is top of screen).
105         // Only valid after reformat has been called at least once
106         const ChatFormattedLine& getFormattedLine(u32 row) const;
107         // Scrolling in formatted buffer (relative)
108         // positive rows == scroll up, negative rows == scroll down
109         void scroll(s32 rows);
110         // Scrolling in formatted buffer (absolute)
111         void scrollAbsolute(s32 scroll);
112         // Scroll to bottom of buffer (newest)
113         void scrollBottom();
114
115         // Functions for keeping track of whether the lines were modified by any
116         // preceding operations
117         // If they were not changed, getLineCount() and getLine() output the same as
118         // before
119         bool getLinesModified() const { return m_lines_modified; }
120         void resetLinesModified() { m_lines_modified = false; }
121
122         // Format a chat line for the given number of columns.
123         // Appends the formatted lines to the destination array and
124         // returns the number of formatted lines.
125         u32 formatChatLine(const ChatLine& line, u32 cols,
126                         std::vector<ChatFormattedLine>& destination) const;
127
128         void resize(u32 scrollback);
129
130 protected:
131         s32 getTopScrollPos() const;
132         s32 getBottomScrollPos() const;
133
134 private:
135         // Scrollback size
136         u32 m_scrollback;
137         // Array of unformatted chat lines
138         std::vector<ChatLine> m_unformatted;
139
140         // Number of character columns in console
141         u32 m_cols = 0;
142         // Number of character rows in console
143         u32 m_rows = 0;
144         // Scroll position (console's top line index into m_formatted)
145         s32 m_scroll = 0;
146         // Array of formatted lines
147         std::vector<ChatFormattedLine> m_formatted;
148         // Empty formatted line, for error returns
149         ChatFormattedLine m_empty_formatted_line;
150
151         // Enable clickable chat weblinks
152         bool m_cache_clickable_chat_weblinks;
153         // Color of clickable chat weblinks
154         irr::video::SColor m_cache_chat_weblink_color;
155
156         // Whether the lines were modified since last markLinesUnchanged()
157         // Is always set to true when m_unformatted is modified, because that's what
158         // determines the output of getLineCount() and getLine()
159         bool m_lines_modified = true;
160 };
161
162 class ChatPrompt
163 {
164 public:
165         ChatPrompt(const std::wstring &prompt, u32 history_limit);
166         ~ChatPrompt() = default;
167
168         // Input character or string
169         void input(wchar_t ch);
170         void input(const std::wstring &str);
171
172         // Add a string to the history
173         void addToHistory(const std::wstring &line);
174
175         // Get current line
176         std::wstring getLine() const { return getLineRef(); }
177
178         // Get section of line that is currently selected
179         std::wstring getSelection() const { return getLineRef().substr(m_cursor, m_cursor_len); }
180
181         // Clear the current line
182         void clear();
183
184         // Replace the current line with the given text
185         std::wstring replace(const std::wstring &line);
186
187         // Select previous command from history
188         void historyPrev();
189         // Select next command from history
190         void historyNext();
191
192         // Nick completion
193         void nickCompletion(const std::list<std::string>& names, bool backwards);
194
195         // Update console size and reformat the visible portion of the prompt
196         void reformat(u32 cols);
197         // Get visible portion of the prompt.
198         std::wstring getVisiblePortion() const;
199         // Get cursor position (relative to visible portion). -1 if invalid
200         s32 getVisibleCursorPosition() const;
201         // Get length of cursor selection
202         s32 getCursorLength() const { return m_cursor_len; }
203
204         // Cursor operations
205         enum CursorOp {
206                 CURSOROP_MOVE,
207                 CURSOROP_SELECT,
208                 CURSOROP_DELETE
209         };
210
211         // Cursor operation direction
212         enum CursorOpDir {
213                 CURSOROP_DIR_LEFT,
214                 CURSOROP_DIR_RIGHT
215         };
216
217         // Cursor operation scope
218         enum CursorOpScope {
219                 CURSOROP_SCOPE_CHARACTER,
220                 CURSOROP_SCOPE_WORD,
221                 CURSOROP_SCOPE_LINE,
222                 CURSOROP_SCOPE_SELECTION
223         };
224
225         // Cursor operation
226         // op specifies whether it's a move or delete operation
227         // dir specifies whether the operation goes left or right
228         // scope specifies how far the operation will reach (char/word/line)
229         // Examples:
230         //   cursorOperation(CURSOROP_MOVE, CURSOROP_DIR_RIGHT, CURSOROP_SCOPE_LINE)
231         //     moves the cursor to the end of the line.
232         //   cursorOperation(CURSOROP_DELETE, CURSOROP_DIR_LEFT, CURSOROP_SCOPE_WORD)
233         //     deletes the word to the left of the cursor.
234         void cursorOperation(CursorOp op, CursorOpDir dir, CursorOpScope scope);
235
236 protected:
237         const std::wstring &getLineRef() const;
238
239         std::wstring &makeLineRef();
240
241         // set m_view to ensure that 0 <= m_view <= m_cursor < m_view + m_cols
242         // if line can be fully shown, set m_view to zero
243         // else, also ensure m_view <= m_line.size() + 1 - m_cols
244         void clampView();
245
246 private:
247         struct HistoryEntry {
248                 std::wstring line;
249                 // If line is edited, saved holds the unedited version.
250                 Optional<std::wstring> saved;
251
252                 HistoryEntry(const std::wstring &line): line(line) {}
253
254                 bool operator==(const HistoryEntry &other);
255                 bool operator!=(const HistoryEntry &other) { return !(*this == other); }
256         };
257
258         // Prompt prefix
259         std::wstring m_prompt = L"";
260         // Non-historical edited line
261         std::wstring m_line = L"";
262         // History buffer
263         std::vector<HistoryEntry> m_history;
264         // History index (0 <= m_history_index <= m_history.size())
265         u32 m_history_index = 0;
266         // Maximum number of history entries
267         u32 m_history_limit;
268
269         // Number of columns excluding columns reserved for the prompt
270         s32 m_cols = 0;
271         // Start of visible portion (index into m_line)
272         s32 m_view = 0;
273         // Cursor (index into m_line)
274         s32 m_cursor = 0;
275         // Cursor length (length of selected portion of line)
276         s32 m_cursor_len = 0;
277
278         // Last nick completion start (index into m_line)
279         s32 m_nick_completion_start = 0;
280         // Last nick completion start (index into m_line)
281         s32 m_nick_completion_end = 0;
282 };
283
284 class ChatBackend
285 {
286 public:
287         ChatBackend();
288         ~ChatBackend() = default;
289
290         // Add chat message
291         void addMessage(const std::wstring &name, std::wstring text);
292         // Parse and add unparsed chat message
293         void addUnparsedMessage(std::wstring line);
294
295         // Get the console buffer
296         ChatBuffer& getConsoleBuffer();
297         // Get the recent messages buffer
298         ChatBuffer& getRecentBuffer();
299         // Concatenate all recent messages
300         EnrichedString getRecentChat() const;
301         // Get the console prompt
302         ChatPrompt& getPrompt();
303
304         // Reformat all buffers
305         void reformat(u32 cols, u32 rows);
306
307         // Clear all recent messages
308         void clearRecentChat();
309
310         // Age recent messages
311         void step(float dtime);
312
313         // Scrolling
314         void scroll(s32 rows);
315         void scrollPageDown();
316         void scrollPageUp();
317
318         // Resize recent buffer based on settings
319         void applySettings();
320
321 private:
322         ChatBuffer m_console_buffer;
323         ChatBuffer m_recent_buffer;
324         ChatPrompt m_prompt;
325 };